SpecialStatistics.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Special page lists various statistics, including the contents of
  4. * `site_stats`, plus page view details if enabled
  5. *
  6. * @file
  7. * @ingroup SpecialPage
  8. */
  9. /**
  10. * Show the special page
  11. *
  12. * @param mixed $par (not used)
  13. */
  14. class SpecialStatistics extends SpecialPage {
  15. private $views, $edits, $good, $images, $total, $users,
  16. $activeUsers, $admins, $numJobs = 0;
  17. public function __construct() {
  18. parent::__construct( 'Statistics' );
  19. }
  20. public function execute( $par ) {
  21. global $wgOut, $wgRequest, $wgMessageCache;
  22. global $wgDisableCounters, $wgMiserMode;
  23. $wgMessageCache->loadAllMessages();
  24. $this->setHeaders();
  25. $this->views = SiteStats::views();
  26. $this->edits = SiteStats::edits();
  27. $this->good = SiteStats::articles();
  28. $this->images = SiteStats::images();
  29. $this->total = SiteStats::pages();
  30. $this->users = SiteStats::users();
  31. $this->activeUsers = SiteStats::activeUsers();
  32. $this->admins = SiteStats::numberingroup('sysop');
  33. $this->numJobs = SiteStats::jobs();
  34. # Staticic - views
  35. $viewsStats = '';
  36. if( !$wgDisableCounters ) {
  37. $viewsStats = $this->getViewsStats();
  38. }
  39. # Set active user count
  40. if( !$wgMiserMode ) {
  41. $dbw = wfGetDB( DB_MASTER );
  42. SiteStatsUpdate::cacheUpdate( $dbw );
  43. }
  44. # Do raw output
  45. if( $wgRequest->getVal( 'action' ) == 'raw' ) {
  46. $this->doRawOutput();
  47. }
  48. $text = Xml::openElement( 'table', array( 'class' => 'mw-statistics-table' ) );
  49. # Statistic - pages
  50. $text .= $this->getPageStats();
  51. # Statistic - edits
  52. $text .= $this->getEditStats();
  53. # Statistic - users
  54. $text .= $this->getUserStats();
  55. # Statistic - usergroups
  56. $text .= $this->getGroupStats();
  57. $text .= $viewsStats;
  58. # Statistic - popular pages
  59. if( !$wgDisableCounters && !$wgMiserMode ) {
  60. $text .= $this->getMostViewedPages();
  61. }
  62. $text .= Xml::closeElement( 'table' );
  63. # Customizable footer
  64. $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
  65. if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
  66. $text .= "\n" . $footer;
  67. }
  68. $wgOut->addHTML( $text );
  69. }
  70. /**
  71. * Format a row
  72. * @param string $text description of the row
  73. * @param float $number a number
  74. * @param array $trExtraParams
  75. * @param string $descMsg
  76. * @param string $descMsgParam
  77. * @return string table row in HTML format
  78. */
  79. private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
  80. global $wgStylePath;
  81. if( $descMsg ) {
  82. $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
  83. if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
  84. $descriptionText = " ($descriptionText)";
  85. $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
  86. $descriptionText );
  87. }
  88. }
  89. return Xml::openElement( 'tr', $trExtraParams ) .
  90. Xml::openElement( 'td' ) . $text . Xml::closeElement( 'td' ) .
  91. Xml::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml::closeElement( 'td' ) .
  92. Xml::closeElement( 'tr' );
  93. }
  94. /**
  95. * Each of these methods is pretty self-explanatory, get a particular
  96. * row for the table of statistics
  97. * @return string
  98. */
  99. private function getPageStats() {
  100. global $wgLang;
  101. return Xml::openElement( 'tr' ) .
  102. Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
  103. Xml::closeElement( 'tr' ) .
  104. $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
  105. $wgLang->formatNum( $this->good ),
  106. array( 'class' => 'mw-statistics-articles' ) ) .
  107. $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
  108. $wgLang->formatNum( $this->total ),
  109. array( 'class' => 'mw-statistics-pages' ),
  110. 'statistics-pages-desc' ) .
  111. $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
  112. $wgLang->formatNum( $this->images ),
  113. array( 'class' => 'mw-statistics-files' ) );
  114. }
  115. private function getEditStats() {
  116. global $wgLang;
  117. return Xml::openElement( 'tr' ) .
  118. Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
  119. Xml::closeElement( 'tr' ) .
  120. $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
  121. $wgLang->formatNum( $this->edits ),
  122. array( 'class' => 'mw-statistics-edits' ) ) .
  123. $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
  124. $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
  125. array( 'class' => 'mw-statistics-edits-average' ) ) .
  126. $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
  127. $wgLang->formatNum( $this->numJobs ),
  128. array( 'class' => 'mw-statistics-jobqueue' ) );
  129. }
  130. private function getUserStats() {
  131. global $wgLang, $wgRCMaxAge;
  132. return Xml::openElement( 'tr' ) .
  133. Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
  134. Xml::closeElement( 'tr' ) .
  135. $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
  136. $wgLang->formatNum( $this->users ),
  137. array( 'class' => 'mw-statistics-users' ) ) .
  138. $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
  139. $wgLang->formatNum( $this->activeUsers ),
  140. array( 'class' => 'mw-statistics-users-active' ),
  141. 'statistics-users-active-desc',
  142. $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
  143. }
  144. private function getGroupStats() {
  145. global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
  146. $sk = $wgUser->getSkin();
  147. $text = '';
  148. foreach( $wgGroupPermissions as $group => $permissions ) {
  149. # Skip generic * and implicit groups
  150. if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
  151. continue;
  152. }
  153. $groupname = htmlspecialchars( $group );
  154. $msg = wfMsg( 'group-' . $groupname );
  155. if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
  156. $groupnameLocalized = $groupname;
  157. } else {
  158. $groupnameLocalized = $msg;
  159. }
  160. $msg = wfMsgForContent( 'grouppage-' . $groupname );
  161. if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
  162. $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
  163. } else {
  164. $grouppageLocalized = $msg;
  165. }
  166. $grouppage = $sk->makeLink( $grouppageLocalized, htmlspecialchars( $groupnameLocalized ) );
  167. $grouplink = $sk->link( SpecialPage::getTitleFor( 'Listusers' ),
  168. wfMsgHtml( 'listgrouprights-members' ),
  169. array(),
  170. array( 'group' => $group ),
  171. 'known' );
  172. # Add a class when a usergroup contains no members to allow hiding these rows
  173. $classZero = '';
  174. $countUsers = SiteStats::numberingroup( $groupname );
  175. if( $countUsers == 0 ) {
  176. $classZero = ' statistics-group-zero';
  177. }
  178. $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
  179. $wgLang->formatNum( $countUsers ),
  180. array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
  181. }
  182. return $text;
  183. }
  184. private function getViewsStats() {
  185. global $wgLang;
  186. return Xml::openElement( 'tr' ) .
  187. Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
  188. Xml::closeElement( 'tr' ) .
  189. $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
  190. $wgLang->formatNum( $this->views ),
  191. array ( 'class' => 'mw-statistics-views-total' ) ) .
  192. $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
  193. $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
  194. $this->views / $this->edits : 0 ) ),
  195. array ( 'class' => 'mw-statistics-views-peredit' ) );
  196. }
  197. private function getMostViewedPages() {
  198. global $wgLang, $wgUser;
  199. $text = '';
  200. $dbr = wfGetDB( DB_SLAVE );
  201. $sk = $wgUser->getSkin();
  202. $res = $dbr->select(
  203. 'page',
  204. array(
  205. 'page_namespace',
  206. 'page_title',
  207. 'page_counter',
  208. ),
  209. array(
  210. 'page_is_redirect' => 0,
  211. 'page_counter > 0',
  212. ),
  213. __METHOD__,
  214. array(
  215. 'ORDER BY' => 'page_counter DESC',
  216. 'LIMIT' => 10,
  217. )
  218. );
  219. if( $res->numRows() > 0 ) {
  220. $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
  221. while( $row = $res->fetchObject() ) {
  222. $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
  223. if( $title instanceof Title ) {
  224. $text .= $this->formatRow( $sk->link( $title ),
  225. $wgLang->formatNum( $row->page_counter ) );
  226. }
  227. }
  228. $res->free();
  229. }
  230. return $text;
  231. }
  232. /**
  233. * Do the action=raw output for this page. Legacy, but we support
  234. * it for backwards compatibility
  235. * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
  236. */
  237. private function doRawOutput() {
  238. global $wgOut;
  239. $wgOut->disable();
  240. header( 'Pragma: nocache' );
  241. echo "total=" . $this->total . ";good=" . $this->good . ";views=" .
  242. $this->views . ";edits=" . $this->edits . ";users=" . $this->users . ";";
  243. echo "activeusers=" . $this->activeUsers . ";admins=" . $this->admins .
  244. ";images=" . $this->images . ";jobs=" . $this->numJobs . "\n";
  245. return;
  246. }
  247. }