MediaWiki REL1_33
moveToExternal.php
Go to the documentation of this file.
1<?php
24define( 'REPORTING_INTERVAL', 1 );
25
26if ( !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
47function moveToExternal( $cluster, $maxID, $minID = 1 ) {
48 $fname = 'moveToExternal';
49 $dbw = wfGetDB( DB_MASTER );
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";
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";
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}
unserialize( $serialized)
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:123
if( $line===false) $args
Definition cdb.php:64
DB accessible external objects.
global $optionsWithArgs
$res
Definition database.txt:21
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
if(!defined('MEDIAWIKI')) moveToExternal( $cluster, $maxID, $minID=1)
const REPORTING_INTERVAL
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26
if(!is_readable( $file)) $ext
Definition router.php:48