MediaWiki  master
SpecialStatistics.php
Go to the documentation of this file.
1 <?php
25 
35 
36  public function __construct() {
37  parent::__construct( 'Statistics' );
38  }
39 
40  public function execute( $par ) {
41  $this->setHeaders();
42  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
43 
44  $this->edits = SiteStats::edits();
45  $this->good = SiteStats::articles();
46  $this->images = SiteStats::images();
47  $this->total = SiteStats::pages();
48  $this->users = SiteStats::users();
49  $this->activeUsers = SiteStats::activeUsers();
50  $this->hook = '';
51 
52  $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
53 
54  # Statistic - pages
55  $text .= $this->getPageStats();
56 
57  # Statistic - edits
58  $text .= $this->getEditStats();
59 
60  # Statistic - users
61  $text .= $this->getUserStats();
62 
63  # Statistic - usergroups
64  $text .= $this->getGroupStats();
65 
66  # Statistic - other
67  $extraStats = [];
68  if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
69  $text .= $this->getOtherStats( $extraStats );
70  }
71 
72  $text .= Xml::closeElement( 'table' );
73 
74  # Customizable footer
75  $footer = $this->msg( 'statistics-footer' );
76  if ( !$footer->isBlank() ) {
77  $text .= "\n" . $footer->parse();
78  }
79 
80  $this->getOutput()->addHTML( $text );
81  }
82 
92  private function formatRow( $text, $number, $trExtraParams = [],
93  $descMsg = '', $descMsgParam = ''
94  ) {
95  if ( $descMsg ) {
96  $msg = $this->msg( $descMsg, $descMsgParam );
97  if ( !$msg->isDisabled() ) {
98  $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
99  ->escaped();
100  $text .= "<br />" . Html::rawElement(
101  'small',
102  [ 'class' => 'mw-statistic-desc' ],
103  " $descriptionHtml"
104  );
105  }
106  }
107 
108  return Html::rawElement( 'tr', $trExtraParams,
109  Html::rawElement( 'td', [], $text ) .
110  Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
111  );
112  }
113 
119  private function getPageStats() {
120  $linkRenderer = $this->getLinkRenderer();
121 
122  $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
123  $pageStatsHtml = Xml::openElement( 'tr' ) .
124  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
125  ->parse() ) .
126  Xml::closeElement( 'tr' ) .
127  $this->formatRow( $linkRenderer->makeKnownLink(
128  $specialAllPagesTitle,
129  $this->msg( 'statistics-articles' )->text(),
130  [], [ 'hideredirects' => 1 ] ),
131  $this->getLanguage()->formatNum( $this->good ),
132  [ 'class' => 'mw-statistics-articles' ],
133  'statistics-articles-desc' ) .
134  $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
135  $this->msg( 'statistics-pages' )->text() ),
136  $this->getLanguage()->formatNum( $this->total ),
137  [ 'class' => 'mw-statistics-pages' ],
138  'statistics-pages-desc' );
139 
140  // Show the image row only, when there are files or upload is possible
141  if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
142  $pageStatsHtml .= $this->formatRow(
143  $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
144  $this->msg( 'statistics-files' )->text() ),
145  $this->getLanguage()->formatNum( $this->images ),
146  [ 'class' => 'mw-statistics-files' ] );
147  }
148 
149  return $pageStatsHtml;
150  }
151 
152  private function getEditStats() {
153  return Xml::openElement( 'tr' ) .
154  Xml::tags( 'th', [ 'colspan' => '2' ],
155  $this->msg( 'statistics-header-edits' )->parse() ) .
156  Xml::closeElement( 'tr' ) .
157  $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
158  $this->getLanguage()->formatNum( $this->edits ),
159  [ 'class' => 'mw-statistics-edits' ]
160  ) .
161  $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
162  $this->getLanguage()->formatNum(
163  sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
164  ), [ 'class' => 'mw-statistics-edits-average' ]
165  );
166  }
167 
168  private function getUserStats() {
169  return Xml::openElement( 'tr' ) .
170  Xml::tags( 'th', [ 'colspan' => '2' ],
171  $this->msg( 'statistics-header-users' )->parse() ) .
172  Xml::closeElement( 'tr' ) .
173  $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
174  $this->getLinkRenderer()->makeKnownLink(
175  SpecialPage::getTitleFor( 'Listusers' ),
176  $this->msg( 'listgrouprights-members' )->text()
177  ),
178  $this->getLanguage()->formatNum( $this->users ),
179  [ 'class' => 'mw-statistics-users' ]
180  ) .
181  $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
182  $this->getLinkRenderer()->makeKnownLink(
183  SpecialPage::getTitleFor( 'Activeusers' ),
184  $this->msg( 'listgrouprights-members' )->text()
185  ),
186  $this->getLanguage()->formatNum( $this->activeUsers ),
187  [ 'class' => 'mw-statistics-users-active' ],
188  'statistics-users-active-desc',
189  $this->getLanguage()->formatNum(
190  $this->getConfig()->get( 'ActiveUserDays' ) )
191  );
192  }
193 
194  private function getGroupStats() {
195  $linkRenderer = $this->getLinkRenderer();
196  $text = '';
197  foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
198  # Skip generic * and implicit groups
199  if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
200  || $group == '*' ) {
201  continue;
202  }
203  $groupname = htmlspecialchars( $group );
204  $msg = $this->msg( 'group-' . $groupname );
205  if ( $msg->isBlank() ) {
206  $groupnameLocalized = $groupname;
207  } else {
208  $groupnameLocalized = $msg->text();
209  }
210  $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
211  if ( $msg->isBlank() ) {
212  $grouppageLocalized = MediaWikiServices::getInstance()->getNamespaceInfo()->
213  getCanonicalName( NS_PROJECT ) . ':' . $groupname;
214  } else {
215  $grouppageLocalized = $msg->text();
216  }
217  $linkTarget = Title::newFromText( $grouppageLocalized );
218 
219  if ( $linkTarget ) {
220  $grouppage = $linkRenderer->makeLink(
221  $linkTarget,
222  $groupnameLocalized
223  );
224  } else {
225  $grouppage = htmlspecialchars( $groupnameLocalized );
226  }
227 
228  $grouplink = $linkRenderer->makeKnownLink(
229  SpecialPage::getTitleFor( 'Listusers' ),
230  $this->msg( 'listgrouprights-members' )->text(),
231  [],
232  [ 'group' => $group ]
233  );
234  # Add a class when a usergroup contains no members to allow hiding these rows
235  $classZero = '';
236  $countUsers = SiteStats::numberingroup( $groupname );
237  if ( $countUsers == 0 ) {
238  $classZero = ' statistics-group-zero';
239  }
240  $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
241  $this->getLanguage()->formatNum( $countUsers ),
242  [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
243  $classZero ] );
244  }
245 
246  return $text;
247  }
248 
256  private function getOtherStats( array $stats ) {
257  $return = '';
258 
259  foreach ( $stats as $header => $items ) {
260  // Identify the structure used
261  if ( is_array( $items ) ) {
262  // Ignore headers that are recursively set as legacy header
263  if ( $header !== 'statistics-header-hooks' ) {
264  $return .= $this->formatRowHeader( $header );
265  }
266 
267  // Collect all items that belong to the same header
268  foreach ( $items as $key => $value ) {
269  if ( is_array( $value ) ) {
270  $name = $value['name'];
271  $number = $value['number'];
272  } else {
273  $name = $this->msg( $key )->parse();
274  $number = $value;
275  }
276 
277  $return .= $this->formatRow(
278  $name,
279  $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
280  [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
281  );
282  }
283  } else {
284  // Create the legacy header only once
285  if ( $return === '' ) {
286  $return .= $this->formatRowHeader( 'statistics-header-hooks' );
287  }
288 
289  // Recursively remap the legacy structure
290  $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
291  [ $header => $items ] ] );
292  }
293  }
294 
295  return $return;
296  }
297 
304  private function formatRowHeader( $header ) {
305  return Xml::openElement( 'tr' ) .
306  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
307  Xml::closeElement( 'tr' );
308  }
309 
310  protected function getGroupName() {
311  return 'wiki';
312  }
313 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
getPageStats()
Each of these methods is pretty self-explanatory, get a particular row for the table of statistics...
formatRowHeader( $header)
Format row header.
getContext()
Gets the context this SpecialPage is executed in.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
formatRow( $text, $number, $trExtraParams=[], $descMsg='', $descMsgParam='')
Format a row.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
$value
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
static activeUsers()
Definition: SiteStats.php:130
getOutput()
Get the OutputPage being used for this instance.
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
static numberingroup( $group)
Find the number of users in a given user group.
Definition: SiteStats.php:150
static edits()
Definition: SiteStats.php:94
const NS_PROJECT
Definition: Defines.php:64
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
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
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
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:130
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
static escapeClass( $class)
Given a value, escape it so that it can be used as a CSS class and return it.
Definition: Sanitizer.php:1411
Special page lists various statistics, including the contents of site_stats, plus page view details i...
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
$header
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
static images()
Definition: SiteStats.php:139
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:117
getOtherStats(array $stats)
Conversion of external statistics into an internal representation Following a ([<header-message>][<it...
static articles()
Definition: SiteStats.php:103
static pages()
Definition: SiteStats.php:112
getConfig()
Shortcut to get main config object.
getLanguage()
Shortcut to get user&#39;s language.
msg( $key)
Wrapper around wfMessage that sets the current context.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
$footer
static users()
Definition: SiteStats.php:121
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:67
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319