Combine deleteArchived{Files,Revisions}.inc into the .php scripts
authorKevin Israel <pleasestand@live.com>
Fri, 12 Sep 2014 05:44:14 +0000 (01:44 -0400)
committerPleaseStand <pleasestand@live.com>
Sun, 17 May 2015 00:01:42 +0000 (00:01 +0000)
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
maintenance/deleteArchivedFiles.inc [deleted file]
maintenance/deleteArchivedFiles.php
maintenance/deleteArchivedRevisions.inc [deleted file]
maintenance/deleteArchivedRevisions.php

index c81c4bb..274c87e 100644 (file)
@@ -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 (file)
index 0c0b34a..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-<?php
-/**
- * Core functions for deleteArchivedFiles.php
- *
- * 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
- *
- * @file
- * @ingroup Maintenance
- */
-
-/**
- * Core functions for deleteArchivedFiles.php
- *
- * @ingroup Maintenance
- */
-class DeleteArchivedFilesImplementation {
-       public static function doDelete( $output, $force ) {
-               # Data should come off the master, wrapped in a transaction
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->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" );
-       }
-}
index 286b1f2..bd8ca10 100644 (file)
@@ -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 (file)
index ed620ee..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-<?php
-/**
- * Helper methods for the deleteArchivedRevisions.php maintenance script.
- *
- * 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
- *
- * @file
- * @ingroup Maintenance
- */
-
-/**
- * Helper methods for the deleteArchivedRevisions.php maintenance script.
- *
- * @ingroup Maintenance
- */
-class DeleteArchivedRevisionsImplementation {
-
-       /**
-        * Perform the delete on archived revisions.
-        * @param object $maint An object (typically of class Maintenance)
-        * that implements two methods: handleOutput() and
-        * purgeRedundantText().  See Maintenance for a description of
-        * those methods.
-        */
-       public static function doDelete( $maint ) {
-               $dbw = wfGetDB( DB_MASTER );
-
-               $dbw->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 );
-               }
-       }
-}
index 30883ba..9924eb0 100644 (file)
@@ -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 );
                }
        }
 }