Merge "Align "What's this" vertically"
[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 GNU GPL v2+
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 */
39 public function execute() {
40 $siteStore = MediaWikiServices::getInstance()->getSiteStore();
41 $siteStore->reset();
42
43 $globalId = $this->getArg( 0 );
44 $group = $this->getArg( 1 );
45 $language = $this->getOption( 'language' );
46 $interwikiId = $this->getOption( 'interwiki-id' );
47 $navigationId = $this->getOption( 'navigation-id' );
48 $pagepath = $this->getOption( 'pagepath' );
49 $filepath = $this->getOption( 'filepath' );
50
51 if ( !is_string( $globalId ) || !is_string( $group ) ) {
52 echo "Arguments globalid and group need to be strings.\n";
53 return false;
54 }
55
56 if ( $siteStore->getSite( $globalId ) !== null ) {
57 echo "Site with global id $globalId already exists.\n";
58 return false;
59 }
60
61 $site = new MediaWikiSite();
62 $site->setGlobalId( $globalId );
63 $site->setGroup( $group );
64 if ( $language !== null ) {
65 $site->setLanguageCode( $language );
66 }
67 if ( $interwikiId !== null ) {
68 $site->addInterwikiId( $interwikiId );
69 }
70 if ( $navigationId !== null ) {
71 $site->addNavigationId( $navigationId );
72 }
73 if ( $pagepath !== null ) {
74 $site->setPagePath( $pagepath );
75 }
76 if ( $filepath !== null ) {
77 $site->setFilePath( $filepath );
78 }
79
80 $siteStore->saveSites( [ $site ] );
81
82 if ( method_exists( $siteStore, 'reset' ) ) {
83 $siteStore->reset();
84 }
85
86 echo "Done.\n";
87 }
88 }
89
90 $maintClass = 'AddSite';
91 require_once RUN_MAINTENANCE_IF_MAIN;