Merge "Introduce deleteAutoPatrolLogs maintenance script"
[lhc/web/wiklou.git] / maintenance / deleteAutoPatrolLogs.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 require_once __DIR__ . '/Maintenance.php';
20
21 /**
22 * Remove autopatrol logs in the logging table.
23 *
24 * @ingroup Maintenance
25 */
26 class DeleteAutoPatrolLogs extends Maintenance {
27
28 public function __construct() {
29 parent::__construct();
30 $this->addDescription( 'Remove autopatrol logs in the logging table' );
31 $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
32 $this->addOption(
33 'check-old',
34 'Check old patrol logs (for deleting old format autopatrols).' .
35 'Note that this will not delete rows older than 2011 (MediaWiki 1.18).'
36 );
37 $this->addOption(
38 'before',
39 'Timestamp to delete only before that time, all MediaWiki timestamp formats are accepted',
40 false,
41 true
42 );
43 $this->addOption(
44 'from-id',
45 'First row (log id) to start updating from',
46 false,
47 true
48 );
49 $this->addOption(
50 'sleep',
51 'Sleep time (in seconds) between every batch',
52 false,
53 true
54 );
55 $this->setBatchSize( 1000 );
56 }
57
58 public function execute() {
59 $sleep = (int)$this->getOption( 'sleep', 10 );
60 $fromId = $this->getOption( 'from-id', null );
61 $this->countDown( 5 );
62 while ( true ) {
63 if ( $this->hasOption( 'check-old' ) ) {
64 $rowsData = $this->getRowsOld( $fromId );
65 // We reached end of the table
66 if ( !$rowsData ) {
67 break;
68 }
69 $rows = $rowsData['rows'];
70 $fromId = $rowsData['lastId'];
71
72 // There is nothing to delete in this batch
73 if ( !$rows ) {
74 continue;
75 }
76 } else {
77 $rows = $this->getRows( $fromId );
78 if ( !$rows ) {
79 break;
80 }
81 $fromId = end( $rows );
82 }
83
84 if ( $this->hasOption( 'dry-run' ) ) {
85 $this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" );
86 } else {
87 $this->deleteRows( $rows );
88 $this->output( 'Processed up to row id ' . end( $rows ) . "\n" );
89 }
90
91 if ( $sleep > 0 ) {
92 sleep( $sleep );
93 }
94 }
95 }
96
97 private function getRows( $fromId ) {
98 $dbr = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
99 DB_REPLICA
100 );
101 $before = $this->getOption( 'before', false );
102
103 $conds = [
104 'log_type' => 'patrol',
105 'log_action' => 'autopatrol',
106 ];
107
108 if ( $fromId ) {
109 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
110 }
111
112 if ( $before ) {
113 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
114 }
115
116 return $dbr->selectFieldValues(
117 'logging',
118 'log_id',
119 $conds,
120 __METHOD__,
121 [ 'LIMIT' => $this->getBatchSize() ]
122 );
123 }
124
125 private function getRowsOld( $fromId ) {
126 $dbr = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
127 DB_REPLICA
128 );
129 $batchSize = $this->getBatchSize();
130 $before = $this->getOption( 'before', false );
131
132 $conds = [
133 'log_type' => 'patrol',
134 'log_action' => 'patrol',
135 ];
136
137 if ( $fromId ) {
138 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
139 }
140
141 if ( $before ) {
142 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
143 }
144
145 $result = $dbr->select(
146 'logging',
147 [ 'log_id', 'log_params' ],
148 $conds,
149 __METHOD__,
150 [ 'LIMIT' => $batchSize ]
151 );
152
153 $last = null;
154 $autopatrolls = [];
155 foreach ( $result as $row ) {
156 $last = $row->log_id;
157 Wikimedia\suppressWarnings();
158 $params = unserialize( $row->log_params );
159 Wikimedia\restoreWarnings();
160
161 // Skipping really old rows, before 2011
162 if ( is_array( $params ) && !array_key_exists( '6::auto', $params ) ) {
163 continue;
164 }
165
166 $auto = $params['6::auto'];
167 if ( $auto ) {
168 $autopatrolls[] = $row->log_id;
169 }
170 }
171
172 if ( $last === null ) {
173 return null;
174 }
175
176 return [ 'rows' => $autopatrolls, 'lastId' => $last ];
177 }
178
179 private function deleteRows( array $rows ) {
180 $dbw = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
181 DB_MASTER
182 );
183
184 $dbw->delete(
185 'logging',
186 [ 'log_id' => $rows ],
187 __METHOD__
188 );
189
190 MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
191 }
192
193 }
194
195 $maintClass = DeleteAutoPatrolLogs::class;
196 require_once RUN_MAINTENANCE_IF_MAIN;