MediaWiki  master
addSite.php
Go to the documentation of this file.
1 <?php
2 
4 
5 require_once __DIR__ . '/Maintenance.php';
6 
19 class AddSite extends Maintenance {
20 
21  public function __construct() {
22  parent::__construct();
23 
24  $this->addDescription( 'Add a site definition into the sites table.' );
25 
26  $this->addArg( 'globalid', 'The global id of the site to add, e.g. "wikipedia".', true );
27  $this->addArg( 'group', 'In which group this site should be sorted in.', true );
28  $this->addOption( 'language', 'The language code of the site, e.g. "de".', false, true );
29  $this->addOption( 'interwiki-id', 'The interwiki ID of the site.', false, true );
30  $this->addOption( 'navigation-id', 'The navigation ID of the site.', false, true );
31  $this->addOption( 'pagepath', 'The URL to pages of this site, e.g.' .
32  ' https://example.com/wiki/\$1.', false, true );
33  $this->addOption( 'filepath', 'The URL to files of this site, e.g. https://example' .
34  '.com/w/\$1.', false, true );
35  }
36 
42  public function execute() {
43  $siteStore = MediaWikiServices::getInstance()->getSiteStore();
44  if ( method_exists( $siteStore, 'reset' ) ) {
45  // @phan-suppress-next-line PhanUndeclaredMethod
46  $siteStore->reset();
47  }
48 
49  $globalId = $this->getArg( 0 );
50  $group = $this->getArg( 1 );
51  $language = $this->getOption( 'language' );
52  $interwikiId = $this->getOption( 'interwiki-id' );
53  $navigationId = $this->getOption( 'navigation-id' );
54  $pagepath = $this->getOption( 'pagepath' );
55  $filepath = $this->getOption( 'filepath' );
56 
57  if ( !is_string( $globalId ) || !is_string( $group ) ) {
58  $this->error( 'Arguments globalid and group need to be strings.' );
59  return false;
60  }
61 
62  if ( $siteStore->getSite( $globalId ) !== null ) {
63  $this->error( "Site with global id $globalId already exists." );
64  return false;
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 $maintClass = AddSite::class;
101 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
Definition: addSite.php:100
Maintenance script for adding a site definition into the sites table.
Definition: addSite.php:19
__construct()
Default constructor.
Definition: addSite.php:21
execute()
Imports the site described by the parameters (see self::__construct()) passed to this maintenance scc...
Definition: addSite.php:42
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
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.
Service locator for MediaWiki core services.