Debian Codegraph @

maintenance/deleteOldRevisions.php

raw symbols SCIP
1 <?php
2 /**
3 * Delete old (non-current) revisions from the database
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <[email protected]>
23 */
24
25 // @codeCoverageIgnoreStart
26 require_once __DIR__ . '/Maintenance.php';
27 // @codeCoverageIgnoreEnd
28
29 /**
30 * Maintenance script that deletes old (non-current) revisions from the database.
31 *
32 * @ingroup Maintenance
33 */
34 class DeleteOldRevisions extends Maintenance {
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;
121 require_once RUN_MAINTENANCE_IF_MAIN;
122 // @codeCoverageIgnoreEnd