MediaWiki master
addSite.php
Go to the documentation of this file.
1<?php
2
3// @codeCoverageIgnoreStart
4require_once __DIR__ . '/Maintenance.php';
5// @codeCoverageIgnoreEnd
6
9
22class AddSite extends Maintenance {
23
24 public function __construct() {
25 parent::__construct();
26
27 $this->addDescription( 'Add a site definition into the sites table.' );
28
29 $this->addArg( 'globalid', 'The global id of the site to add, e.g. "wikipedia".', true );
30 $this->addArg( 'group', 'In which group this site should be sorted in.', true );
31 $this->addOption( 'language', 'The language code of the site, e.g. "de".', false, true );
32 $this->addOption( 'interwiki-id', 'The interwiki ID of the site.', false, true );
33 $this->addOption( 'navigation-id', 'The navigation ID of the site.', false, true );
34 $this->addOption( 'pagepath', 'The URL to pages of this site, e.g.' .
35 ' https://example.com/wiki/\$1.', false, true );
36 $this->addOption( 'filepath', 'The URL to files of this site, e.g. https://example' .
37 '.com/w/\$1.', false, true );
38 }
39
44 public function execute() {
45 $siteStore = $this->getServiceContainer()->getSiteStore();
46 if ( method_exists( $siteStore, 'reset' ) ) {
47 // @phan-suppress-next-line PhanUndeclaredMethod
48 $siteStore->reset();
49 }
50
51 $globalId = $this->getArg( 0 );
52 $group = $this->getArg( 1 );
53 $language = $this->getOption( 'language' );
54 $interwikiId = $this->getOption( 'interwiki-id' );
55 $navigationId = $this->getOption( 'navigation-id' );
56 $pagepath = $this->getOption( 'pagepath' );
57 $filepath = $this->getOption( 'filepath' );
58
59 if ( !is_string( $globalId ) || !is_string( $group ) ) {
60 $this->fatalError( 'Arguments globalid and group need to be strings.' );
61 }
62
63 if ( $siteStore->getSite( $globalId ) !== null ) {
64 $this->fatalError( "Site with global id $globalId already exists." );
65 }
66
67 $site = new MediaWikiSite();
68 $site->setGlobalId( $globalId );
69 $site->setGroup( $group );
70 if ( $language !== null ) {
71 $site->setLanguageCode( $language );
72 }
73 if ( $interwikiId !== null ) {
74 $site->addInterwikiId( $interwikiId );
75 }
76 if ( $navigationId !== null ) {
77 $site->addNavigationId( $navigationId );
78 }
79 if ( $pagepath !== null ) {
80 $site->setPagePath( $pagepath );
81 }
82 if ( $filepath !== null ) {
83 $site->setFilePath( $filepath );
84 }
85
86 $siteStore->saveSites( [ $site ] );
87
88 if ( method_exists( $siteStore, 'reset' ) ) {
89 // @phan-suppress-next-line PhanUndeclaredMethod
90 $siteStore->reset();
91 }
92
93 $this->output(
94 'Done. Reload the web server and other long-running PHP processes '
95 . "to refresh the local-server cache of the sites table.\n"
96 );
97 }
98}
99
100// @codeCoverageIgnoreStart
101$maintClass = AddSite::class;
102require_once RUN_MAINTENANCE_IF_MAIN;
103// @codeCoverageIgnoreEnd
$maintClass
Definition addSite.php:101
Maintenance script for adding a site definition into the sites table.
Definition addSite.php:22
__construct()
Default constructor.
Definition addSite.php:24
execute()
Imports the site described by the parameters (see self::__construct()) passed to this maintenance scc...
Definition addSite.php:44
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
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.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Class representing a MediaWiki site.