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 
51  $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
52 
53  # Statistic - pages
54  $text .= $this->getPageStats();
55 
56  # Statistic - edits
57  $text .= $this->getEditStats();
58 
59  # Statistic - users
60  $text .= $this->getUserStats();
61 
62  # Statistic - usergroups
63  $text .= $this->getGroupStats();
64 
65  # Statistic - other
66  $extraStats = [];
67  if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
68  $text .= $this->getOtherStats( $extraStats );
69  }
70 
71  $text .= Xml::closeElement( 'table' );
72 
73  # Customizable footer
74  $footer = $this->msg( 'statistics-footer' );
75  if ( !$footer->isBlank() ) {
76  $text .= "\n" . $footer->parse();
77  }
78 
79  $this->getOutput()->addHTML( $text );
80  }
81 
91  private function formatRow( $text, $number, $trExtraParams = [],
92  $descMsg = '', $descMsgParam = ''
93  ) {
94  if ( $descMsg ) {
95  $msg = $this->msg( $descMsg, $descMsgParam );
96  if ( !$msg->isDisabled() ) {
97  $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
98  ->escaped();
99  $text .= "<br />" . Html::rawElement(
100  'small',
101  [ 'class' => 'mw-statistic-desc' ],
102  " $descriptionHtml"
103  );
104  }
105  }
106 
107  return Html::rawElement( 'tr', $trExtraParams,
108  Html::rawElement( 'td', [], $text ) .
109  Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
110  );
111  }
112 
118  private function getPageStats() {
119  $linkRenderer = $this->getLinkRenderer();
120 
121  $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
122  $pageStatsHtml = Xml::openElement( 'tr' ) .
123  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
124  ->parse() ) .
125  Xml::closeElement( 'tr' ) .
126  $this->formatRow( $linkRenderer->makeKnownLink(
127  $specialAllPagesTitle,
128  $this->msg( 'statistics-articles' )->text(),
129  [], [ 'hideredirects' => 1 ] ),
130  $this->getLanguage()->formatNum( $this->good ),
131  [ 'class' => 'mw-statistics-articles' ],
132  'statistics-articles-desc' ) .
133  $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
134  $this->msg( 'statistics-pages' )->text() ),
135  $this->getLanguage()->formatNum( $this->total ),
136  [ 'class' => 'mw-statistics-pages' ],
137  'statistics-pages-desc' );
138 
139  // Show the image row only, when there are files or upload is possible
140  if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
141  $pageStatsHtml .= $this->formatRow(
142  $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
143  $this->msg( 'statistics-files' )->text() ),
144  $this->getLanguage()->formatNum( $this->images ),
145  [ 'class' => 'mw-statistics-files' ] );
146  }
147 
148  return $pageStatsHtml;
149  }
150 
151  private function getEditStats() {
152  return Xml::openElement( 'tr' ) .
153  Xml::tags( 'th', [ 'colspan' => '2' ],
154  $this->msg( 'statistics-header-edits' )->parse() ) .
155  Xml::closeElement( 'tr' ) .
156  $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
157  $this->getLanguage()->formatNum( $this->edits ),
158  [ 'class' => 'mw-statistics-edits' ]
159  ) .
160  $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
161  $this->getLanguage()->formatNum(
162  sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
163  ), [ 'class' => 'mw-statistics-edits-average' ]
164  );
165  }
166 
167  private function getUserStats() {
168  return Xml::openElement( 'tr' ) .
169  Xml::tags( 'th', [ 'colspan' => '2' ],
170  $this->msg( 'statistics-header-users' )->parse() ) .
171  Xml::closeElement( 'tr' ) .
172  $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
173  $this->getLinkRenderer()->makeKnownLink(
174  SpecialPage::getTitleFor( 'Listusers' ),
175  $this->msg( 'listgrouprights-members' )->text()
176  ),
177  $this->getLanguage()->formatNum( $this->users ),
178  [ 'class' => 'mw-statistics-users' ]
179  ) .
180  $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
181  $this->getLinkRenderer()->makeKnownLink(
182  SpecialPage::getTitleFor( 'Activeusers' ),
183  $this->msg( 'listgrouprights-members' )->text()
184  ),
185  $this->getLanguage()->formatNum( $this->activeUsers ),
186  [ 'class' => 'mw-statistics-users-active' ],
187  'statistics-users-active-desc',
188  $this->getLanguage()->formatNum(
189  $this->getConfig()->get( 'ActiveUserDays' ) )
190  );
191  }
192 
193  private function getGroupStats() {
194  $linkRenderer = $this->getLinkRenderer();
195  $text = '';
196  foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
197  # Skip generic * and implicit groups
198  if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
199  || $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 = MediaWikiServices::getInstance()->getNamespaceInfo()->
212  getCanonicalName( NS_PROJECT ) . ':' . $groupname;
213  } else {
214  $grouppageLocalized = $msg->text();
215  }
216  $linkTarget = Title::newFromText( $grouppageLocalized );
217 
218  if ( $linkTarget ) {
219  $grouppage = $linkRenderer->makeLink(
220  $linkTarget,
221  $groupnameLocalized
222  );
223  } else {
224  $grouppage = htmlspecialchars( $groupnameLocalized );
225  }
226 
227  $grouplink = $linkRenderer->makeKnownLink(
228  SpecialPage::getTitleFor( 'Listusers' ),
229  $this->msg( 'listgrouprights-members' )->text(),
230  [],
231  [ 'group' => $group ]
232  );
233  # Add a class when a usergroup contains no members to allow hiding these rows
234  $classZero = '';
235  $countUsers = SiteStats::numberingroup( $groupname );
236  if ( $countUsers == 0 ) {
237  $classZero = ' statistics-group-zero';
238  }
239  $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
240  $this->getLanguage()->formatNum( $countUsers ),
241  [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
242  $classZero ] );
243  }
244 
245  return $text;
246  }
247 
255  private function getOtherStats( array $stats ) {
256  $return = '';
257 
258  foreach ( $stats as $header => $items ) {
259  // Identify the structure used
260  if ( is_array( $items ) ) {
261  // Ignore headers that are recursively set as legacy header
262  if ( $header !== 'statistics-header-hooks' ) {
263  $return .= $this->formatRowHeader( $header );
264  }
265 
266  // Collect all items that belong to the same header
267  foreach ( $items as $key => $value ) {
268  if ( is_array( $value ) ) {
269  $name = $value['name'];
270  $number = $value['number'];
271  } else {
272  $name = $this->msg( $key )->parse();
273  $number = $value;
274  }
275 
276  $return .= $this->formatRow(
277  $name,
278  $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
279  [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
280  );
281  }
282  } else {
283  // Create the legacy header only once
284  if ( $return === '' ) {
285  $return .= $this->formatRowHeader( 'statistics-header-hooks' );
286  }
287 
288  // Recursively remap the legacy structure
289  $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
290  [ $header => $items ] ] );
291  }
292  }
293 
294  return $return;
295  }
296 
303  private function formatRowHeader( $header ) {
304  return Xml::openElement( 'tr' ) .
305  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
306  Xml::closeElement( 'tr' );
307  }
308 
309  protected function getGroupName() {
310  return 'wiki';
311  }
312 }
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.
formatRow( $text, $number, $trExtraParams=[], $descMsg='', $descMsgParam='')
Format a row.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
static activeUsers()
Definition: SiteStats.php:130
getOutput()
Get the OutputPage being used for this instance.
static numberingroup( $group)
Find the number of users in a given user group.
Definition: SiteStats.php:150
static edits()
Definition: SiteStats.php:94
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
const NS_PROJECT
Definition: Defines.php:64
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
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:1418
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
static images()
Definition: SiteStats.php:139
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.
$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