MediaWiki  master
SqlModuleDependencyStore.php
Go to the documentation of this file.
1 <?php
22 
23 use 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->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  [ $module, $variant ] = $this->getEntityNameComponents( $entity );
131  $disjunctionConds[] = $dbw->makeList(
132  [ 'md_skin' => $variant, 'md_module' => $module ],
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  [ $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 ],
163  );
164  }
165 
166  $depsBlobByEntity = [];
167 
168  if ( $disjunctionConds ) {
169  $res = $db->newSelectQueryBuilder()
170  ->select( [ 'md_module', 'md_skin', 'md_deps' ] )
171  ->from( 'module_deps' )
172  ->where( $db->makeList( $disjunctionConds, $db::LIST_OR ) )
173  ->caller( __METHOD__ )->fetchResultSet();
174 
175  foreach ( $res as $row ) {
176  $entity = "{$row->md_module}|{$row->md_skin}";
177  $depsBlobByEntity[$entity] = $row->md_deps;
178  }
179  }
180 
181  return $depsBlobByEntity;
182  }
183 
187  private function getReplicaDb() {
188  return $this->lb
189  ->getConnectionRef( DB_REPLICA, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
190  }
191 
195  private function getPrimaryDb() {
196  return $this->lb
197  ->getConnectionRef( DB_PRIMARY, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
198  }
199 
204  private function getEntityNameComponents( $entity ) {
205  $parts = explode( '|', $entity, 2 );
206  if ( count( $parts ) !== 2 ) {
207  throw new InvalidArgumentException( "Invalid module entity '$entity'" );
208  }
209 
210  return $parts;
211  }
212 }
const LIST_OR
Definition: Defines.php:46
const LIST_AND
Definition: Defines.php:43
$modules
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: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.
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