3 if (!defined( 'MEDIAWIKI' ))
7 static function formatSummaryRow( $tags, $page ) {
9 return array('',array());
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" );
23 $markers = '(' . implode( ', ', $displayTags ) . ')';
24 $markers = Xml
::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
25 return array( $markers, $classes );
28 static function tagDescription( $tag ) {
29 $msg = wfMsgExt( "tag-$tag", 'parseinline' );
30 if ( wfEmptyMsg( "tag-$tag", $msg ) ) {
31 return htmlspecialchars($tag);
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 );
42 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
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!" );
48 $dbr = wfGetDB( DB_SLAVE
);
50 // Might as well look for rcids and so on.
52 $dbr = wfGetDB( DB_MASTER
); // Info might be out of date, somewhat fractionally, on slave.
54 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__
);
56 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__
);
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__
);
64 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
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 ) );
74 if ( $prevTags == $newTags ) {
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__
);
82 // Insert the tags rows.
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 ) );
88 $dbw->insert( 'change_tag', $tagsRows, __METHOD__
, array('IGNORE') );
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.
98 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
99 &$join_conds, &$options, $filter_tag = false ) {
100 global $wgRequest, $wgUseTagFilter;
102 if ($filter_tag === false) {
103 $filter_tag = $wgRequest->getVal( 'tagfilter' );
106 // Figure out which conditions can be done.
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';
115 throw new MWException( "Unable to determine appropriate JOIN condition for tagging." );
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';
123 if ($wgUseTagFilter && $filter_tag) {
124 // Somebody wants to filter on a tag.
125 // Add an INNER JOIN on change_tag
127 // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
128 global $wgOldChangeTagsIndex;
129 $index = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
130 $options['USE INDEX'] = array( 'change_tag' => $index );
131 unset( $options['FORCE INDEX'] );
132 $tables[] = 'change_tag';
133 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
134 $conds['ct_tag'] = $filter_tag;
139 * If $fullForm is set to false, then it returns an array of (label, form).
140 * If $fullForm is true, it returns an entire form.
142 static function buildTagFilterSelector( $selected='', $fullForm = false /* used to put a full form around the selector */ ) {
143 global $wgUseTagFilter;
145 if ( !$wgUseTagFilter ||
!count( self
::listDefinedTags() ) )
146 return $fullForm ?
'' : array();
150 $data = array( wfMsgExt( 'tag-filter', 'parseinline' ), Xml
::input( 'tagfilter', 20, $selected ) );
156 $html = implode( ' ', $data );
157 $html .= "\n" . Xml
::element( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'tag-filter-submit' ) ) );
158 $html .= "\n" . Xml
::hidden( 'title', $wgTitle-> getPrefixedText() );
159 $html = Xml
::tags( 'form', array( 'action' => $wgTitle->getLocalURL(), 'method' => 'get' ), $html );
164 /** Basically lists defined tags which count even if they aren't applied to anything */
165 static function listDefinedTags() {
168 $key = wfMemcKey( 'valid-tags' );
170 if ($tags = $wgMemc->get( $key ))
173 $emptyTags = array();
176 $dbr = wfGetDB( DB_SLAVE
);
177 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__
);
178 while( $row = $res->fetchObject() ) {
179 $emptyTags[] = $row->vt_tag
;
182 wfRunHooks( 'ListDefinedTags', array(&$emptyTags) );
184 $emptyTags = array_filter( array_unique( $emptyTags ) );
186 // Short-term caching.
187 $wgMemc->set( $key, $emptyTags, 300 );