MediaWiki  1.30.0
deleteOrphanedRevisions.php
Go to the documentation of this file.
1 <?php
27 require_once __DIR__ . '/Maintenance.php';
28 
30 
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription(
40  'Maintenance script to delete revisions which refer to a nonexisting page' );
41  $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
42  }
43 
44  public function execute() {
45  $this->output( "Delete Orphaned Revisions\n" );
46 
47  $report = $this->hasOption( 'report' );
48 
49  $dbw = $this->getDB( DB_MASTER );
50  $this->beginTransaction( $dbw, __METHOD__ );
51  list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
52 
53  # Find all the orphaned revisions
54  $this->output( "Checking for orphaned revisions..." );
55  $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id "
56  . "WHERE page_namespace IS NULL";
57  $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
58 
59  # Stash 'em all up for deletion (if needed)
60  $revisions = [];
61  foreach ( $res as $row ) {
62  $revisions[] = $row->rev_id;
63  }
64  $count = count( $revisions );
65  $this->output( "found {$count}.\n" );
66 
67  # Nothing to do?
68  if ( $report || $count == 0 ) {
69  $this->commitTransaction( $dbw, __METHOD__ );
70  exit( 0 );
71  }
72 
73  # Delete each revision
74  $this->output( "Deleting..." );
75  $this->deleteRevs( $revisions, $dbw );
76  $this->output( "done.\n" );
77 
78  # Close the transaction and call the script to purge unused text records
79  $this->commitTransaction( $dbw, __METHOD__ );
80  $this->purgeRedundantText( true );
81  }
82 
90  private function deleteRevs( $id, &$dbw ) {
91  if ( !is_array( $id ) ) {
92  $id = [ $id ];
93  }
94  $dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ );
95 
96  // Delete from ip_changes should a record exist.
97  $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $id ], __METHOD__ );
98  }
99 }
100 
101 $maintClass = "DeleteOrphanedRevisions";
102 require_once RUN_MAINTENANCE_IF_MAIN;
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$maintClass
$maintClass
Definition: deleteOrphanedRevisions.php:101
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
DeleteOrphanedRevisions\execute
execute()
Do the actual work.
Definition: deleteOrphanedRevisions.php:44
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1278
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
DB_MASTER
const DB_MASTER
Definition: defines.php:26
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
DeleteOrphanedRevisions
Maintenance script that deletes revisions which refer to a nonexisting page.
Definition: deleteOrphanedRevisions.php:36
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1293
Maintenance\purgeRedundantText
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
Definition: Maintenance.php:1183
DeleteOrphanedRevisions\__construct
__construct()
Default constructor.
Definition: deleteOrphanedRevisions.php:37
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
DeleteOrphanedRevisions\deleteRevs
deleteRevs( $id, &$dbw)
Delete one or more revisions from the database Do this inside a transaction.
Definition: deleteOrphanedRevisions.php:90
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236