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