MediaWiki  1.23.13
ChangeTags.php
Go to the documentation of this file.
1 <?php
23 class ChangeTags {
34  public static function formatSummaryRow( $tags, $page ) {
36 
37  if ( !$tags ) {
38  return array( '', array() );
39  }
40 
41  $classes = array();
42 
43  $tags = explode( ',', $tags );
44  $displayTags = array();
45  foreach ( $tags as $tag ) {
46  $displayTags[] = Xml::tags(
47  'span',
48  array( 'class' => 'mw-tag-marker ' .
49  Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ),
50  self::tagDescription( $tag )
51  );
52  $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
53  }
54  $markers = wfMessage( 'tag-list-wrapper' )
55  ->numParams( count( $displayTags ) )
56  ->rawParams( $wgLang->commaList( $displayTags ) )
57  ->parse();
58  $markers = Xml::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
59 
60  return array( $markers, $classes );
61  }
62 
71  public static function tagDescription( $tag ) {
72  $msg = wfMessage( "tag-$tag" );
73  return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
74  }
75 
90  public static function addTags( $tags, $rc_id = null, $rev_id = null,
91  $log_id = null, $params = null
92  ) {
93  if ( !is_array( $tags ) ) {
94  $tags = array( $tags );
95  }
96 
97  $tags = array_filter( $tags ); // Make sure we're submitting all tags...
98 
99  if ( !$rc_id && !$rev_id && !$log_id ) {
100  throw new MWException( 'At least one of: RCID, revision ID, and log ID MUST be ' .
101  'specified when adding a tag to a change!' );
102  }
103 
104  $dbr = wfGetDB( DB_SLAVE );
105 
106  // Might as well look for rcids and so on.
107  if ( !$rc_id ) {
108  // Info might be out of date, somewhat fractionally, on slave.
109  $dbr = wfGetDB( DB_MASTER );
110  if ( $log_id ) {
111  $rc_id = $dbr->selectField(
112  'recentchanges',
113  'rc_id',
114  array( 'rc_logid' => $log_id ),
115  __METHOD__
116  );
117  } elseif ( $rev_id ) {
118  $rc_id = $dbr->selectField(
119  'recentchanges',
120  'rc_id',
121  array( 'rc_this_oldid' => $rev_id ),
122  __METHOD__
123  );
124  }
125  } elseif ( !$log_id && !$rev_id ) {
126  // Info might be out of date, somewhat fractionally, on slave.
127  $dbr = wfGetDB( DB_MASTER );
128  $log_id = $dbr->selectField(
129  'recentchanges',
130  'rc_logid',
131  array( 'rc_id' => $rc_id ),
132  __METHOD__
133  );
134  $rev_id = $dbr->selectField(
135  'recentchanges',
136  'rc_this_oldid',
137  array( 'rc_id' => $rc_id ),
138  __METHOD__
139  );
140  }
141 
142  $tsConds = array_filter( array(
143  'ts_rc_id' => $rc_id,
144  'ts_rev_id' => $rev_id,
145  'ts_log_id' => $log_id )
146  );
147 
148  ## Update the summary row.
149  $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
150  $prevTags = $prevTags ? $prevTags : '';
151  $prevTags = array_filter( explode( ',', $prevTags ) );
152  $newTags = array_unique( array_merge( $prevTags, $tags ) );
153  sort( $prevTags );
154  sort( $newTags );
155 
156  if ( $prevTags == $newTags ) {
157  // No change.
158  return false;
159  }
160 
161  $dbw = wfGetDB( DB_MASTER );
162  $dbw->replace(
163  'tag_summary',
164  array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
165  array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
166  __METHOD__
167  );
168 
169  // Insert the tags rows.
170  $tagsRows = array();
171  foreach ( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
172  $tagsRows[] = array_filter(
173  array(
174  'ct_tag' => $tag,
175  'ct_rc_id' => $rc_id,
176  'ct_log_id' => $log_id,
177  'ct_rev_id' => $rev_id,
178  'ct_params' => $params
179  )
180  );
181  }
182 
183  $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
184 
185  return true;
186  }
187 
202  public static function modifyDisplayQuery( &$tables, &$fields, &$conds,
203  &$join_conds, &$options, $filter_tag = false ) {
204  global $wgRequest, $wgUseTagFilter;
205 
206  if ( $filter_tag === false ) {
207  $filter_tag = $wgRequest->getVal( 'tagfilter' );
208  }
209 
210  // Figure out which conditions can be done.
211  if ( in_array( 'recentchanges', $tables ) ) {
212  $join_cond = 'ct_rc_id=rc_id';
213  } elseif ( in_array( 'logging', $tables ) ) {
214  $join_cond = 'ct_log_id=log_id';
215  } elseif ( in_array( 'revision', $tables ) ) {
216  $join_cond = 'ct_rev_id=rev_id';
217  } elseif ( in_array( 'archive', $tables ) ) {
218  $join_cond = 'ct_rev_id=ar_rev_id';
219  } else {
220  throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
221  }
222 
223  $fields['ts_tags'] = wfGetDB( DB_SLAVE )->buildGroupConcatField(
224  ',', 'change_tag', 'ct_tag', $join_cond
225  );
226 
227  if ( $wgUseTagFilter && $filter_tag ) {
228  // Somebody wants to filter on a tag.
229  // Add an INNER JOIN on change_tag
230 
231  $tables[] = 'change_tag';
232  $join_conds['change_tag'] = array( 'INNER JOIN', $join_cond );
233  $conds['ct_tag'] = $filter_tag;
234  }
235  }
236 
250  public static function buildTagFilterSelector( $selected = '',
251  $fullForm = false, Title $title = null
252  ) {
253  global $wgUseTagFilter;
254 
255  if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) ) {
256  return $fullForm ? '' : array();
257  }
258 
259  $data = array(
261  'label',
262  array( 'for' => 'tagfilter' ),
263  wfMessage( 'tag-filter' )->parse()
264  ),
265  Xml::input(
266  'tagfilter',
267  20,
268  $selected,
269  array( 'class' => 'mw-tagfilter-input', 'id' => 'tagfilter' )
270  )
271  );
272 
273  if ( !$fullForm ) {
274  return $data;
275  }
276 
277  $html = implode( '&#160;', $data );
278  $html .= "\n" .
279  Xml::element(
280  'input',
281  array( 'type' => 'submit', 'value' => wfMessage( 'tag-filter-submit' )->text() )
282  );
283  $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
284  $html = Xml::tags(
285  'form',
286  array( 'action' => $title->getLocalURL(), 'class' => 'mw-tagfilter-form', 'method' => 'get' ),
287  $html
288  );
289 
290  return $html;
291  }
292 
302  public static function listDefinedTags() {
303  // Caching...
304  global $wgMemc;
305  $key = wfMemcKey( 'valid-tags' );
306  $tags = $wgMemc->get( $key );
307  if ( $tags ) {
308  return $tags;
309  }
310 
311  $emptyTags = array();
312 
313  // Some DB stuff
314  $dbr = wfGetDB( DB_SLAVE );
315  $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
316  foreach ( $res as $row ) {
317  $emptyTags[] = $row->vt_tag;
318  }
319 
320  wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
321 
322  $emptyTags = array_filter( array_unique( $emptyTags ) );
323 
324  // Short-term caching.
325  $wgMemc->set( $key, $emptyTags, 300 );
326  return $emptyTags;
327  }
328 
335  public static function tagUsageStatistics() {
336  $out = array();
337 
338  $dbr = wfGetDB( DB_SLAVE );
339  $res = $dbr->select(
340  'change_tag',
341  array( 'ct_tag', 'hitcount' => 'count(*)' ),
342  array(),
343  __METHOD__,
344  array( 'GROUP BY' => 'ct_tag', 'ORDER BY' => 'hitcount DESC' )
345  );
346 
347  foreach ( $res as $row ) {
348  $out[$row->ct_tag] = $row->hitcount;
349  }
350  foreach ( self::listDefinedTags() as $tag ) {
351  if ( !isset( $out[$tag] ) ) {
352  $out[$tag] = 0;
353  }
354  }
355 
356  return $out;
357  }
358 }
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ChangeTags\addTags
static addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params=null)
Add tags to a change given its rc_id, rev_id and/or log_id.
Definition: ChangeTags.php:90
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1530
Xml\tags
static tags( $element, $attribs=null, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:131
$tables
namespace and then decline to actually register it RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist & $tables
Definition: hooks.txt:815
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3706
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
ChangeTags\tagDescription
static tagDescription( $tag)
Get a short description for a tag.
Definition: ChangeTags.php:71
$params
$params
Definition: styleTest.css.php:40
ChangeTags\buildTagFilterSelector
static buildTagFilterSelector( $selected='', $fullForm=false, Title $title=null)
Build a text box to select a change tag.
Definition: ChangeTags.php:250
Sanitizer\escapeClass
static escapeClass( $class)
Given a value, escape it so that it can be used as a CSS class and return it.
Definition: Sanitizer.php:1143
Html\hidden
static hidden( $name, $value, $attribs=array())
Convenience function to produce an input element with type=hidden.
Definition: Html.php:665
$dbr
$dbr
Definition: testCompression.php:48
MWException
MediaWiki exception.
Definition: MWException.php:26
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3627
$out
$out
Definition: UtfNormalGenerate.php:167
ChangeTags\modifyDisplayQuery
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag=false)
Applies all tags-related changes to a query.
Definition: ChangeTags.php:202
ChangeTags
Definition: ChangeTags.php:23
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4058
ChangeTags\listDefinedTags
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything.
Definition: ChangeTags.php:302
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ChangeTags\tagUsageStatistics
static tagUsageStatistics()
Returns a map of any tags used on the wiki to number of edits tagged with them, ordered descending by...
Definition: ChangeTags.php:335
Xml\input
static input( $name, $size=false, $value=false, $attribs=array())
Convenience function to build an HTML text input field.
Definition: Xml.php:294
Html\rawElement
static rawElement( $element, $attribs=array(), $contents='')
Returns an HTML element in a string.
Definition: Html.php:124
$res
$res
Definition: database.txt:21
ChangeTags\formatSummaryRow
static formatSummaryRow( $tags, $page)
Creates HTML for the given tags.
Definition: ChangeTags.php:34