]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/deleteOrphanedRevisions.php
MediaWiki 1.16.0
[autoinstallsdev/mediawiki.git] / maintenance / deleteOrphanedRevisions.php
index 0842018eac6152065395cd31c7b47822ffd6c8f8..1146befba81d57d4846bc10953e0f4259ffd3486 100644 (file)
@@ -4,50 +4,87 @@
  * Maintenance script to delete revisions which refer to a nonexisting page
  * Sometimes manual deletion done in a rush leaves crap in the database
  *
- * @addtogroup Maintenance
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @ingroup Maintenance
  * @author Rob Church <robchur@gmail.com>
  * @todo More efficient cleanup of text records
  */
-$options = array( 'report', 'help' );
-require_once( 'commandLine.inc' );
-require_once( 'deleteOrphanedRevisions.inc.php' );
-echo( "Delete Orphaned Revisions\n" );
-
-if( isset( $options['help'] ) )
-       showUsage();
-
-$report = isset( $options['report'] );
-
-$dbw = wfGetDB( DB_MASTER );
-$dbw->immediateBegin();
-extract( $dbw->tableNames( 'page', 'revision' ) );
-
-# Find all the orphaned revisions
-echo( "Checking for orphaned revisions..." );
-$sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL";
-$res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
-
-# Stash 'em all up for deletion (if needed)
-while( $row = $dbw->fetchObject( $res ) )
-       $revisions[] = $row->rev_id;
-$dbw->freeResult( $res );
-$count = count( $revisions );
-echo( "found {$count}.\n" );
-
-# Nothing to do?
-if( $report || $count == 0 ) {
-       $dbw->immediateCommit();
-       exit();
-}
 
-# Delete each revision
-echo( "Deleting..." );
-deleteRevisions( $revisions, $dbw );
-echo( "done.\n" );
+require_once( dirname(__FILE__) . '/Maintenance.php' );
+
+class DeleteOrphanedRevisions extends Maintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = "Maintenance script to delete revisions which refer to a nonexisting page";
+               $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
+       }
+
+       public function execute() {
+               $this->output( "Delete Orphaned Revisions\n" );
+
+               $report = $this->hasOption('report');
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin();
+               list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
+
+               # Find all the orphaned revisions
+               $this->output( "Checking for orphaned revisions..." );
+               $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL";
+               $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
+       
+               # Stash 'em all up for deletion (if needed)
+               $revisions = array();
+               foreach( $res as $row )
+                       $revisions[] = $row->rev_id;
+               $dbw->freeResult( $res );
+               $count = count( $revisions );
+               $this->output( "found {$count}.\n" );
+       
+               # Nothing to do?
+               if( $report || $count == 0 ) {
+                       $dbw->commit();
+                       exit(0);
+               }
+       
+               # Delete each revision
+               $this->output( "Deleting..." );
+               $this->deleteRevs( $revisions, $dbw );
+               $this->output( "done.\n" );
+       
+               # Close the transaction and call the script to purge unused text records
+               $dbw->commit();
+               $this->purgeRedundantText( true );
+       }
+       
+       /**
+        * Delete one or more revisions from the database
+        * Do this inside a transaction
+        *
+        * @param $id Array of revision id values
+        * @param $db Database class (needs to be a master)
+        */
+       private function deleteRevs( $id, &$dbw ) {
+               if( !is_array( $id ) )
+                       $id = array( $id );
+               $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
+       }
+}
 
-# Close the transaction and call the script to purge unused text records
-$dbw->immediateCommit();
-require_once( 'purgeOldText.inc' );
-PurgeRedundantText( true );
+$maintClass = "DeleteOrphanedRevisions";
+require_once( DO_MAINTENANCE );