Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FixLegacyEncoding
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getConditions
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 resolveText
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
56
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
22use MediaWiki\Storage\SqlBlobStore;
23use Wikimedia\Rdbms\IExpression;
24use Wikimedia\Rdbms\IReadableDatabase;
25use Wikimedia\Rdbms\LikeValue;
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/moveToExternal.php';
29// @codeCoverageIgnoreEnd
30
31class FixLegacyEncoding extends MoveToExternal {
32    public function __construct() {
33        parent::__construct();
34        $this->addDescription( 'Change encoding of stored content from legacy encoding to UTF-8' );
35    }
36
37    protected function getConditions( int $blockStart, int $blockEnd, IReadableDatabase $dbr ): array {
38        return [
39            $dbr->expr( 'old_id', '>=', $blockStart ),
40            $dbr->expr( 'old_id', '<=', $blockEnd ),
41            $dbr->expr( 'old_flags', IExpression::NOT_LIKE,
42                new LikeValue( $dbr->anyString(), 'utf-8', $dbr->anyString() ) ),
43            $dbr->expr( 'old_flags', IExpression::NOT_LIKE,
44                new LikeValue( $dbr->anyString(), 'utf8', $dbr->anyString() ) ),
45        ];
46    }
47
48    protected function resolveText( string $text, array $flags ): array {
49        if ( in_array( 'error', $flags ) ) {
50            return [ $text, $flags ];
51        }
52        $blobStore = $this->getServiceContainer()->getBlobStore();
53        if ( in_array( 'external', $flags ) && $blobStore instanceof SqlBlobStore ) {
54            $newText = $blobStore->expandBlob( $text, $flags );
55            if ( $newText === false ) {
56                return [ false, $flags ];
57            }
58            $text = $newText;
59            // It will be put back in external storage again
60            $flags = array_diff( $flags, [ 'external' ] );
61        }
62        if ( in_array( 'gzip', $flags ) ) {
63            $newText = gzinflate( $text );
64            if ( $newText === false ) {
65                return [ false, $flags ];
66            }
67            $text = $newText;
68            $flags = array_diff( $flags, [ 'gzip' ] );
69        }
70        return [ $text, $flags ];
71    }
72
73}
74
75// @codeCoverageIgnoreStart
76$maintClass = FixLegacyEncoding::class;
77require_once RUN_MAINTENANCE_IF_MAIN;
78// @codeCoverageIgnoreEnd