purgeParserCache.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Remove old objects from the parser cache.
  4. * This only works when the parser cache is in an SQL database.
  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. */
  24. require __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script to remove old objects from the parser cache.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class PurgeParserCache extends Maintenance {
  31. public $lastProgress;
  32. function __construct() {
  33. parent::__construct();
  34. $this->addDescription( "Remove old objects from the parser cache. " .
  35. "This only works when the parser cache is in an SQL database." );
  36. $this->addOption( 'expiredate', 'Delete objects expiring before this date.', false, true );
  37. $this->addOption(
  38. 'age',
  39. 'Delete objects created more than this many seconds ago, assuming $wgParserCacheExpireTime ' .
  40. 'has been consistent.',
  41. false, true );
  42. }
  43. function execute() {
  44. $inputDate = $this->getOption( 'expiredate' );
  45. $inputAge = $this->getOption( 'age' );
  46. if ( $inputDate !== null ) {
  47. $date = wfTimestamp( TS_MW, strtotime( $inputDate ) );
  48. } elseif ( $inputAge !== null ) {
  49. global $wgParserCacheExpireTime;
  50. $date = wfTimestamp( TS_MW, time() + $wgParserCacheExpireTime - intval( $inputAge ) );
  51. } else {
  52. $this->error( "Must specify either --expiredate or --age", 1 );
  53. }
  54. $english = Language::factory( 'en' );
  55. $this->output( "Deleting objects expiring before " . $english->timeanddate( $date ) . "\n" );
  56. $pc = wfGetParserCacheStorage();
  57. $success = $pc->deleteObjectsExpiringBefore( $date, [ $this, 'showProgress' ] );
  58. if ( !$success ) {
  59. $this->error( "\nCannot purge this kind of parser cache.", 1 );
  60. }
  61. $this->showProgress( 100 );
  62. $this->output( "\nDone\n" );
  63. }
  64. function showProgress( $percent ) {
  65. $percentString = sprintf( "%.2f", $percent );
  66. if ( $percentString === $this->lastProgress ) {
  67. return;
  68. }
  69. $this->lastProgress = $percentString;
  70. $stars = floor( $percent / 2 );
  71. $this->output( '[' . str_repeat( '*', $stars ) . str_repeat( '.', 50 - $stars ) . '] ' .
  72. "$percentString%\r" );
  73. }
  74. }
  75. $maintClass = 'PurgeParserCache';
  76. require_once RUN_MAINTENANCE_IF_MAIN;