Merge "(bug 30625) Return warnings, if they exist, despite ignorewarnings"
[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 * @return bool: false if no changes are made, otherwise true
85 *
86 * @exception MWException when $rc_id, $rev_id and $log_id are all null
87 */
88 static function addTags( $tags, $rc_id = null, $rev_id = null, $log_id = null, $params = null ) {
89 if ( !is_array( $tags ) ) {
90 $tags = array( $tags );
91 }
92
93 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
94
95 if( !$rc_id && !$rev_id && !$log_id ) {
96 throw new MWException( "At least one of: RCID, revision ID, and log ID MUST be specified when adding a tag to a change!" );
97 }
98
99 $dbr = wfGetDB( DB_SLAVE );
100
101 // Might as well look for rcids and so on.
102 if( !$rc_id ) {
103 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
104 if( $log_id ) {
105 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
106 } elseif( $rev_id ) {
107 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
108 }
109 } elseif( !$log_id && !$rev_id ) {
110 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
111 $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__ );
112 $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__ );
113 }
114
115 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
116
117 ## Update the summary row.
118 $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
119 $prevTags = $prevTags ? $prevTags : '';
120 $prevTags = array_filter( explode( ',', $prevTags ) );
121 $newTags = array_unique( array_merge( $prevTags, $tags ) );
122 sort( $prevTags );
123 sort( $newTags );
124
125 if ( $prevTags == $newTags ) {
126 // No change.
127 return false;
128 }
129
130 $dbw = wfGetDB( DB_MASTER );
131 $dbw->replace(
132 'tag_summary',
133 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
134 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
135 __METHOD__
136 );
137
138 // Insert the tags rows.
139 $tagsRows = array();
140 foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
141 $tagsRows[] = array_filter(
142 array(
143 'ct_tag' => $tag,
144 'ct_rc_id' => $rc_id,
145 'ct_log_id' => $log_id,
146 'ct_rev_id' => $rev_id,
147 'ct_params' => $params
148 )
149 );
150 }
151
152 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
153
154 return true;
155 }
156
157 /**
158 * Applies all tags-related changes to a query.
159 * Handles selecting tags, and filtering.
160 * Needs $tables to be set up properly, so we can figure out which join conditions to use.
161 *
162 * @param $tables String|Array: Tabel names, see DatabaseBase::select
163 * @param $fields String|Array: Fields used in query, see DatabaseBase::select
164 * @param $conds String|Array: conditions used in query, see DatabaseBase::select
165 * @param $join_conds Array: join conditions, see DatabaseBase::select
166 * @param $options Array: options, see Database::select
167 * @param $filter_tag String: tag to select on
168 *
169 * @exception MWException when unable to determine appropriate JOIN condition for tagging
170 *
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 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMsgExt( 'tag-filter', 'parseinline' ) ),
231 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input' ) ) );
232
233 if ( !$fullForm ) {
234 return $data;
235 }
236
237 $html = implode( '&#160;', $data );
238 $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'tag-filter-submit' ) ) );
239 $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
240 $html = Xml::tags( 'form', array( 'action' => $title->getLocalURL(), 'class' => 'mw-tagfilter-form', 'method' => 'get' ), $html );
241
242 return $html;
243 }
244
245 /**
246 * Basically lists defined tags which count even if they aren't applied to anything.
247 * Tags on items in table 'change_tag' which are not (or no longer) in table 'valid_tag'
248 * are not included.
249 *
250 * Tries memcached first.
251 *
252 * @return Array of strings: tags
253 */
254 static function listDefinedTags() {
255 // Caching...
256 global $wgMemc;
257 $key = wfMemcKey( 'valid-tags' );
258 $tags = $wgMemc->get( $key );
259 if ( $tags ) {
260 return $tags;
261 }
262
263 $emptyTags = array();
264
265 // Some DB stuff
266 $dbr = wfGetDB( DB_SLAVE );
267 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
268 foreach ( $res as $row ) {
269 $emptyTags[] = $row->vt_tag;
270 }
271
272 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
273
274 $emptyTags = array_filter( array_unique( $emptyTags ) );
275
276 // Short-term caching.
277 $wgMemc->set( $key, $emptyTags, 300 );
278 return $emptyTags;
279 }
280 }