SpecialStatistics.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Implements Special:Statistics
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. /**
  25. * Special page lists various statistics, including the contents of
  26. * `site_stats`, plus page view details if enabled
  27. *
  28. * @ingroup SpecialPage
  29. */
  30. class SpecialStatistics extends SpecialPage {
  31. private $edits, $good, $images, $total, $users,
  32. $activeUsers = 0;
  33. public function __construct() {
  34. parent::__construct( 'Statistics' );
  35. }
  36. public function execute( $par ) {
  37. $this->setHeaders();
  38. $this->getOutput()->addModuleStyles( 'mediawiki.special' );
  39. $this->edits = SiteStats::edits();
  40. $this->good = SiteStats::articles();
  41. $this->images = SiteStats::images();
  42. $this->total = SiteStats::pages();
  43. $this->users = SiteStats::users();
  44. $this->activeUsers = SiteStats::activeUsers();
  45. $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
  46. # Statistic - pages
  47. $text .= $this->getPageStats();
  48. # Statistic - edits
  49. $text .= $this->getEditStats();
  50. # Statistic - users
  51. $text .= $this->getUserStats();
  52. # Statistic - usergroups
  53. $text .= $this->getGroupStats();
  54. # Statistic - other
  55. $extraStats = [];
  56. if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
  57. $text .= $this->getOtherStats( $extraStats );
  58. }
  59. $text .= Xml::closeElement( 'table' );
  60. # Customizable footer
  61. $footer = $this->msg( 'statistics-footer' );
  62. if ( !$footer->isBlank() ) {
  63. $text .= "\n" . $footer->parse();
  64. }
  65. $this->getOutput()->addHTML( $text );
  66. }
  67. /**
  68. * Format a row
  69. * @param string $text Description of the row
  70. * @param float $number A statistical number
  71. * @param array $trExtraParams Params to table row, see Html::elememt
  72. * @param string $descMsg Message key
  73. * @param array|string $descMsgParam Message parameters
  74. * @return string Table row in HTML format
  75. */
  76. private function formatRow( $text, $number, $trExtraParams = [],
  77. $descMsg = '', $descMsgParam = ''
  78. ) {
  79. if ( $descMsg ) {
  80. $msg = $this->msg( $descMsg, $descMsgParam );
  81. if ( !$msg->isDisabled() ) {
  82. $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
  83. ->escaped();
  84. $text .= "<br />" . Html::rawElement(
  85. 'small',
  86. [ 'class' => 'mw-statistic-desc' ],
  87. " $descriptionHtml"
  88. );
  89. }
  90. }
  91. return Html::rawElement( 'tr', $trExtraParams,
  92. Html::rawElement( 'td', [], $text ) .
  93. Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
  94. );
  95. }
  96. /**
  97. * Each of these methods is pretty self-explanatory, get a particular
  98. * row for the table of statistics
  99. * @return string
  100. */
  101. private function getPageStats() {
  102. $linkRenderer = $this->getLinkRenderer();
  103. $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
  104. $pageStatsHtml = Xml::openElement( 'tr' ) .
  105. Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
  106. ->parse() ) .
  107. Xml::closeElement( 'tr' ) .
  108. $this->formatRow( $linkRenderer->makeKnownLink(
  109. $specialAllPagesTitle,
  110. $this->msg( 'statistics-articles' )->text(),
  111. [], [ 'hideredirects' => 1 ] ),
  112. $this->getLanguage()->formatNum( $this->good ),
  113. [ 'class' => 'mw-statistics-articles' ],
  114. 'statistics-articles-desc' ) .
  115. $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
  116. $this->msg( 'statistics-pages' )->text() ),
  117. $this->getLanguage()->formatNum( $this->total ),
  118. [ 'class' => 'mw-statistics-pages' ],
  119. 'statistics-pages-desc' );
  120. // Show the image row only, when there are files or upload is possible
  121. if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
  122. $pageStatsHtml .= $this->formatRow(
  123. $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
  124. $this->msg( 'statistics-files' )->text() ),
  125. $this->getLanguage()->formatNum( $this->images ),
  126. [ 'class' => 'mw-statistics-files' ] );
  127. }
  128. return $pageStatsHtml;
  129. }
  130. private function getEditStats() {
  131. return Xml::openElement( 'tr' ) .
  132. Xml::tags( 'th', [ 'colspan' => '2' ],
  133. $this->msg( 'statistics-header-edits' )->parse() ) .
  134. Xml::closeElement( 'tr' ) .
  135. $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
  136. $this->getLanguage()->formatNum( $this->edits ),
  137. [ 'class' => 'mw-statistics-edits' ]
  138. ) .
  139. $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
  140. $this->getLanguage()->formatNum(
  141. sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
  142. ), [ 'class' => 'mw-statistics-edits-average' ]
  143. );
  144. }
  145. private function getUserStats() {
  146. return Xml::openElement( 'tr' ) .
  147. Xml::tags( 'th', [ 'colspan' => '2' ],
  148. $this->msg( 'statistics-header-users' )->parse() ) .
  149. Xml::closeElement( 'tr' ) .
  150. $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
  151. $this->getLinkRenderer()->makeKnownLink(
  152. SpecialPage::getTitleFor( 'Listusers' ),
  153. $this->msg( 'listgrouprights-members' )->text()
  154. ),
  155. $this->getLanguage()->formatNum( $this->users ),
  156. [ 'class' => 'mw-statistics-users' ]
  157. ) .
  158. $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
  159. $this->getLinkRenderer()->makeKnownLink(
  160. SpecialPage::getTitleFor( 'Activeusers' ),
  161. $this->msg( 'listgrouprights-members' )->text()
  162. ),
  163. $this->getLanguage()->formatNum( $this->activeUsers ),
  164. [ 'class' => 'mw-statistics-users-active' ],
  165. 'statistics-users-active-desc',
  166. $this->getLanguage()->formatNum(
  167. $this->getConfig()->get( 'ActiveUserDays' ) )
  168. );
  169. }
  170. private function getGroupStats() {
  171. $linkRenderer = $this->getLinkRenderer();
  172. $text = '';
  173. foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
  174. # Skip generic * and implicit groups
  175. if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
  176. || $group == '*' ) {
  177. continue;
  178. }
  179. $groupname = htmlspecialchars( $group );
  180. $msg = $this->msg( 'group-' . $groupname );
  181. if ( $msg->isBlank() ) {
  182. $groupnameLocalized = $groupname;
  183. } else {
  184. $groupnameLocalized = $msg->text();
  185. }
  186. $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
  187. if ( $msg->isBlank() ) {
  188. $grouppageLocalized = MediaWikiServices::getInstance()->getNamespaceInfo()->
  189. getCanonicalName( NS_PROJECT ) . ':' . $groupname;
  190. } else {
  191. $grouppageLocalized = $msg->text();
  192. }
  193. $linkTarget = Title::newFromText( $grouppageLocalized );
  194. if ( $linkTarget ) {
  195. $grouppage = $linkRenderer->makeLink(
  196. $linkTarget,
  197. $groupnameLocalized
  198. );
  199. } else {
  200. $grouppage = htmlspecialchars( $groupnameLocalized );
  201. }
  202. $grouplink = $linkRenderer->makeKnownLink(
  203. SpecialPage::getTitleFor( 'Listusers' ),
  204. $this->msg( 'listgrouprights-members' )->text(),
  205. [],
  206. [ 'group' => $group ]
  207. );
  208. # Add a class when a usergroup contains no members to allow hiding these rows
  209. $classZero = '';
  210. $countUsers = SiteStats::numberingroup( $groupname );
  211. if ( $countUsers == 0 ) {
  212. $classZero = ' statistics-group-zero';
  213. }
  214. $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
  215. $this->getLanguage()->formatNum( $countUsers ),
  216. [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
  217. $classZero ] );
  218. }
  219. return $text;
  220. }
  221. /**
  222. * Conversion of external statistics into an internal representation
  223. * Following a ([<header-message>][<item-message>] = number) pattern
  224. *
  225. * @param array $stats
  226. * @return string
  227. */
  228. private function getOtherStats( array $stats ) {
  229. $return = '';
  230. foreach ( $stats as $header => $items ) {
  231. // Identify the structure used
  232. if ( is_array( $items ) ) {
  233. // Ignore headers that are recursively set as legacy header
  234. if ( $header !== 'statistics-header-hooks' ) {
  235. $return .= $this->formatRowHeader( $header );
  236. }
  237. // Collect all items that belong to the same header
  238. foreach ( $items as $key => $value ) {
  239. if ( is_array( $value ) ) {
  240. $name = $value['name'];
  241. $number = $value['number'];
  242. } else {
  243. $name = $this->msg( $key )->parse();
  244. $number = $value;
  245. }
  246. $return .= $this->formatRow(
  247. $name,
  248. $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
  249. [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
  250. );
  251. }
  252. } else {
  253. // Create the legacy header only once
  254. if ( $return === '' ) {
  255. $return .= $this->formatRowHeader( 'statistics-header-hooks' );
  256. }
  257. // Recursively remap the legacy structure
  258. $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
  259. [ $header => $items ] ] );
  260. }
  261. }
  262. return $return;
  263. }
  264. /**
  265. * Format row header
  266. *
  267. * @param string $header
  268. * @return string
  269. */
  270. private function formatRowHeader( $header ) {
  271. return Xml::openElement( 'tr' ) .
  272. Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
  273. Xml::closeElement( 'tr' );
  274. }
  275. protected function getGroupName() {
  276. return 'wiki';
  277. }
  278. }