2006-05-17 07:37:20 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-07-08 20:47:27 +00:00
|
|
|
* Delete revisions which refer to a nonexisting page.
|
|
|
|
|
* Sometimes manual deletion done in a rush leaves crap in the database.
|
2006-05-17 07:37:20 +00:00
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
2012-07-08 20:47:27 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2006-05-17 07:37:20 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>
|
|
|
|
|
* @todo More efficient cleanup of text records
|
|
|
|
|
*/
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2006-05-17 07:37:20 +00:00
|
|
|
|
2017-03-30 20:46:06 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
|
2012-07-08 20:47:27 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that deletes revisions which refer to a nonexisting page.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class DeleteOrphanedRevisions extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription(
|
|
|
|
|
'Maintenance script to delete revisions which refer to a nonexisting page' );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
|
|
|
|
|
}
|
2006-05-17 07:37:20 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
|
|
|
|
$this->output( "Delete Orphaned Revisions\n" );
|
2006-05-17 07:37:20 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
$report = $this->hasOption( 'report' );
|
2006-05-17 07:37:20 +00:00
|
|
|
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbw = $this->getPrimaryDB();
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->beginTransaction( $dbw, __METHOD__ );
|
2006-05-17 07:37:20 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# Find all the orphaned revisions
|
|
|
|
|
$this->output( "Checking for orphaned revisions..." );
|
2022-07-21 09:36:56 +00:00
|
|
|
$res = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( 'rev_id' )
|
|
|
|
|
->from( 'revision' )
|
|
|
|
|
->leftJoin( 'page', null, 'rev_page = page_id' )
|
|
|
|
|
->where( [ 'page_namespace' => null ] )
|
|
|
|
|
->caller( 'deleteOrphanedRevisions' )
|
|
|
|
|
->fetchResultSet();
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# Stash 'em all up for deletion (if needed)
|
2016-02-17 09:09:32 +00:00
|
|
|
$revisions = [];
|
2013-04-18 18:48:44 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$revisions[] = $row->rev_id;
|
2013-04-18 18:48:44 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
$count = count( $revisions );
|
|
|
|
|
$this->output( "found {$count}.\n" );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# Nothing to do?
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $report || $count == 0 ) {
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
2021-08-30 18:54:47 +00:00
|
|
|
return;
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# Delete each revision
|
|
|
|
|
$this->output( "Deleting..." );
|
|
|
|
|
$this->deleteRevs( $revisions, $dbw );
|
|
|
|
|
$this->output( "done.\n" );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# Close the transaction and call the script to purge unused text records
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->purgeRedundantText( true );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Delete one or more revisions from the database
|
|
|
|
|
* Do this inside a transaction
|
|
|
|
|
*
|
2019-02-28 11:33:47 +00:00
|
|
|
* @param int[] $id Array of revision id values
|
2021-05-14 20:04:02 +00:00
|
|
|
* @param IDatabase $dbw Primary DB handle
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
2021-07-15 09:26:31 +00:00
|
|
|
private function deleteRevs( array $id, $dbw ) {
|
2024-01-02 11:59:05 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
|
->deleteFrom( 'revision' )
|
|
|
|
|
->where( [ 'rev_id' => $id ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2017-04-21 16:17:59 +00:00
|
|
|
|
|
|
|
|
// Delete from ip_changes should a record exist.
|
2024-01-02 11:59:05 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
|
->deleteFrom( 'ip_changes' )
|
|
|
|
|
->where( [ 'ipc_rev_id' => $id ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-05-17 07:37:20 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = DeleteOrphanedRevisions::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|