updateArticleCount.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Provide a better count of the number of articles
  4. * and update the site statistics table, if desired.
  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 Rob Church <robchur@gmail.com>
  24. */
  25. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Maintenance script to provide a better count of the number of articles
  28. * and update the site statistics table, if desired.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class UpdateArticleCount extends Maintenance {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription( 'Count of the number of articles and update the site statistics table' );
  36. $this->addOption( 'update', 'Update the site_stats table with the new count' );
  37. $this->addOption( 'use-master', 'Count using the master database' );
  38. }
  39. public function execute() {
  40. $this->output( "Counting articles..." );
  41. if ( $this->hasOption( 'use-master' ) ) {
  42. $dbr = $this->getDB( DB_MASTER );
  43. } else {
  44. $dbr = $this->getDB( DB_REPLICA, 'vslow' );
  45. }
  46. $counter = new SiteStatsInit( $dbr );
  47. $result = $counter->articles();
  48. $this->output( "found {$result}.\n" );
  49. if ( $this->hasOption( 'update' ) ) {
  50. $this->output( "Updating site statistics table... " );
  51. $dbw = $this->getDB( DB_MASTER );
  52. $dbw->update(
  53. 'site_stats',
  54. [ 'ss_good_articles' => $result ],
  55. [ 'ss_row_id' => 1 ],
  56. __METHOD__
  57. );
  58. $this->output( "done.\n" );
  59. } else {
  60. $this->output( "To update the site statistics table, run the script "
  61. . "with the --update option.\n" );
  62. }
  63. }
  64. }
  65. $maintClass = UpdateArticleCount::class;
  66. require_once RUN_MAINTENANCE_IF_MAIN;