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