wiki.techinc.nl/maintenance/exportSites.php

61 lines
1.3 KiB
PHP
Raw Normal View History

<?php
// @codeCoverageIgnoreStart
Safer autoloading with respect to file-scope code Many files were in the autoloader despite having potentially harmful file-scope code. * Exclude all CommandLineInc maintenance scripts from the autoloader. * Introduce "NO_AUTOLOAD" tag which excludes the file containing it from the autoloader. Use it on CommandLineInc.php and a few suspicious-looking files without classes in case they are refactored to add classes in the future. * Add a test which parses all non-PSR4 class files and confirms that they do not contain dangerous file-scope code. It's slow (15s) but its results were enlightening. * Several maintenance scripts define constants in the file scope, intending to modify the behaviour of MediaWiki. Either move the define() to a later setup function, or protect with NO_AUTOLOAD. * Use require_once consistently with Maintenance.php and doMaintenance.php, per the original convention which is supposed to allow one maintenance script to use the class of another maintenance script. Using require breaks autoloading of these maintenance class files. * When Maintenance.php is included, check if MediaWiki has already started, and if so, return early. Revert the fix for T250003 which is incompatible with this safety measure. Hopefully it was superseded by splitting out the class file. * In runScript.php add a redundant PHP_SAPI check since it does some things in file-scope code before any other check will be run. * Change the if(false) class_alias(...) to something more hackish and more compatible with the new test. * Some site-related scripts found Maintenance.php in a non-standard way. Use the standard way. * fileOpPerfTest.php called error_reporting(). Probably debugging code left in; removed. * Moved mediawiki.compress.7z registration from the class file to the caller. Change-Id: I1b1be90343a5ab678df6f1b1bdd03319dcf6537f
2021-01-08 02:16:02 +00:00
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd
use MediaWiki\Site\SiteExporter;
/**
* Maintenance script for exporting site definitions from XML into the sites table.
*
* @since 1.25
*
* @license GPL-2.0-or-later
* @author Daniel Kinzler
*/
class ExportSites extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Exports site definitions the sites table to XML file' );
$this->addArg( 'file', 'A file to write the XML to (see docs/sitelist.md). ' .
'Use "php://stdout" to write to stdout.', true
);
}
/**
* Do the actual work. All child classes will need to implement this
*/
public function execute() {
$file = $this->getArg( 0 );
if ( $file === 'php://output' || $file === 'php://stdout' ) {
$this->mQuiet = true;
}
$handle = fopen( $file, 'w' );
if ( !$handle ) {
$this->fatalError( "Failed to open $file for writing.\n" );
}
$exporter = new SiteExporter( $handle );
$siteLookup = $this->getServiceContainer()->getSiteLookup();
$exporter->exportSites( $siteLookup->getSites() );
fclose( $handle );
$this->output( "Exported sites to " . realpath( $file ) . ".\n" );
}
}
// @codeCoverageIgnoreStart
$maintClass = ExportSites::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd