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