MediaWiki  1.28.0
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  $this->setHeaders();
40  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
41 
42  $this->edits = SiteStats::edits();
43  $this->good = SiteStats::articles();
44  $this->images = SiteStats::images();
45  $this->total = SiteStats::pages();
46  $this->users = SiteStats::users();
47  $this->activeUsers = SiteStats::activeUsers();
48  $this->hook = '';
49 
50  $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
51 
52  # Statistic - pages
53  $text .= $this->getPageStats();
54 
55  # Statistic - edits
56  $text .= $this->getEditStats();
57 
58  # Statistic - users
59  $text .= $this->getUserStats();
60 
61  # Statistic - usergroups
62  $text .= $this->getGroupStats();
63 
64  # Statistic - other
65  $extraStats = [];
66  if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
67  $text .= $this->getOtherStats( $extraStats );
68  }
69 
70  $text .= Xml::closeElement( 'table' );
71 
72  # Customizable footer
73  $footer = $this->msg( 'statistics-footer' );
74  if ( !$footer->isBlank() ) {
75  $text .= "\n" . $footer->parse();
76  }
77 
78  $this->getOutput()->addHTML( $text );
79  }
80 
90  private function formatRow( $text, $number, $trExtraParams = [],
91  $descMsg = '', $descMsgParam = ''
92  ) {
93  if ( $descMsg ) {
94  $msg = $this->msg( $descMsg, $descMsgParam );
95  if ( !$msg->isDisabled() ) {
96  $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
97  ->escaped();
98  $text .= "<br />" . Html::rawElement( 'small', [ 'class' => 'mw-statistic-desc' ],
99  " $descriptionHtml" );
100  }
101  }
102 
103  return Html::rawElement( 'tr', $trExtraParams,
104  Html::rawElement( 'td', [], $text ) .
105  Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
106  );
107  }
108 
114  private function getPageStats() {
115  $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
116  $pageStatsHtml = Xml::openElement( 'tr' ) .
117  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
118  ->parse() ) .
119  Xml::closeElement( 'tr' ) .
120  $this->formatRow( Linker::linkKnown( $specialAllPagesTitle,
121  $this->msg( 'statistics-articles' )->parse(), [], [ 'hideredirects' => 1 ] ),
122  $this->getLanguage()->formatNum( $this->good ),
123  [ 'class' => 'mw-statistics-articles' ],
124  'statistics-articles-desc' ) .
125  $this->formatRow( Linker::linkKnown( $specialAllPagesTitle,
126  $this->msg( 'statistics-pages' )->parse() ),
127  $this->getLanguage()->formatNum( $this->total ),
128  [ 'class' => 'mw-statistics-pages' ],
129  'statistics-pages-desc' );
130 
131  // Show the image row only, when there are files or upload is possible
132  if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
133  $pageStatsHtml .= $this->formatRow(
134  Linker::linkKnown( SpecialPage::getTitleFor( 'MediaStatistics' ),
135  $this->msg( 'statistics-files' )->parse() ),
136  $this->getLanguage()->formatNum( $this->images ),
137  [ 'class' => 'mw-statistics-files' ] );
138  }
139 
140  return $pageStatsHtml;
141  }
142 
143  private function getEditStats() {
144  return Xml::openElement( 'tr' ) .
145  Xml::tags( 'th', [ 'colspan' => '2' ],
146  $this->msg( 'statistics-header-edits' )->parse() ) .
147  Xml::closeElement( 'tr' ) .
148  $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
149  $this->getLanguage()->formatNum( $this->edits ),
150  [ 'class' => 'mw-statistics-edits' ]
151  ) .
152  $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
153  $this->getLanguage()
154  ->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
155  [ 'class' => 'mw-statistics-edits-average' ]
156  );
157  }
158 
159  private function getUserStats() {
160  return Xml::openElement( 'tr' ) .
161  Xml::tags( 'th', [ 'colspan' => '2' ],
162  $this->msg( 'statistics-header-users' )->parse() ) .
163  Xml::closeElement( 'tr' ) .
164  $this->formatRow( $this->msg( 'statistics-users' )->parse(),
165  $this->getLanguage()->formatNum( $this->users ),
166  [ 'class' => 'mw-statistics-users' ]
167  ) .
168  $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
170  SpecialPage::getTitleFor( 'Activeusers' ),
171  $this->msg( 'listgrouprights-members' )->escaped()
172  ),
173  $this->getLanguage()->formatNum( $this->activeUsers ),
174  [ 'class' => 'mw-statistics-users-active' ],
175  'statistics-users-active-desc',
176  $this->getLanguage()->formatNum( $this->getConfig()->get( 'ActiveUserDays' ) )
177  );
178  }
179 
180  private function getGroupStats() {
181  $text = '';
182  foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
183  # Skip generic * and implicit groups
184  if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) ) || $group == '*' ) {
185  continue;
186  }
187  $groupname = htmlspecialchars( $group );
188  $msg = $this->msg( 'group-' . $groupname );
189  if ( $msg->isBlank() ) {
190  $groupnameLocalized = $groupname;
191  } else {
192  $groupnameLocalized = $msg->text();
193  }
194  $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
195  if ( $msg->isBlank() ) {
196  $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
197  } else {
198  $grouppageLocalized = $msg->text();
199  }
200  $linkTarget = Title::newFromText( $grouppageLocalized );
201 
202  if ( $linkTarget ) {
203  $grouppage = Linker::link(
204  $linkTarget,
205  htmlspecialchars( $groupnameLocalized )
206  );
207  } else {
208  $grouppage = htmlspecialchars( $groupnameLocalized );
209  }
210 
211  $grouplink = Linker::linkKnown(
212  SpecialPage::getTitleFor( 'Listusers' ),
213  $this->msg( 'listgrouprights-members' )->escaped(),
214  [],
215  [ 'group' => $group ]
216  );
217  # Add a class when a usergroup contains no members to allow hiding these rows
218  $classZero = '';
219  $countUsers = SiteStats::numberingroup( $groupname );
220  if ( $countUsers == 0 ) {
221  $classZero = ' statistics-group-zero';
222  }
223  $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
224  $this->getLanguage()->formatNum( $countUsers ),
225  [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
226  $classZero ] );
227  }
228 
229  return $text;
230  }
231 
239  private function getOtherStats( array $stats ) {
240  $return = '';
241 
242  foreach ( $stats as $header => $items ) {
243  // Identify the structure used
244  if ( is_array( $items ) ) {
245 
246  // Ignore headers that are recursively set as legacy header
247  if ( $header !== 'statistics-header-hooks' ) {
248  $return .= $this->formatRowHeader( $header );
249  }
250 
251  // Collect all items that belong to the same header
252  foreach ( $items as $key => $value ) {
253  if ( is_array( $value ) ) {
254  $name = $value['name'];
255  $number = $value['number'];
256  } else {
257  $name = $this->msg( $key )->parse();
258  $number = $value;
259  }
260 
261  $return .= $this->formatRow(
262  $name,
263  $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
264  [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
265  );
266  }
267  } else {
268  // Create the legacy header only once
269  if ( $return === '' ) {
270  $return .= $this->formatRowHeader( 'statistics-header-hooks' );
271  }
272 
273  // Recursively remap the legacy structure
274  $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
275  [ $header => $items ] ] );
276  }
277  }
278 
279  return $return;
280  }
281 
288  private function formatRowHeader( $header ) {
289  return Xml::openElement( 'tr' ) .
290  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
291  Xml::closeElement( 'tr' );
292  }
293 
294  protected function getGroupName() {
295  return 'wiki';
296  }
297 }
getPageStats()
Each of these methods is pretty self-explanatory, get a particular row for the table of statistics...
the array() calling protocol came about after MediaWiki 1.4rc1.
getContext()
Gets the context this SpecialPage is executed in.
static getTitleFor($name, $subpage=false, $fragment= '')
Get a localised Title object for a specified special page name If you don't need a full Title object...
Definition: SpecialPage.php:82
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:209
$value
static escapeClass($class)
Given a value, escape it so that it can be used as a CSS class and return it.
Definition: Sanitizer.php:1247
static activeUsers()
Definition: SiteStats.php:165
msg()
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
formatRowHeader($header)
Format row header.
static edits()
Definition: SiteStats.php:133
static closeElement($element)
Shortcut to close an XML element.
Definition: Xml.php:118
Parent class for all special pages.
Definition: SpecialPage.php:36
const NS_PROJECT
Definition: Defines.php:60
static getCanonicalName($index)
Returns the canonical (English) name for a given index.
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
static openElement($element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
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
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
static linkKnown($target, $html=null, $customAttribs=[], $query=[], $options=[ 'known'])
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:255
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
Special page lists various statistics, including the contents of site_stats, plus page view details i...
$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 link($target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition: Linker.php:203
static images()
Definition: SiteStats.php:173
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 tags($element, $attribs=null, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:131
getOtherStats(array $stats)
Conversion of external statistics into an internal representation Following a ([][
static articles()
Definition: SiteStats.php:141
formatRow($text, $number, $trExtraParams=[], $descMsg= '', $descMsgParam= '')
Format a row.
static pages()
Definition: SiteStats.php:149
getConfig()
Shortcut to get main config object.
getLanguage()
Shortcut to get user's language.
static numberingroup($group)
Find the number of users in a given user group.
Definition: SiteStats.php:183
$footer
static users()
Definition: SiteStats.php:157
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300