Non-word characters don't terminate tag names.
[mediawiki.git] / includes / ChangeTags.php
blobe94143aa2ccfbfb53d7a83f1dfd0081d0ccef59f
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( '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 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 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 specified when adding a tag to a change!" );
104 $dbr = wfGetDB( DB_SLAVE );
106 // Might as well look for rcids and so on.
107 if ( !$rc_id ) {
108 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
109 if ( $log_id ) {
110 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
111 } elseif ( $rev_id ) {
112 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
114 } elseif ( !$log_id && !$rev_id ) {
115 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
116 $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__ );
117 $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__ );
120 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
122 ## Update the summary row.
123 $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
124 $prevTags = $prevTags ? $prevTags : '';
125 $prevTags = array_filter( explode( ',', $prevTags ) );
126 $newTags = array_unique( array_merge( $prevTags, $tags ) );
127 sort( $prevTags );
128 sort( $newTags );
130 if ( $prevTags == $newTags ) {
131 // No change.
132 return false;
135 $dbw = wfGetDB( DB_MASTER );
136 $dbw->replace(
137 'tag_summary',
138 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
139 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
140 __METHOD__
143 // Insert the tags rows.
144 $tagsRows = array();
145 foreach ( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
146 $tagsRows[] = array_filter(
147 array(
148 'ct_tag' => $tag,
149 'ct_rc_id' => $rc_id,
150 'ct_log_id' => $log_id,
151 'ct_rev_id' => $rev_id,
152 'ct_params' => $params
157 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
159 return true;
163 * Applies all tags-related changes to a query.
164 * Handles selecting tags, and filtering.
165 * Needs $tables to be set up properly, so we can figure out which join conditions to use.
167 * @param string|array $tables Table names, see DatabaseBase::select
168 * @param string|array $fields Fields used in query, see DatabaseBase::select
169 * @param string|array $conds conditions used in query, see DatabaseBase::select
170 * @param $join_conds Array: join conditions, see DatabaseBase::select
171 * @param array $options options, see Database::select
172 * @param bool|string $filter_tag Tag to select on
174 * @throws MWException When unable to determine appropriate JOIN condition for tagging
176 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
177 &$join_conds, &$options, $filter_tag = false ) {
178 global $wgRequest, $wgUseTagFilter;
180 if ( $filter_tag === false ) {
181 $filter_tag = $wgRequest->getVal( 'tagfilter' );
184 // Figure out which conditions can be done.
185 if ( in_array( 'recentchanges', $tables ) ) {
186 $join_cond = 'rc_id';
187 } elseif ( in_array( 'logging', $tables ) ) {
188 $join_cond = 'log_id';
189 } elseif ( in_array( 'revision', $tables ) ) {
190 $join_cond = 'rev_id';
191 } else {
192 throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
195 // JOIN on tag_summary
196 $tables[] = 'tag_summary';
197 $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
198 $fields[] = 'ts_tags';
200 if ( $wgUseTagFilter && $filter_tag ) {
201 // Somebody wants to filter on a tag.
202 // Add an INNER JOIN on change_tag
204 // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
205 $options['USE INDEX'] = array( 'change_tag' => 'change_tag_tag_id' );
206 unset( $options['FORCE INDEX'] );
207 $tables[] = 'change_tag';
208 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
209 $conds['ct_tag'] = $filter_tag;
214 * Build a text box to select a change tag
216 * @param string $selected tag to select by default
217 * @param $fullForm Boolean:
218 * - if false, then it returns an array of (label, form).
219 * - if true, it returns an entire form around the selector.
220 * @param $title Title object to send the form to.
221 * Used when, and only when $fullForm is true.
222 * @return String or array:
223 * - if $fullForm is false: Array with
224 * - if $fullForm is true: String, html fragment
226 public static function buildTagFilterSelector( $selected = '', $fullForm = false, Title $title = null ) {
227 global $wgUseTagFilter;
229 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
230 return $fullForm ? '' : array();
233 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMessage( 'tag-filter' )->parse() ),
234 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input' ) ) );
236 if ( !$fullForm ) {
237 return $data;
240 $html = implode( '&#160;', $data );
241 $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMessage( 'tag-filter-submit' )->text() ) );
242 $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
243 $html = Xml::tags( 'form', array( 'action' => $title->getLocalURL(), 'class' => 'mw-tagfilter-form', 'method' => 'get' ), $html );
245 return $html;
249 * Basically lists defined tags which count even if they aren't applied to anything.
250 * Tags on items in table 'change_tag' which are not (or no longer) in table 'valid_tag'
251 * are not included.
253 * Tries memcached first.
255 * @return Array of strings: tags
257 static function listDefinedTags() {
258 // Caching...
259 global $wgMemc;
260 $key = wfMemcKey( 'valid-tags' );
261 $tags = $wgMemc->get( $key );
262 if ( $tags ) {
263 return $tags;
266 $emptyTags = array();
268 // Some DB stuff
269 $dbr = wfGetDB( DB_SLAVE );
270 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
271 foreach ( $res as $row ) {
272 $emptyTags[] = $row->vt_tag;
275 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
277 $emptyTags = array_filter( array_unique( $emptyTags ) );
279 // Short-term caching.
280 $wgMemc->set( $key, $emptyTags, 300 );
281 return $emptyTags;