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 // Skip update of shared virtual domain
50 continue;
51 }
52 if ( $this->lbFactory->isLocalDomain( $virtualDomain ) ) {
53 // No need to create the main wiki domain
54 continue;
55 }
56 $lb = $this->lbFactory->getLoadBalancer( $virtualDomain );
57 $realDomainId = $this->lbFactory->getMappedDomain( $virtualDomain );
58 $status->merge( $this->maybeCreateDomain( $lb, $realDomainId ) );
59 }
60
61 return $status;
62 }
63
64 private function createExternalStoreDomains() {
65 $status = Status::newGood();
66 foreach ( $this->esFactory->getWriteBaseUrls() as $url ) {
67 $store = $this->esFactory->getStoreForUrl( $url );
68 if ( $store instanceof ExternalStoreDB ) {
69 $cluster = $store->getClusterForUrl( $url );
70 if ( $cluster === null ) {
71 throw new \RuntimeException( "Invalid store url \"$url\"" );
72 }
73 $lb = $this->lbFactory->getExternalLB( $cluster );
74 $domainId = $store->getDomainIdForCluster( $cluster );
75 if ( $domainId !== false && !$this->shouldDoShared() ) {
76 // Skip potentially shared domain
77 continue;
78 }
79 $status->merge( $this->maybeCreateDomain( $lb, $domainId ) );
80
81 $conn = $lb->getMaintenanceConnectionRef( DB_PRIMARY, [], $domainId );
82 $conn->setSchemaVars( $this->getContext()->getSchemaVars() );
83 if ( !$conn->tableExists( $store->getTable( $cluster ) ) ) {
84 $store->initializeTable( $cluster );
85 }
86 }
87 }
88 return $status;
89 }
90
101 private function maybeCreateDomain( $lb, $domainId ) {
102 $databaseCreator = $this->getDatabaseCreator();
103 if ( $domainId === false ) {
104 $domainId = $this->lbFactory->getLocalDomainID();
105 }
106 $database = DatabaseDomain::newFromId( $domainId )->getDatabase();
107 if ( !$databaseCreator->existsInLoadBalancer( $lb, $database ) ) {
108 return $databaseCreator->createInLoadBalancer( $lb, $database );
109 }
110 return Status::newGood();
111 }
112
118 private function shouldDoShared() {
119 return (bool)$this->getOption( 'Shared' );
120 }
121}
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:283
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