Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddSite
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3require_once __DIR__ . '/Maintenance.php';
4
5use MediaWiki\Site\MediaWikiSite;
6
7/**
8 * Maintenance script for adding a site definition into the sites table.
9 *
10 * The sites table is cached in the local-server cache,
11 * so you should reload your webserver and other long-running MediaWiki
12 * PHP processes after running this script.
13 *
14 * @since 1.29
15 *
16 * @license GPL-2.0-or-later
17 * @author Florian Schmidt
18 */
19class 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
37    /**
38     * Imports the site described by the parameters (see self::__construct()) passed to this
39     * maintenance sccript into the sites table of MediaWiki.
40     * @return bool
41     */
42    public function execute() {
43        $siteStore = $this->getServiceContainer()->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;
101require_once RUN_MAINTENANCE_IF_MAIN;