MediaWiki  1.32.5
deleteOldRevisions.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
33  public function __construct() {
34  parent::__construct();
35  $this->addDescription( 'Delete old (non-current) revisions from the database' );
36  $this->addOption( 'delete', 'Actually perform the deletion' );
37  $this->addOption( 'page_id', 'List of page ids to work on', false );
38  }
39 
40  public function execute() {
41  $this->output( "Delete old revisions\n\n" );
42  $this->doDelete( $this->hasOption( 'delete' ), $this->mArgs );
43  }
44 
45  function doDelete( $delete = false, $args = [] ) {
46  # Data should come off the master, wrapped in a transaction
47  $dbw = $this->getDB( DB_MASTER );
48  $this->beginTransaction( $dbw, __METHOD__ );
49 
50  $pageConds = [];
51  $revConds = [];
52 
53  # If a list of page_ids was provided, limit results to that set of page_ids
54  if ( count( $args ) > 0 ) {
55  $pageConds['page_id'] = $args;
56  $revConds['rev_page'] = $args;
57  $this->output( "Limiting to page IDs " . implode( ',', $args ) . "\n" );
58  }
59 
60  # Get "active" revisions from the page table
61  $this->output( "Searching for active revisions..." );
62  $res = $dbw->select( 'page', 'page_latest', $pageConds, __METHOD__ );
63  $latestRevs = [];
64  foreach ( $res as $row ) {
65  $latestRevs[] = $row->page_latest;
66  }
67  $this->output( "done.\n" );
68 
69  # Get all revisions that aren't in this set
70  $this->output( "Searching for inactive revisions..." );
71  if ( count( $latestRevs ) > 0 ) {
72  $revConds[] = 'rev_id NOT IN (' . $dbw->makeList( $latestRevs ) . ')';
73  }
74  $res = $dbw->select( 'revision', 'rev_id', $revConds, __METHOD__ );
75  $oldRevs = [];
76  foreach ( $res as $row ) {
77  $oldRevs[] = $row->rev_id;
78  }
79  $this->output( "done.\n" );
80 
81  # Inform the user of what we're going to do
82  $count = count( $oldRevs );
83  $this->output( "$count old revisions found.\n" );
84 
85  # Delete as appropriate
86  if ( $delete && $count ) {
87  $this->output( "Deleting..." );
88  $dbw->delete( 'revision', [ 'rev_id' => $oldRevs ], __METHOD__ );
89  $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $oldRevs ], __METHOD__ );
90  $this->output( "done.\n" );
91  }
92 
93  # This bit's done
94  # Purge redundant text records
95  $this->commitTransaction( $dbw, __METHOD__ );
96  if ( $delete ) {
97  $this->purgeRedundantText( true );
98  }
99  }
100 }
101 
103 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
$maintClass
Definition: deleteOldRevisions.php:102
DeleteOldRevisions
Maintenance script that deletes old (non-current) revisions from the database.
Definition: deleteOldRevisions.php:32
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$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
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1378
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:236
DB_MASTER
const DB_MASTER
Definition: defines.php:26
DeleteOldRevisions\doDelete
doDelete( $delete=false, $args=[])
Definition: deleteOldRevisions.php:45
DeleteOldRevisions\execute
execute()
Do the actual work.
Definition: deleteOldRevisions.php:40
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1393
Maintenance\purgeRedundantText
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
Definition: Maintenance.php:1283
$args
if( $line===false) $args
Definition: cdb.php:64
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:1351
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:414
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:257
DeleteOldRevisions\__construct
__construct()
Default constructor.
Definition: deleteOldRevisions.php:33