Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / maintenance / addSite.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 $basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/..';
6
7 require_once $basePath . '/maintenance/Maintenance.php';
8
9 /**
10 * Maintenance script for adding a site definition into the sites table.
11 *
12 * @since 1.29
13 *
14 * @license GPL-2.0-or-later
15 * @author Florian Schmidt
16 */
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
35 /**
36 * Imports the site described by the parameters (see self::__construct()) passed to this
37 * maintenance sccript into the sites table of MediaWiki.
38 * @return bool
39 */
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;