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