MediaWiki master
dumpBackup.php
Go to the documentation of this file.
1<?php
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/includes/BackupDumper.php';
19// @codeCoverageIgnoreEnd
20
21class DumpBackup extends BackupDumper {
22 public function __construct( ?array $args = null ) {
23 parent::__construct();
24
25 $this->addDescription( <<<TEXT
26This script dumps the wiki page or logging database into an
27XML interchange wrapper format for export or backup.
28
29XML output is sent to stdout; progress reports are sent to stderr.
30
31WARNING: this is not a full database dump! It is merely for public export
32 of your wiki. For full backup, see our online help at:
33 https://www.mediawiki.org/wiki/Backup
34TEXT
35 );
36
37 // Actions
38 $this->addOption( 'full', 'Dump all revisions of every page' );
39 $this->addOption( 'current', 'Dump only the latest revision of every page.' );
40 $this->addOption( 'logs', 'Dump all log events' );
41 $this->addOption( 'stable', 'Dump stable versions of pages' );
42 $this->addOption( 'revrange', 'Dump range of revisions specified by revstart and ' .
43 'revend parameters' );
44 $this->addOption( 'orderrevs', 'Dump revisions in ascending revision order ' .
45 '(implies dump of a range of pages)' );
46 $this->addOption( 'pagelist',
47 'Dump only pages included in the file', false, true );
48 // Options
49 $this->addOption( 'start', 'Start from page_id or log_id', false, true );
50 $this->addOption( 'end', 'Stop before page_id or log_id n (exclusive)', false, true );
51 $this->addOption( 'revstart', 'Start from rev_id', false, true );
52 $this->addOption( 'revend', 'Stop before rev_id n (exclusive)', false, true );
53 $this->addOption( 'skip-header', 'Don\'t output the <mediawiki> header' );
54 $this->addOption( 'skip-footer', 'Don\'t output the </mediawiki> footer' );
55 $this->addOption( 'stub', 'Don\'t perform old_text lookups; for 2-pass dump' );
56 $this->addOption( 'uploads', 'Include upload records without files' );
57 $this->addOption( 'include-files', 'Include files within the XML stream' );
58 $this->addOption( 'namespaces', 'Limit to this comma-separated list of namespace numbers' );
59
60 if ( $args ) {
61 $this->loadWithArgv( $args );
62 $this->processOptions();
63 }
64 }
65
66 public function execute() {
67 $this->processOptions();
68
69 $textMode = $this->hasOption( 'stub' ) ? WikiExporter::STUB : WikiExporter::TEXT;
70
71 if ( $this->hasOption( 'full' ) ) {
72 $this->dump( WikiExporter::FULL, $textMode );
73 } elseif ( $this->hasOption( 'current' ) ) {
74 $this->dump( WikiExporter::CURRENT, $textMode );
75 } elseif ( $this->hasOption( 'stable' ) ) {
76 $this->dump( WikiExporter::STABLE, $textMode );
77 } elseif ( $this->hasOption( 'logs' ) ) {
78 $this->dump( WikiExporter::LOGS );
79 } elseif ( $this->hasOption( 'revrange' ) ) {
80 $this->dump( WikiExporter::RANGE, $textMode );
81 } else {
82 $this->fatalError( 'No valid action specified.' );
83 }
84 }
85
86 protected function processOptions() {
87 parent::processOptions();
88
89 // Evaluate options specific to this class
90 $this->reporting = !$this->hasOption( 'quiet' );
91
92 if ( $this->hasOption( 'pagelist' ) ) {
93 $filename = $this->getOption( 'pagelist' );
94 $pages = file( $filename );
95 if ( $pages === false ) {
96 $this->fatalError( "Unable to open file {$filename}\n" );
97 }
98 $pages = array_map( 'trim', $pages );
99 $this->pages = array_filter( $pages, static function ( $x ) {
100 return $x !== '';
101 } );
102 }
103
104 if ( $this->hasOption( 'start' ) ) {
105 $this->startId = intval( $this->getOption( 'start' ) );
106 }
107
108 if ( $this->hasOption( 'end' ) ) {
109 $this->endId = intval( $this->getOption( 'end' ) );
110 }
111
112 if ( $this->hasOption( 'revstart' ) ) {
113 $this->revStartId = intval( $this->getOption( 'revstart' ) );
114 }
115
116 if ( $this->hasOption( 'revend' ) ) {
117 $this->revEndId = intval( $this->getOption( 'revend' ) );
118 }
119
120 $this->skipHeader = $this->hasOption( 'skip-header' );
121 $this->skipFooter = $this->hasOption( 'skip-footer' );
122 $this->dumpUploads = $this->hasOption( 'uploads' );
123 $this->dumpUploadFileContents = $this->hasOption( 'include-files' );
124 $this->orderRevs = $this->hasOption( 'orderrevs' );
125 if ( $this->hasOption( 'namespaces' ) ) {
126 $this->limitNamespaces = explode( ',', $this->getOption( 'namespaces' ) );
127 } else {
128 $this->limitNamespaces = null;
129 }
130 }
131}
132
133// @codeCoverageIgnoreStart
134$maintClass = DumpBackup::class;
135require_once RUN_MAINTENANCE_IF_MAIN;
136// @codeCoverageIgnoreEnd
execute()
Do the actual work.
processOptions()
Processes arguments and sets $this->$sink accordingly.
__construct(?array $args=null)
dump( $history, $text=WikiExporter::TEXT)
string[] null $pages
null means all pages
output( $out, $channel=null)
Throw some output to the user.
loadWithArgv( $argv)
Load params and arguments from a given array of command-line arguments.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
$maintClass