MediaWiki master
CreateExternalDomainsTask.php
Go to the documentation of this file.
1<?php
2
4
12
19 private $lbFactory;
20
22 private $esFactory;
23
25 public function getName() {
26 return 'external-domains';
27 }
28
30 public function getDependencies() {
31 return [ 'VirtualDomains', 'services' ];
32 }
33
34 public function execute(): Status {
35 $this->initServices( $this->getServices() );
36 $status = $this->createVirtualDomains();
37 $status->merge( $this->createExternalStoreDomains() );
38 return $status;
39 }
40
41 private function initServices( MediaWikiServices $services ) {
42 $this->lbFactory = $services->getDBLoadBalancerFactory();
43 $this->esFactory = $services->getExternalStoreFactory();
44 }
45
46 private function createVirtualDomains(): Status {
47 $status = Status::newGood();
48 foreach ( $this->getVirtualDomains() as $virtualDomain ) {
49 if ( !$this->shouldDoShared()
50 && $this->lbFactory->isSharedVirtualDomain( $virtualDomain )
51 ) {
52 $status->warning( 'config-skip-shared-domain', $virtualDomain );
53 // Skip update of shared virtual domain
54 continue;
55 }
56 if ( $this->lbFactory->isLocalDomain( $virtualDomain ) ) {
57 // No need to create the main wiki domain
58 continue;
59 }
60 $lb = $this->lbFactory->getLoadBalancer( $virtualDomain );
61 $realDomainId = $this->lbFactory->getMappedDomain( $virtualDomain );
62 $status->merge( $this->maybeCreateDomain( $lb, $realDomainId ) );
63 }
64
65 return $status;
66 }
67
68 private function createExternalStoreDomains(): Status {
69 $status = Status::newGood();
70 $localDomainId = $this->lbFactory->getLocalDomainID();
71 foreach ( $this->esFactory->getWriteBaseUrls() as $url ) {
72 $store = $this->esFactory->getStoreForUrl( $url );
73 if ( $store instanceof ExternalStoreDB ) {
74 $cluster = $store->getClusterForUrl( $url );
75 if ( $cluster === null ) {
76 throw new \RuntimeException( "Invalid store url \"$url\"" );
77 }
78 $lb = $this->lbFactory->getExternalLB( $cluster );
79 $domainId = $store->getDomainIdForCluster( $cluster );
80 if ( $domainId !== false && $domainId !== $localDomainId && !$this->shouldDoShared() ) {
81 // Skip potentially shared domain
82 $status->warning( 'config-skip-shared-domain', "$cluster/$domainId" );
83 continue;
84 }
85 $status->merge( $this->maybeCreateDomain( $lb, $domainId ) );
86
87 $conn = $lb->getMaintenanceConnectionRef( DB_PRIMARY, [], $domainId );
88 $conn->setSchemaVars( $this->getContext()->getSchemaVars() );
89 if ( !$conn->tableExists( $store->getTable( $cluster ), __METHOD__ ) ) {
90 $store->initializeTable( $cluster );
91 }
92 }
93 }
94 return $status;
95 }
96
107 private function maybeCreateDomain( $lb, $domainId ) {
108 $databaseCreator = $this->getDatabaseCreator();
109 if ( $domainId === false ) {
110 $domainId = $this->lbFactory->getLocalDomainID();
111 }
112 $database = DatabaseDomain::newFromId( $domainId )->getDatabase();
113 if ( !$databaseCreator->existsInLoadBalancer( $lb, $database ) ) {
114 return $databaseCreator->createInLoadBalancer( $lb, $database );
115 }
116 return Status::newGood();
117 }
118
124 private function shouldDoShared() {
125 return (bool)$this->getOption( 'Shared' );
126 }
127}
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
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
warning( $message,... $parameters)
Add a new warning.
Class to handle database/schema/prefix specifications for IDatabase.
This class is a delegate to ILBFactory for a given database cluster.