mark ApiEditPageTest as being slow tests
[lhc/web/wiklou.git] / includes / ChangeTags.php
1 <?php
2 /**
3 * Recent changes tagging.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 class ChangeTags {
24
25 /**
26 * Creates HTML for the given tags
27 *
28 * @param $tags String: Comma-separated list of tags
29 * @param $page String: A label for the type of action which is being displayed,
30 * for example: 'history', 'contributions' or 'newpages'
31 *
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
35 *
36 */
37 static function formatSummaryRow( $tags, $page ) {
38 global $wgLang;
39
40 if( !$tags )
41 return array( '', array() );
42
43 $classes = array();
44
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 )
53 );
54 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
55 }
56 $markers = wfMessage( 'parentheses' )->rawParams( $wgLang->commaList( $displayTags ) )->text();
57 $markers = Xml::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
58
59 return array( $markers, $classes );
60 }
61
62 /**
63 * Get a short description for a tag
64 *
65 * @param $tag String: tag
66 *
67 * @return String: Short description of the tag from "mediawiki:tag-$tag" if this message exists,
68 * html-escaped version of $tag otherwise
69 */
70 static function tagDescription( $tag ) {
71 $msg = wfMessage( "tag-$tag" );
72 return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
73 }
74
75 /**
76 * Add tags to a change given its rc_id, rev_id and/or log_id
77 *
78 * @param $tags String|Array: 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 $params String: params to put in the ct_params field of tabel 'change_tag'
83 *
84 * @throws MWException
85 * @return bool: false if no changes are made, otherwise true
86 *
87 * @exception MWException when $rc_id, $rev_id and $log_id are all null
88 */
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 );
92 }
93
94 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
95
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!" );
98 }
99
100 $dbr = wfGetDB( DB_SLAVE );
101
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__ );
109 }
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__ );
114 }
115
116 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
117
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 );
125
126 if ( $prevTags == $newTags ) {
127 // No change.
128 return false;
129 }
130
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__
137 );
138
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
149 )
150 );
151 }
152
153 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
154
155 return true;
156 }
157
158 /**
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.
162 *
163 * @param $tables String|Array: Tabel names, see DatabaseBase::select
164 * @param $fields String|Array: Fields used in query, see DatabaseBase::select
165 * @param $conds String|Array: conditions used in query, see DatabaseBase::select
166 * @param $join_conds Array: join conditions, see DatabaseBase::select
167 * @param $options Array: options, see Database::select
168 * @param bool|string $filter_tag Tag to select on
169 *
170 * @throws MWException When unable to determine appropriate JOIN condition for tagging
171 */
172 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
173 &$join_conds, &$options, $filter_tag = false ) {
174 global $wgRequest, $wgUseTagFilter;
175
176 if( $filter_tag === false ) {
177 $filter_tag = $wgRequest->getVal( 'tagfilter' );
178 }
179
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.' );
189 }
190
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';
195
196 if( $wgUseTagFilter && $filter_tag ) {
197 // Somebody wants to filter on a tag.
198 // Add an INNER JOIN on change_tag
199
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;
208 }
209 }
210
211 /**
212 * Build a text box to select a change tag
213 *
214 * @param $selected String: 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
223 */
224 public static function buildTagFilterSelector( $selected='', $fullForm = false, Title $title = null ) {
225 global $wgUseTagFilter;
226
227 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
228 return $fullForm ? '' : array();
229 }
230
231 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMessage( 'tag-filter' )->parse() ),
232 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input' ) ) );
233
234 if ( !$fullForm ) {
235 return $data;
236 }
237
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 );
242
243 return $html;
244 }
245
246 /**
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.
250 *
251 * Tries memcached first.
252 *
253 * @return Array of strings: tags
254 */
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;
262 }
263
264 $emptyTags = array();
265
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;
271 }
272
273 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
274
275 $emptyTags = array_filter( array_unique( $emptyTags ) );
276
277 // Short-term caching.
278 $wgMemc->set( $key, $emptyTags, 300 );
279 return $emptyTags;
280 }
281 }