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