showSiteStats.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Show the cached statistics.
  4. * Give out the same output as [[Special:Statistics]]
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Maintenance
  23. * @author Antoine Musso <hashar at free dot fr>
  24. * Based on initSiteStats.php by:
  25. * @author Brion Vibber
  26. * @author Rob Church <robchur@gmail.com>
  27. *
  28. * @license GNU General Public License 2.0 or later
  29. */
  30. require_once __DIR__ . '/Maintenance.php';
  31. /**
  32. * Maintenance script to show the cached statistics.
  33. *
  34. * @ingroup Maintenance
  35. */
  36. class ShowSiteStats extends Maintenance {
  37. public function __construct() {
  38. parent::__construct();
  39. $this->addDescription( 'Show the cached statistics' );
  40. }
  41. public function execute() {
  42. $fields = [
  43. 'ss_total_edits' => 'Total edits',
  44. 'ss_good_articles' => 'Number of articles',
  45. 'ss_total_pages' => 'Total pages',
  46. 'ss_users' => 'Number of users',
  47. 'ss_active_users' => 'Active users',
  48. 'ss_images' => 'Number of images',
  49. ];
  50. // Get cached stats from slave database
  51. $dbr = $this->getDB( DB_SLAVE );
  52. $stats = $dbr->selectRow( 'site_stats', '*', '', __METHOD__ );
  53. // Get maximum size for each column
  54. $max_length_value = $max_length_desc = 0;
  55. foreach ( $fields as $field => $desc ) {
  56. $max_length_value = max( $max_length_value, strlen( $stats->$field ) );
  57. $max_length_desc = max( $max_length_desc, strlen( $desc ) );
  58. }
  59. // Show them
  60. foreach ( $fields as $field => $desc ) {
  61. $this->output( sprintf(
  62. "%-{$max_length_desc}s: %{$max_length_value}d\n",
  63. $desc,
  64. $stats->$field
  65. ) );
  66. }
  67. }
  68. }
  69. $maintClass = "ShowSiteStats";
  70. require_once RUN_MAINTENANCE_IF_MAIN;