639ef5846fc4819319fdc244b430efde8017bc8b
3 * Move revision's text to external storage
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance ExternalStorage
24 define( 'REPORTING_INTERVAL', 1 );
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 $optionsWithArgs = [ 'e', 's' ];
28 require_once __DIR__
. '/../commandLine.inc';
29 require_once 'resolveStubs.php';
31 $fname = 'moveToExternal';
33 if ( !isset( $args[0] ) ) {
34 print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
39 $dbw = wfGetDB( DB_MASTER
);
41 if ( isset( $options['e'] ) ) {
42 $maxID = $options['e'];
44 $maxID = $dbw->selectField( 'text', 'MAX(old_id)', '', $fname );
46 $minID = $options['s'] ??
1;
48 moveToExternal( $cluster, $maxID, $minID );
51 function moveToExternal( $cluster, $maxID, $minID = 1 ) {
52 $fname = 'moveToExternal';
53 $dbw = wfGetDB( DB_MASTER
);
54 $dbr = wfGetDB( DB_REPLICA
);
56 $count = $maxID - $minID +
1;
58 $numBlocks = ceil( $count / $blockSize );
59 print "Moving text rows from $minID to $maxID to external storage\n";
60 $ext = new ExternalStoreDB
;
63 for ( $block = 0; $block < $numBlocks; $block++
) {
64 $blockStart = $block * $blockSize +
$minID;
65 $blockEnd = $blockStart +
$blockSize - 1;
67 if ( !( $block % REPORTING_INTERVAL
) ) {
68 print "oldid=$blockStart, moved=$numMoved\n";
72 $res = $dbr->select( 'text', [ 'old_id', 'old_flags', 'old_text' ],
74 "old_id BETWEEN $blockStart AND $blockEnd",
75 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
77 foreach ( $res as $row ) {
79 $text = $row->old_text
;
81 if ( $row->old_flags
=== '' ) {
84 $flags = "{$row->old_flags},external";
87 if ( strpos( $flags, 'object' ) !== false ) {
88 $obj = unserialize( $text );
89 $className = strtolower( get_class( $obj ) );
90 if ( $className == 'historyblobstub' ) {
91 # resolveStub( $id, $row->old_text, $row->old_flags );
94 } elseif ( $className == 'historyblobcurstub' ) {
95 $text = gzdeflate( $obj->getText() );
96 $flags = 'utf-8,gzip,external';
97 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
100 print "Warning: unrecognised object class \"$className\"\n";
107 if ( strlen( $text ) < 100 && $className === false ) {
108 // Don't move tiny revisions
112 # print "Storing " . strlen( $text ) . " bytes to $url\n";
113 # print "old_id=$id\n";
115 $url = $ext->store( $cluster, $text );
117 print "Error writing to external storage\n";
120 $dbw->update( 'text',
121 [ 'old_flags' => $flags, 'old_text' => $url ],
122 [ 'old_id' => $id ], $fname );