MediaWiki  1.33.0
moveToExternal.php
Go to the documentation of this file.
1 <?php
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";
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 }
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
ExternalStoreDB
DB accessible external objects.
Definition: ExternalStoreDB.php:38
$optionsWithArgs
global $optionsWithArgs
Definition: commandLine.inc:24
moveToExternal
if(!defined( 'MEDIAWIKI')) moveToExternal( $cluster, $maxID, $minID=1)
Definition: moveToExternal.php:47
$res
$res
Definition: database.txt:21
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2790
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$dbr
$dbr
Definition: testCompression.php:50
REPORTING_INTERVAL
const REPORTING_INTERVAL
Definition: moveToExternal.php:24
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:123
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
$args
if( $line===false) $args
Definition: cdb.php:64
$options
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:1985
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48