Localisation updates from http://translatewiki.net.
[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 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'
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
44 $classes = array();
45
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 )
54 );
55 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
56 }
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 );
62
63 return array( $markers, $classes );
64 }
65
66 /**
67 * Get a short description for a tag
68 *
69 * @param string $tag tag
70 *
71 * @return String: Short description of the tag from "mediawiki:tag-$tag" if this message exists,
72 * html-escaped version of $tag otherwise
73 */
74 static function tagDescription( $tag ) {
75 $msg = wfMessage( "tag-$tag" );
76 return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
77 }
78
79 /**
80 * Add tags to a change given its rc_id, rev_id and/or log_id
81 *
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'
87 *
88 * @throws MWException
89 * @return bool: false if no changes are made, otherwise true
90 *
91 * @exception MWException when $rc_id, $rev_id and $log_id are all null
92 */
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 );
96 }
97
98 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
99
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!" );
102 }
103
104 $dbr = wfGetDB( DB_SLAVE );
105
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__ );
113 }
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__ );
118 }
119
120 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
121
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 );
129
130 if ( $prevTags == $newTags ) {
131 // No change.
132 return false;
133 }
134
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__
141 );
142
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
153 )
154 );
155 }
156
157 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
158
159 return true;
160 }
161
162 /**
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.
166 *
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
173 *
174 * @throws MWException When unable to determine appropriate JOIN condition for tagging
175 */
176 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
177 &$join_conds, &$options, $filter_tag = false ) {
178 global $wgRequest, $wgUseTagFilter;
179
180 if ( $filter_tag === false ) {
181 $filter_tag = $wgRequest->getVal( 'tagfilter' );
182 }
183
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.' );
193 }
194
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';
199
200 if ( $wgUseTagFilter && $filter_tag ) {
201 // Somebody wants to filter on a tag.
202 // Add an INNER JOIN on change_tag
203
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;
210 }
211 }
212
213 /**
214 * Build a text box to select a change tag
215 *
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
225 */
226 public static function buildTagFilterSelector( $selected = '', $fullForm = false, Title $title = null ) {
227 global $wgUseTagFilter;
228
229 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
230 return $fullForm ? '' : array();
231 }
232
233 $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMessage( 'tag-filter' )->parse() ),
234 Xml::input( 'tagfilter', 20, $selected, array( 'class' => 'mw-tagfilter-input' ) ) );
235
236 if ( !$fullForm ) {
237 return $data;
238 }
239
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 );
244
245 return $html;
246 }
247
248 /**
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.
252 *
253 * Tries memcached first.
254 *
255 * @return Array of strings: tags
256 */
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;
264 }
265
266 $emptyTags = array();
267
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;
273 }
274
275 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
276
277 $emptyTags = array_filter( array_unique( $emptyTags ) );
278
279 // Short-term caching.
280 $wgMemc->set( $key, $emptyTags, 300 );
281 return $emptyTags;
282 }
283 }