Adding initial language file for Tajik (tg); currently includes only namespace transl...
[mediawiki.git] / maintenance / backup.inc
blob12dab66a4658f57e77fd559d7748a471c09f771e
1 <?php
2 /**
3  * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4  * http://www.mediawiki.org/
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  * http://www.gnu.org/copyleft/gpl.html
20  *
21  * @package MediaWiki
22  * @subpackage SpecialPage
23  */
25 class DumpDBZip2Output extends DumpPipeOutput {
26         function DumpDBZip2Output( $file ) {
27                 parent::DumpPipeOutput( "dbzip2", $file );
28         }
31 class BackupDumper {
32         var $reportingInterval = 100;
33         var $reporting = true;
34         var $pageCount = 0;
35         var $revCount  = 0;
36         var $server    = null; // use default
37         var $pages     = null; // all pages
38         var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
39         var $skipFooter = false; // don't output </mediawiki>
40         var $startId    = 0;
41         var $endId      = 0;
42         var $sink       = null; // Output filters
43         var $stubText   = false; // include rev_text_id instead of text; for 2-pass dump
45         function BackupDumper( $args ) {
46                 $this->stderr = fopen( "php://stderr", "wt" );
48                 // Built-in output and filter plugins
49                 $this->registerOutput( 'file', 'DumpFileOutput' );
50                 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
51                 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
52                 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
53                 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
55                 $this->registerFilter( 'latest', 'DumpLatestFilter' );
56                 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
57                 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
59                 $this->sink = $this->processArgs( $args );
60         }
62         /**
63          * @param string $name
64          * @param string $class name of output filter plugin class
65          */
66         function registerOutput( $name, $class ) {
67                 $this->outputTypes[$name] = $class;
68         }
70         /**
71          * @param string $name
72          * @param string $class name of filter plugin class
73          */
74         function registerFilter( $name, $class ) {
75                 $this->filterTypes[$name] = $class;
76         }
78         /**
79          * Load a plugin and register it
80          * @param string $class Name of plugin class; must have a static 'register'
81          *                      method that takes a BackupDumper as a parameter.
82          * @param string $file Full or relative path to the PHP file to load, or empty
83          */
84         function loadPlugin( $class, $file ) {
85                 if( $file != '' ) {
86                         require_once( $file );
87                 }
88                 $register = array( $class, 'register' );
89                 call_user_func_array( $register, array( &$this ) );
90         }
92         /**
93          * @param array $args
94          * @return array
95          * @static
96          */
97         function processArgs( $args ) {
98                 $sink = null;
99                 $sinks = array();
100                 foreach( $args as $arg ) {
101                         if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
102                                 @list( $full, $opt, $val, $param ) = $matches;
103                                 switch( $opt ) {
104                                 case "plugin":
105                                         $this->loadPlugin( $val, $param );
106                                         break;
107                                 case "output":
108                                         if( !is_null( $sink ) ) {
109                                                 $sinks[] = $sink;
110                                         }
111                                         if( !isset( $this->outputTypes[$val] ) ) {
112                                                 wfDie( "Unrecognized output sink type '$val'\n" );
113                                         }
114                                         $type = $this->outputTypes[$val];
115                                         $sink = new $type( $param );
116                                         break;
117                                 case "filter":
118                                         if( is_null( $sink ) ) {
119                                                 $this->progress( "Warning: assuming stdout for filter output\n" );
120                                                 $sink = new DumpOutput();
121                                         }
122                                         if( !isset( $this->filterTypes[$val] ) ) {
123                                                 wfDie( "Unrecognized filter type '$val'\n" );
124                                         }
125                                         $type = $this->filterTypes[$val];
126                                         $filter = new $type( $sink, $param );
128                                         // references are lame in php...
129                                         unset( $sink );
130                                         $sink = $filter;
132                                         break;
133                                 case "report":
134                                         $this->reportingInterval = intval( $val );
135                                         break;
136                                 case "server":
137                                         $this->server = $val;
138                                         break;
139                                 case "force-normal":
140                                         if( !function_exists( 'utf8_normalize' ) ) {
141                                                 dl( "php_utfnormal.so" );
142                                                 if( !function_exists( 'utf8_normalize' ) ) {
143                                                         wfDie( "Failed to load UTF-8 normalization extension. " .
144                                                                 "Install or remove --force-normal parameter to use slower code.\n" );
145                                                 }
146                                         }
147                                         break;
148                                 default:
149                                         $this->processOption( $opt, $val, $param );
150                                 }
151                         }
152                 }
154                 if( is_null( $sink ) ) {
155                         $sink = new DumpOutput();
156                 }
157                 $sinks[] = $sink;
159                 if( count( $sinks ) > 1 ) {
160                         return new DumpMultiWriter( $sinks );
161                 } else {
162                         return $sink;
163                 }
164         }
166         function processOption( $opt, $val, $param ) {
167                 // extension point for subclasses to add options
168         }
170         function dump( $history, $text = MW_EXPORT_TEXT ) {
171                 # This shouldn't happen if on console... ;)
172                 header( 'Content-type: text/html; charset=UTF-8' );
174                 # Notice messages will foul up your XML output even if they're
175                 # relatively harmless.
176                 ini_set( 'display_errors', false );
178                 $this->initProgress( $history );
180                 $db =& $this->backupDb();
181                 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM, $text );
183                 $wrapper = new ExportProgressFilter( $this->sink, $this );
184                 $exporter->setOutputSink( $wrapper );
186                 if( !$this->skipHeader )
187                         $exporter->openStream();
189                 if( is_null( $this->pages ) ) {
190                         if( $this->startId || $this->endId ) {
191                                 $exporter->pagesByRange( $this->startId, $this->endId );
192                         } else {
193                                 $exporter->allPages();
194                         }
195                 } else {
196                         $exporter->pagesByName( $this->pages );
197                 }
199                 if( !$this->skipFooter )
200                         $exporter->closeStream();
202                 $this->report( true );
203         }
204         
205         /**
206          * Initialise starting time and maximum revision count.
207          * We'll make ETA calculations based an progress, assuming relatively
208          * constant per-revision rate.
209          * @param int $history MW_EXPORT_CURRENT or MW_EXPORT_FULL
210          */
211         function initProgress( $history = MW_EXPORT_FULL ) {
212                 $table = ($history == MW_EXPORT_CURRENT) ? 'page' : 'revision';
213                 $field = ($history == MW_EXPORT_CURRENT) ? 'page_id' : 'rev_id';
214                 
215                 $dbr =& wfGetDB( DB_SLAVE );
216                 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', 'BackupDumper::dump' );
217                 $this->startTime = wfTime();
218         }
220         function &backupDb() {
221                 global $wgDBadminuser, $wgDBadminpassword;
222                 global $wgDBname, $wgDebugDumpSql;
223                 $flags = ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT; // god-damn hack
224                 $db = new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname, false, $flags );
225                 $timeout = 3600 * 24;
226                 $db->query( "SET net_read_timeout=$timeout" );
227                 $db->query( "SET net_write_timeout=$timeout" );
228                 return $db;
229         }
231         function backupServer() {
232                 global $wgDBserver;
233                 return $this->server
234                         ? $this->server
235                         : $wgDBserver;
236         }
238         function reportPage() {
239                 $this->pageCount++;
240         }
242         function revCount() {
243                 $this->revCount++;
244                 $this->report();
245         }
247         function report( $final = false ) {
248                 if( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
249                         $this->showReport();
250                 }
251         }
253         function showReport() {
254                 if( $this->reporting ) {
255                         $delta = wfTime() - $this->startTime;
256                         $now = wfTimestamp( TS_DB );
257                         if( $delta ) {
258                                 $rate = $this->pageCount / $delta;
259                                 $revrate = $this->revCount / $delta;
260                                 $portion = $this->revCount / $this->maxCount;
261                                 $eta = $this->startTime + $delta / $portion;
262                                 $etats = wfTimestamp( TS_DB, intval( $eta ) );
263                         } else {
264                                 $rate = '-';
265                                 $revrate = '-';
266                                 $etats = '-';
267                         }
268                         global $wgDBname;
269                         $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
270                                 $now, $wgDBname, $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
271                 }
272         }
274         function progress( $string ) {
275                 fwrite( $this->stderr, $string . "\n" );
276         }
279 class ExportProgressFilter extends DumpFilter {
280         function ExportProgressFilter( &$sink, &$progress ) {
281                 parent::DumpFilter( $sink );
282                 $this->progress = $progress;
283         }
285         function writeClosePage( $string ) {
286                 parent::writeClosePage( $string );
287                 $this->progress->reportPage();
288         }
290         function writeRevision( $rev, $string ) {
291                 parent::writeRevision( $rev, $string );
292                 $this->progress->revCount();
293         }