Revert "Remove unsupported and mostly non-functional Mssql support"
[lhc/web/wiklou.git] / includes / ChangeTags.php
1 <?php
2 /**
3 * Recent changes tagging.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 class ChangeTags {
24
25 /**
26 * Creates HTML for the given tags
27 *
28 * @param string $tags Comma-separated list of tags
29 * @param string $page A label for the type of action which is being displayed,
30 * for example: 'history', 'contributions' or 'newpages'
31 *
32 * @return Array with two items: (html, classes)
33 * - html: String: HTML for displaying the tags (empty string when param $tags is empty)
34 * - classes: Array of strings: CSS classes used in the generated html, one class for each tag
35 *
36 */
37 public static function formatSummaryRow( $tags, $page ) {
38 global $wgLang;
39
40 if ( !$tags ) {
41 return array( '', array() );
42 }
43
44 $classes = array();
45
46 $tags = explode( ',', $tags );
47 $displayTags = array();
48 foreach ( $tags as $tag ) {
49 $displayTags[] = Xml::tags(
50 'span',
51 array( 'class' => 'mw-tag-marker ' .
52 Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ),
53 self::tagDescription( $tag )
54 );
55 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
56 }
57 $markers = wfMessage( 'tag-list-wrapper' )
58 ->numParams( count( $displayTags ) )
59 ->rawParams( $wgLang->commaList( $displayTags ) )
60 ->parse();
61 $markers = Xml::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
62
63 return array( $markers, $classes );
64 }
65
66 /**
67 * Get a short description for a tag
68 *
69 * @param string $tag tag
70 *
71 * @return String: Short description of the tag from "mediawiki:tag-$tag" if this message exists,
72 * html-escaped version of $tag otherwise
73 */
74 public static function tagDescription( $tag ) {
75 $msg = wfMessage( "tag-$tag" );
76 return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
77 }
78
79 /**
80 * Add tags to a change given its rc_id, rev_id and/or log_id
81 *
82 * @param string|array $tags Tags to add to the change
83 * @param $rc_id int: rc_id of the change to add the tags to
84 * @param $rev_id int: rev_id of the change to add the tags to
85 * @param $log_id int: log_id of the change to add the tags to
86 * @param string $params params to put in the ct_params field of table 'change_tag'
87 *
88 * @throws MWException
89 * @return bool: false if no changes are made, otherwise true
90 *
91 * @exception MWException when $rc_id, $rev_id and $log_id are all null
92 */
93 public static function addTags( $tags, $rc_id = null, $rev_id = null, $log_id = null, $params = null ) {
94 if ( !is_array( $tags ) ) {
95 $tags = array( $tags );
96 }
97
98 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
99
100 if ( !$rc_id && !$rev_id && !$log_id ) {
101 throw new MWException( 'At least one of: RCID, revision ID, and log ID MUST be ' .
102 'specified when adding a tag to a change!' );
103 }
104
105 $dbr = wfGetDB( DB_SLAVE );
106
107 // Might as well look for rcids and so on.
108 if ( !$rc_id ) {
109 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
110 if ( $log_id ) {
111 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
112 } elseif ( $rev_id ) {
113 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
114 }
115 } elseif ( !$log_id && !$rev_id ) {
116 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
117 $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__ );
118 $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__ );
119 }
120
121 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
122
123 ## Update the summary row.
124 $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
125 $prevTags = $prevTags ? $prevTags : '';
126 $prevTags = array_filter( explode( ',', $prevTags ) );
127 $newTags = array_unique( array_merge( $prevTags, $tags ) );
128 sort( $prevTags );
129 sort( $newTags );
130
131 if ( $prevTags == $newTags ) {
132 // No change.
133 return false;
134 }
135
136 $dbw = wfGetDB( DB_MASTER );
137 $dbw->replace(
138 'tag_summary',
139 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
140 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
141 __METHOD__
142 );
143
144 // Insert the tags rows.
145 $tagsRows = array();
146 foreach ( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
147 $tagsRows[] = array_filter(
148 array(
149 'ct_tag' => $tag,
150 'ct_rc_id' => $rc_id,
151 'ct_log_id' => $log_id,
152 'ct_rev_id' => $rev_id,
153 'ct_params' => $params
154 )
155 );
156 }
157
158 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
159
160 return true;
161 }
162
163 /**
164 * Applies all tags-related changes to a query.
165 * Handles selecting tags, and filtering.
166 * Needs $tables to be set up properly, so we can figure out which join conditions to use.
167 *
168 * @param string|array $tables Table names, see DatabaseBase::select
169 * @param string|array $fields Fields used in query, see DatabaseBase::select
170 * @param string|array $conds conditions used in query, see DatabaseBase::select
171 * @param $join_conds Array: join conditions, see DatabaseBase::select
172 * @param array $options options, see Database::select
173 * @param bool|string $filter_tag Tag to select on
174 *
175 * @throws MWException When unable to determine appropriate JOIN condition for tagging
176 */
177 public static function modifyDisplayQuery( &$tables, &$fields, &$conds,
178 &$join_conds, &$options, $filter_tag = false ) {
179 global $wgRequest, $wgUseTagFilter;
180
181 if ( $filter_tag === false ) {
182 $filter_tag = $wgRequest->getVal( 'tagfilter' );
183 }
184
185 // Figure out which conditions can be done.
186 if ( in_array( 'recentchanges', $tables ) ) {
187 $join_cond = 'rc_id';
188 } elseif ( in_array( 'logging', $tables ) ) {
189 $join_cond = 'log_id';
190 } elseif ( in_array( 'revision', $tables ) ) {
191 $join_cond = 'rev_id';
192 } else {
193 throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
194 }
195
196 // JOIN on tag_summary
197 $tables[] = 'tag_summary';
198 $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
199 $fields[] = 'ts_tags';
200
201 if ( $wgUseTagFilter && $filter_tag ) {
202 // Somebody wants to filter on a tag.
203 // Add an INNER JOIN on change_tag
204
205 // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
206 $options['USE INDEX'] = array( 'change_tag' => 'change_tag_tag_id' );
207 unset( $options['FORCE INDEX'] );
208 $tables[] = 'change_tag';
209 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
210 $conds['ct_tag'] = $filter_tag;
211 }
212 }
213
214 /**
215 * Build a text box to select a change tag
216 *
217 * @param string $selected tag to select by default
218 * @param $fullForm Boolean:
219 * - if false, then it returns an array of (label, form).
220 * - if true, it returns an entire form around the selector.
221 * @param $title Title object to send the form to.
222 * Used when, and only when $fullForm is true.
223 * @return String or array:
224 * - if $fullForm is false: Array with
225 * - if $fullForm is true: String, html fragment
226 */
227 public static function buildTagFilterSelector( $selected = '', $fullForm = false, Title $title = null ) {
228 global $wgUseTagFilter;
229
230 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
231 return $fullForm ? '' : array();
232 }
233
234 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMessage( 'tag-filter' )->parse() ),
235 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input', 'id' => 'tagfilter' ) ) );
236
237 if ( !$fullForm ) {
238 return $data;
239 }
240
241 $html = implode( '&#160;', $data );
242 $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMessage( 'tag-filter-submit' )->text() ) );
243 $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
244 $html = Xml::tags( 'form', array( 'action' => $title->getLocalURL(), 'class' => 'mw-tagfilter-form', 'method' => 'get' ), $html );
245
246 return $html;
247 }
248
249 /**
250 * Basically lists defined tags which count even if they aren't applied to anything.
251 * Tags on items in table 'change_tag' which are not (or no longer) in table 'valid_tag'
252 * are not included.
253 *
254 * Tries memcached first.
255 *
256 * @return Array of strings: tags
257 */
258 public static function listDefinedTags() {
259 // Caching...
260 global $wgMemc;
261 $key = wfMemcKey( 'valid-tags' );
262 $tags = $wgMemc->get( $key );
263 if ( $tags ) {
264 return $tags;
265 }
266
267 $emptyTags = array();
268
269 // Some DB stuff
270 $dbr = wfGetDB( DB_SLAVE );
271 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
272 foreach ( $res as $row ) {
273 $emptyTags[] = $row->vt_tag;
274 }
275
276 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
277
278 $emptyTags = array_filter( array_unique( $emptyTags ) );
279
280 // Short-term caching.
281 $wgMemc->set( $key, $emptyTags, 300 );
282 return $emptyTags;
283 }
284
285 /**
286 * Returns a map of any tags used on the wiki to number of edits
287 * tagged with them, ordered descending by the hitcount.
288 *
289 * @return array Array of string => int
290 */
291 public static function tagUsageStatistics() {
292 $out = array();
293
294 $dbr = wfGetDB( DB_SLAVE );
295 $res = $dbr->select(
296 'change_tag',
297 array( 'ct_tag', 'hitcount' => 'count(*)' ),
298 array(),
299 __METHOD__,
300 array( 'GROUP BY' => 'ct_tag', 'ORDER BY' => 'hitcount DESC' )
301 );
302
303 foreach ( $res as $row ) {
304 $out[$row->ct_tag] = $row->hitcount;
305 }
306 foreach ( self::listDefinedTags() as $tag ) {
307 if ( !isset( $out[$tag] ) ) {
308 $out[$tag] = 0;
309 }
310 }
311
312 return $out;
313 }
314 }