registration: Improve duplicate config setting exception
[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 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
60
61 $sleep = (int)$this->getOption( 'sleep', 10 );
62 $fromId = $this->getOption( 'from-id', null );
63 $this->countDown( 5 );
64 while ( true ) {
65 if ( $this->hasOption( 'check-old' ) ) {
66 $rowsData = $this->getRowsOld( $fromId );
67 // We reached end of the table
68 if ( !$rowsData ) {
69 break;
70 }
71 $rows = $rowsData['rows'];
72 $fromId = $rowsData['lastId'];
73
74 // There is nothing to delete in this batch
75 if ( !$rows ) {
76 continue;
77 }
78 } else {
79 $rows = $this->getRows( $fromId );
80 if ( !$rows ) {
81 break;
82 }
83 $fromId = end( $rows );
84 }
85
86 if ( $this->hasOption( 'dry-run' ) ) {
87 $this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" );
88 } else {
89 $this->deleteRows( $rows );
90 $this->output( 'Processed up to row id ' . end( $rows ) . "\n" );
91 }
92
93 if ( $sleep > 0 ) {
94 sleep( $sleep );
95 }
96 }
97 }
98
99 private function getRows( $fromId ) {
100 $dbr = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
101 DB_REPLICA
102 );
103 $before = $this->getOption( 'before', false );
104
105 $conds = [
106 'log_type' => 'patrol',
107 'log_action' => 'autopatrol',
108 ];
109
110 if ( $fromId ) {
111 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
112 }
113
114 if ( $before ) {
115 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
116 }
117
118 return $dbr->selectFieldValues(
119 'logging',
120 'log_id',
121 $conds,
122 __METHOD__,
123 [ 'LIMIT' => $this->getBatchSize() ]
124 );
125 }
126
127 private function getRowsOld( $fromId ) {
128 $dbr = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
129 DB_REPLICA
130 );
131 $batchSize = $this->getBatchSize();
132 $before = $this->getOption( 'before', false );
133
134 $conds = [
135 'log_type' => 'patrol',
136 'log_action' => 'patrol',
137 ];
138
139 if ( $fromId ) {
140 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
141 }
142
143 if ( $before ) {
144 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
145 }
146
147 $result = $dbr->select(
148 'logging',
149 [ 'log_id', 'log_params' ],
150 $conds,
151 __METHOD__,
152 [ 'LIMIT' => $batchSize ]
153 );
154
155 $last = null;
156 $autopatrols = [];
157 foreach ( $result as $row ) {
158 $last = $row->log_id;
159 Wikimedia\suppressWarnings();
160 $params = unserialize( $row->log_params );
161 Wikimedia\restoreWarnings();
162
163 // Skipping really old rows, before 2011
164 if ( !is_array( $params ) || !array_key_exists( '6::auto', $params ) ) {
165 continue;
166 }
167
168 $auto = $params['6::auto'];
169 if ( $auto ) {
170 $autopatrols[] = $row->log_id;
171 }
172 }
173
174 if ( $last === null ) {
175 return null;
176 }
177
178 return [ 'rows' => $autopatrols, 'lastId' => $last ];
179 }
180
181 private function deleteRows( array $rows ) {
182 $dbw = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
183 DB_MASTER
184 );
185
186 $dbw->delete(
187 'logging',
188 [ 'log_id' => $rows ],
189 __METHOD__
190 );
191
192 MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
193 }
194
195 }
196
197 $maintClass = DeleteAutoPatrolLogs::class;
198 require_once RUN_MAINTENANCE_IF_MAIN;