@@ -60,24 +60,42 @@ public function remove_all_tags_from_topic($topic_id, $delete_unused_tags = true
60
60
}
61
61
62
62
/**
63
- * Removes all tags that are not assigned to at least one topic (garbage collection).
64
- *
65
- * @return count of deleted tags
63
+ * Gets the ids of all tags, that are not assigned to a topic.
66
64
*/
67
- public function delete_unused_tags ()
65
+ private function get_unused_tag_ids ()
68
66
{
69
- // TODO maybe we are not allowed to use subqueries, because some DBALS supported by phpBB do not support them.
70
- // https://www.phpbb.com/community/viewtopic.php?f=461&t=2263646
71
- // so we would need 2 queries, but this is slow... so we use subqueries and hope - yeah! :D
72
-
73
- $ sql = 'DELETE
67
+ $ sql = 'SELECT t.id
74
68
FROM ' . $ this ->table_prefix . TABLES ::TAGS . ' t
75
69
WHERE NOT EXISTS (
76
70
SELECT 1
77
71
FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . ' tt
78
72
WHERE tt.tag_id = t.id
79
73
) ' ;
80
-
74
+ $ result = $ this ->db ->sql_query ($ sql );
75
+ $ ids = array ();
76
+ while ($ row = $ this ->db ->sql_fetchrow ($ result ))
77
+ {
78
+ $ ids [] = $ row ['id ' ];
79
+ }
80
+ $ this ->db ->sql_freeresult ($ result );
81
+ return $ ids ;
82
+ }
83
+
84
+ /**
85
+ * Removes all tags that are not assigned to at least one topic (garbage collection).
86
+ *
87
+ * @return count of deleted tags
88
+ */
89
+ public function delete_unused_tags ()
90
+ {
91
+ $ ids = $ this ->get_unused_tag_ids ();
92
+ if (empty ($ ids ))
93
+ {
94
+ // nothing to do
95
+ return 0 ;
96
+ }
97
+ $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TAGS . '
98
+ WHERE ' . $ this ->db ->sql_in_set ('id ' , $ ids );
81
99
$ this ->db ->sql_query ($ sql );
82
100
return $ this ->db ->sql_affectedrows ();
83
101
}
@@ -92,18 +110,23 @@ public function delete_assignments_of_invalid_tags()
92
110
// get all tags to check them
93
111
$ tags = $ this ->get_existing_tags (null );
94
112
95
- $ ids_of_invalid_tags_ = array ();
113
+ $ ids_of_invalid_tags = array ();
96
114
foreach ($ tags as $ tag )
97
115
{
98
116
if (!$ this ->is_valid_tag ($ tag ['tag ' ]))
99
117
{
100
118
$ ids_of_invalid_tags [] = (int ) $ tag ['id ' ];
101
119
}
102
120
}
121
+ if (empty ($ ids_of_invalid_tags ))
122
+ {
123
+ // nothing to do
124
+ return 0 ;
125
+ }
126
+
103
127
// delete all tag-assignments where the tag is not valid
104
- $ sql = 'DELETE
105
- FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . ' tt
106
- WHERE ' . $ this ->db ->sql_in_set ('tt.tag_id ' , $ ids_of_invalid_tags );
128
+ $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . '
129
+ WHERE ' . $ this ->db ->sql_in_set ('tag_id ' , $ ids_of_invalid_tags );
107
130
$ this ->db ->sql_query ($ sql );
108
131
$ removed_count = $ this ->db ->sql_affectedrows ();
109
132
@@ -112,26 +135,46 @@ public function delete_assignments_of_invalid_tags()
112
135
return $ removed_count ;
113
136
}
114
137
138
+ private function get_assignment_ids_where_topic_does_not_exist ()
139
+ {
140
+ $ sql = 'SELECT tt.id
141
+ FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . ' tt
142
+ WHERE NOT EXISTS (
143
+ SELECT 1
144
+ FROM ' . TOPICS_TABLE . ' topics
145
+ WHERE topics.topic_id = tt.topic_id
146
+ ) ' ;
147
+ $ result = $ this ->db ->sql_query ($ sql );
148
+ $ ids = array ();
149
+ while ($ row = $ this ->db ->sql_fetchrow ($ result ))
150
+ {
151
+ $ ids [] = $ row ['id ' ];
152
+ }
153
+ $ this ->db ->sql_freeresult ($ result );
154
+ return $ ids ;
155
+ }
156
+
115
157
/**
116
158
* Removes all topic-tag-assignments where the topic does not exist anymore.
117
159
*
118
160
* @return count of deleted assignments
119
161
*/
120
162
public function delete_assignments_where_topic_does_not_exist ()
121
163
{
164
+ $ ids = $ this ->get_assignment_ids_where_topic_does_not_exist ();
165
+ if (empty ($ ids ))
166
+ {
167
+ // nothing to do
168
+ return 0 ;
169
+ }
122
170
// delete all tag-assignments where the topic does not exist anymore
123
- $ sql = 'DELETE
124
- FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . ' tt
125
- WHERE NOT EXISTS (
126
- SELECT 1
127
- FROM ' . TOPICS_TABLE . ' topics
128
- WHERE topics.topic_id = tt.topic_id
129
- ) ' ;
171
+ $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . '
172
+ WHERE ' . $ this ->db ->sql_in_set ('id ' , $ ids );
130
173
$ this ->db ->sql_query ($ sql );
131
174
$ removed_count = $ this ->db ->sql_affectedrows ();
132
175
133
176
$ this ->calc_count_tags ();
134
-
177
+
135
178
return $ removed_count ;
136
179
}
137
180
@@ -152,16 +195,11 @@ public function delete_tags_from_tagdisabled_forums($forum_ids = null)
152
195
// performance improvement because we already know the result of querying the db.
153
196
return 0 ;
154
197
}
155
- // ensure forum_ids are ints before using them in sql
156
- $ int_ids = array ();
157
- foreach ($ forum_ids as $ id )
158
- {
159
- $ int_ids [] = (int ) $ id ;
160
- }
161
- $ forums_sql_where = ' AND ' . $ this ->db ->sql_in_set ('f.forum_id ' , $ int_ids );
198
+ $ forums_sql_where = ' AND ' . $ this ->db ->sql_in_set ('f.forum_id ' , $ forum_ids );
162
199
}
163
- // Deletes all topic-assignments to topics that reside in a forum with tagging disabled.
164
- $ sql = 'DELETE
200
+
201
+ // get ids of all topic-assignments to topics that reside in a forum with tagging disabled.
202
+ $ sql = 'SELECT tt.id
165
203
FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . ' tt
166
204
WHERE EXISTS (
167
205
SELECT 1
@@ -172,6 +210,22 @@ public function delete_tags_from_tagdisabled_forums($forum_ids = null)
172
210
AND f.rh_topictags_enabled = 0
173
211
$ forums_sql_where
174
212
) " ;
213
+ $ result = $ this ->db ->sql_query ($ sql );
214
+ $ delete_ids = array ();
215
+ while ($ row = $ this ->db ->sql_fetchrow ($ result ))
216
+ {
217
+ $ delete_ids [] = $ row ['id ' ];
218
+ }
219
+ $ this ->db ->sql_freeresult ($ result );
220
+
221
+ if (empty ($ delete_ids ))
222
+ {
223
+ // nothing to do
224
+ return 0 ;
225
+ }
226
+ // delete these assignments
227
+ $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . '
228
+ WHERE ' . $ this ->db ->sql_in_set ('id ' , $ delete_ids );
175
229
$ this ->db ->sql_query ($ sql );
176
230
$ removed_count = $ this ->db ->sql_affectedrows ();
177
231
@@ -202,6 +256,7 @@ public function get_assigned_tags($topic_id)
202
256
{
203
257
$ tags [] = $ row ['tag ' ];
204
258
}
259
+ $ this ->db ->sql_freeresult ($ result );
205
260
return $ tags ;
206
261
}
207
262
@@ -220,13 +275,18 @@ public function get_tag_suggestions($query, $exclude, $count)
220
275
{
221
276
return array ();
222
277
}
278
+ $ exclude_sql = '' ;
279
+ if (!empty ($ exclude ))
280
+ {
281
+ $ exclude_sql = ' AND ' . $ this ->db ->sql_in_set ('t.tag ' , $ exclude , true , true );
282
+ }
223
283
$ sql_array = array (
224
284
'SELECT ' => 't.tag ' ,
225
285
'FROM ' => array (
226
286
$ this ->table_prefix . TABLES ::TAGS => 't ' ,
227
287
),
228
- 'WHERE ' => 't.tag ' . $ this ->db ->sql_like_expression ($ query . $ this ->db ->get_any_char ()) . '
229
- AND ' . $ this -> db -> sql_in_set ( ' t.tag ' , $ exclude , true , true ) ,
288
+ 'WHERE ' => 't.tag ' . $ this ->db ->sql_like_expression ($ query . $ this ->db ->get_any_char ()) . "
289
+ $ exclude_sql " ,
230
290
'ORDER BY ' => 't.count DESC ' ,
231
291
);
232
292
$ sql = $ this ->db ->sql_build_query ('SELECT_DISTINCT ' , $ sql_array );
@@ -236,6 +296,7 @@ public function get_tag_suggestions($query, $exclude, $count)
236
296
{
237
297
$ tags [] = array ('text ' => $ row ['tag ' ]);
238
298
}
299
+ $ this ->db ->sql_freeresult ($ result );
239
300
return $ tags ;
240
301
}
241
302
@@ -359,6 +420,7 @@ public function get_existing_tags($tags = null, $only_ids = false)
359
420
);
360
421
}
361
422
}
423
+ $ this ->db ->sql_freeresult ($ result );
362
424
return $ existing_tags ;
363
425
}
364
426
@@ -377,6 +439,7 @@ private function get_used_tag_ids()
377
439
{
378
440
$ ids [] = $ row ['tag_id ' ];
379
441
}
442
+ $ this ->db ->sql_freeresult ($ result );
380
443
return $ ids ;
381
444
}
382
445
@@ -471,9 +534,19 @@ private function get_topics_build_query(array $tags, $mode = 'AND', $casesensiti
471
534
$ forum_ary = array_unique ($ forum_ary );
472
535
473
536
// Get sql-source for the topics that reside in forums that the user can read and which are approved.
474
- $ sql_where_topic_access = $ this ->db ->sql_in_set ('topics.forum_id ' , $ forum_ary , false , true );
537
+ $ sql_where_topic_access = '' ;
538
+ if (empty ($ forum_ary ))
539
+ {
540
+ $ sql_where_topic_access = ' 1=0 ' ;
541
+ }
542
+ else
543
+ {
544
+ $ sql_where_topic_access = $ this ->db ->sql_in_set ('topics.forum_id ' , $ forum_ary , false , true );
545
+ }
475
546
$ sql_where_topic_access .= ' AND topics.topic_visibility = 1 ' ;
476
547
548
+ // TODO store a utf8_clean_string string and compare this, instead of LOWER(...) see http://phpcrossref.com/xref/phpbb/_functions/utf8_clean_string.html
549
+ // tags is not an empty array here
477
550
$ sql_where_tag_in = $ this ->db ->sql_in_set ($ casesensitive ? ' t.tag ' : 'LOWER(t.tag) ' , $ tags );
478
551
479
552
$ sql = '' ;
@@ -610,9 +683,9 @@ public function clean_tag($tag)
610
683
$ tag = trim ($ tag );
611
684
612
685
// db-field is max 30 characters!
613
- $ tag = substr ($ tag , 0 , 30 );
686
+ $ tag = utf8_substr ($ tag , 0 , 30 );
614
687
615
- //might have a space at the end now, so trim again
688
+ // might have a space at the end now, so trim again
616
689
$ tag = trim ($ tag );
617
690
618
691
if ($ this ->config [PREFIXES ::CONFIG .'_convert_space_to_minus ' ])
@@ -636,7 +709,9 @@ public function is_tagging_enabled_in_forum($forum_id)
636
709
FROM " . FORUMS_TABLE . '
637
710
WHERE ' . $ this ->db ->sql_build_array ('SELECT ' , array ('forum_id ' => (int ) $ forum_id ));
638
711
$ result = $ this ->db ->sql_query ($ sql );
639
- return (int ) $ this ->db ->sql_fetchfield ($ field );
712
+ $ enabled = ((int ) $ this ->db ->sql_fetchfield ($ field )) > 0 ;
713
+ $ this ->db ->sql_freeresult ($ result );
714
+ return $ enabled ;
640
715
}
641
716
642
717
/**
@@ -697,7 +772,9 @@ public function is_enabled_in_all_forums()
697
772
);
698
773
$ sql = $ this ->db ->sql_build_query ('SELECT ' , $ sql_array );
699
774
$ this ->db ->sql_query ($ sql );
700
- return ((int ) $ this ->db ->sql_fetchfield ('all_enabled ' )) == 0 ;
775
+ $ enabled = ((int ) $ this ->db ->sql_fetchfield ('all_enabled ' )) == 0 ;
776
+ $ this ->db ->sql_freeresult ($ result );
777
+ return $ enabled ;
701
778
}
702
779
703
780
/**
@@ -718,7 +795,9 @@ public function is_disabled_in_all_forums()
718
795
);
719
796
$ sql = $ this ->db ->sql_build_query ('SELECT ' , $ sql_array );
720
797
$ this ->db ->sql_query ($ sql );
721
- return ((int ) $ this ->db ->sql_fetchfield ('all_disabled ' )) == 0 ;
798
+ $ disabled = ((int ) $ this ->db ->sql_fetchfield ('all_disabled ' )) == 0 ;
799
+ $ this ->db ->sql_freeresult ($ result );
800
+ return $ disabled ;
722
801
}
723
802
724
803
/**
@@ -783,11 +862,13 @@ public function merge($tag_to_delete, $tag_to_delete_id, $tag_to_keep, $tag_to_k
783
862
784
863
// delete assignments where the new tag is already assigned
785
864
$ topic_ids_already_assigned = $ this ->get_topic_ids_by_tag_id ($ tag_to_keep_id );
786
- $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . ' tt
787
- WHERE ' . $ this ->db ->sql_in_set ('tt.topic_id ' , $ topic_ids_already_assigned ) . '
788
- AND tt.tag_id = ' . (int ) $ tag_to_delete_id ;
789
- $ this ->db ->sql_query ($ sql );
790
-
865
+ if (!empty ($ topic_ids_already_assigned ))
866
+ {
867
+ $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . '
868
+ WHERE ' . $ this ->db ->sql_in_set ('topic_id ' , $ topic_ids_already_assigned ) . '
869
+ AND tag_id = ' . (int ) $ tag_to_delete_id ;
870
+ $ this ->db ->sql_query ($ sql );
871
+ }
791
872
// renew assignments where the new tag is not assigned, yet
792
873
$ sql_ary = array (
793
874
'tt.tag_id ' => $ tag_to_keep_id ,
@@ -809,14 +890,12 @@ public function merge($tag_to_delete, $tag_to_delete_id, $tag_to_keep, $tag_to_k
809
890
*/
810
891
public function delete_tag ($ tag_id )
811
892
{
812
- $ sql = 'DELETE
813
- FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . ' tt
814
- WHERE tt.tag_id = ' . ((int ) $ tag_id );
893
+ $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TOPICTAGS . '
894
+ WHERE tag_id = ' . ((int ) $ tag_id );
815
895
$ this ->db ->sql_query ($ sql );
816
896
817
- $ sql = 'DELETE
818
- FROM ' . $ this ->table_prefix . TABLES ::TAGS . ' t
819
- WHERE t.id = ' . ((int ) $ tag_id );
897
+ $ sql = 'DELETE FROM ' . $ this ->table_prefix . TABLES ::TAGS . '
898
+ WHERE id = ' . ((int ) $ tag_id );
820
899
$ this ->db ->sql_query ($ sql );
821
900
}
822
901
@@ -900,11 +979,12 @@ public function get_all_tags($start, $limit, $sort_field = 'tag', $asc = true)
900
979
*
901
980
* @return int the count of all tags
902
981
*/
903
- public function count_tags () {
982
+ public function count_tags ()
983
+ {
904
984
$ sql = 'SELECT COUNT(*) as count_tags FROM ' . $ this ->table_prefix . TABLES ::TAGS ;
905
985
$ result = $ this ->db ->sql_query ($ sql );
906
986
$ count = (int ) $ this ->db ->sql_fetchfield ('count_tags ' );
907
987
$ this ->db ->sql_freeresult ($ result );
908
- return $ count ;
988
+ return $ count ;
909
989
}
910
990
}
0 commit comments