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