3589560b6e11b9be6445f7694c5cfe827afae514
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(
18 array( 'class' => 'mw-tag-marker ' .
19 Sanitizer
::escapeClass( "mw-tag-marker-$tag" ) ),
20 self
::tagDescription( $tag )
22 $classes[] = Sanitizer
::escapeClass( "mw-tag-$tag" );
25 $markers = '(' . implode( ', ', $displayTags ) . ')';
26 $markers = Xml
::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
27 return array( $markers, $classes );
30 static function tagDescription( $tag ) {
31 $msg = wfMsgExt( "tag-$tag", 'parseinline' );
32 if ( wfEmptyMsg( "tag-$tag", $msg ) ) {
33 return htmlspecialchars( $tag );
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 );
44 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
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!" );
50 $dbr = wfGetDB( DB_SLAVE
);
52 // Might as well look for rcids and so on.
54 $dbr = wfGetDB( DB_MASTER
); // Info might be out of date, somewhat fractionally, on slave.
56 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__
);
58 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__
);
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__
);
66 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
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 ) );
76 if ( $prevTags == $newTags ) {
81 $dbw = wfGetDB( DB_MASTER
);
84 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
85 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
89 // Insert the tags rows.
91 foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
92 $tagsRows[] = array_filter(
96 'ct_log_id' => $log_id,
97 'ct_rev_id' => $rev_id,
98 'ct_params' => $params
103 $dbw->insert( 'change_tag', $tagsRows, __METHOD__
, array( 'IGNORE' ) );
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.
113 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
114 &$join_conds, &$options, $filter_tag = false ) {
115 global $wgRequest, $wgUseTagFilter;
117 if( $filter_tag === false ) {
118 $filter_tag = $wgRequest->getVal( 'tagfilter' );
121 // Figure out which conditions can be done.
123 if ( in_array( 'recentchanges', $tables ) ) {
124 $join_cond = 'rc_id';
125 } elseif( in_array( 'logging', $tables ) ) {
126 $join_cond = 'log_id';
127 } elseif ( in_array( 'revision', $tables ) ) {
128 $join_cond = 'rev_id';
130 throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
133 // JOIN on tag_summary
134 $tables[] = 'tag_summary';
135 $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
136 $fields[] = 'ts_tags';
138 if( $wgUseTagFilter && $filter_tag ) {
139 // Somebody wants to filter on a tag.
140 // Add an INNER JOIN on change_tag
142 // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
143 global $wgOldChangeTagsIndex;
144 $index = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
145 $options['USE INDEX'] = array( 'change_tag' => $index );
146 unset( $options['FORCE INDEX'] );
147 $tables[] = 'change_tag';
148 $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
149 $conds['ct_tag'] = $filter_tag;
154 * If $fullForm is set to false, then it returns an array of (label, form).
155 * If $fullForm is true, it returns an entire form.
157 static function buildTagFilterSelector( $selected='', $fullForm = false /* used to put a full form around the selector */ ) {
158 global $wgUseTagFilter;
160 if ( !$wgUseTagFilter ||
!count( self
::listDefinedTags() ) )
161 return $fullForm ?
'' : array();
165 $data = array( wfMsgExt( 'tag-filter', 'parseinline' ), Xml
::input( 'tagfilter', 20, $selected ) );
171 $html = implode( ' ', $data );
172 $html .= "\n" . Xml
::element( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'tag-filter-submit' ) ) );
173 $html .= "\n" . Xml
::hidden( 'title', $wgTitle-> getPrefixedText() );
174 $html = Xml
::tags( 'form', array( 'action' => $wgTitle->getLocalURL(), 'method' => 'get' ), $html );
179 /** Basically lists defined tags which count even if they aren't applied to anything */
180 static function listDefinedTags() {
183 $key = wfMemcKey( 'valid-tags' );
185 if ( $tags = $wgMemc->get( $key ) )
188 $emptyTags = array();
191 $dbr = wfGetDB( DB_SLAVE
);
192 $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__
);
193 while( $row = $res->fetchObject() ) {
194 $emptyTags[] = $row->vt_tag
;
197 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
199 $emptyTags = array_filter( array_unique( $emptyTags ) );
201 // Short-term caching.
202 $wgMemc->set( $key, $emptyTags, 300 );