[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> deleteOrphanedRevisions.php (source)

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


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1