MediaWiki REL1_39
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 list( $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->upsert(
108 'module_deps',
109 $row,
110 [ [ 'md_module', 'md_skin' ] ],
111 [
112 'md_deps' => $row['md_deps'],
113 ],
114 __METHOD__
115 );
116 }
117 }
118
119 public function remove( $type, $entities ) {
120 // Avoid opening a primary DB connection when it's not needed.
121 // ResourceLoader::saveModuleDependenciesInternal calls this method unconditionally
122 // with empty values most of the time.
123 if ( !$entities ) {
124 return;
125 }
126
127 $dbw = $this->getPrimaryDB();
128 $disjunctionConds = [];
129 foreach ( (array)$entities as $entity ) {
130 list( $module, $variant ) = $this->getEntityNameComponents( $entity );
131 $disjunctionConds[] = $dbw->makeList(
132 [ 'md_skin' => $variant, 'md_module' => $module ],
133 $dbw::LIST_AND
134 );
135 }
136
137 if ( $disjunctionConds ) {
138 $dbw->delete(
139 'module_deps',
140 $dbw->makeList( $disjunctionConds, $dbw::LIST_OR ),
141 __METHOD__
142 );
143 }
144 }
145
151 private function fetchDependencyBlobs( array $entities, IDatabase $db ) {
152 $modulesByVariant = [];
153 foreach ( $entities as $entity ) {
154 list( $module, $variant ) = $this->getEntityNameComponents( $entity );
155 $modulesByVariant[$variant][] = $module;
156 }
157
158 $disjunctionConds = [];
159 foreach ( $modulesByVariant as $variant => $modules ) {
160 $disjunctionConds[] = $db->makeList(
161 [ 'md_skin' => $variant, 'md_module' => $modules ],
162 $db::LIST_AND
163 );
164 }
165
166 $depsBlobByEntity = [];
167
168 if ( $disjunctionConds ) {
169 $res = $db->select(
170 'module_deps',
171 [ 'md_module', 'md_skin', 'md_deps' ],
172 $db->makeList( $disjunctionConds, $db::LIST_OR ),
173 __METHOD__
174 );
175
176 foreach ( $res as $row ) {
177 $entity = "{$row->md_module}|{$row->md_skin}";
178 $depsBlobByEntity[$entity] = $row->md_deps;
179 }
180 }
181
182 return $depsBlobByEntity;
183 }
184
188 private function getReplicaDb() {
189 return $this->lb
190 ->getConnectionRef( DB_REPLICA, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
191 }
192
196 private function getPrimaryDb() {
197 return $this->lb
198 ->getConnectionRef( DB_PRIMARY, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
199 }
200
205 private function getEntityNameComponents( $entity ) {
206 $parts = explode( '|', $entity, 2 );
207 if ( count( $parts ) !== 2 ) {
208 throw new InvalidArgumentException( "Invalid module entity '$entity'" );
209 }
210
211 return $parts;
212 }
213}
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
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
Create and track the database connections and transactions for a given database cluster.
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28