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