MediaWiki  master
addSite.php
Go to the documentation of this file.
1 <?php
2 
4 
5 $basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/..';
6 
7 require_once $basePath . '/maintenance/Maintenance.php';
8 
17 class AddSite extends Maintenance {
18 
19  public function __construct() {
20  $this->addDescription( 'Add a site definition into the sites table.' );
21 
22  $this->addArg( 'globalid', 'The global id of the site to add, e.g. "wikipedia".', true );
23  $this->addArg( 'group', 'In which group this site should be sorted in.', true );
24  $this->addOption( 'language', 'The language code of the site, e.g. "de".' );
25  $this->addOption( 'interwiki-id', 'The interwiki ID of the site.' );
26  $this->addOption( 'navigation-id', 'The navigation ID of the site.' );
27  $this->addOption( 'pagepath', 'The URL to pages of this site, e.g.' .
28  ' https://example.com/wiki/\$1.' );
29  $this->addOption( 'filepath', 'The URL to files of this site, e.g. https://example
30  .com/w/\$1.' );
31 
32  parent::__construct();
33  }
34 
40  public function execute() {
41  $siteStore = MediaWikiServices::getInstance()->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  echo "Arguments globalid and group need to be strings.\n";
57  return false;
58  }
59 
60  if ( $siteStore->getSite( $globalId ) !== null ) {
61  echo "Site with global id $globalId already exists.\n";
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  echo "Done.\n";
92  }
93 }
94 
95 $maintClass = AddSite::class;
96 require_once RUN_MAINTENANCE_IF_MAIN;
getArg( $argId=0, $default=null)
Get an argument.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
getOption( $name, $default=null)
Get an option, or return the default.
$basePath
Definition: addSite.php:5
Maintenance script for adding a site definition into the sites table.
Definition: addSite.php:17
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
execute()
Imports the site described by the parameters (see self::__construct()) passed to this maintenance scc...
Definition: addSite.php:40
addDescription( $text)
Set the description text.
addArg( $arg, $description, $required=true)
Add some args that are needed.
__construct()
Definition: addSite.php:19
$maintClass
Definition: addSite.php:95
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.