Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 114
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrateESRefToAflTable
0.00% covered (danger)
0.00%
0 / 114
0.00% covered (danger)
0.00%
0 / 2
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 91
0.00% covered (danger)
0.00%
0 / 1
420
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 * @file
19 * @ingroup Maintenance ExternalStorage
20 */
21
22namespace MediaWiki\Extension\AbuseFilter\Maintenance;
23
24// @codeCoverageIgnoreStart
25$IP = getenv( 'MW_INSTALL_PATH' );
26if ( $IP === false ) {
27    $IP = __DIR__ . '/../../..';
28}
29require_once "$IP/maintenance/Maintenance.php";
30// @codeCoverageIgnoreEnd
31
32use InvalidArgumentException;
33use MediaWiki\MainConfigNames;
34use MediaWiki\Maintenance\Maintenance;
35use MediaWiki\Storage\SqlBlobStore;
36use Wikimedia\Rdbms\SelectQueryBuilder;
37
38/**
39 * Migrates references of the text table which again references external storage in the abuse_filter_log
40 * table to the abuse_filter_log table directly referencing external storage and getting rid of the row
41 * in the text table.
42 */
43class MigrateESRefToAflTable extends Maintenance {
44
45    public function __construct() {
46        parent::__construct();
47
48        $this->addOption( 'start', 'start afl_id', false, true, 's' );
49        $this->addOption( 'end', 'end afl_id', false, true, 'e' );
50        $this->addOption( 'dry-run', 'Don\'t modify any rows' );
51        $this->addOption(
52            'sleep',
53            'Sleep time (in seconds) between every batch. Default: 0',
54            false,
55            true
56        );
57        $this->addOption(
58            'deletedump',
59            'Filename to dump the list of text table rows which can later be deleted',
60            true,
61            true
62        );
63        $this->addOption(
64            'dump',
65            'Filename to dump tt: -> es:DB references to.',
66            false,
67            true
68        );
69
70        $this->setBatchSize( 100 );
71    }
72
73    public function execute() {
74        if ( !in_array( 'DB', $this->getConfig()->get( MainConfigNames::ExternalStores ) ) ) {
75            $this->fatalError( 'This maintenance script is for use with external storage.' );
76        }
77
78        $dbw = $this->getPrimaryDB();
79        $batchSize = $this->getBatchSize();
80        $dryRun = $this->getOption( 'dry-run', false );
81        $sleep = (float)$this->getOption( 'sleep', 0 );
82
83        $maxID = $this->getOption( 'end' );
84        if ( $maxID === null ) {
85            $maxID = $dbw->newSelectQueryBuilder()
86                ->select( 'afl_id' )
87                ->from( 'abuse_filter_log' )
88                ->orderBy( 'afl_id', SelectQueryBuilder::SORT_DESC )
89                ->limit( 1 )
90                ->caller( __METHOD__ )
91                ->fetchField();
92        }
93        $maxID = (int)$maxID;
94        $minID = (int)$this->getOption( 'start', 1 );
95
96        $diff = $maxID - $minID + 1;
97
98        $dump = $this->getOption( 'dump', false );
99        if ( $dump ) {
100            $dumpfile = fopen( $dump, 'a' );
101            if ( !$dumpfile ) {
102                $this->fatalError( "Failed to open file {$dump}." );
103            }
104        } else {
105            $dumpfile = false;
106        }
107
108        $deletedump = $this->getOption( 'deletedump' );
109        $deletedumpfile = fopen( $deletedump, 'a' );
110        if ( !$deletedumpfile ) {
111            $this->fatalError( "Failed to open file {$deletedump}." );
112        }
113
114        while ( true ) {
115            $res = $dbw->newSelectQueryBuilder()
116                ->select( [ 'afl_id', 'afl_var_dump' ] )
117                ->from( 'abuse_filter_log' )
118                ->conds( [
119                    $dbw->expr( 'afl_id', '>=', $minID ),
120                    $dbw->expr( 'afl_id', '<', $minID + $batchSize ),
121                ] )
122                ->caller( __METHOD__ )
123                ->fetchResultSet();
124
125            foreach ( $res as $row ) {
126                try {
127                    [ $schema, $id, ] = SqlBlobStore::splitBlobAddress( $row->afl_var_dump );
128                } catch ( InvalidArgumentException $ex ) {
129                    $this->output( $ex->getMessage() . ". Use findBadBlobs.php to remedy.\n" );
130                    continue;
131                }
132
133                // Skip blobs which already reference external storage directly
134                if ( $schema === 'es' ) {
135                    continue;
136                }
137
138                // Skip bad blobs
139                if ( $schema !== 'tt' ) {
140                    $this->output( "abuse filter log id {$row->afl_id} has special stuff: {$row->afl_var_dump}\n" );
141                    continue;
142                }
143
144                $oldId = intval( $id );
145
146                if ( !$oldId ) {
147                    $this->output( "Malformed text_id: $oldId\n" );
148                    continue;
149                }
150
151                $textRow = $dbw->newSelectQueryBuilder()
152                    ->select( [ 'old_text', 'old_flags' ] )
153                    ->from( 'text' )
154                    ->where( [ 'old_id' => $oldId ] )
155                    ->caller( __METHOD__ )
156                    ->fetchRow();
157
158                if ( !$textRow ) {
159                    $this->output( "Text row for a blob of metadata of {$row->afl_id} is missing.\n" );
160                    continue;
161                }
162
163                $flags = SqlBlobStore::explodeFlags( $textRow->old_flags );
164
165                if ( !in_array( 'external', $flags ) ) {
166                    $this->output( "old_id {$oldId} is not external.\n" );
167                    continue;
168                }
169
170                $newFlags = implode( ',', array_diff( $flags, [ 'external' ] ) );
171                $newBlobAddress = 'es:' . $textRow->old_text . '?flags=' . $newFlags;
172
173                if ( !$dryRun ) {
174                    $dbw->newUpdateQueryBuilder()
175                        ->update( 'abuse_filter_log' )
176                        ->set( [ 'afl_var_dump' => $newBlobAddress ] )
177                        ->where( [ 'afl_id' => $row->afl_id ] )
178                        ->caller( __METHOD__ )
179                        ->execute();
180
181                    fwrite( $deletedumpfile, $row->afl_var_dump . "\n" );
182
183                    if ( $dumpfile ) {
184                        fwrite( $dumpfile, $row->afl_var_dump . " => " . $newBlobAddress . ";\n" );
185                    }
186                } else {
187                    $this->output( "DRY-RUN: Would set blob address for {$row->afl_id} to "
188                        . "{$newBlobAddress} and mark text row {$oldId} for deletion.\n" );
189                }
190            }
191
192            $this->waitForReplication();
193            if ( $sleep > 0 ) {
194                if ( $sleep >= 1 ) {
195                    sleep( (int)$sleep );
196                } else {
197                    usleep( (int)( $sleep * 1000000 ) );
198                }
199            }
200
201            $this->output( "Processed {$res->numRows()} rows out of $diff.\n" );
202
203            $minID += $batchSize;
204            if ( $minID > $maxID ) {
205                break;
206            }
207        }
208
209        if ( $dumpfile ) {
210            fclose( $dumpfile );
211        }
212
213        fclose( $deletedumpfile );
214    }
215}
216
217// @codeCoverageIgnoreStart
218$maintClass = MigrateESRefToAflTable::class;
219require_once RUN_MAINTENANCE_IF_MAIN;
220// @codeCoverageIgnoreEnd