MediaWiki REL1_41
addSite.php
Go to the documentation of this file.
1<?php
2
3require_once __DIR__ . '/Maintenance.php';
4
17class AddSite extends Maintenance {
18
19 public function __construct() {
20 parent::__construct();
21
22 $this->addDescription( 'Add a site definition into the sites table.' );
23
24 $this->addArg( 'globalid', 'The global id of the site to add, e.g. "wikipedia".', true );
25 $this->addArg( 'group', 'In which group this site should be sorted in.', true );
26 $this->addOption( 'language', 'The language code of the site, e.g. "de".', false, true );
27 $this->addOption( 'interwiki-id', 'The interwiki ID of the site.', false, true );
28 $this->addOption( 'navigation-id', 'The navigation ID of the site.', false, true );
29 $this->addOption( 'pagepath', 'The URL to pages of this site, e.g.' .
30 ' https://example.com/wiki/\$1.', false, true );
31 $this->addOption( 'filepath', 'The URL to files of this site, e.g. https://example' .
32 '.com/w/\$1.', false, true );
33 }
34
40 public function execute() {
41 $siteStore = $this->getServiceContainer()->getSiteStore();
42 if ( method_exists( $siteStore, 'reset' ) ) {
43 // @phan-suppress-next-line PhanUndeclaredMethod
44 $siteStore->reset();
45 }
46
47 $globalId = $this->getArg( 0 );
48 $group = $this->getArg( 1 );
49 $language = $this->getOption( 'language' );
50 $interwikiId = $this->getOption( 'interwiki-id' );
51 $navigationId = $this->getOption( 'navigation-id' );
52 $pagepath = $this->getOption( 'pagepath' );
53 $filepath = $this->getOption( 'filepath' );
54
55 if ( !is_string( $globalId ) || !is_string( $group ) ) {
56 $this->error( 'Arguments globalid and group need to be strings.' );
57 return false;
58 }
59
60 if ( $siteStore->getSite( $globalId ) !== null ) {
61 $this->error( "Site with global id $globalId already exists." );
62 return false;
63 }
64
65 $site = new MediaWikiSite();
66 $site->setGlobalId( $globalId );
67 $site->setGroup( $group );
68 if ( $language !== null ) {
69 $site->setLanguageCode( $language );
70 }
71 if ( $interwikiId !== null ) {
72 $site->addInterwikiId( $interwikiId );
73 }
74 if ( $navigationId !== null ) {
75 $site->addNavigationId( $navigationId );
76 }
77 if ( $pagepath !== null ) {
78 $site->setPagePath( $pagepath );
79 }
80 if ( $filepath !== null ) {
81 $site->setFilePath( $filepath );
82 }
83
84 $siteStore->saveSites( [ $site ] );
85
86 if ( method_exists( $siteStore, 'reset' ) ) {
87 // @phan-suppress-next-line PhanUndeclaredMethod
88 $siteStore->reset();
89 }
90
91 $this->output(
92 'Done. Reload the web server and other long-running PHP processes '
93 . "to refresh the local-server cache of the sites table.\n"
94 );
95 }
96}
97
98$maintClass = AddSite::class;
99require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
Definition addSite.php:98
Maintenance script for adding a site definition into the sites table.
Definition addSite.php:17
__construct()
Default constructor.
Definition addSite.php:19
execute()
Imports the site described by the parameters (see self::__construct()) passed to this maintenance scc...
Definition addSite.php:40
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.