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