From 5fca8e5130a5a40fdf68d4a106bb4336055d097d Mon Sep 17 00:00:00 2001 From: Kevin Israel Date: Fri, 12 Sep 2014 01:44:14 -0400 Subject: [PATCH] Combine deleteArchived{Files,Revisions}.inc into the .php scripts Some of deleteArchivedFiles.php and deleteArchivedRevisions.php were split off to deleteArchivedFiles.inc and deleteArchivedRevisions.inc respectively in r62948 (04b2413aacfd) for use in tests. The tests no longer use those methods, so I moved them back and inlined them into execute(). I also did some minor cleanup -- changing direct calls to tableName() and query() to use query builder functions and clarifying/fixing some of the logic that was in deleteArchivedFiles.inc. Change-Id: Ica49dcac18a9c702cffe02e562c6dff48d2d2784 --- autoload.php | 2 - maintenance/deleteArchivedFiles.inc | 84 ------------------------- maintenance/deleteArchivedFiles.php | 79 ++++++++++++++++++++--- maintenance/deleteArchivedRevisions.inc | 61 ------------------ maintenance/deleteArchivedRevisions.php | 28 +++++---- 5 files changed, 86 insertions(+), 168 deletions(-) delete mode 100644 maintenance/deleteArchivedFiles.inc delete mode 100644 maintenance/deleteArchivedRevisions.inc diff --git a/autoload.php b/autoload.php index c81c4bb118..274c87e322 100644 --- a/autoload.php +++ b/autoload.php @@ -311,9 +311,7 @@ $wgAutoloadLocalClasses = array( 'DeferredUpdates' => __DIR__ . '/includes/deferred/DeferredUpdates.php', 'DeleteAction' => __DIR__ . '/includes/actions/DeleteAction.php', 'DeleteArchivedFiles' => __DIR__ . '/maintenance/deleteArchivedFiles.php', - 'DeleteArchivedFilesImplementation' => __DIR__ . '/maintenance/deleteArchivedFiles.inc', 'DeleteArchivedRevisions' => __DIR__ . '/maintenance/deleteArchivedRevisions.php', - 'DeleteArchivedRevisionsImplementation' => __DIR__ . '/maintenance/deleteArchivedRevisions.inc', 'DeleteBatch' => __DIR__ . '/maintenance/deleteBatch.php', 'DeleteDefaultMessages' => __DIR__ . '/maintenance/deleteDefaultMessages.php', 'DeleteEqualMessages' => __DIR__ . '/maintenance/deleteEqualMessages.php', diff --git a/maintenance/deleteArchivedFiles.inc b/maintenance/deleteArchivedFiles.inc deleted file mode 100644 index 0c0b34a333..0000000000 --- a/maintenance/deleteArchivedFiles.inc +++ /dev/null @@ -1,84 +0,0 @@ -begin( __METHOD__ ); - $tbl_arch = $dbw->tableName( 'filearchive' ); - $repo = RepoGroup::singleton()->getLocalRepo(); - # Get "active" revisions from the filearchive table - $output->handleOutput( "Searching for and deleting archived files...\n" ); - $res = $dbw->query( "SELECT fa_id,fa_storage_group,fa_storage_key,fa_sha1 FROM $tbl_arch" ); - $count = 0; - foreach ( $res as $row ) { - $key = $row->fa_storage_key; - if ( !strlen( $key ) ) { - $output->handleOutput( "Entry with ID {$row->fa_id} has empty key, skipping\n" ); - continue; - } - $group = $row->fa_storage_group; - $id = $row->fa_id; - $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key; - if ( isset( $row->fa_sha1 ) ) { - $sha1 = $row->fa_sha1; - } else { - // old row, populate from key - $sha1 = LocalRepo::getHashFromKey( $key ); - } - // Check if the file is used anywhere... - $inuse = $dbw->selectField( - 'oldimage', - '1', - array( - 'oi_sha1' => $sha1, - 'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE - ), - __METHOD__, - array( 'FOR UPDATE' ) - ); - if ( $path && $repo->fileExists( $path ) && !$inuse ) { - if ( $repo->quickPurge( $path ) ) { - $count++; - $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" ); - } else { - $output->handleOutput( "Unable to remove file $path, skipping\n" ); - } - } else { - $output->handleOutput( "Notice - file '$key' not found in group '$group'\n" ); - if ( $force ) { - $output->handleOutput( "Got --force, deleting DB entry\n" ); - $dbw->query( "DELETE FROM $tbl_arch WHERE fa_id = $id" ); - } - } - } - $dbw->commit( __METHOD__ ); - $output->handleOutput( "Done! [$count file(s)]\n" ); - } -} diff --git a/maintenance/deleteArchivedFiles.php b/maintenance/deleteArchivedFiles.php index 286b1f246b..bd8ca1090a 100644 --- a/maintenance/deleteArchivedFiles.php +++ b/maintenance/deleteArchivedFiles.php @@ -25,7 +25,6 @@ */ require_once __DIR__ . '/Maintenance.php'; -require_once __DIR__ . '/deleteArchivedFiles.inc'; /** * Maintenance script to delete archived (non-current) files from the database. @@ -40,18 +39,82 @@ class DeleteArchivedFiles extends Maintenance { $this->addOption( 'force', 'Force deletion of rows from filearchive' ); } - public function handleOutput( $str ) { - return $this->output( $str ); - } - public function execute() { if ( !$this->hasOption( 'delete' ) ) { $this->output( "Use --delete to actually confirm this script\n" ); - return; } - $force = $this->hasOption( 'force' ); - DeleteArchivedFilesImplementation::doDelete( $this, $force ); + + # Data should come off the master, wrapped in a transaction + $dbw = $this->getDB( DB_MASTER ); + $dbw->begin( __METHOD__ ); + $repo = RepoGroup::singleton()->getLocalRepo(); + + # Get "active" revisions from the filearchive table + $this->output( "Searching for and deleting archived files...\n" ); + $res = $dbw->select( + 'filearchive', + array( 'fa_id', 'fa_storage_group', 'fa_storage_key', 'fa_sha1' ), + '', + __METHOD__ + ); + + $count = 0; + foreach ( $res as $row ) { + $key = $row->fa_storage_key; + if ( !strlen( $key ) ) { + $this->output( "Entry with ID {$row->fa_id} has empty key, skipping\n" ); + continue; + } + + $group = $row->fa_storage_group; + $id = $row->fa_id; + $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key; + if ( isset( $row->fa_sha1 ) ) { + $sha1 = $row->fa_sha1; + } else { + // old row, populate from key + $sha1 = LocalRepo::getHashFromKey( $key ); + } + + // Check if the file is used anywhere... + $inuse = $dbw->selectField( + 'oldimage', + '1', + array( + 'oi_sha1' => $sha1, + $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE + ), + __METHOD__, + array( 'FOR UPDATE' ) + ); + + $needForce = true; + if ( !$repo->fileExists( $path ) ) { + $this->output( "Notice - file '$key' not found in group '$group'\n" ); + } elseif ( $inuse ) { + $this->output( "Notice - file '$key' is still in use\n" ); + } elseif ( !$repo->quickPurge( $path ) ) { + $this->output( "Unable to remove file $path, skipping\n" ); + continue; // don't delete even with --force + } else { + $needForce = false; + } + + if ( $needForce ) { + if ( $this->hasOption( 'force' ) ) { + $this->output( "Got --force, deleting DB entry\n" ); + } else { + continue; + } + } + + $count++; + $dbw->delete( 'filearchive', array( 'fa_id' => $id ), __METHOD__ ); + } + + $dbw->commit( __METHOD__ ); + $this->output( "Done! [$count file(s)]\n" ); } } diff --git a/maintenance/deleteArchivedRevisions.inc b/maintenance/deleteArchivedRevisions.inc deleted file mode 100644 index ed620ee36a..0000000000 --- a/maintenance/deleteArchivedRevisions.inc +++ /dev/null @@ -1,61 +0,0 @@ -begin( __METHOD__ ); - - $tbl_arch = $dbw->tableName( 'archive' ); - - # Delete as appropriate - $maint->handleOutput( "Deleting archived revisions... " ); - $dbw->query( "DELETE FROM $tbl_arch" ); - - $count = $dbw->affectedRows(); - $deletedRows = $count != 0; - - $maint->handleOutput( "done. $count revisions deleted.\n" ); - - # This bit's done - # Purge redundant text records - $dbw->commit( __METHOD__ ); - if ( $deletedRows ) { - $maint->purgeRedundantText( true ); - } - } -} diff --git a/maintenance/deleteArchivedRevisions.php b/maintenance/deleteArchivedRevisions.php index 30883ba4f5..9924eb0cd5 100644 --- a/maintenance/deleteArchivedRevisions.php +++ b/maintenance/deleteArchivedRevisions.php @@ -25,7 +25,6 @@ */ require_once __DIR__ . '/Maintenance.php'; -require_once __DIR__ . '/deleteArchivedRevisions.inc'; /** * Maintenance script to delete archived (deleted from public) revisions @@ -41,21 +40,24 @@ class DeleteArchivedRevisions extends Maintenance { $this->addOption( 'delete', 'Performs the deletion' ); } - public function handleOutput( $str ) { - $this->output( $str ); - } - public function execute() { - $this->output( "Delete archived revisions\n\n" ); - # Data should come off the master, wrapped in a transaction - if ( $this->hasOption( 'delete' ) ) { - DeleteArchivedRevisionsImplementation::doDelete( $this ); - } else { - $dbw = wfGetDB( DB_MASTER ); - $res = $dbw->selectRow( 'archive', 'COUNT(*) as count', array(), __FUNCTION__ ); - $this->output( "Found {$res->count} revisions to delete.\n" ); + $dbw = $this->getDB( DB_MASTER ); + + if ( !$this->hasOption( 'delete' ) ) { + $count = $dbw->selectField( 'archive', 'COUNT(*)', '', __METHOD__ ); + $this->output( "Found $count revisions to delete.\n" ); $this->output( "Please run the script again with the --delete option " . "to really delete the revisions.\n" ); + return; + } + + $this->output( "Deleting archived revisions... " ); + $dbw->delete( 'archive', '*', __METHOD__ ); + $count = $dbw->affectedRows(); + $this->output( "done. $count revisions deleted.\n" ); + + if ( $count ) { + $this->purgeRedundantText( true ); } } } -- 2.20.1