MediaWiki master
SqlModuleDependencyStore.php
Go to the documentation of this file.
1<?php
22
23use InvalidArgumentException;
28
44 private $lb;
45
49 public function __construct( ILoadBalancer $lb ) {
50 $this->lb = $lb;
51 }
52
53 public function retrieveMulti( $type, array $entities ) {
54 $dbr = $this->getReplicaDb();
55
56 $depsBlobByEntity = $this->fetchDependencyBlobs( $entities, $dbr );
57
58 $storedPathsByEntity = [];
59 foreach ( $depsBlobByEntity as $entity => $depsBlob ) {
60 $storedPathsByEntity[$entity] = json_decode( $depsBlob, true );
61 }
62
63 $results = [];
64 foreach ( $entities as $entity ) {
65 $paths = $storedPathsByEntity[$entity] ?? [];
66 $results[$entity] = $this->newEntityDependencies( $paths, null );
67 }
68
69 return $results;
70 }
71
72 public function storeMulti( $type, array $dataByEntity, $ttl ) {
73 // Avoid opening a primary DB connection when it's not needed.
74 // ResourceLoader::saveModuleDependenciesInternal calls this method unconditionally
75 // with empty values most of the time.
76 if ( !$dataByEntity ) {
77 return;
78 }
79
80 $dbw = $this->getPrimaryDb();
81 $depsBlobByEntity = $this->fetchDependencyBlobs( array_keys( $dataByEntity ), $dbw );
82
83 $rows = [];
84 foreach ( $dataByEntity as $entity => $data ) {
85 [ $module, $variant ] = $this->getEntityNameComponents( $entity );
86 if ( !is_array( $data[self::KEY_PATHS] ) ) {
87 throw new InvalidArgumentException( "Invalid entry for '$entity'" );
88 }
89
90 // Normalize the list by removing duplicates and sortings
91 $paths = array_values( array_unique( $data[self::KEY_PATHS] ) );
92 sort( $paths, SORT_STRING );
93 $blob = json_encode( $paths, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
94
95 $existingBlob = $depsBlobByEntity[$entity] ?? null;
96 if ( $blob !== $existingBlob ) {
97 $rows[] = [
98 'md_module' => $module,
99 'md_skin' => $variant,
100 'md_deps' => $blob
101 ];
102 }
103 }
104
105 // @TODO: use a single query with VALUES()/aliases support in DB wrapper
106 // See https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
107 foreach ( $rows as $row ) {
108 $dbw->newInsertQueryBuilder()
109 ->insertInto( 'module_deps' )
110 ->row( $row )
111 ->onDuplicateKeyUpdate()
112 ->uniqueIndexFields( [ 'md_module', 'md_skin' ] )
113 ->set( [ 'md_deps' => $row['md_deps'] ] )
114 ->caller( __METHOD__ )->execute();
115 }
116 }
117
118 public function remove( $type, $entities ) {
119 // Avoid opening a primary DB connection when it's not needed.
120 // ResourceLoader::saveModuleDependenciesInternal calls this method unconditionally
121 // with empty values most of the time.
122 if ( !$entities ) {
123 return;
124 }
125
126 $dbw = $this->getPrimaryDb();
127 $disjunctionConds = [];
128 foreach ( (array)$entities as $entity ) {
129 [ $module, $variant ] = $this->getEntityNameComponents( $entity );
130 $disjunctionConds[] = $dbw
131 ->expr( 'md_skin', '=', $variant )
132 ->and( 'md_module', '=', $module );
133 }
134
135 if ( $disjunctionConds ) {
136 $dbw->newDeleteQueryBuilder()
137 ->deleteFrom( 'module_deps' )
138 ->where( new OrExpressionGroup( ...$disjunctionConds ) )
139 ->caller( __METHOD__ )->execute();
140 }
141 }
142
148 private function fetchDependencyBlobs( array $entities, IDatabase $db ) {
149 $modulesByVariant = [];
150 foreach ( $entities as $entity ) {
151 [ $module, $variant ] = $this->getEntityNameComponents( $entity );
152 $modulesByVariant[$variant][] = $module;
153 }
154
155 $disjunctionConds = [];
156 foreach ( $modulesByVariant as $variant => $modules ) {
157 $disjunctionConds[] = $db
158 ->expr( 'md_skin', '=', $variant )
159 ->and( 'md_module', '=', $modules );
160 }
161
162 $depsBlobByEntity = [];
163
164 if ( $disjunctionConds ) {
165 $res = $db->newSelectQueryBuilder()
166 ->select( [ 'md_module', 'md_skin', 'md_deps' ] )
167 ->from( 'module_deps' )
168 ->where( new OrExpressionGroup( ...$disjunctionConds ) )
169 ->caller( __METHOD__ )->fetchResultSet();
170
171 foreach ( $res as $row ) {
172 $entity = "{$row->md_module}|{$row->md_skin}";
173 $depsBlobByEntity[$entity] = $row->md_deps;
174 }
175 }
176
177 return $depsBlobByEntity;
178 }
179
183 private function getReplicaDb() {
184 return $this->lb
185 ->getConnectionRef( DB_REPLICA, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
186 }
187
191 private function getPrimaryDb() {
192 return $this->lb
193 ->getConnectionRef( DB_PRIMARY, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
194 }
195
200 private function getEntityNameComponents( $entity ) {
201 $parts = explode( '|', $entity, 2 );
202 if ( count( $parts ) !== 2 ) {
203 throw new InvalidArgumentException( "Invalid module entity '$entity'" );
204 }
205
206 return $parts;
207 }
208}
Track per-module dependency file paths that are expensive to mass compute.
newEntityDependencies(array $paths=[], $asOf=null)
Track per-module file dependencies in the core module_deps table.
retrieveMulti( $type, array $entities)
Get the currently tracked dependencies for a set of entities.
storeMulti( $type, array $dataByEntity, $ttl)
Set the currently tracked dependencies for a set of entities.
Helper class used for automatically marking an IDatabase connection as reusable (once it no longer ma...
Definition DBConnRef.php:29
Representing a group of expressions chained via OR.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
This class is a delegate to ILBFactory for a given database cluster.
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.
expr(string $field, string $op, $value)
See Expression::__construct()
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28