MediaWiki  1.30.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(
99  'small',
100  [ 'class' => 'mw-statistic-desc' ],
101  " $descriptionHtml"
102  );
103  }
104  }
105 
106  return Html::rawElement( 'tr', $trExtraParams,
107  Html::rawElement( 'td', [], $text ) .
108  Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
109  );
110  }
111 
117  private function getPageStats() {
118  $linkRenderer = $this->getLinkRenderer();
119 
120  $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
121  $pageStatsHtml = Xml::openElement( 'tr' ) .
122  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
123  ->parse() ) .
124  Xml::closeElement( 'tr' ) .
125  $this->formatRow( $linkRenderer->makeKnownLink(
126  $specialAllPagesTitle,
127  $this->msg( 'statistics-articles' )->text(),
128  [], [ 'hideredirects' => 1 ] ),
129  $this->getLanguage()->formatNum( $this->good ),
130  [ 'class' => 'mw-statistics-articles' ],
131  'statistics-articles-desc' ) .
132  $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
133  $this->msg( 'statistics-pages' )->text() ),
134  $this->getLanguage()->formatNum( $this->total ),
135  [ 'class' => 'mw-statistics-pages' ],
136  'statistics-pages-desc' );
137 
138  // Show the image row only, when there are files or upload is possible
139  if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
140  $pageStatsHtml .= $this->formatRow(
141  $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
142  $this->msg( 'statistics-files' )->text() ),
143  $this->getLanguage()->formatNum( $this->images ),
144  [ 'class' => 'mw-statistics-files' ] );
145  }
146 
147  return $pageStatsHtml;
148  }
149 
150  private function getEditStats() {
151  return Xml::openElement( 'tr' ) .
152  Xml::tags( 'th', [ 'colspan' => '2' ],
153  $this->msg( 'statistics-header-edits' )->parse() ) .
154  Xml::closeElement( 'tr' ) .
155  $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
156  $this->getLanguage()->formatNum( $this->edits ),
157  [ 'class' => 'mw-statistics-edits' ]
158  ) .
159  $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
160  $this->getLanguage()->formatNum(
161  sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
162  ), [ 'class' => 'mw-statistics-edits-average' ]
163  );
164  }
165 
166  private function getUserStats() {
167  return Xml::openElement( 'tr' ) .
168  Xml::tags( 'th', [ 'colspan' => '2' ],
169  $this->msg( 'statistics-header-users' )->parse() ) .
170  Xml::closeElement( 'tr' ) .
171  $this->formatRow( $this->msg( 'statistics-users' )->parse(),
172  $this->getLanguage()->formatNum( $this->users ),
173  [ 'class' => 'mw-statistics-users' ]
174  ) .
175  $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
176  $this->getLinkRenderer()->makeKnownLink(
177  SpecialPage::getTitleFor( 'Activeusers' ),
178  $this->msg( 'listgrouprights-members' )->text()
179  ),
180  $this->getLanguage()->formatNum( $this->activeUsers ),
181  [ 'class' => 'mw-statistics-users-active' ],
182  'statistics-users-active-desc',
183  $this->getLanguage()->formatNum(
184  $this->getConfig()->get( 'ActiveUserDays' ) )
185  );
186  }
187 
188  private function getGroupStats() {
189  $linkRenderer = $this->getLinkRenderer();
190  $text = '';
191  foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
192  # Skip generic * and implicit groups
193  if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
194  || $group == '*' ) {
195  continue;
196  }
197  $groupname = htmlspecialchars( $group );
198  $msg = $this->msg( 'group-' . $groupname );
199  if ( $msg->isBlank() ) {
200  $groupnameLocalized = $groupname;
201  } else {
202  $groupnameLocalized = $msg->text();
203  }
204  $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
205  if ( $msg->isBlank() ) {
206  $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) .
207  ':' . $groupname;
208  } else {
209  $grouppageLocalized = $msg->text();
210  }
211  $linkTarget = Title::newFromText( $grouppageLocalized );
212 
213  if ( $linkTarget ) {
214  $grouppage = $linkRenderer->makeLink(
215  $linkTarget,
216  $groupnameLocalized
217  );
218  } else {
219  $grouppage = htmlspecialchars( $groupnameLocalized );
220  }
221 
222  $grouplink = $linkRenderer->makeKnownLink(
223  SpecialPage::getTitleFor( 'Listusers' ),
224  $this->msg( 'listgrouprights-members' )->text(),
225  [],
226  [ 'group' => $group ]
227  );
228  # Add a class when a usergroup contains no members to allow hiding these rows
229  $classZero = '';
230  $countUsers = SiteStats::numberingroup( $groupname );
231  if ( $countUsers == 0 ) {
232  $classZero = ' statistics-group-zero';
233  }
234  $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
235  $this->getLanguage()->formatNum( $countUsers ),
236  [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
237  $classZero ] );
238  }
239 
240  return $text;
241  }
242 
250  private function getOtherStats( array $stats ) {
251  $return = '';
252 
253  foreach ( $stats as $header => $items ) {
254  // Identify the structure used
255  if ( is_array( $items ) ) {
256  // Ignore headers that are recursively set as legacy header
257  if ( $header !== 'statistics-header-hooks' ) {
258  $return .= $this->formatRowHeader( $header );
259  }
260 
261  // Collect all items that belong to the same header
262  foreach ( $items as $key => $value ) {
263  if ( is_array( $value ) ) {
264  $name = $value['name'];
265  $number = $value['number'];
266  } else {
267  $name = $this->msg( $key )->parse();
268  $number = $value;
269  }
270 
271  $return .= $this->formatRow(
272  $name,
273  $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
274  [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
275  );
276  }
277  } else {
278  // Create the legacy header only once
279  if ( $return === '' ) {
280  $return .= $this->formatRowHeader( 'statistics-header-hooks' );
281  }
282 
283  // Recursively remap the legacy structure
284  $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
285  [ $header => $items ] ] );
286  }
287  }
288 
289  return $return;
290  }
291 
298  private function formatRowHeader( $header ) {
299  return Xml::openElement( 'tr' ) .
300  Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
301  Xml::closeElement( 'tr' );
302  }
303 
304  protected function getGroupName() {
305  return 'wiki';
306  }
307 }
SiteStats\articles
static articles()
Definition: SiteStats.php:146
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:268
SpecialPage\msg
msg( $key)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:746
SiteStats\users
static users()
Definition: SiteStats.php:162
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:170
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:675
SpecialStatistics\$edits
$edits
Definition: SpecialStatistics.php:31
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
SiteStats\pages
static pages()
Definition: SiteStats.php:154
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:188
SpecialStatistics\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialStatistics.php:304
SpecialPage\getTitleFor
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
SpecialStatistics\$good
$good
Definition: SpecialStatistics.php:31
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:705
SpecialStatistics\$activeUsers
$activeUsers
Definition: SpecialStatistics.php:32
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
php
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
SpecialStatistics\$total
$total
Definition: SpecialStatistics.php:31
SiteStats\images
static images()
Definition: SiteStats.php:178
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
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:714
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:69
SpecialStatistics\getGroupStats
getGroupStats()
Definition: SpecialStatistics.php:188
SpecialStatistics\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialStatistics.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:484
SpecialStatistics\__construct
__construct()
Definition: SpecialStatistics.php:34
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
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:648
SpecialStatistics\getUserStats
getUserStats()
Definition: SpecialStatistics.php:166
SpecialStatistics\formatRow
formatRow( $text, $number, $trExtraParams=[], $descMsg='', $descMsgParam='')
Format a row.
Definition: SpecialStatistics.php:90
$value
$value
Definition: styleTest.css.php:45
$header
$header
Definition: updateCredits.php:35
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
SpecialStatistics\$users
$users
Definition: SpecialStatistics.php:31
SpecialStatistics\getEditStats
getEditStats()
Definition: SpecialStatistics.php:150
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:860
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:298
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
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
SpecialStatistics\getPageStats
getPageStats()
Each of these methods is pretty self-explanatory, get a particular row for the table of statistics.
Definition: SpecialStatistics.php:117
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:250
SpecialPage\$linkRenderer
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:66
$footer
$footer
Definition: updateCredits.php:37
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
MWNamespace\getCanonicalName
static getCanonicalName( $index)
Returns the canonical (English) name for a given index.
Definition: MWNamespace.php:228
SiteStats\edits
static edits()
Definition: SiteStats.php:138
array
the array() calling protocol came about after MediaWiki 1.4rc1.