Debian Codegraph @

includes/ResourceLoader/dependencystore/SqlModuleDependencyStore.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace Wikimedia\DependencyStore;
22
23 use InvalidArgumentException;
24 use Wikimedia\Rdbms\IDatabase;
25 use Wikimedia\Rdbms\ILoadBalancer;
26 use Wikimedia\Rdbms\IReadableDatabase;
27
28 /**
29 * Track per-module file dependencies in the core module_deps table
30 *
31 * Wiki farms that are too big for maintenance/update.php, can clean up
32 * unneeded data for modules that no longer exist after a MW upgrade,
33 * by running maintenance/cleanupRemovedModules.php.
34 *
35 * To force a rebuild and incurr a small penalty in browser cache churn,
36 * run maintenance/purgeModuleDeps.php instead.
37 *
38 * @internal For use by ResourceLoader\Module only
39 * @since 1.35
40 */
41 class SqlModuleDependencyStore extends DependencyStore {
42 /** @var ILoadBalancer */
43 private $lb;
44
45 /**
46 * @param ILoadBalancer $lb Storage backend
47 */
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
142 /**
143 * @param string[] $entities
144 * @param IReadableDatabase $db
145 * @return string[]
146 */
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
179 /**
180 * @return IReadableDatabase
181 */
182 private function getReplicaDb() {
183 return $this->lb
184 ->getConnection( DB_REPLICA, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
185 }
186
187 /**
188 * @return IDatabase
189 */
190 private function getPrimaryDb() {
191 return $this->lb
192 ->getConnection( DB_PRIMARY, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
193 }
194
195 /**
196 * @param string $entity
197 * @return string[]
198 */
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 }