MediaWiki REL1_31
deleteAutoPatrolLogs.php
Go to the documentation of this file.
1<?php
19require_once __DIR__ . '/Maintenance.php';
20
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(
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(
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(
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;
198require_once RUN_MAINTENANCE_IF_MAIN;
unserialize( $serialized)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
hasOption( $name)
Checks to see if a particular param exists.
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Set the batch size.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2783
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped true if there is text before this autocomment $auto
Definition hooks.txt:1587
require_once RUN_MAINTENANCE_IF_MAIN
$last
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29
$params