Debian Codegraph @

maintenance/addSite.php

raw symbols SCIP
1 <?php
2
3 // @codeCoverageIgnoreStart
4 require_once __DIR__ . '/Maintenance.php';
5 // @codeCoverageIgnoreEnd
6
7 use MediaWiki\Maintenance\Maintenance;
8 use MediaWiki\Site\MediaWikiSite;
9
10 /**
11 * Maintenance script for adding a site definition into the sites table.
12 *
13 * The sites table is cached in the local-server cache,
14 * so you should reload your webserver and other long-running MediaWiki
15 * PHP processes after running this script.
16 *
17 * @since 1.29
18 *
19 * @license GPL-2.0-or-later
20 * @author Florian Schmidt
21 */
22 class AddSite extends Maintenance {
23
24 public function __construct() {
25 parent::__construct();
26
27 $this->addDescription( 'Add a site definition into the sites table.' );
28
29 $this->addArg( 'globalid', 'The global id of the site to add, e.g. "wikipedia".', true );
30 $this->addArg( 'group', 'In which group this site should be sorted in.', true );
31 $this->addOption( 'language', 'The language code of the site, e.g. "de".', false, true );
32 $this->addOption( 'interwiki-id', 'The interwiki ID of the site.', false, true );
33 $this->addOption( 'navigation-id', 'The navigation ID of the site.', false, true );
34 $this->addOption( 'pagepath', 'The URL to pages of this site, e.g.' .
35 ' https://example.com/wiki/\$1.', false, true );
36 $this->addOption( 'filepath', 'The URL to files of this site, e.g. https://example' .
37 '.com/w/\$1.', false, true );
38 }
39
40 /**
41 * Imports the site described by the parameters (see self::__construct()) passed to this
42 * maintenance sccript into the sites table of MediaWiki.
43 * @return bool
44 */
45 public function execute() {
46 $siteStore = $this->getServiceContainer()->getSiteStore();
47 if ( method_exists( $siteStore, 'reset' ) ) {
48 // @phan-suppress-next-line PhanUndeclaredMethod
49 $siteStore->reset();
50 }
51
52 $globalId = $this->getArg( 0 );
53 $group = $this->getArg( 1 );
54 $language = $this->getOption( 'language' );
55 $interwikiId = $this->getOption( 'interwiki-id' );
56 $navigationId = $this->getOption( 'navigation-id' );
57 $pagepath = $this->getOption( 'pagepath' );
58 $filepath = $this->getOption( 'filepath' );
59
60 if ( !is_string( $globalId ) || !is_string( $group ) ) {
61 $this->error( 'Arguments globalid and group need to be strings.' );
62 return false;
63 }
64
65 if ( $siteStore->getSite( $globalId ) !== null ) {
66 $this->error( "Site with global id $globalId already exists." );
67 return false;
68 }
69
70 $site = new MediaWikiSite();
71 $site->setGlobalId( $globalId );
72 $site->setGroup( $group );
73 if ( $language !== null ) {
74 $site->setLanguageCode( $language );
75 }
76 if ( $interwikiId !== null ) {
77 $site->addInterwikiId( $interwikiId );
78 }
79 if ( $navigationId !== null ) {
80 $site->addNavigationId( $navigationId );
81 }
82 if ( $pagepath !== null ) {
83 $site->setPagePath( $pagepath );
84 }
85 if ( $filepath !== null ) {
86 $site->setFilePath( $filepath );
87 }
88
89 $siteStore->saveSites( [ $site ] );
90
91 if ( method_exists( $siteStore, 'reset' ) ) {
92 // @phan-suppress-next-line PhanUndeclaredMethod
93 $siteStore->reset();
94 }
95
96 $this->output(
97 'Done. Reload the web server and other long-running PHP processes '
98 . "to refresh the local-server cache of the sites table.\n"
99 );
100 }
101 }
102
103 // @codeCoverageIgnoreStart
104 $maintClass = AddSite::class;
105 require_once RUN_MAINTENANCE_IF_MAIN;
106 // @codeCoverageIgnoreEnd