MediaWiki master
deleteOldRevisions.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Delete old (non-current) revisions from the database' );
40 $this->addOption( 'delete', 'Actually perform the deletion' );
41 $this->addArg( 'page_id', 'List of page ids to work on', false, true );
42 }
43
44 public function execute() {
45 $this->output( "Delete old revisions\n\n" );
46 $this->doDelete( $this->hasOption( 'delete' ), $this->getArgs( 'page_id' ) );
47 }
48
49 private function doDelete( $delete = false, $pageIds = [] ) {
50 # Data should come off the master, wrapped in a transaction
51 $dbw = $this->getPrimaryDB();
52 $this->beginTransaction( $dbw, __METHOD__ );
53
54 $pageConds = [];
55 $revConds = [];
56
57 # If a list of page_ids was provided, limit results to that set of page_ids
58 if ( count( $pageIds ) > 0 ) {
59 $pageConds['page_id'] = $pageIds;
60 $revConds['rev_page'] = $pageIds;
61 $this->output( "Limiting to page IDs " . implode( ',', $pageIds ) . "\n" );
62 }
63
64 # Get "active" revisions from the page table
65 $this->output( "Searching for active revisions..." );
66 $res = $dbw->newSelectQueryBuilder()
67 ->select( 'page_latest' )
68 ->from( 'page' )
69 ->where( $pageConds )
70 ->caller( __METHOD__ )
71 ->fetchResultSet();
72 $latestRevs = [];
73 foreach ( $res as $row ) {
74 $latestRevs[] = $row->page_latest;
75 }
76 $this->output( "done.\n" );
77
78 # Get all revisions that aren't in this set
79 $this->output( "Searching for inactive revisions..." );
80 if ( count( $latestRevs ) > 0 ) {
81 $revConds[] = $dbw->expr( 'rev_id', '!=', $latestRevs );
82 }
83 $res = $dbw->newSelectQueryBuilder()
84 ->select( 'rev_id' )
85 ->from( 'revision' )
86 ->where( $revConds )
87 ->caller( __METHOD__ )
88 ->fetchResultSet();
89 $oldRevs = [];
90 foreach ( $res as $row ) {
91 $oldRevs[] = $row->rev_id;
92 }
93 $this->output( "done.\n" );
94
95 # Inform the user of what we're going to do
96 $count = count( $oldRevs );
97 $this->output( "$count old revisions found.\n" );
98
99 # Delete as appropriate
100 if ( $delete && $count ) {
101 $this->output( "Deleting..." );
102 $dbw->newDeleteQueryBuilder()
103 ->deleteFrom( 'revision' )
104 ->where( [ 'rev_id' => $oldRevs ] )
105 ->caller( __METHOD__ )->execute();
106 $dbw->newDeleteQueryBuilder()
107 ->deleteFrom( 'ip_changes' )
108 ->where( [ 'ipc_rev_id' => $oldRevs ] )
109 ->caller( __METHOD__ )->execute();
110 $this->output( "done.\n" );
111 }
112
113 # Purge redundant text records
114 $this->commitTransaction( $dbw, __METHOD__ );
115 if ( $delete ) {
116 $this->purgeRedundantText( true );
117 }
118 }
119}
120
121// @codeCoverageIgnoreStart
122$maintClass = DeleteOldRevisions::class;
123require_once RUN_MAINTENANCE_IF_MAIN;
124// @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.
addDescription( $text)
Set the description text.