This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | /** |
||
3 | * Import XML dump files into the current wiki. |
||
4 | * |
||
5 | * Copyright © 2005 Brion Vibber <[email protected]> |
||
6 | * https://www.mediawiki.org/ |
||
7 | * |
||
8 | * This program is free software; you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation; either version 2 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License along |
||
19 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
21 | * http://www.gnu.org/copyleft/gpl.html |
||
22 | * |
||
23 | * @file |
||
24 | * @ingroup Maintenance |
||
25 | */ |
||
26 | |||
27 | require_once __DIR__ . '/Maintenance.php'; |
||
28 | |||
29 | /** |
||
30 | * Maintenance script that imports XML dump files into the current wiki. |
||
31 | * |
||
32 | * @ingroup Maintenance |
||
33 | */ |
||
34 | class BackupReader extends Maintenance { |
||
35 | public $reportingInterval = 100; |
||
36 | public $pageCount = 0; |
||
37 | public $revCount = 0; |
||
38 | public $dryRun = false; |
||
39 | public $uploads = false; |
||
40 | public $imageBasePath = false; |
||
41 | public $nsFilter = false; |
||
42 | |||
43 | function __construct() { |
||
44 | parent::__construct(); |
||
45 | $gz = in_array( 'compress.zlib', stream_get_wrappers() ) |
||
46 | ? 'ok' |
||
47 | : '(disabled; requires PHP zlib module)'; |
||
48 | $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() ) |
||
49 | ? 'ok' |
||
50 | : '(disabled; requires PHP bzip2 module)'; |
||
51 | |||
52 | $this->addDescription( |
||
53 | <<<TEXT |
||
54 | This script reads pages from an XML file as produced from Special:Export or |
||
55 | dumpBackup.php, and saves them into the current wiki. |
||
56 | |||
57 | Compressed XML files may be read directly: |
||
58 | .gz $gz |
||
59 | .bz2 $bz2 |
||
60 | .7z (if 7za executable is in PATH) |
||
61 | |||
62 | Note that for very large data sets, importDump.php may be slow; there are |
||
63 | alternate methods which can be much faster for full site restoration: |
||
64 | <https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps> |
||
65 | TEXT |
||
66 | ); |
||
67 | $this->stderr = fopen( "php://stderr", "wt" ); |
||
0 ignored issues
–
show
The property
stderr does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
68 | $this->addOption( 'report', |
||
69 | 'Report position and speed after every n pages processed', false, true ); |
||
70 | $this->addOption( 'namespaces', |
||
71 | 'Import only the pages from namespaces belonging to the list of ' . |
||
72 | 'pipe-separated namespace names or namespace indexes', false, true ); |
||
73 | $this->addOption( 'rootpage', 'Pages will be imported as subpages of the specified page', |
||
74 | false, true ); |
||
75 | $this->addOption( 'dry-run', 'Parse dump without actually importing pages' ); |
||
76 | $this->addOption( 'debug', 'Output extra verbose debug information' ); |
||
77 | $this->addOption( 'uploads', 'Process file upload data if included (experimental)' ); |
||
78 | $this->addOption( |
||
79 | 'no-updates', |
||
80 | 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state' |
||
81 | ); |
||
82 | $this->addOption( 'image-base-path', 'Import files from a specified path', false, true ); |
||
83 | $this->addArg( 'file', 'Dump file to import [else use stdin]', false ); |
||
84 | } |
||
85 | |||
86 | public function execute() { |
||
87 | if ( wfReadOnly() ) { |
||
88 | $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true ); |
||
89 | } |
||
90 | |||
91 | $this->reportingInterval = intval( $this->getOption( 'report', 100 ) ); |
||
92 | if ( !$this->reportingInterval ) { |
||
93 | $this->reportingInterval = 100; // avoid division by zero |
||
94 | } |
||
95 | |||
96 | $this->dryRun = $this->hasOption( 'dry-run' ); |
||
97 | $this->uploads = $this->hasOption( 'uploads' ); // experimental! |
||
98 | if ( $this->hasOption( 'image-base-path' ) ) { |
||
99 | $this->imageBasePath = $this->getOption( 'image-base-path' ); |
||
100 | } |
||
101 | if ( $this->hasOption( 'namespaces' ) ) { |
||
102 | $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) ); |
||
103 | } |
||
104 | |||
105 | if ( $this->hasArg() ) { |
||
106 | $this->importFromFile( $this->getArg() ); |
||
107 | } else { |
||
108 | $this->importFromStdin(); |
||
109 | } |
||
110 | |||
111 | $this->output( "Done!\n" ); |
||
112 | $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" ); |
||
113 | } |
||
114 | |||
115 | function setNsfilter( array $namespaces ) { |
||
116 | if ( count( $namespaces ) == 0 ) { |
||
117 | $this->nsFilter = false; |
||
118 | |||
119 | return; |
||
120 | } |
||
121 | $this->nsFilter = array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) ); |
||
0 ignored issues
–
show
It seems like
array_unique(array_map(a...sIndex'), $namespaces)) of type array is incompatible with the declared type boolean of property $nsFilter .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||
122 | } |
||
123 | |||
124 | private function getNsIndex( $namespace ) { |
||
125 | global $wgContLang; |
||
126 | $result = $wgContLang->getNsIndex( $namespace ); |
||
127 | if ( $result !== false ) { |
||
128 | return $result; |
||
129 | } |
||
130 | $ns = intval( $namespace ); |
||
131 | if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) { |
||
132 | return $ns; |
||
133 | } |
||
134 | $this->error( "Unknown namespace text / index specified: $namespace", true ); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param Title|Revision $obj |
||
139 | * @return bool |
||
140 | */ |
||
141 | private function skippedNamespace( $obj ) { |
||
142 | $title = null; |
||
0 ignored issues
–
show
$title is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
143 | if ( $obj instanceof Title ) { |
||
144 | $title = $obj; |
||
145 | } elseif ( $obj instanceof Revision ) { |
||
146 | $title = $obj->getTitle(); |
||
147 | } elseif ( $obj instanceof WikiRevision ) { |
||
148 | $title = $obj->title; |
||
149 | } else { |
||
150 | throw new MWException( "Cannot get namespace of object in " . __METHOD__ ); |
||
151 | } |
||
152 | |||
153 | if ( is_null( $title ) ) { |
||
154 | // Probably a log entry |
||
155 | return false; |
||
156 | } |
||
157 | |||
158 | $ns = $title->getNamespace(); |
||
159 | |||
160 | return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter ); |
||
161 | } |
||
162 | |||
163 | function reportPage( $page ) { |
||
164 | $this->pageCount++; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param Revision $rev |
||
169 | */ |
||
170 | function handleRevision( $rev ) { |
||
171 | $title = $rev->getTitle(); |
||
172 | if ( !$title ) { |
||
173 | $this->progress( "Got bogus revision with null title!" ); |
||
174 | |||
175 | return; |
||
176 | } |
||
177 | |||
178 | if ( $this->skippedNamespace( $title ) ) { |
||
179 | return; |
||
180 | } |
||
181 | |||
182 | $this->revCount++; |
||
183 | $this->report(); |
||
184 | |||
185 | if ( !$this->dryRun ) { |
||
186 | call_user_func( $this->importCallback, $rev ); |
||
0 ignored issues
–
show
The property
importCallback does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
187 | } |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param Revision $revision |
||
192 | * @return bool |
||
193 | */ |
||
194 | function handleUpload( $revision ) { |
||
195 | if ( $this->uploads ) { |
||
196 | if ( $this->skippedNamespace( $revision ) ) { |
||
197 | return false; |
||
198 | } |
||
199 | $this->uploadCount++; |
||
0 ignored issues
–
show
The property
uploadCount does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
200 | // $this->report(); |
||
201 | $this->progress( "upload: " . $revision->getFilename() ); |
||
0 ignored issues
–
show
The method
getFilename() does not seem to exist on object<Revision> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||
202 | |||
203 | if ( !$this->dryRun ) { |
||
204 | // bluuuh hack |
||
205 | // call_user_func( $this->uploadCallback, $revision ); |
||
206 | $dbw = $this->getDB( DB_MASTER ); |
||
207 | |||
208 | return $dbw->deadlockLoop( [ $revision, 'importUpload' ] ); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return false; |
||
213 | } |
||
214 | |||
215 | function handleLogItem( $rev ) { |
||
216 | if ( $this->skippedNamespace( $rev ) ) { |
||
217 | return; |
||
218 | } |
||
219 | $this->revCount++; |
||
220 | $this->report(); |
||
221 | |||
222 | if ( !$this->dryRun ) { |
||
223 | call_user_func( $this->logItemCallback, $rev ); |
||
0 ignored issues
–
show
The property
logItemCallback does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
224 | } |
||
225 | } |
||
226 | |||
227 | function report( $final = false ) { |
||
228 | if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) { |
||
229 | $this->showReport(); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | function showReport() { |
||
234 | if ( !$this->mQuiet ) { |
||
235 | $delta = microtime( true ) - $this->startTime; |
||
0 ignored issues
–
show
The property
startTime does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
236 | if ( $delta ) { |
||
237 | $rate = sprintf( "%.2f", $this->pageCount / $delta ); |
||
238 | $revrate = sprintf( "%.2f", $this->revCount / $delta ); |
||
239 | } else { |
||
240 | $rate = '-'; |
||
241 | $revrate = '-'; |
||
242 | } |
||
243 | # Logs dumps don't have page tallies |
||
244 | if ( $this->pageCount ) { |
||
245 | $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" ); |
||
246 | } else { |
||
247 | $this->progress( "$this->revCount ($revrate revs/sec)" ); |
||
248 | } |
||
249 | } |
||
250 | wfWaitForSlaves(); |
||
0 ignored issues
–
show
The function
wfWaitForSlaves() has been deprecated with message: since 1.27 Use LBFactory::waitForReplication
This function has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead. ![]() |
|||
251 | } |
||
252 | |||
253 | function progress( $string ) { |
||
254 | fwrite( $this->stderr, $string . "\n" ); |
||
255 | } |
||
256 | |||
257 | function importFromFile( $filename ) { |
||
258 | if ( preg_match( '/\.gz$/', $filename ) ) { |
||
259 | $filename = 'compress.zlib://' . $filename; |
||
260 | } elseif ( preg_match( '/\.bz2$/', $filename ) ) { |
||
261 | $filename = 'compress.bzip2://' . $filename; |
||
262 | } elseif ( preg_match( '/\.7z$/', $filename ) ) { |
||
263 | $filename = 'mediawiki.compress.7z://' . $filename; |
||
264 | } |
||
265 | |||
266 | $file = fopen( $filename, 'rt' ); |
||
267 | |||
268 | return $this->importFromHandle( $file ); |
||
269 | } |
||
270 | |||
271 | function importFromStdin() { |
||
272 | $file = fopen( 'php://stdin', 'rt' ); |
||
273 | if ( self::posix_isatty( $file ) ) { |
||
274 | $this->maybeHelp( true ); |
||
275 | } |
||
276 | |||
277 | return $this->importFromHandle( $file ); |
||
278 | } |
||
279 | |||
280 | function importFromHandle( $handle ) { |
||
281 | $this->startTime = microtime( true ); |
||
282 | |||
283 | $source = new ImportStreamSource( $handle ); |
||
284 | $importer = new WikiImporter( $source, $this->getConfig() ); |
||
285 | |||
286 | if ( $this->hasOption( 'debug' ) ) { |
||
287 | $importer->setDebug( true ); |
||
288 | } |
||
289 | if ( $this->hasOption( 'no-updates' ) ) { |
||
290 | $importer->setNoUpdates( true ); |
||
291 | } |
||
292 | if ( $this->hasOption( 'rootpage' ) ) { |
||
293 | $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) ); |
||
294 | if ( !$statusRootPage->isGood() ) { |
||
295 | // Die here so that it doesn't print "Done!" |
||
296 | $this->error( $statusRootPage->getMessage()->text(), 1 ); |
||
297 | return false; |
||
298 | } |
||
299 | } |
||
300 | $importer->setPageCallback( [ $this, 'reportPage' ] ); |
||
301 | $this->importCallback = $importer->setRevisionCallback( |
||
302 | [ $this, 'handleRevision' ] ); |
||
303 | $this->uploadCallback = $importer->setUploadCallback( |
||
0 ignored issues
–
show
The property
uploadCallback does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
304 | [ $this, 'handleUpload' ] ); |
||
305 | $this->logItemCallback = $importer->setLogItemCallback( |
||
306 | [ $this, 'handleLogItem' ] ); |
||
307 | if ( $this->uploads ) { |
||
308 | $importer->setImportUploads( true ); |
||
309 | } |
||
310 | if ( $this->imageBasePath ) { |
||
311 | $importer->setImageBasePath( $this->imageBasePath ); |
||
312 | } |
||
313 | |||
314 | if ( $this->dryRun ) { |
||
315 | $importer->setPageOutCallback( null ); |
||
316 | } |
||
317 | |||
318 | return $importer->doImport(); |
||
319 | } |
||
320 | } |
||
321 | |||
322 | $maintClass = 'BackupReader'; |
||
323 | require_once RUN_MAINTENANCE_IF_MAIN; |
||
324 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.