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 mw-tag-marker-$tag" ),
18 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 = wfMsgExt( "tag-$tag", 'parseinline' );
29 if ( wfEmptyMsg( "tag-$tag", $msg ) ) {
30 return htmlspecialchars($tag);
35 ## Basic utility method to add tags to a particular change, given its rc_id, rev_id and/or log_id.
36 static function addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params = null ) {
37 if ( !is_array($tags) ) {
38 $tags = array( $tags );
41 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
43 if (!$rc_id && !$rev_id && !$log_id) {
44 throw new MWException( "At least one of: RCID, revision ID, and log ID MUST be specified when adding a tag to a change!" );
47 $dbr = wfGetDB( DB_SLAVE
);
49 // Might as well look for rcids and so on.
51 $dbr = wfGetDB( DB_MASTER
); // Info might be out of date, somewhat fractionally, on slave.
53 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__
);
55 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__
);
57 } elseif (!$log_id && !$rev_id) {
58 $dbr = wfGetDB( DB_MASTER
); // Info might be out of date, somewhat fractionally, on slave.
59 $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__
);
60 $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__
);
63 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
65 ## Update the summary row.
66 $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__
);
67 $prevTags = $prevTags ?
$prevTags : '';
68 $prevTags = array_filter( explode( ',', $prevTags ) );
69 $newTags = array_unique( array_merge( $prevTags, $tags ) );
73 if ( $prevTags == $newTags ) {
78 $dbw = wfGetDB( DB_MASTER
);
79 $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 // Insert the tags rows.
83 foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
84 $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 ) );
87 $dbw->insert( 'change_tag', $tagsRows, __METHOD__
, array('IGNORE') );
93 * Applies all tags-related changes to a query.
94 * Handles selecting tags, and filtering.
95 * Needs $tables to be set up properly, so we can figure out which join conditions to use.
97 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
98 &$join_conds, &$options, $filter_tag = false ) {
99 global $wgRequest, $wgUseTagFilter;
101 if ($filter_tag === false) {
102 $filter_tag = $wgRequest->getVal( 'tagfilter' );
105 // Figure out which conditions can be done.
107 if ( in_array('recentchanges', $tables) ) {
108 $join_cond = 'rc_id';
109 } elseif( in_array('logging', $tables) ) {
110 $join_cond = 'log_id';
111 } elseif ( in_array('revision', $tables) ) {
112 $join_cond = 'rev_id';
114 throw new MWException( "Unable to determine appropriate JOIN condition for tagging." );
117 // JOIN on tag_summary
118 $tables[] = 'tag_summary';
119 $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
120 $fields[] = 'ts_tags';
122 if ($wgUseTagFilter && $filter_tag) {
123 // Somebody wants to filter on a tag.
124 // Add an INNER JOIN on change_tag
126 // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
127 $options['USE INDEX'] = array( 'change_tag' => 'change_tag_tag_id' );
128 unset( $options['FORCE INDEX'] );
129 $tables[] = 'change_tag';
130 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
131 $conds['ct_tag'] = $filter_tag;
136 * If $fullForm is set to false, then it returns an array of (label, form).
137 * If $fullForm is true, it returns an entire form.
139 static function buildTagFilterSelector( $selected='', $fullForm = false /* used to put a full form around the selector */ ) {
140 global $wgUseTagFilter;
142 if ( !$wgUseTagFilter ||
!count( self
::listDefinedTags() ) )
143 return $fullForm ?
'' : array();
147 $data = array( wfMsgExt( 'tag-filter', 'parseinline' ), Xml
::input( 'tagfilter', 20, $selected ) );
153 $html = implode( ' ', $data );
154 $html .= "\n" . Xml
::element( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'tag-filter-submit' ) ) );
155 $html .= "\n" . Xml
::hidden( 'title', $wgTitle-> getPrefixedText() );
156 $html = Xml
::tags( 'form', array( 'action' => $wgTitle->getLocalURL(), 'method' => 'get' ), $html );
161 /** Basically lists defined tags which count even if they aren't applied to anything */
162 static function listDefinedTags() {
165 $key = wfMemcKey( 'valid-tags' );
167 if ($tags = $wgMemc->get( $key ))
170 $emptyTags = array();
173 $dbr = wfGetDB( DB_SLAVE
);
174 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__
);
175 while( $row = $res->fetchObject() ) {
176 $emptyTags[] = $row->vt_tag
;
179 wfRunHooks( 'ListDefinedTags', array(&$emptyTags) );
181 $emptyTags = array_filter( array_unique( $emptyTags ) );
183 // Short-term caching.
184 $wgMemc->set( $key, $emptyTags, 300 );