DifferenceEngine cleanup
[mediawiki.git] / includes / ChangeTags.php
blob7ec641d9d637e8d242d888a9aa7475fa7c04efc1
1 <?php
2 /**
3 * Recent changes tagging.
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.
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.
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
20 * @file
23 class ChangeTags {
25 /**
26 * Creates HTML for the given tags
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'
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
37 public static function formatSummaryRow( $tags, $page ) {
38 global $wgLang;
40 if ( !$tags ) {
41 return array( '', array() );
44 $classes = array();
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 )
55 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
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 );
63 return array( $markers, $classes );
66 /**
67 * Get a short description for a tag
69 * @param string $tag tag
71 * @return String: Short description of the tag from "mediawiki:tag-$tag" if this message exists,
72 * html-escaped version of $tag otherwise
74 public static function tagDescription( $tag ) {
75 $msg = wfMessage( "tag-$tag" );
76 return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
79 /**
80 * Add tags to a change given its rc_id, rev_id and/or log_id
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'
88 * @throws MWException
89 * @return bool: false if no changes are made, otherwise true
91 * @exception MWException when $rc_id, $rev_id and $log_id are all null
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 );
98 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
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!' );
105 $dbr = wfGetDB( DB_SLAVE );
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__ );
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__ );
121 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
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 );
131 if ( $prevTags == $newTags ) {
132 // No change.
133 return false;
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__
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
158 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
160 return true;
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.
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
175 * @throws MWException When unable to determine appropriate JOIN condition for tagging
177 public static function modifyDisplayQuery( &$tables, &$fields, &$conds,
178 &$join_conds, &$options, $filter_tag = false ) {
179 global $wgRequest, $wgUseTagFilter;
181 if ( $filter_tag === false ) {
182 $filter_tag = $wgRequest->getVal( 'tagfilter' );
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.' );
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';
201 if ( $wgUseTagFilter && $filter_tag ) {
202 // Somebody wants to filter on a tag.
203 // Add an INNER JOIN on change_tag
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;
215 * Build a text box to select a change tag
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
227 public static function buildTagFilterSelector( $selected = '', $fullForm = false, Title $title = null ) {
228 global $wgUseTagFilter;
230 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
231 return $fullForm ? '' : array();
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' ) ) );
237 if ( !$fullForm ) {
238 return $data;
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 );
246 return $html;
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.
254 * Tries memcached first.
256 * @return Array of strings: tags
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;
267 $emptyTags = array();
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;
276 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
278 $emptyTags = array_filter( array_unique( $emptyTags ) );
280 // Short-term caching.
281 $wgMemc->set( $key, $emptyTags, 300 );
282 return $emptyTags;
286 * Returns a map of any tags used on the wiki to number of edits
287 * tagged with them, ordered descending by the hitcount.
289 * @return array Array of string => int
291 public static function tagUsageStatistics() {
292 $out = array();
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' )
303 foreach ( $res as $row ) {
304 $out[$row->ct_tag] = $row->hitcount;
306 foreach ( self::listDefinedTags() as $tag ) {
307 if ( !isset( $out[$tag] ) ) {
308 $out[$tag] = 0;
312 return $out;