123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- /**
- * Implements Special:Statistics
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @ingroup SpecialPage
- */
- use MediaWiki\MediaWikiServices;
- /**
- * Special page lists various statistics, including the contents of
- * `site_stats`, plus page view details if enabled
- *
- * @ingroup SpecialPage
- */
- class SpecialStatistics extends SpecialPage {
- private $edits, $good, $images, $total, $users,
- $activeUsers = 0;
- public function __construct() {
- parent::__construct( 'Statistics' );
- }
- public function execute( $par ) {
- $this->setHeaders();
- $this->getOutput()->addModuleStyles( 'mediawiki.special' );
- $this->edits = SiteStats::edits();
- $this->good = SiteStats::articles();
- $this->images = SiteStats::images();
- $this->total = SiteStats::pages();
- $this->users = SiteStats::users();
- $this->activeUsers = SiteStats::activeUsers();
- $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
- # Statistic - pages
- $text .= $this->getPageStats();
- # Statistic - edits
- $text .= $this->getEditStats();
- # Statistic - users
- $text .= $this->getUserStats();
- # Statistic - usergroups
- $text .= $this->getGroupStats();
- # Statistic - other
- $extraStats = [];
- if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
- $text .= $this->getOtherStats( $extraStats );
- }
- $text .= Xml::closeElement( 'table' );
- # Customizable footer
- $footer = $this->msg( 'statistics-footer' );
- if ( !$footer->isBlank() ) {
- $text .= "\n" . $footer->parse();
- }
- $this->getOutput()->addHTML( $text );
- }
- /**
- * Format a row
- * @param string $text Description of the row
- * @param float $number A statistical number
- * @param array $trExtraParams Params to table row, see Html::elememt
- * @param string $descMsg Message key
- * @param array|string $descMsgParam Message parameters
- * @return string Table row in HTML format
- */
- private function formatRow( $text, $number, $trExtraParams = [],
- $descMsg = '', $descMsgParam = ''
- ) {
- if ( $descMsg ) {
- $msg = $this->msg( $descMsg, $descMsgParam );
- if ( !$msg->isDisabled() ) {
- $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
- ->escaped();
- $text .= "<br />" . Html::rawElement(
- 'small',
- [ 'class' => 'mw-statistic-desc' ],
- " $descriptionHtml"
- );
- }
- }
- return Html::rawElement( 'tr', $trExtraParams,
- Html::rawElement( 'td', [], $text ) .
- Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
- );
- }
- /**
- * Each of these methods is pretty self-explanatory, get a particular
- * row for the table of statistics
- * @return string
- */
- private function getPageStats() {
- $linkRenderer = $this->getLinkRenderer();
- $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
- $pageStatsHtml = Xml::openElement( 'tr' ) .
- Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
- ->parse() ) .
- Xml::closeElement( 'tr' ) .
- $this->formatRow( $linkRenderer->makeKnownLink(
- $specialAllPagesTitle,
- $this->msg( 'statistics-articles' )->text(),
- [], [ 'hideredirects' => 1 ] ),
- $this->getLanguage()->formatNum( $this->good ),
- [ 'class' => 'mw-statistics-articles' ],
- 'statistics-articles-desc' ) .
- $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
- $this->msg( 'statistics-pages' )->text() ),
- $this->getLanguage()->formatNum( $this->total ),
- [ 'class' => 'mw-statistics-pages' ],
- 'statistics-pages-desc' );
- // Show the image row only, when there are files or upload is possible
- if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
- $pageStatsHtml .= $this->formatRow(
- $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
- $this->msg( 'statistics-files' )->text() ),
- $this->getLanguage()->formatNum( $this->images ),
- [ 'class' => 'mw-statistics-files' ] );
- }
- return $pageStatsHtml;
- }
- private function getEditStats() {
- return Xml::openElement( 'tr' ) .
- Xml::tags( 'th', [ 'colspan' => '2' ],
- $this->msg( 'statistics-header-edits' )->parse() ) .
- Xml::closeElement( 'tr' ) .
- $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
- $this->getLanguage()->formatNum( $this->edits ),
- [ 'class' => 'mw-statistics-edits' ]
- ) .
- $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
- $this->getLanguage()->formatNum(
- sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
- ), [ 'class' => 'mw-statistics-edits-average' ]
- );
- }
- private function getUserStats() {
- return Xml::openElement( 'tr' ) .
- Xml::tags( 'th', [ 'colspan' => '2' ],
- $this->msg( 'statistics-header-users' )->parse() ) .
- Xml::closeElement( 'tr' ) .
- $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
- $this->getLinkRenderer()->makeKnownLink(
- SpecialPage::getTitleFor( 'Listusers' ),
- $this->msg( 'listgrouprights-members' )->text()
- ),
- $this->getLanguage()->formatNum( $this->users ),
- [ 'class' => 'mw-statistics-users' ]
- ) .
- $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
- $this->getLinkRenderer()->makeKnownLink(
- SpecialPage::getTitleFor( 'Activeusers' ),
- $this->msg( 'listgrouprights-members' )->text()
- ),
- $this->getLanguage()->formatNum( $this->activeUsers ),
- [ 'class' => 'mw-statistics-users-active' ],
- 'statistics-users-active-desc',
- $this->getLanguage()->formatNum(
- $this->getConfig()->get( 'ActiveUserDays' ) )
- );
- }
- private function getGroupStats() {
- $linkRenderer = $this->getLinkRenderer();
- $text = '';
- foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
- # Skip generic * and implicit groups
- if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
- || $group == '*' ) {
- continue;
- }
- $groupname = htmlspecialchars( $group );
- $msg = $this->msg( 'group-' . $groupname );
- if ( $msg->isBlank() ) {
- $groupnameLocalized = $groupname;
- } else {
- $groupnameLocalized = $msg->text();
- }
- $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
- if ( $msg->isBlank() ) {
- $grouppageLocalized = MediaWikiServices::getInstance()->getNamespaceInfo()->
- getCanonicalName( NS_PROJECT ) . ':' . $groupname;
- } else {
- $grouppageLocalized = $msg->text();
- }
- $linkTarget = Title::newFromText( $grouppageLocalized );
- if ( $linkTarget ) {
- $grouppage = $linkRenderer->makeLink(
- $linkTarget,
- $groupnameLocalized
- );
- } else {
- $grouppage = htmlspecialchars( $groupnameLocalized );
- }
- $grouplink = $linkRenderer->makeKnownLink(
- SpecialPage::getTitleFor( 'Listusers' ),
- $this->msg( 'listgrouprights-members' )->text(),
- [],
- [ 'group' => $group ]
- );
- # Add a class when a usergroup contains no members to allow hiding these rows
- $classZero = '';
- $countUsers = SiteStats::numberingroup( $groupname );
- if ( $countUsers == 0 ) {
- $classZero = ' statistics-group-zero';
- }
- $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
- $this->getLanguage()->formatNum( $countUsers ),
- [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
- $classZero ] );
- }
- return $text;
- }
- /**
- * Conversion of external statistics into an internal representation
- * Following a ([<header-message>][<item-message>] = number) pattern
- *
- * @param array $stats
- * @return string
- */
- private function getOtherStats( array $stats ) {
- $return = '';
- foreach ( $stats as $header => $items ) {
- // Identify the structure used
- if ( is_array( $items ) ) {
- // Ignore headers that are recursively set as legacy header
- if ( $header !== 'statistics-header-hooks' ) {
- $return .= $this->formatRowHeader( $header );
- }
- // Collect all items that belong to the same header
- foreach ( $items as $key => $value ) {
- if ( is_array( $value ) ) {
- $name = $value['name'];
- $number = $value['number'];
- } else {
- $name = $this->msg( $key )->parse();
- $number = $value;
- }
- $return .= $this->formatRow(
- $name,
- $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
- [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
- );
- }
- } else {
- // Create the legacy header only once
- if ( $return === '' ) {
- $return .= $this->formatRowHeader( 'statistics-header-hooks' );
- }
- // Recursively remap the legacy structure
- $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
- [ $header => $items ] ] );
- }
- }
- return $return;
- }
- /**
- * Format row header
- *
- * @param string $header
- * @return string
- */
- private function formatRowHeader( $header ) {
- return Xml::openElement( 'tr' ) .
- Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
- Xml::closeElement( 'tr' );
- }
- protected function getGroupName() {
- return 'wiki';
- }
- }
|