deleteOldRevisions.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Delete old (non-current) revisions from the database
  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 Maintenance
  22. * @author Rob Church <robchur@gmail.com>
  23. */
  24. require_once __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script that deletes old (non-current) revisions from the database.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class DeleteOldRevisions extends Maintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Delete old (non-current) revisions from the database' );
  34. $this->addOption( 'delete', 'Actually perform the deletion' );
  35. $this->addOption( 'page_id', 'List of page ids to work on', false );
  36. }
  37. public function execute() {
  38. $this->output( "Delete old revisions\n\n" );
  39. $this->doDelete( $this->hasOption( 'delete' ), $this->mArgs );
  40. }
  41. function doDelete( $delete = false, $args = [] ) {
  42. # Data should come off the master, wrapped in a transaction
  43. $dbw = $this->getDB( DB_MASTER );
  44. $this->beginTransaction( $dbw, __METHOD__ );
  45. $pageConds = [];
  46. $revConds = [];
  47. # If a list of page_ids was provided, limit results to that set of page_ids
  48. if ( count( $args ) > 0 ) {
  49. $pageConds['page_id'] = $args;
  50. $revConds['rev_page'] = $args;
  51. $this->output( "Limiting to page IDs " . implode( ',', $args ) . "\n" );
  52. }
  53. # Get "active" revisions from the page table
  54. $this->output( "Searching for active revisions..." );
  55. $res = $dbw->select( 'page', 'page_latest', $pageConds, __METHOD__ );
  56. $latestRevs = [];
  57. foreach ( $res as $row ) {
  58. $latestRevs[] = $row->page_latest;
  59. }
  60. $this->output( "done.\n" );
  61. # Get all revisions that aren't in this set
  62. $this->output( "Searching for inactive revisions..." );
  63. if ( count( $latestRevs ) > 0 ) {
  64. $revConds[] = 'rev_id NOT IN (' . $dbw->makeList( $latestRevs ) . ')';
  65. }
  66. $res = $dbw->select( 'revision', 'rev_id', $revConds, __METHOD__ );
  67. $oldRevs = [];
  68. foreach ( $res as $row ) {
  69. $oldRevs[] = $row->rev_id;
  70. }
  71. $this->output( "done.\n" );
  72. # Inform the user of what we're going to do
  73. $count = count( $oldRevs );
  74. $this->output( "$count old revisions found.\n" );
  75. # Delete as appropriate
  76. if ( $delete && $count ) {
  77. $this->output( "Deleting..." );
  78. $dbw->delete( 'revision', [ 'rev_id' => $oldRevs ], __METHOD__ );
  79. $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $oldRevs ], __METHOD__ );
  80. $this->output( "done.\n" );
  81. }
  82. # This bit's done
  83. # Purge redundant text records
  84. $this->commitTransaction( $dbw, __METHOD__ );
  85. if ( $delete ) {
  86. $this->purgeRedundantText( true );
  87. }
  88. }
  89. }
  90. $maintClass = DeleteOldRevisions::class;
  91. require_once RUN_MAINTENANCE_IF_MAIN;