Remove most named character references from output
[lhc/web/wiklou.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[] = Xml::tags(
17 'span',
18 array( 'class' => 'mw-tag-marker ' .
19 Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ),
20 self::tagDescription( $tag )
21 );
22 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
23 }
24
25 $markers = '(' . implode( ', ', $displayTags ) . ')';
26 $markers = Xml::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
27 return array( $markers, $classes );
28 }
29
30 static function tagDescription( $tag ) {
31 $msg = wfMsgExt( "tag-$tag", 'parseinline' );
32 if ( wfEmptyMsg( "tag-$tag", $msg ) ) {
33 return htmlspecialchars( $tag );
34 }
35 return $msg;
36 }
37
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 );
42 }
43
44 $tags = array_filter( $tags ); // Make sure we're submitting all tags...
45
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!" );
48 }
49
50 $dbr = wfGetDB( DB_SLAVE );
51
52 // Might as well look for rcids and so on.
53 if( !$rc_id ) {
54 $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
55 if( $log_id ) {
56 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
57 } elseif( $rev_id ) {
58 $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
59 }
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__ );
64 }
65
66 $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
67
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 ) );
73 sort( $prevTags );
74 sort( $newTags );
75
76 if ( $prevTags == $newTags ) {
77 // No change.
78 return false;
79 }
80
81 $dbw = wfGetDB( DB_MASTER );
82 $dbw->replace(
83 'tag_summary',
84 array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
85 array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
86 __METHOD__
87 );
88
89 // Insert the tags rows.
90 $tagsRows = array();
91 foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
92 $tagsRows[] = array_filter(
93 array(
94 'ct_tag' => $tag,
95 'ct_rc_id' => $rc_id,
96 'ct_log_id' => $log_id,
97 'ct_rev_id' => $rev_id,
98 'ct_params' => $params
99 )
100 );
101 }
102
103 $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
104
105 return true;
106 }
107
108 /**
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.
112 */
113 static function modifyDisplayQuery( &$tables, &$fields, &$conds,
114 &$join_conds, &$options, $filter_tag = false ) {
115 global $wgRequest, $wgUseTagFilter;
116
117 if( $filter_tag === false ) {
118 $filter_tag = $wgRequest->getVal( 'tagfilter' );
119 }
120
121 // Figure out which conditions can be done.
122 $join_field = '';
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';
129 } else {
130 throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
131 }
132
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';
137
138 if( $wgUseTagFilter && $filter_tag ) {
139 // Somebody wants to filter on a tag.
140 // Add an INNER JOIN on change_tag
141
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;
150 }
151 }
152
153 /**
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.
156 */
157 static function buildTagFilterSelector( $selected='', $fullForm = false /* used to put a full form around the selector */ ) {
158 global $wgUseTagFilter;
159
160 if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) )
161 return $fullForm ? '' : array();
162
163 global $wgTitle;
164
165 $data = array( wfMsgExt( 'tag-filter', 'parseinline' ), Xml::input( 'tagfilter', 20, $selected ) );
166
167 if ( !$fullForm ) {
168 return $data;
169 }
170
171 $html = implode( '&#160;', $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 );
175
176 return $html;
177 }
178
179 /** Basically lists defined tags which count even if they aren't applied to anything */
180 static function listDefinedTags() {
181 // Caching...
182 global $wgMemc;
183 $key = wfMemcKey( 'valid-tags' );
184
185 if ( $tags = $wgMemc->get( $key ) )
186 return $tags;
187
188 $emptyTags = array();
189
190 // Some DB stuff
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;
195 }
196
197 wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
198
199 $emptyTags = array_filter( array_unique( $emptyTags ) );
200
201 // Short-term caching.
202 $wgMemc->set( $key, $emptyTags, 300 );
203 return $emptyTags;
204 }
205 }