MediaWiki REL1_35
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
7require_once $basePath . '/maintenance/Maintenance.php';
8
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 = 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;
96require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
$maintClass
Definition addSite.php:95
$basePath
Definition addSite.php:5
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...
addArg( $arg, $description, $required=true)
Add some args that are needed.
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.