MediaWiki master
CreateExternalDomainsTask.php
Go to the documentation of this file.
1<?php
2
4
13
20 private $lbFactory;
21
23 private $esFactory;
24
26 public function getName() {
27 return 'external-domains';
28 }
29
31 public function getDependencies() {
32 return [ 'services' ];
33 }
34
35 public function execute(): Status {
36 $this->initServices( $this->getServices() );
37 $status = $this->createVirtualDomains();
38 $status->merge( $this->createExternalStoreDomains() );
39 return $status;
40 }
41
42 private function initServices( MediaWikiServices $services ) {
43 $this->lbFactory = $services->getDBLoadBalancerFactory();
44 $this->esFactory = $services->getExternalStoreFactory();
45 }
46
47 private function createVirtualDomains(): Status {
48 $status = Status::newGood();
49 $virtualDomains = array_keys( $this->getConfigVar( MainConfigNames::VirtualDomainsMapping ) );
50 foreach ( $virtualDomains as $virtualDomain ) {
51 if ( !$this->shouldDoShared()
52 && $this->lbFactory->isSharedVirtualDomain( $virtualDomain )
53 ) {
54 $status->warning( 'config-skip-shared-domain', $virtualDomain );
55 // Skip update of shared virtual domain
56 continue;
57 }
58 if ( $this->lbFactory->isLocalDomain( $virtualDomain ) ) {
59 // No need to create the main wiki domain
60 continue;
61 }
62 $lb = $this->lbFactory->getLoadBalancer( $virtualDomain );
63 $realDomainId = $this->lbFactory->getMappedDomain( $virtualDomain );
64 $status->merge( $this->maybeCreateDomain( $lb, $realDomainId ) );
65 }
66
67 return $status;
68 }
69
70 private function createExternalStoreDomains(): Status {
71 $status = Status::newGood();
72 $localDomainId = $this->lbFactory->getLocalDomainID();
73 foreach ( $this->esFactory->getWriteBaseUrls() as $url ) {
74 $store = $this->esFactory->getStoreForUrl( $url );
75 if ( $store instanceof ExternalStoreDB ) {
76 $cluster = $store->getClusterForUrl( $url );
77 if ( $cluster === null ) {
78 throw new \RuntimeException( "Invalid store url \"$url\"" );
79 }
80 $lb = $this->lbFactory->getExternalLB( $cluster );
81 $domainId = $store->getDomainIdForCluster( $cluster );
82 if ( $domainId !== false && $domainId !== $localDomainId && !$this->shouldDoShared() ) {
83 // Skip potentially shared domain
84 $status->warning( 'config-skip-shared-domain', "$cluster/$domainId" );
85 continue;
86 }
87 $status->merge( $this->maybeCreateDomain( $lb, $domainId ) );
88
89 $conn = $lb->getMaintenanceConnectionRef( DB_PRIMARY, [], $domainId );
90 $conn->setSchemaVars( $this->getContext()->getSchemaVars() );
91 if ( !$conn->tableExists( $store->getTable( $cluster ), __METHOD__ ) ) {
92 $store->initializeTable( $cluster );
93 }
94 }
95 }
96 return $status;
97 }
98
109 private function maybeCreateDomain( $lb, $domainId ) {
110 $databaseCreator = $this->getDatabaseCreator();
111 if ( $domainId === false ) {
112 $domainId = $this->lbFactory->getLocalDomainID();
113 }
114 $database = DatabaseDomain::newFromId( $domainId )->getDatabase();
115 if ( !$databaseCreator->existsInLoadBalancer( $lb, $database ) ) {
116 return $databaseCreator->createInLoadBalancer( $lb, $database );
117 }
118 return Status::newGood();
119 }
120
126 private function shouldDoShared() {
127 return (bool)$this->getOption( 'Shared' );
128 }
129}
const DB_PRIMARY
Definition defines.php:28
External storage in a SQL database.
Create databases referenced in virtual domain and external store config.
getDependencies()
Get a list of names or aliases of tasks that must be done prior to this task.to override string|strin...
getName()
Get the symbolic name of the task.string
Base class for installer tasks.
Definition Task.php:24
getServices()
Get the restored services.
Definition Task.php:292
A class containing constants representing the names of configuration variables.
const VirtualDomainsMapping
Name constant for the VirtualDomainsMapping setting, for use with Config::get()
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Class to handle database/schema/prefix specifications for IDatabase.
This class is a delegate to ILBFactory for a given database cluster.