Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / ChangeTags.php
blob3adf58f8d25ab9cec597abcd04e6e36ae91f83fa
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() );
43 $classes = array();
45 $tags = explode( ',', $tags );
46 $displayTags = array();
47 foreach( $tags as $tag ) {
48 $displayTags[] = Xml::tags(
49 'span',
50 array( 'class' => 'mw-tag-marker ' .
51 Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ),
52 self::tagDescription( $tag )
54 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
56 $markers = wfMessage( 'parentheses' )->rawParams( $wgLang->commaList( $displayTags ) )->text();
57 $markers = Xml::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
59 return array( $markers, $classes );
62 /**
63 * Get a short description for a tag
65 * @param string $tag tag
67 * @return String: Short description of the tag from "mediawiki:tag-$tag" if this message exists,
68 * html-escaped version of $tag otherwise
70 static function tagDescription( $tag ) {
71 $msg = wfMessage( "tag-$tag" );
72 return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
75 /**
76 * Add tags to a change given its rc_id, rev_id and/or log_id
78 * @param string|array $tags Tags to add to the change
79 * @param $rc_id int: rc_id of the change to add the tags to
80 * @param $rev_id int: rev_id of the change to add the tags to
81 * @param $log_id int: log_id of the change to add the tags to
82 * @param string $params params to put in the ct_params field of table 'change_tag'
84 * @throws MWException
85 * @return bool: false if no changes are made, otherwise true
87 * @exception MWException when $rc_id, $rev_id and $log_id are all null
89 static function addTags( $tags, $rc_id = null, $rev_id = null, $log_id = null, $params = null ) {
90 if ( !is_array( $tags ) ) {
91 $tags = array( $tags );
94 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
96 if( !$rc_id && !$rev_id && !$log_id ) {
97 throw new MWException( "At least one of: RCID, revision ID, and log ID MUST be specified when adding a tag to a change!" );
100 $dbr = wfGetDB( DB_SLAVE );
102 // Might as well look for rcids and so on.
103 if( !$rc_id ) {
104 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
105 if( $log_id ) {
106 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
107 } elseif( $rev_id ) {
108 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
110 } elseif( !$log_id && !$rev_id ) {
111 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
112 $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__ );
113 $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__ );
116 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
118 ## Update the summary row.
119 $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
120 $prevTags = $prevTags ? $prevTags : '';
121 $prevTags = array_filter( explode( ',', $prevTags ) );
122 $newTags = array_unique( array_merge( $prevTags, $tags ) );
123 sort( $prevTags );
124 sort( $newTags );
126 if ( $prevTags == $newTags ) {
127 // No change.
128 return false;
131 $dbw = wfGetDB( DB_MASTER );
132 $dbw->replace(
133 'tag_summary',
134 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
135 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
136 __METHOD__
139 // Insert the tags rows.
140 $tagsRows = array();
141 foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
142 $tagsRows[] = array_filter(
143 array(
144 'ct_tag' => $tag,
145 'ct_rc_id' => $rc_id,
146 'ct_log_id' => $log_id,
147 'ct_rev_id' => $rev_id,
148 'ct_params' => $params
153 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
155 return true;
159 * Applies all tags-related changes to a query.
160 * Handles selecting tags, and filtering.
161 * Needs $tables to be set up properly, so we can figure out which join conditions to use.
163 * @param string|array $tables Table names, see DatabaseBase::select
164 * @param string|array $fields Fields used in query, see DatabaseBase::select
165 * @param string|array $conds conditions used in query, see DatabaseBase::select
166 * @param $join_conds Array: join conditions, see DatabaseBase::select
167 * @param array $options options, see Database::select
168 * @param bool|string $filter_tag Tag to select on
170 * @throws MWException When unable to determine appropriate JOIN condition for tagging
172 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
173 &$join_conds, &$options, $filter_tag = false ) {
174 global $wgRequest, $wgUseTagFilter;
176 if( $filter_tag === false ) {
177 $filter_tag = $wgRequest->getVal( 'tagfilter' );
180 // Figure out which conditions can be done.
181 if ( in_array( 'recentchanges', $tables ) ) {
182 $join_cond = 'rc_id';
183 } elseif( in_array( 'logging', $tables ) ) {
184 $join_cond = 'log_id';
185 } elseif ( in_array( 'revision', $tables ) ) {
186 $join_cond = 'rev_id';
187 } else {
188 throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
191 // JOIN on tag_summary
192 $tables[] = 'tag_summary';
193 $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
194 $fields[] = 'ts_tags';
196 if( $wgUseTagFilter && $filter_tag ) {
197 // Somebody wants to filter on a tag.
198 // Add an INNER JOIN on change_tag
200 // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
201 global $wgOldChangeTagsIndex;
202 $index = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
203 $options['USE INDEX'] = array( 'change_tag' => $index );
204 unset( $options['FORCE INDEX'] );
205 $tables[] = 'change_tag';
206 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
207 $conds['ct_tag'] = $filter_tag;
212 * Build a text box to select a change tag
214 * @param string $selected tag to select by default
215 * @param $fullForm Boolean:
216 * - if false, then it returns an array of (label, form).
217 * - if true, it returns an entire form around the selector.
218 * @param $title Title object to send the form to.
219 * Used when, and only when $fullForm is true.
220 * @return String or array:
221 * - if $fullForm is false: Array with
222 * - if $fullForm is true: String, html fragment
224 public static function buildTagFilterSelector( $selected = '', $fullForm = false, Title $title = null ) {
225 global $wgUseTagFilter;
227 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
228 return $fullForm ? '' : array();
231 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMessage( 'tag-filter' )->parse() ),
232 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input' ) ) );
234 if ( !$fullForm ) {
235 return $data;
238 $html = implode( '&#160;', $data );
239 $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMessage( 'tag-filter-submit' )->text() ) );
240 $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
241 $html = Xml::tags( 'form', array( 'action' => $title->getLocalURL(), 'class' => 'mw-tagfilter-form', 'method' => 'get' ), $html );
243 return $html;
247 * Basically lists defined tags which count even if they aren't applied to anything.
248 * Tags on items in table 'change_tag' which are not (or no longer) in table 'valid_tag'
249 * are not included.
251 * Tries memcached first.
253 * @return Array of strings: tags
255 static function listDefinedTags() {
256 // Caching...
257 global $wgMemc;
258 $key = wfMemcKey( 'valid-tags' );
259 $tags = $wgMemc->get( $key );
260 if ( $tags ) {
261 return $tags;
264 $emptyTags = array();
266 // Some DB stuff
267 $dbr = wfGetDB( DB_SLAVE );
268 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
269 foreach ( $res as $row ) {
270 $emptyTags[] = $row->vt_tag;
273 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
275 $emptyTags = array_filter( array_unique( $emptyTags ) );
277 // Short-term caching.
278 $wgMemc->set( $key, $emptyTags, 300 );
279 return $emptyTags;