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