wiki.techinc.nl/maintenance/addSite.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
3.2 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\MediaWikiSite;
/**
* Maintenance script for adding a site definition into the sites table.
*
* The sites table is cached in the local-server cache,
* so you should reload your webserver and other long-running MediaWiki
* PHP processes after running this script.
*
* @since 1.29
*
* @license GPL-2.0-or-later
* @author Florian Schmidt
*/
class AddSite extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Add a site definition into the sites table.' );
$this->addArg( 'globalid', 'The global id of the site to add, e.g. "wikipedia".', true );
$this->addArg( 'group', 'In which group this site should be sorted in.', true );
$this->addOption( 'language', 'The language code of the site, e.g. "de".', false, true );
$this->addOption( 'interwiki-id', 'The interwiki ID of the site.', false, true );
$this->addOption( 'navigation-id', 'The navigation ID of the site.', false, true );
$this->addOption( 'pagepath', 'The URL to pages of this site, e.g.' .
' https://example.com/wiki/\$1.', false, true );
$this->addOption( 'filepath', 'The URL to files of this site, e.g. https://example' .
'.com/w/\$1.', false, true );
}
/**
* Imports the site described by the parameters (see self::__construct()) passed to this
* maintenance sccript into the sites table of MediaWiki.
* @return bool
*/
public function execute() {
$siteStore = $this->getServiceContainer()->getSiteStore();
if ( method_exists( $siteStore, 'reset' ) ) {
// @phan-suppress-next-line PhanUndeclaredMethod
$siteStore->reset();
}
$globalId = $this->getArg( 0 );
$group = $this->getArg( 1 );
$language = $this->getOption( 'language' );
$interwikiId = $this->getOption( 'interwiki-id' );
$navigationId = $this->getOption( 'navigation-id' );
$pagepath = $this->getOption( 'pagepath' );
$filepath = $this->getOption( 'filepath' );
if ( !is_string( $globalId ) || !is_string( $group ) ) {
$this->error( 'Arguments globalid and group need to be strings.' );
return false;
}
if ( $siteStore->getSite( $globalId ) !== null ) {
$this->error( "Site with global id $globalId already exists." );
return false;
}
$site = new MediaWikiSite();
$site->setGlobalId( $globalId );
$site->setGroup( $group );
if ( $language !== null ) {
$site->setLanguageCode( $language );
}
if ( $interwikiId !== null ) {
$site->addInterwikiId( $interwikiId );
}
if ( $navigationId !== null ) {
$site->addNavigationId( $navigationId );
}
if ( $pagepath !== null ) {
$site->setPagePath( $pagepath );
}
if ( $filepath !== null ) {
$site->setFilePath( $filepath );
}
$siteStore->saveSites( [ $site ] );
if ( method_exists( $siteStore, 'reset' ) ) {
// @phan-suppress-next-line PhanUndeclaredMethod
$siteStore->reset();
}
$this->output(
'Done. Reload the web server and other long-running PHP processes '
. "to refresh the local-server cache of the sites table.\n"
);
}
}
// @codeCoverageIgnoreStart
$maintClass = AddSite::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd