MediaWiki REL1_39
deleteOldRevisions.php
Go to the documentation of this file.
1<?php
25require_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 private function doDelete( $delete = false, $args = [] ) {
46 # Data should come off the master, wrapped in a transaction
47 $dbw = $this->getDB( DB_PRIMARY );
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->newSelectQueryBuilder()
63 ->select( 'page_latest' )
64 ->from( 'page' )
65 ->where( $pageConds )
66 ->caller( __METHOD__ )
67 ->fetchResultSet();
68 $latestRevs = [];
69 foreach ( $res as $row ) {
70 $latestRevs[] = $row->page_latest;
71 }
72 $this->output( "done.\n" );
73
74 # Get all revisions that aren't in this set
75 $this->output( "Searching for inactive revisions..." );
76 if ( count( $latestRevs ) > 0 ) {
77 $revConds[] = 'rev_id NOT IN (' . $dbw->makeList( $latestRevs ) . ')';
78 }
79 $res = $dbw->newSelectQueryBuilder()
80 ->select( 'rev_id' )
81 ->from( 'revision' )
82 ->where( $revConds )
83 ->caller( __METHOD__ )
84 ->fetchResultSet();
85 $oldRevs = [];
86 foreach ( $res as $row ) {
87 $oldRevs[] = $row->rev_id;
88 }
89 $this->output( "done.\n" );
90
91 # Inform the user of what we're going to do
92 $count = count( $oldRevs );
93 $this->output( "$count old revisions found.\n" );
94
95 # Delete as appropriate
96 if ( $delete && $count ) {
97 $this->output( "Deleting..." );
98 $dbw->delete( 'revision', [ 'rev_id' => $oldRevs ], __METHOD__ );
99 $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $oldRevs ], __METHOD__ );
100 $this->output( "done.\n" );
101 }
102
103 # Purge redundant text records
104 $this->commitTransaction( $dbw, __METHOD__ );
105 if ( $delete ) {
106 $this->purgeRedundantText( true );
107 }
108 }
109}
110
111$maintClass = DeleteOldRevisions::class;
112require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
if( $line===false) $args
Definition mcc.php:124
const DB_PRIMARY
Definition defines.php:28