4 static function formatSummaryRow( $tags, $page ) {
6 return array( '', array() );
10 $tags = explode( ',', $tags );
11 $displayTags = array();
12 foreach( $tags as $tag ) {
13 $displayTags[] = Xml
::tags(
15 array( 'class' => 'mw-tag-marker ' .
16 Sanitizer
::escapeClass( "mw-tag-marker-$tag" ) ),
17 self
::tagDescription( $tag )
19 $classes[] = Sanitizer
::escapeClass( "mw-tag-$tag" );
22 $markers = '(' . implode( ', ', $displayTags ) . ')';
23 $markers = Xml
::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
24 return array( $markers, $classes );
27 static function tagDescription( $tag ) {
28 $msg = wfMessage( "tag-$tag" );
29 return $msg->exists() ?
$msg->parse() : htmlspecialchars( $tag );
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 );
38 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
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!" );
44 $dbr = wfGetDB( DB_SLAVE
);
46 // Might as well look for rcids and so on.
48 $dbr = wfGetDB( DB_MASTER
); // Info might be out of date, somewhat fractionally, on slave.
50 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__
);
52 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__
);
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__
);
60 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
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 ) );
70 if ( $prevTags == $newTags ) {
75 $dbw = wfGetDB( DB_MASTER
);
78 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
79 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
83 // Insert the tags rows.
85 foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
86 $tagsRows[] = array_filter(
90 'ct_log_id' => $log_id,
91 'ct_rev_id' => $rev_id,
92 'ct_params' => $params
97 $dbw->insert( 'change_tag', $tagsRows, __METHOD__
, array( 'IGNORE' ) );
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.
107 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
108 &$join_conds, &$options, $filter_tag = false ) {
109 global $wgRequest, $wgUseTagFilter;
111 if( $filter_tag === false ) {
112 $filter_tag = $wgRequest->getVal( 'tagfilterdropdown', '' );
113 if ( $filter_tag === '' ||
$filter_tag === 'other' ) {
114 $filter_tag = $wgRequest->getVal( 'tagfilter' );
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';
126 throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
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';
134 if( $wgUseTagFilter && $filter_tag ) {
135 // Somebody wants to filter on a tag.
136 // Add an INNER JOIN on change_tag
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;
150 * Build a text box to select a change tag
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
162 public static function buildTagFilterSelector( $selected='', $fullForm = false, Title
$title = null ) {
163 global $wgUseTagFilter;
165 if ( !$wgUseTagFilter ||
!count( self
::listDefinedTags() ) )
166 return $fullForm ?
'' : array();
168 $data = array( Html
::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMsgExt( 'tag-filter', 'parseinline' ) ),
169 Xml
::input( 'tagfilter', 20, $selected ) );
175 $html = implode( ' ', $data );
176 $html .= "\n" . Xml
::element( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'tag-filter-submit' ) ) );
177 $html .= "\n" . Html
::hidden( 'title', $title->getPrefixedText() );
178 $html = Xml
::tags( 'form', array( 'action' => $title->getLocalURL(), 'method' => 'get' ), $html );
184 * Build a text box and a dropdown list to select a change tag
186 * @param $msgkey String: message key for the message where a (newline delimited)
187 * list of tags can be specified. These tags will be put in the dropdown list.
188 * If the specified message is empty, only the text field will be returned.
189 * @param $selectedtxt String: tag to put in text box
190 * @param $selecteddropdown String: tag to select in dropdown
191 * @return Array of html fragments of which the first element is the label
192 * If $wgUseTagFilter is false or there are no defined tags, an empty array is returned
194 public static function buildTagFilterWithDropdown( $msgkey, $selectedtxt = '', $selecteddropdown = 'other' ) {
196 global $wgUseTagFilter;
198 $definedtags = self
::listDefinedTags();
200 if ( !$wgUseTagFilter ||
!count( $definedtags ) ) {
201 // no tag filter if there are no tags defined or $wgUseTagFilter is false
205 // Extract tags from message: one tag per line
206 $tags = preg_split( "/\n/", wfMsgForContent( $msgkey ), -1, PREG_SPLIT_NO_EMPTY
);
208 $data[] = Html
::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMsgExt( 'tag-filter', 'parseinline' ) );
210 if ( count( $tags ) ) {
211 // Add dropdown list only when message $msgkey contains at least one tag
212 $data[] = Xml
::listDropDown( 'tagfilterdropdown', implode( "\n", $tags ),
213 wfMsgForContent( 'tag-filter-dropdown-other' ), $selecteddropdown );
216 $data[] = Xml
::input( 'tagfilter', 20, $selectedtxt );
223 *Basically lists defined tags which count even if they aren't applied to anything
227 static function listDefinedTags() {
230 $key = wfMemcKey( 'valid-tags' );
231 $tags = $wgMemc->get( $key );
236 $emptyTags = array();
239 $dbr = wfGetDB( DB_SLAVE
);
240 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__
);
241 foreach ( $res as $row ) {
242 $emptyTags[] = $row->vt_tag
;
245 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
247 $emptyTags = array_filter( array_unique( $emptyTags ) );
249 // Short-term caching.
250 $wgMemc->set( $key, $emptyTags, 300 );