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