3 * Convert history stubs that point to an external row to direct external
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance ExternalStorage
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 $optionsWithArgs = [ 'm' ];
28 require_once __DIR__
. '/../commandLine.inc';
34 * Convert history stubs that point to an external row to direct
37 function resolveStubs() {
38 $fname = 'resolveStubs';
40 $dbr = wfGetDB( DB_SLAVE
);
41 $maxID = $dbr->selectField( 'text', 'MAX(old_id)', false, $fname );
43 $numBlocks = intval( $maxID / $blockSize ) +
1;
45 for ( $b = 0; $b < $numBlocks; $b++
) {
48 printf( "%5.2f%%\n", $b / $numBlocks * 100 );
49 $start = intval( $maxID / $numBlocks ) * $b +
1;
50 $end = intval( $maxID / $numBlocks ) * ( $b +
1 );
52 $res = $dbr->select( 'text', [ 'old_id', 'old_text', 'old_flags' ],
53 "old_id>=$start AND old_id<=$end " .
54 "AND old_flags LIKE '%object%' AND old_flags NOT LIKE '%external%' " .
55 'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\'',
57 foreach ( $res as $row ) {
58 resolveStub( $row->old_id
, $row->old_text
, $row->old_flags
);
65 * Resolve a history stub
67 * @param string $stubText
68 * @param string $flags
70 function resolveStub( $id, $stubText, $flags ) {
71 $fname = 'resolveStub';
73 $stub = unserialize( $stubText );
74 $flags = explode( ',', $flags );
76 $dbr = wfGetDB( DB_SLAVE
);
77 $dbw = wfGetDB( DB_MASTER
);
79 if ( strtolower( get_class( $stub ) ) !== 'historyblobstub' ) {
80 print "Error found object of class " . get_class( $stub ) . ", expecting historyblobstub\n";
85 # Get the (maybe) external row
86 $externalRow = $dbr->selectRow(
90 'old_id' => $stub->mOldId
,
91 'old_flags' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() )
96 if ( !$externalRow ) {
97 # Object wasn't external
101 # Preserve the legacy encoding flag, but switch from object to external
102 if ( in_array( 'utf-8', $flags ) ) {
103 $newFlags = 'external,utf-8';
105 $newFlags = 'external';
109 # print "oldid=$id\n";
110 $dbw->update( 'text',
112 'old_flags' => $newFlags,
113 'old_text' => $externalRow->old_text
. '/' . $stub->mHash