MediaWiki  1.23.2
backup.inc
Go to the documentation of this file.
1 <?php
31  function __construct( $file ) {
32  parent::__construct( "dbzip2", $file );
33  }
34 }
35 
39 class BackupDumper {
40  var $reportingInterval = 100;
41  var $reporting = true;
42  var $pageCount = 0;
43  var $revCount = 0;
44  var $server = null; // use default
45  var $pages = null; // all pages
46  var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
47  var $skipFooter = false; // don't output </mediawiki>
48  var $startId = 0;
49  var $endId = 0;
50  var $revStartId = 0;
51  var $revEndId = 0;
52  var $sink = null; // Output filters
53  var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
54  var $dumpUploads = false;
56  var $lastTime = 0;
57  var $pageCountLast = 0;
58  var $revCountLast = 0;
59  var $ID = 0;
60 
62 
70  protected $forcedDb = null;
71 
75  protected $lb;
76 
77  function __construct( $args ) {
78  $this->stderr = fopen( "php://stderr", "wt" );
79 
80  // Built-in output and filter plugins
81  $this->registerOutput( 'file', 'DumpFileOutput' );
82  $this->registerOutput( 'gzip', 'DumpGZipOutput' );
83  $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
84  $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
85  $this->registerOutput( '7zip', 'Dump7ZipOutput' );
86 
87  $this->registerFilter( 'latest', 'DumpLatestFilter' );
88  $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
89  $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
90 
91  $this->sink = $this->processArgs( $args );
92  }
93 
98  function registerOutput( $name, $class ) {
99  $this->outputTypes[$name] = $class;
100  }
101 
106  function registerFilter( $name, $class ) {
107  $this->filterTypes[$name] = $class;
108  }
109 
117  function loadPlugin( $class, $file ) {
118  if ( $file != '' ) {
119  require_once $file;
120  }
121  $register = array( $class, 'register' );
122  call_user_func_array( $register, array( &$this ) );
123  }
124 
129  function processArgs( $args ) {
130  $sink = null;
131  $sinks = array();
132  foreach ( $args as $arg ) {
133  $matches = array();
134  if ( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
135  @list( /* $full */ , $opt, $val, $param ) = $matches;
136  switch ( $opt ) {
137  case "plugin":
138  $this->loadPlugin( $val, $param );
139  break;
140  case "output":
141  if ( !is_null( $sink ) ) {
142  $sinks[] = $sink;
143  }
144  if ( !isset( $this->outputTypes[$val] ) ) {
145  $this->fatalError( "Unrecognized output sink type '$val'" );
146  }
147  $type = $this->outputTypes[$val];
148  $sink = new $type( $param );
149  break;
150  case "filter":
151  if ( is_null( $sink ) ) {
152  $sink = new DumpOutput();
153  }
154  if ( !isset( $this->filterTypes[$val] ) ) {
155  $this->fatalError( "Unrecognized filter type '$val'" );
156  }
157  $type = $this->filterTypes[$val];
158  $filter = new $type( $sink, $param );
159 
160  // references are lame in php...
161  unset( $sink );
162  $sink = $filter;
163 
164  break;
165  case "report":
166  $this->reportingInterval = intval( $val );
167  break;
168  case "server":
169  $this->server = $val;
170  break;
171  case "force-normal":
172  if ( !function_exists( 'utf8_normalize' ) ) {
173  $this->fatalError( "UTF-8 normalization extension not loaded. " .
174  "Install or remove --force-normal parameter to use slower code." );
175  }
176  break;
177  default:
178  $this->processOption( $opt, $val, $param );
179  }
180  }
181  }
182 
183  if ( is_null( $sink ) ) {
184  $sink = new DumpOutput();
185  }
186  $sinks[] = $sink;
187 
188  if ( count( $sinks ) > 1 ) {
189  return new DumpMultiWriter( $sinks );
190  } else {
191  return $sink;
192  }
193  }
194 
195  function processOption( $opt, $val, $param ) {
196  // extension point for subclasses to add options
197  }
198 
199  function dump( $history, $text = WikiExporter::TEXT ) {
200  # Notice messages will foul up your XML output even if they're
201  # relatively harmless.
202  if ( ini_get( 'display_errors' ) ) {
203  ini_set( 'display_errors', 'stderr' );
204  }
205 
206  $this->initProgress( $history );
207 
208  $db = $this->backupDb();
209  $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
210  $exporter->dumpUploads = $this->dumpUploads;
211  $exporter->dumpUploadFileContents = $this->dumpUploadFileContents;
212 
213  $wrapper = new ExportProgressFilter( $this->sink, $this );
214  $exporter->setOutputSink( $wrapper );
215 
216  if ( !$this->skipHeader ) {
217  $exporter->openStream();
218  }
219  # Log item dumps: all or by range
220  if ( $history & WikiExporter::LOGS ) {
221  if ( $this->startId || $this->endId ) {
222  $exporter->logsByRange( $this->startId, $this->endId );
223  } else {
224  $exporter->allLogs();
225  }
226  # Page dumps: all or by page ID range
227  } elseif ( is_null( $this->pages ) ) {
228  if ( $this->startId || $this->endId ) {
229  $exporter->pagesByRange( $this->startId, $this->endId );
230  } elseif ( $this->revStartId || $this->revEndId ) {
231  $exporter->revsByRange( $this->revStartId, $this->revEndId );
232  } else {
233  $exporter->allPages();
234  }
235  # Dump of specific pages
236  } else {
237  $exporter->pagesByName( $this->pages );
238  }
239 
240  if ( !$this->skipFooter ) {
241  $exporter->closeStream();
242  }
243 
244  $this->report( true );
245  }
246 
253  function initProgress( $history = WikiExporter::FULL ) {
254  $table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
255  $field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
256 
258  if ( $this->forcedDb === null ) {
259  $dbr = wfGetDB( DB_SLAVE );
260  }
261  $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
262  $this->startTime = microtime( true );
263  $this->lastTime = $this->startTime;
264  $this->ID = getmypid();
265  }
266 
273  function backupDb() {
274  if ( $this->forcedDb !== null ) {
275  return $this->forcedDb;
276  }
277 
278  $this->lb = wfGetLBFactory()->newMainLB();
279  $db = $this->lb->getConnection( DB_SLAVE, 'dump' );
280 
281  // Discourage the server from disconnecting us if it takes a long time
282  // to read out the big ol' batch query.
283  $db->setSessionOptions( array( 'connTimeout' => 3600 * 24 ) );
284 
285  return $db;
286  }
287 
296  function setDb( DatabaseBase $db = null ) {
297  $this->forcedDb = $db;
298  }
299 
300  function __destruct() {
301  if ( isset( $this->lb ) ) {
302  $this->lb->closeAll();
303  }
304  }
305 
306  function backupServer() {
307  global $wgDBserver;
308  return $this->server
309  ? $this->server
310  : $wgDBserver;
311  }
312 
313  function reportPage() {
314  $this->pageCount++;
315  }
316 
317  function revCount() {
318  $this->revCount++;
319  $this->report();
320  }
321 
322  function report( $final = false ) {
323  if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
324  $this->showReport();
325  }
326  }
327 
328  function showReport() {
329  if ( $this->reporting ) {
330  $now = wfTimestamp( TS_DB );
331  $nowts = microtime( true );
332  $deltaAll = $nowts - $this->startTime;
333  $deltaPart = $nowts - $this->lastTime;
334  $this->pageCountPart = $this->pageCount - $this->pageCountLast;
335  $this->revCountPart = $this->revCount - $this->revCountLast;
336 
337  if ( $deltaAll ) {
338  $portion = $this->revCount / $this->maxCount;
339  $eta = $this->startTime + $deltaAll / $portion;
340  $etats = wfTimestamp( TS_DB, intval( $eta ) );
341  $pageRate = $this->pageCount / $deltaAll;
342  $revRate = $this->revCount / $deltaAll;
343  } else {
344  $pageRate = '-';
345  $revRate = '-';
346  $etats = '-';
347  }
348  if ( $deltaPart ) {
349  $pageRatePart = $this->pageCountPart / $deltaPart;
350  $revRatePart = $this->revCountPart / $deltaPart;
351  } else {
352  $pageRatePart = '-';
353  $revRatePart = '-';
354  }
355  $this->progress( sprintf( "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), %d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]",
356  $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, $pageRatePart, $this->revCount, $revRate, $revRatePart, $etats, $this->maxCount ) );
357  $this->lastTime = $nowts;
358  $this->revCountLast = $this->revCount;
359  }
360  }
361 
362  function progress( $string ) {
363  fwrite( $this->stderr, $string . "\n" );
364  }
365 
366  function fatalError( $msg ) {
367  $this->progress( "$msg\n" );
368  die( 1 );
369  }
370 }
371 
372 class ExportProgressFilter extends DumpFilter {
373  function __construct( &$sink, &$progress ) {
374  parent::__construct( $sink );
375  $this->progress = $progress;
376  }
377 
378  function writeClosePage( $string ) {
379  parent::writeClosePage( $string );
380  $this->progress->reportPage();
381  }
382 
383  function writeRevision( $rev, $string ) {
384  parent::writeRevision( $rev, $string );
385  $this->progress->revCount();
386  }
387 }
BackupDumper\registerOutput
registerOutput( $name, $class)
Definition: backup.inc:96
BackupDumper\$endId
$endId
Definition: backup.inc:49
ID
occurs before session is loaded can be modified ID
Definition: hooks.txt:2818
BackupDumper\revCount
revCount()
Definition: backup.inc:315
BackupDumper\backupDb
backupDb()
Definition: backup.inc:271
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
BackupDumper\fatalError
fatalError( $msg)
Definition: backup.inc:364
WikiExporter\CURRENT
const CURRENT
Definition: Export.php:41
BackupDumper\$revEndId
$revEndId
Definition: backup.inc:51
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
BackupDumper\backupServer
backupServer()
Definition: backup.inc:304
BackupDumper\$forcedDb
DatabaseBase null $forcedDb
The dependency-injected database to use.
Definition: backup.inc:69
BackupDumper\__destruct
__destruct()
Definition: backup.inc:298
skipFooter
$dumper skipFooter
Definition: dumpBackup.php:68
BackupDumper\$revCountLast
$revCountLast
Definition: backup.inc:58
ExportProgressFilter\writeRevision
writeRevision( $rev, $string)
Definition: backup.inc:381
BackupDumper\$pageCountLast
$pageCountLast
Definition: backup.inc:57
TS_DB
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition: GlobalFunctions.php:2436
BackupDumper\$revCount
$revCount
Definition: backup.inc:43
BackupDumper\$sink
$sink
Definition: backup.inc:52
BackupDumper\$lb
LoadBalancer $lb
Definition: backup.inc:73
BackupDumper\$skipFooter
$skipFooter
Definition: backup.inc:47
pages
The ContentHandler facility adds support for arbitrary content types on wiki pages
Definition: contenthandler.txt:1
$dbr
$dbr
Definition: testCompression.php:48
DumpMultiWriter
Base class for output stream; prints to stdout or buffer or wherever.
Definition: Export.php:1459
BackupDumper\$skipHeader
$skipHeader
Definition: backup.inc:46
BackupDumper\$reporting
$reporting
Definition: backup.inc:41
BackupDumper\showReport
showReport()
Definition: backup.inc:326
WikiExporter\TEXT
const TEXT
Definition: Export.php:49
BackupDumper\$stubText
$stubText
Definition: backup.inc:53
ExportProgressFilter\writeClosePage
writeClosePage( $string)
Definition: backup.inc:376
BackupDumper\$startId
$startId
Definition: backup.inc:48
BackupDumper\loadPlugin
loadPlugin( $class, $file)
Load a plugin and register it.
Definition: backup.inc:115
BackupDumper\setDb
setDb(DatabaseBase $db=null)
Force the dump to use the provided database connection for database operations, wherever possible.
Definition: backup.inc:294
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
BackupDumper\$dumpUploadFileContents
$dumpUploadFileContents
Definition: backup.inc:55
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DumpOutput
Base class for output stream; prints to stdout or buffer or wherever.
Definition: Export.php:890
BackupDumper\processOption
processOption( $opt, $val, $param)
Definition: backup.inc:193
WikiExporter
Definition: Export.php:33
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
BackupDumper\$ID
$ID
Definition: backup.inc:59
LoadBalancer
Database load balancing object.
Definition: LoadBalancer.php:30
BackupDumper\initProgress
initProgress( $history=WikiExporter::FULL)
Initialise starting time and maximum revision count.
Definition: backup.inc:251
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3604
BackupDumper\report
report( $final=false)
Definition: backup.inc:320
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$matches
if(!defined( 'MEDIAWIKI')) if(!isset( $wgVersion)) $matches
Definition: NoLocalSettings.php:33
BackupDumper\processArgs
processArgs( $args)
Definition: backup.inc:127
BackupDumper\$outputTypes
$outputTypes
Definition: backup.inc:61
ExportProgressFilter\__construct
__construct(&$sink, &$progress)
Definition: backup.inc:371
BackupDumper\$reportingInterval
$reportingInterval
Definition: backup.inc:40
DatabaseBase
Database abstraction object.
Definition: Database.php:219
BackupDumper\$pages
$pages
Definition: backup.inc:45
DumpDBZip2Output\__construct
__construct( $file)
Definition: backup.inc:31
WikiExporter\STREAM
const STREAM
Definition: Export.php:47
WikiExporter\FULL
const FULL
Definition: Export.php:40
BackupDumper\$dumpUploads
$dumpUploads
Definition: backup.inc:54
BackupDumper\progress
progress( $string)
Definition: backup.inc:360
DumpFilter
Dump output filter class.
Definition: Export.php:1228
DumpFilter\$sink
DumpOutput $sink
FIXME will need to be made protected whenever legacy code is updated.
Definition: Export.php:1234
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1337
$args
if( $line===false) $args
Definition: cdb.php:62
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
BackupDumper
Definition: backup.inc:39
wfGetLBFactory
& wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:3669
WikiExporter\LOGS
const LOGS
Definition: Export.php:43
BackupDumper\$server
$server
Definition: backup.inc:44
BackupDumper\reportPage
reportPage()
Definition: backup.inc:311
DumpPipeOutput
Stream outputter to send data to a file via some filter program.
Definition: Export.php:1075
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
DumpDBZip2Output
Definition: backup.inc:30
BackupDumper\$lastTime
$lastTime
Definition: backup.inc:56
BackupDumper\dump
dump( $history, $text=WikiExporter::TEXT)
Definition: backup.inc:197
BackupDumper\$filterTypes
$filterTypes
Definition: backup.inc:61
ExportProgressFilter
Definition: backup.inc:370
server
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 and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break but it is *strongly *advised not to try any more intrusive changes to get MediaWiki to conform more closely to your filesystem hierarchy Any such attempt will almost certainly result in unnecessary bugs The standard recommended location to install relative to the web is it should be possible to enable the appropriate rewrite rules by if you can reconfigure the web server
Definition: distributors.txt:53
BackupDumper\__construct
__construct( $args)
Definition: backup.inc:75
skipHeader
if(isset( $options['quiet'])) if(isset( $options['pagelist'])) if(isset( $options['start'])) if(isset( $options['end'])) if(isset( $options['revstart'])) if(isset( $options['revend'])) $dumper skipHeader
Definition: dumpBackup.php:67
BackupDumper\$revStartId
$revStartId
Definition: backup.inc:50
BackupDumper\$pageCount
$pageCount
Definition: backup.inc:42
$type
$type
Definition: testCompression.php:46
BackupDumper\registerFilter
registerFilter( $name, $class)
Definition: backup.inc:104