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