Merge "Revert "Add type hint against LinkTarget""
[lhc/web/wiklou.git] / maintenance / storage / moveToExternal.php
1 <?php
2 /**
3 * Move revision's text to external storage
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance ExternalStorage
22 */
23
24 define( 'REPORTING_INTERVAL', 1 );
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 require_once __DIR__ . '/../commandLine.inc';
28 require_once 'resolveStubs.php';
29
30 $fname = 'moveToExternal';
31
32 if ( !isset( $args[0] ) ) {
33 print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
34 exit;
35 }
36
37 $cluster = $args[0];
38 $dbw = wfGetDB( DB_MASTER );
39
40 if ( isset( $options['e'] ) ) {
41 $maxID = $options['e'];
42 } else {
43 $maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
44 }
45 $minID = isset( $options['s'] ) ? $options['s'] : 1;
46
47 moveToExternal( $cluster, $maxID, $minID );
48 }
49
50 function moveToExternal( $cluster, $maxID, $minID = 1 ) {
51 $fname = 'moveToExternal';
52 $dbw = wfGetDB( DB_MASTER );
53 $dbr = wfGetDB( DB_SLAVE );
54
55 $count = $maxID - $minID + 1;
56 $blockSize = 1000;
57 $numBlocks = ceil( $count / $blockSize );
58 print "Moving text rows from $minID to $maxID to external storage\n";
59 $ext = new ExternalStoreDB;
60 $numMoved = 0;
61
62 for ( $block = 0; $block < $numBlocks; $block++ ) {
63 $blockStart = $block * $blockSize + $minID;
64 $blockEnd = $blockStart + $blockSize - 1;
65
66 if ( !( $block % REPORTING_INTERVAL ) ) {
67 print "oldid=$blockStart, moved=$numMoved\n";
68 wfWaitForSlaves();
69 }
70
71 $res = $dbr->select( 'text', [ 'old_id', 'old_flags', 'old_text' ],
72 [
73 "old_id BETWEEN $blockStart AND $blockEnd",
74 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
75 ], $fname );
76 foreach ( $res as $row ) {
77 # Resolve stubs
78 $text = $row->old_text;
79 $id = $row->old_id;
80 if ( $row->old_flags === '' ) {
81 $flags = 'external';
82 } else {
83 $flags = "{$row->old_flags},external";
84 }
85
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 );
91 # $numStubs++;
92 continue;
93 } elseif ( $className == 'historyblobcurstub' ) {
94 $text = gzdeflate( $obj->getText() );
95 $flags = 'utf-8,gzip,external';
96 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
97 // Do nothing
98 } else {
99 print "Warning: unrecognised object class \"$className\"\n";
100 continue;
101 }
102 } else {
103 $className = false;
104 }
105
106 if ( strlen( $text ) < 100 && $className === false ) {
107 // Don't move tiny revisions
108 continue;
109 }
110
111 # print "Storing " . strlen( $text ) . " bytes to $url\n";
112 # print "old_id=$id\n";
113
114 $url = $ext->store( $cluster, $text );
115 if ( !$url ) {
116 print "Error writing to external storage\n";
117 exit;
118 }
119 $dbw->update( 'text',
120 [ 'old_flags' => $flags, 'old_text' => $url ],
121 [ 'old_id' => $id ], $fname );
122 $numMoved++;
123 }
124 }
125 }