Maintenance script to remove orphaned revisions from the database
authorRob Church <robchurch@users.mediawiki.org>
Wed, 17 May 2006 07:37:20 +0000 (07:37 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Wed, 17 May 2006 07:37:20 +0000 (07:37 +0000)
RELEASE-NOTES
maintenance/deleteOrphanedRevisions.inc.php [new file with mode: 0644]
maintenance/deleteOrphanedRevisions.php [new file with mode: 0644]

index 1806739..acacccc 100644 (file)
@@ -281,6 +281,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 5962) Update for Italian language (it)
 * Suppress images in galleries which appear on the bad image list (when rendering
   for a wiki page; galleries in special pages and categories are unaffected)
+* Maintenance script to remove orphaned revisions from the database
 
 == Compatibility ==
 
diff --git a/maintenance/deleteOrphanedRevisions.inc.php b/maintenance/deleteOrphanedRevisions.inc.php
new file mode 100644 (file)
index 0000000..7cfb1c6
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * Support functions for the deleteOrphanedRevisions maintenance script
+ *
+ * @package MediaWiki
+ * @subpackage Maintenance
+ * @author Rob Church <robchur@gmail.com>
+ */
+
+/**
+ * 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)
+ */
+function deleteRevisions( $id, &$dbw ) {
+       if( !is_array( $id ) )
+               $id = array( $id );
+       $dbw->delete( 'revision', array( 'rev_id' => $id ), 'deleteRevision' );
+}
+
+/**
+ * Spit out script usage information and exit
+ */
+function showUsage() {
+       echo( "Finds revisions which refer to nonexisting pages and deletes them from the database\n" );
+       echo( "USAGE: php deleteOrphanedRevisions.php [--report]\n\n" );
+       echo( " --report : Prints out a count of affected revisions but doesn't delete them\n\n" );
+}
+
+?>
\ No newline at end of file
diff --git a/maintenance/deleteOrphanedRevisions.php b/maintenance/deleteOrphanedRevisions.php
new file mode 100644 (file)
index 0000000..b4f5b51
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * Maintenance script to delete revisions which refer to a nonexisting page
+ * Sometimes manual deletion done in a rush leaves crap in the database
+ *
+ * @package MediaWiki
+ * @subpackage 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" );
+
+# Close the transaction and call the script to purge unused text records
+$dbw->immediateCommit();
+require_once( 'purgeOldText.inc' );
+PurgeRedundantText( true );
+
+?>
\ No newline at end of file