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