Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / ChangeTags.php
blob54783966a47c30824dd4ee6ea1efd53ed217f3af
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 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( 'parentheses' )->rawParams( $wgLang->commaList( $displayTags ) )->text();
58 $markers = Xml::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
60 return array( $markers, $classes );
63 /**
64 * Get a short description for a tag
66 * @param string $tag tag
68 * @return String: Short description of the tag from "mediawiki:tag-$tag" if this message exists,
69 * html-escaped version of $tag otherwise
71 static function tagDescription( $tag ) {
72 $msg = wfMessage( "tag-$tag" );
73 return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
76 /**
77 * Add tags to a change given its rc_id, rev_id and/or log_id
79 * @param string|array $tags Tags to add to the change
80 * @param $rc_id int: rc_id of the change to add the tags to
81 * @param $rev_id int: rev_id of the change to add the tags to
82 * @param $log_id int: log_id of the change to add the tags to
83 * @param string $params params to put in the ct_params field of table 'change_tag'
85 * @throws MWException
86 * @return bool: false if no changes are made, otherwise true
88 * @exception MWException when $rc_id, $rev_id and $log_id are all null
90 static function addTags( $tags, $rc_id = null, $rev_id = null, $log_id = null, $params = null ) {
91 if ( !is_array( $tags ) ) {
92 $tags = array( $tags );
95 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
97 if ( !$rc_id && !$rev_id && !$log_id ) {
98 throw new MWException( "At least one of: RCID, revision ID, and log ID MUST be specified when adding a tag to a change!" );
101 $dbr = wfGetDB( DB_SLAVE );
103 // Might as well look for rcids and so on.
104 if ( !$rc_id ) {
105 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
106 if ( $log_id ) {
107 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
108 } elseif ( $rev_id ) {
109 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
111 } elseif ( !$log_id && !$rev_id ) {
112 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
113 $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__ );
114 $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__ );
117 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
119 ## Update the summary row.
120 $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
121 $prevTags = $prevTags ? $prevTags : '';
122 $prevTags = array_filter( explode( ',', $prevTags ) );
123 $newTags = array_unique( array_merge( $prevTags, $tags ) );
124 sort( $prevTags );
125 sort( $newTags );
127 if ( $prevTags == $newTags ) {
128 // No change.
129 return false;
132 $dbw = wfGetDB( DB_MASTER );
133 $dbw->replace(
134 'tag_summary',
135 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
136 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
137 __METHOD__
140 // Insert the tags rows.
141 $tagsRows = array();
142 foreach ( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
143 $tagsRows[] = array_filter(
144 array(
145 'ct_tag' => $tag,
146 'ct_rc_id' => $rc_id,
147 'ct_log_id' => $log_id,
148 'ct_rev_id' => $rev_id,
149 'ct_params' => $params
154 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
156 return true;
160 * Applies all tags-related changes to a query.
161 * Handles selecting tags, and filtering.
162 * Needs $tables to be set up properly, so we can figure out which join conditions to use.
164 * @param string|array $tables Table names, see DatabaseBase::select
165 * @param string|array $fields Fields used in query, see DatabaseBase::select
166 * @param string|array $conds conditions used in query, see DatabaseBase::select
167 * @param $join_conds Array: join conditions, see DatabaseBase::select
168 * @param array $options options, see Database::select
169 * @param bool|string $filter_tag Tag to select on
171 * @throws MWException When unable to determine appropriate JOIN condition for tagging
173 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
174 &$join_conds, &$options, $filter_tag = false ) {
175 global $wgRequest, $wgUseTagFilter;
177 if ( $filter_tag === false ) {
178 $filter_tag = $wgRequest->getVal( 'tagfilter' );
181 // Figure out which conditions can be done.
182 if ( in_array( 'recentchanges', $tables ) ) {
183 $join_cond = 'rc_id';
184 } elseif ( in_array( 'logging', $tables ) ) {
185 $join_cond = 'log_id';
186 } elseif ( in_array( 'revision', $tables ) ) {
187 $join_cond = 'rev_id';
188 } else {
189 throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
192 // JOIN on tag_summary
193 $tables[] = 'tag_summary';
194 $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
195 $fields[] = 'ts_tags';
197 if ( $wgUseTagFilter && $filter_tag ) {
198 // Somebody wants to filter on a tag.
199 // Add an INNER JOIN on change_tag
201 // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
202 global $wgOldChangeTagsIndex;
203 $index = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
204 $options['USE INDEX'] = array( 'change_tag' => $index );
205 unset( $options['FORCE INDEX'] );
206 $tables[] = 'change_tag';
207 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
208 $conds['ct_tag'] = $filter_tag;
213 * Build a text box to select a change tag
215 * @param string $selected tag to select by default
216 * @param $fullForm Boolean:
217 * - if false, then it returns an array of (label, form).
218 * - if true, it returns an entire form around the selector.
219 * @param $title Title object to send the form to.
220 * Used when, and only when $fullForm is true.
221 * @return String or array:
222 * - if $fullForm is false: Array with
223 * - if $fullForm is true: String, html fragment
225 public static function buildTagFilterSelector( $selected = '', $fullForm = false, Title $title = null ) {
226 global $wgUseTagFilter;
228 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
229 return $fullForm ? '' : array();
232 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMessage( 'tag-filter' )->parse() ),
233 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input' ) ) );
235 if ( !$fullForm ) {
236 return $data;
239 $html = implode( '&#160;', $data );
240 $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMessage( 'tag-filter-submit' )->text() ) );
241 $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
242 $html = Xml::tags( 'form', array( 'action' => $title->getLocalURL(), 'class' => 'mw-tagfilter-form', 'method' => 'get' ), $html );
244 return $html;
248 * Basically lists defined tags which count even if they aren't applied to anything.
249 * Tags on items in table 'change_tag' which are not (or no longer) in table 'valid_tag'
250 * are not included.
252 * Tries memcached first.
254 * @return Array of strings: tags
256 static function listDefinedTags() {
257 // Caching...
258 global $wgMemc;
259 $key = wfMemcKey( 'valid-tags' );
260 $tags = $wgMemc->get( $key );
261 if ( $tags ) {
262 return $tags;
265 $emptyTags = array();
267 // Some DB stuff
268 $dbr = wfGetDB( DB_SLAVE );
269 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
270 foreach ( $res as $row ) {
271 $emptyTags[] = $row->vt_tag;
274 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
276 $emptyTags = array_filter( array_unique( $emptyTags ) );
278 // Short-term caching.
279 $wgMemc->set( $key, $emptyTags, 300 );
280 return $emptyTags;