MediaWiki master
deleteOldRevisions.php
Go to the documentation of this file.
1<?php
12
13// @codeCoverageIgnoreStart
14require_once __DIR__ . '/Maintenance.php';
15// @codeCoverageIgnoreEnd
16
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription( 'Delete old (non-current) revisions from the database' );
26 $this->addOption( 'delete', 'Actually perform the deletion' );
27 $this->addArg( 'page_id', 'List of page ids to work on', false, true );
28 }
29
30 public function execute() {
31 $this->output( "Delete old revisions\n\n" );
32 $this->doDelete( $this->hasOption( 'delete' ), $this->getArgs( 'page_id' ) );
33 }
34
35 private function doDelete( bool $delete = false, array $pageIds = [] ) {
36 # Data should come off the master, wrapped in a transaction
37 $dbw = $this->getPrimaryDB();
38 $this->beginTransaction( $dbw, __METHOD__ );
39
40 $pageConds = [];
41 $revConds = [];
42
43 # If a list of page_ids was provided, limit results to that set of page_ids
44 if ( count( $pageIds ) > 0 ) {
45 $pageConds['page_id'] = $pageIds;
46 $revConds['rev_page'] = $pageIds;
47 $this->output( "Limiting to page IDs " . implode( ',', $pageIds ) . "\n" );
48 }
49
50 # Get "active" revisions from the page table
51 $this->output( "Searching for active revisions..." );
52 $res = $dbw->newSelectQueryBuilder()
53 ->select( 'page_latest' )
54 ->from( 'page' )
55 ->where( $pageConds )
56 ->caller( __METHOD__ )
57 ->fetchResultSet();
58 $latestRevs = [];
59 foreach ( $res as $row ) {
60 $latestRevs[] = $row->page_latest;
61 }
62 $this->output( "done.\n" );
63
64 # Get all revisions that aren't in this set
65 $this->output( "Searching for inactive revisions..." );
66 if ( count( $latestRevs ) > 0 ) {
67 $revConds[] = $dbw->expr( 'rev_id', '!=', $latestRevs );
68 }
69 $res = $dbw->newSelectQueryBuilder()
70 ->select( 'rev_id' )
71 ->from( 'revision' )
72 ->where( $revConds )
73 ->caller( __METHOD__ )
74 ->fetchResultSet();
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->newDeleteQueryBuilder()
89 ->deleteFrom( 'revision' )
90 ->where( [ 'rev_id' => $oldRevs ] )
91 ->caller( __METHOD__ )->execute();
92 $dbw->newDeleteQueryBuilder()
93 ->deleteFrom( 'ip_changes' )
94 ->where( [ 'ipc_rev_id' => $oldRevs ] )
95 ->caller( __METHOD__ )->execute();
96 $this->output( "done.\n" );
97 }
98
99 # Purge redundant text records
100 $this->commitTransaction( $dbw, __METHOD__ );
101 if ( $delete ) {
102 $this->purgeRedundantText( true );
103 }
104 }
105}
106
107// @codeCoverageIgnoreStart
108$maintClass = DeleteOldRevisions::class;
109require_once RUN_MAINTENANCE_IF_MAIN;
110// @codeCoverageIgnoreEnd
Maintenance script that deletes old (non-current) revisions from the database.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DB servers to catch up.
getArgs( $offset=0)
Get arguments.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB handle.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.