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