MediaWiki  1.23.2
SpecialStatistics.php
Go to the documentation of this file.
1 <?php
33 
34  public function __construct() {
35  parent::__construct( 'Statistics' );
36  }
37 
38  public function execute( $par ) {
39  global $wgMemc, $wgDisableCounters, $wgMiserMode;
40 
41  $this->setHeaders();
42  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
43 
44  $this->views = SiteStats::views();
45  $this->edits = SiteStats::edits();
46  $this->good = SiteStats::articles();
47  $this->images = SiteStats::images();
48  $this->total = SiteStats::pages();
49  $this->users = SiteStats::users();
50  $this->activeUsers = SiteStats::activeUsers();
51  $this->hook = '';
52 
53  # Staticic - views
54  $viewsStats = '';
55  if ( !$wgDisableCounters ) {
56  $viewsStats = $this->getViewsStats();
57  }
58 
59  # Set active user count
60  if ( !$wgMiserMode ) {
61  $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
62  // Re-calculate the count if the last tally is old...
63  if ( !$wgMemc->get( $key ) ) {
64  $dbw = wfGetDB( DB_MASTER );
66  $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
67  }
68  }
69 
70  $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
71 
72  # Statistic - pages
73  $text .= $this->getPageStats();
74 
75  # Statistic - edits
76  $text .= $this->getEditStats();
77 
78  # Statistic - users
79  $text .= $this->getUserStats();
80 
81  # Statistic - usergroups
82  $text .= $this->getGroupStats();
83  $text .= $viewsStats;
84 
85  # Statistic - popular pages
86  if ( !$wgDisableCounters && !$wgMiserMode ) {
87  $text .= $this->getMostViewedPages();
88  }
89 
90  # Statistic - other
91  $extraStats = array();
92  if ( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
93  $text .= $this->getOtherStats( $extraStats );
94  }
95 
96  $text .= Xml::closeElement( 'table' );
97 
98  # Customizable footer
99  $footer = $this->msg( 'statistics-footer' );
100  if ( !$footer->isBlank() ) {
101  $text .= "\n" . $footer->parse();
102  }
103 
104  $this->getOutput()->addHTML( $text );
105  }
106 
116  private function formatRow( $text, $number, $trExtraParams = array(),
117  $descMsg = '', $descMsgParam = ''
118  ) {
119  if ( $descMsg ) {
120  $msg = $this->msg( $descMsg, $descMsgParam );
121  if ( $msg->exists() ) {
122  $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )->escaped();
123  $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc' ),
124  " $descriptionText" );
125  }
126  }
127 
128  return Html::rawElement( 'tr', $trExtraParams,
129  Html::rawElement( 'td', array(), $text ) .
130  Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
131  );
132  }
133 
139  private function getPageStats() {
140  return Xml::openElement( 'tr' ) .
141  Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
142  Xml::closeElement( 'tr' ) .
144  $this->msg( 'statistics-articles' )->parse() ),
145  $this->getLanguage()->formatNum( $this->good ),
146  array( 'class' => 'mw-statistics-articles' ) ) .
147  $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
148  $this->getLanguage()->formatNum( $this->total ),
149  array( 'class' => 'mw-statistics-pages' ),
150  'statistics-pages-desc' ) .
151  $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Listfiles' ),
152  $this->msg( 'statistics-files' )->parse() ),
153  $this->getLanguage()->formatNum( $this->images ),
154  array( 'class' => 'mw-statistics-files' ) );
155  }
156 
157  private function getEditStats() {
158  return Xml::openElement( 'tr' ) .
159  Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
160  Xml::closeElement( 'tr' ) .
161  $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
162  $this->getLanguage()->formatNum( $this->edits ),
163  array( 'class' => 'mw-statistics-edits' )
164  ) .
165  $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
166  $this->getLanguage()
167  ->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
168  array( 'class' => 'mw-statistics-edits-average' )
169  );
170  }
171 
172  private function getUserStats() {
173  global $wgActiveUserDays;
174 
175  return Xml::openElement( 'tr' ) .
176  Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
177  Xml::closeElement( 'tr' ) .
178  $this->formatRow( $this->msg( 'statistics-users' )->parse(),
179  $this->getLanguage()->formatNum( $this->users ),
180  array( 'class' => 'mw-statistics-users' )
181  ) .
182  $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
184  SpecialPage::getTitleFor( 'Activeusers' ),
185  $this->msg( 'listgrouprights-members' )->escaped()
186  ),
187  $this->getLanguage()->formatNum( $this->activeUsers ),
188  array( 'class' => 'mw-statistics-users-active' ),
189  'statistics-users-active-desc',
190  $this->getLanguage()->formatNum( $wgActiveUserDays )
191  );
192  }
193 
194  private function getGroupStats() {
195  global $wgGroupPermissions, $wgImplicitGroups;
196  $text = '';
197  foreach ( $wgGroupPermissions as $group => $permissions ) {
198  # Skip generic * and implicit groups
199  if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
200  continue;
201  }
202  $groupname = htmlspecialchars( $group );
203  $msg = $this->msg( 'group-' . $groupname );
204  if ( $msg->isBlank() ) {
205  $groupnameLocalized = $groupname;
206  } else {
207  $groupnameLocalized = $msg->text();
208  }
209  $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
210  if ( $msg->isBlank() ) {
211  $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
212  } else {
213  $grouppageLocalized = $msg->text();
214  }
215  $linkTarget = Title::newFromText( $grouppageLocalized );
216  $grouppage = Linker::link(
217  $linkTarget,
218  htmlspecialchars( $groupnameLocalized )
219  );
220  $grouplink = Linker::linkKnown(
221  SpecialPage::getTitleFor( 'Listusers' ),
222  $this->msg( 'listgrouprights-members' )->escaped(),
223  array(),
224  array( 'group' => $group )
225  );
226  # Add a class when a usergroup contains no members to allow hiding these rows
227  $classZero = '';
228  $countUsers = SiteStats::numberingroup( $groupname );
229  if ( $countUsers == 0 ) {
230  $classZero = ' statistics-group-zero';
231  }
232  $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
233  $this->getLanguage()->formatNum( $countUsers ),
234  array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
235  }
236 
237  return $text;
238  }
239 
240  private function getViewsStats() {
241  return Xml::openElement( 'tr' ) .
242  Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-views' )->parse() ) .
243  Xml::closeElement( 'tr' ) .
244  $this->formatRow( $this->msg( 'statistics-views-total' )->parse(),
245  $this->getLanguage()->formatNum( $this->views ),
246  array( 'class' => 'mw-statistics-views-total' ), 'statistics-views-total-desc' ) .
247  $this->formatRow( $this->msg( 'statistics-views-peredit' )->parse(),
248  $this->getLanguage()->formatNum( sprintf( '%.2f', $this->edits ?
249  $this->views / $this->edits : 0 ) ),
250  array( 'class' => 'mw-statistics-views-peredit' ) );
251  }
252 
253  private function getMostViewedPages() {
254  $text = '';
255  $dbr = wfGetDB( DB_SLAVE );
256  $res = $dbr->select(
257  'page',
258  array(
259  'page_namespace',
260  'page_title',
261  'page_counter',
262  ),
263  array(
264  'page_is_redirect' => 0,
265  'page_counter > 0',
266  ),
267  __METHOD__,
268  array(
269  'ORDER BY' => 'page_counter DESC',
270  'LIMIT' => 10,
271  )
272  );
273 
274  if ( $res->numRows() > 0 ) {
275  $text .= Xml::openElement( 'tr' );
276  $text .= Xml::tags(
277  'th',
278  array( 'colspan' => '2' ),
279  $this->msg( 'statistics-mostpopular' )->parse()
280  );
281  $text .= Xml::closeElement( 'tr' );
282 
283  foreach ( $res as $row ) {
284  $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
285 
286  if ( $title instanceof Title ) {
287  $text .= $this->formatRow( Linker::link( $title ),
288  $this->getLanguage()->formatNum( $row->page_counter ) );
289  }
290  }
291  $res->free();
292  }
293 
294  return $text;
295  }
296 
304  private function getOtherStats( array $stats ) {
305  $return = '';
306 
307  foreach ( $stats as $header => $items ) {
308  // Identify the structure used
309  if ( is_array( $items ) ) {
310 
311  // Ignore headers that are recursively set as legacy header
312  if ( $header !== 'statistics-header-hooks' ) {
313  $return .= $this->formatRowHeader( $header );
314  }
315 
316  // Collect all items that belong to the same header
317  foreach ( $items as $key => $value ) {
318  $name = $this->msg( $key )->parse();
319  $number = htmlspecialchars( $value );
320 
321  $return .= $this->formatRow(
322  $name,
323  $this->getLanguage()->formatNum( $number ),
324  array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
325  );
326  }
327  } else {
328  // Create the legacy header only once
329  if ( $return === '' ) {
330  $return .= $this->formatRowHeader( 'statistics-header-hooks' );
331  }
332 
333  // Recursively remap the legacy structure
334  $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
335  array( $header => $items ) ) );
336  }
337  }
338 
339  return $return;
340  }
341 
348  private function formatRowHeader( $header ) {
349  return Xml::openElement( 'tr' ) .
350  Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
351  Xml::closeElement( 'tr' );
352  }
353 
354  protected function getGroupName() {
355  return 'wiki';
356  }
357 }
SiteStats\articles
static articles()
Definition: SiteStats.php:124
SpecialStatistics\getMostViewedPages
getMostViewedPages()
Definition: SpecialStatistics.php:253
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
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
SiteStats\users
static users()
Definition: SiteStats.php:140
SpecialStatistics\formatRow
formatRow( $text, $number, $trExtraParams=array(), $descMsg='', $descMsgParam='')
Format a row.
Definition: SpecialStatistics.php:116
Xml\tags
static tags( $element, $attribs=null, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:131
SiteStats\activeUsers
static activeUsers()
Definition: SiteStats.php:148
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:535
SiteStatsUpdate\cacheUpdate
static cacheUpdate( $dbw)
Definition: SiteStatsUpdate.php:129
$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
SpecialStatistics\$edits
$edits
Definition: SpecialStatistics.php:31
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
SiteStats\pages
static pages()
Definition: SiteStats.php:132
SpecialStatistics
Special page lists various statistics, including the contents of site_stats, plus page view details i...
Definition: SpecialStatistics.php:30
SiteStats\numberingroup
static numberingroup( $group)
Find the number of users in a given user group.
Definition: SiteStats.php:166
SpecialStatistics\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialStatistics.php:354
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name.
Definition: SpecialPage.php:74
SpecialStatistics\$views
$views
Definition: SpecialStatistics.php:31
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:1126
SpecialStatistics\$good
$good
Definition: SpecialStatistics.php:31
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:578
SpecialStatistics\$activeUsers
$activeUsers
Definition: SpecialStatistics.php:32
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
hook
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of or an object and a method hook function The function part of a hook
Definition: hooks.txt:23
Linker\linkKnown
static linkKnown( $target, $html=null, $customAttribs=array(), $query=array(), $options=array( 'known', 'noclasses'))
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:264
SpecialStatistics\$total
$total
Definition: SpecialStatistics.php:31
$dbr
$dbr
Definition: testCompression.php:48
SiteStats\images
static images()
Definition: SiteStats.php:156
Linker\link
static link( $target, $html=null, $customAttribs=array(), $query=array(), $options=array())
This function returns an HTML link to the given target.
Definition: Linker.php:192
edits
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs which is used to update link tables of transcluding pages after edits
Definition: deferred.txt:11
SpecialStatistics\getViewsStats
getViewsStats()
Definition: SpecialStatistics.php:240
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3571
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:83
SpecialStatistics\getGroupStats
getGroupStats()
Definition: SpecialStatistics.php:194
SpecialStatistics\execute
execute( $par)
Default execute method Checks user permissions, calls the function given in mFunction.
Definition: SpecialStatistics.php:38
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:4001
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:352
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
SpecialStatistics\__construct
__construct()
Definition: SpecialStatistics.php:34
SpecialStatistics\getUserStats
getUserStats()
Definition: SpecialStatistics.php:172
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:609
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:33
SpecialStatistics\$users
$users
Definition: SpecialStatistics.php:31
SpecialStatistics\getEditStats
getEditStats()
Definition: SpecialStatistics.php:157
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:118
SpecialStatistics\formatRowHeader
formatRowHeader( $header)
Format row header.
Definition: SpecialStatistics.php:348
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
SpecialStatistics\getPageStats
getPageStats()
Each of these methods is pretty self-explanatory, get a particular row for the table of statistics.
Definition: SpecialStatistics.php:139
SiteStats\views
static views()
Definition: SiteStats.php:108
SpecialStatistics\$images
$images
Definition: SpecialStatistics.php:31
SpecialStatistics\getOtherStats
getOtherStats(array $stats)
Conversion of external statistics into an internal representation Following a ([<header-message>][<it...
Definition: SpecialStatistics.php:304
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
MWNamespace\getCanonicalName
static getCanonicalName( $index)
Returns the canonical (English) name for a given index.
Definition: Namespace.php:237
SiteStats\edits
static edits()
Definition: SiteStats.php:116