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