Optimized LocalRepo::findFiles()
[mediawiki.git] / includes / ChangeTags.php
blobfd94bea31a1fc33ac3f40fa51a08ffbfa645bda8
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 $fields['ts_tags'] = wfGetDB( DB_SLAVE )->buildGroupConcatField(
197 ',', 'change_tag', 'ct_tag', "ct_$join_cond=$join_cond"
200 if ( $wgUseTagFilter && $filter_tag ) {
201 // Somebody wants to filter on a tag.
202 // Add an INNER JOIN on change_tag
204 $tables[] = 'change_tag';
205 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
206 $conds['ct_tag'] = $filter_tag;
211 * Build a text box to select a change tag
213 * @param string $selected tag to select by default
214 * @param $fullForm Boolean:
215 * - if false, then it returns an array of (label, form).
216 * - if true, it returns an entire form around the selector.
217 * @param $title Title object to send the form to.
218 * Used when, and only when $fullForm is true.
219 * @return String or array:
220 * - if $fullForm is false: Array with
221 * - if $fullForm is true: String, html fragment
223 public static function buildTagFilterSelector( $selected = '', $fullForm = false, Title $title = null ) {
224 global $wgUseTagFilter;
226 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
227 return $fullForm ? '' : array();
230 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMessage( 'tag-filter' )->parse() ),
231 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input', 'id' => 'tagfilter' ) ) );
233 if ( !$fullForm ) {
234 return $data;
237 $html = implode( '&#160;', $data );
238 $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMessage( 'tag-filter-submit' )->text() ) );
239 $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
240 $html = Xml::tags( 'form', array( 'action' => $title->getLocalURL(), 'class' => 'mw-tagfilter-form', 'method' => 'get' ), $html );
242 return $html;
246 * Basically lists defined tags which count even if they aren't applied to anything.
247 * Tags on items in table 'change_tag' which are not (or no longer) in table 'valid_tag'
248 * are not included.
250 * Tries memcached first.
252 * @return Array of strings: tags
254 public static function listDefinedTags() {
255 // Caching...
256 global $wgMemc;
257 $key = wfMemcKey( 'valid-tags' );
258 $tags = $wgMemc->get( $key );
259 if ( $tags ) {
260 return $tags;
263 $emptyTags = array();
265 // Some DB stuff
266 $dbr = wfGetDB( DB_SLAVE );
267 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
268 foreach ( $res as $row ) {
269 $emptyTags[] = $row->vt_tag;
272 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
274 $emptyTags = array_filter( array_unique( $emptyTags ) );
276 // Short-term caching.
277 $wgMemc->set( $key, $emptyTags, 300 );
278 return $emptyTags;
282 * Returns a map of any tags used on the wiki to number of edits
283 * tagged with them, ordered descending by the hitcount.
285 * @return array Array of string => int
287 public static function tagUsageStatistics() {
288 $out = array();
290 $dbr = wfGetDB( DB_SLAVE );
291 $res = $dbr->select(
292 'change_tag',
293 array( 'ct_tag', 'hitcount' => 'count(*)' ),
294 array(),
295 __METHOD__,
296 array( 'GROUP BY' => 'ct_tag', 'ORDER BY' => 'hitcount DESC' )
299 foreach ( $res as $row ) {
300 $out[$row->ct_tag] = $row->hitcount;
302 foreach ( self::listDefinedTags() as $tag ) {
303 if ( !isset( $out[$tag] ) ) {
304 $out[$tag] = 0;
308 return $out;