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 require_once __DIR__
. '/../commandLine.inc';
28 require_once 'resolveStubs.php';
30 $fname = 'moveToExternal';
32 if ( !isset( $args[0] ) ) {
33 print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
38 $dbw = wfGetDB( DB_MASTER
);
40 if ( isset( $options['e'] ) ) {
41 $maxID = $options['e'];
43 $maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
45 $minID = isset( $options['s'] ) ?
$options['s'] : 1;
47 moveToExternal( $cluster, $maxID, $minID );
50 function moveToExternal( $cluster, $maxID, $minID = 1 ) {
51 $fname = 'moveToExternal';
52 $dbw = wfGetDB( DB_MASTER
);
53 $dbr = wfGetDB( DB_SLAVE
);
55 $count = $maxID - $minID +
1;
57 $numBlocks = ceil( $count / $blockSize );
58 print "Moving text rows from $minID to $maxID to external storage\n";
59 $ext = new ExternalStoreDB
;
62 for ( $block = 0; $block < $numBlocks; $block++
) {
63 $blockStart = $block * $blockSize +
$minID;
64 $blockEnd = $blockStart +
$blockSize - 1;
66 if ( !( $block % REPORTING_INTERVAL
) ) {
67 print "oldid=$blockStart, moved=$numMoved\n";
71 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
73 "old_id BETWEEN $blockStart AND $blockEnd",
74 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
76 foreach ( $res as $row ) {
78 $text = $row->old_text
;
80 if ( $row->old_flags
=== '' ) {
83 $flags = "{$row->old_flags},external";
86 if ( strpos( $flags, 'object' ) !== false ) {
87 $obj = unserialize( $text );
88 $className = strtolower( get_class( $obj ) );
89 if ( $className == 'historyblobstub' ) {
90 # resolveStub( $id, $row->old_text, $row->old_flags );
93 } elseif ( $className == 'historyblobcurstub' ) {
94 $text = gzdeflate( $obj->getText() );
95 $flags = 'utf-8,gzip,external';
96 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
99 print "Warning: unrecognised object class \"$className\"\n";
106 if ( strlen( $text ) < 100 && $className === false ) {
107 // Don't move tiny revisions
111 # print "Storing " . strlen( $text ) . " bytes to $url\n";
112 # print "old_id=$id\n";
114 $url = $ext->store( $cluster, $text );
116 print "Error writing to external storage\n";
119 $dbw->update( 'text',
120 array( 'old_flags' => $flags, 'old_text' => $url ),
121 array( 'old_id' => $id ), $fname );