deleteOrphanedRevisions.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Delete revisions which refer to a nonexisting page.
  4. * Sometimes manual deletion done in a rush leaves crap in the 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. * @author Rob Church <robchur@gmail.com>
  24. * @todo More efficient cleanup of text records
  25. */
  26. require_once __DIR__ . '/Maintenance.php';
  27. /**
  28. * Maintenance script that deletes revisions which refer to a nonexisting page.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class DeleteOrphanedRevisions extends Maintenance {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription(
  36. 'Maintenance script to delete revisions which refer to a nonexisting page' );
  37. $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
  38. }
  39. public function execute() {
  40. $this->output( "Delete Orphaned Revisions\n" );
  41. $report = $this->hasOption( 'report' );
  42. $dbw = $this->getDB( DB_MASTER );
  43. $this->beginTransaction( $dbw, __METHOD__ );
  44. list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
  45. # Find all the orphaned revisions
  46. $this->output( "Checking for orphaned revisions..." );
  47. $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id "
  48. . "WHERE page_namespace IS NULL";
  49. $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
  50. # Stash 'em all up for deletion (if needed)
  51. $revisions = [];
  52. foreach ( $res as $row ) {
  53. $revisions[] = $row->rev_id;
  54. }
  55. $count = count( $revisions );
  56. $this->output( "found {$count}.\n" );
  57. # Nothing to do?
  58. if ( $report || $count == 0 ) {
  59. $this->commitTransaction( $dbw, __METHOD__ );
  60. exit( 0 );
  61. }
  62. # Delete each revision
  63. $this->output( "Deleting..." );
  64. $this->deleteRevs( $revisions, $dbw );
  65. $this->output( "done.\n" );
  66. # Close the transaction and call the script to purge unused text records
  67. $this->commitTransaction( $dbw, __METHOD__ );
  68. $this->purgeRedundantText( true );
  69. }
  70. /**
  71. * Delete one or more revisions from the database
  72. * Do this inside a transaction
  73. *
  74. * @param array $id Array of revision id values
  75. * @param DatabaseBase $dbw DatabaseBase class (needs to be a master)
  76. */
  77. private function deleteRevs( $id, &$dbw ) {
  78. if ( !is_array( $id ) ) {
  79. $id = [ $id ];
  80. }
  81. $dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ );
  82. }
  83. }
  84. $maintClass = "DeleteOrphanedRevisions";
  85. require_once RUN_MAINTENANCE_IF_MAIN;