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