File: SqlModuleDependencyStore.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (207 lines) | stat: -rw-r--r-- 6,139 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace Wikimedia\DependencyStore;

use InvalidArgumentException;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\IReadableDatabase;

/**
 * Track per-module file dependencies in the core module_deps table
 *
 * Wiki farms that are too big for maintenance/update.php, can clean up
 * unneeded data for modules that no longer exist after a MW upgrade,
 * by running maintenance/cleanupRemovedModules.php.
 *
 * To force a rebuild and incurr a small penalty in browser cache churn,
 * run maintenance/purgeModuleDeps.php instead.
 *
 * @internal For use by ResourceLoader\Module only
 * @since 1.35
 */
class SqlModuleDependencyStore extends DependencyStore {
	/** @var ILoadBalancer */
	private $lb;

	/**
	 * @param ILoadBalancer $lb Storage backend
	 */
	public function __construct( ILoadBalancer $lb ) {
		$this->lb = $lb;
	}

	public function retrieveMulti( $type, array $entities ) {
		$dbr = $this->getReplicaDb();

		$depsBlobByEntity = $this->fetchDependencyBlobs( $entities, $dbr );

		$storedPathsByEntity = [];
		foreach ( $depsBlobByEntity as $entity => $depsBlob ) {
			$storedPathsByEntity[$entity] = json_decode( $depsBlob, true );
		}

		$results = [];
		foreach ( $entities as $entity ) {
			$paths = $storedPathsByEntity[$entity] ?? [];
			$results[$entity] = $this->newEntityDependencies( $paths, null );
		}

		return $results;
	}

	public function storeMulti( $type, array $dataByEntity, $ttl ) {
		// Avoid opening a primary DB connection when it's not needed.
		// ResourceLoader::saveModuleDependenciesInternal calls this method unconditionally
		// with empty values most of the time.
		if ( !$dataByEntity ) {
			return;
		}

		$dbw = $this->getPrimaryDb();
		$depsBlobByEntity = $this->fetchDependencyBlobs( array_keys( $dataByEntity ), $dbw );

		$rows = [];
		foreach ( $dataByEntity as $entity => $data ) {
			[ $module, $variant ] = $this->getEntityNameComponents( $entity );
			if ( !is_array( $data[self::KEY_PATHS] ) ) {
				throw new InvalidArgumentException( "Invalid entry for '$entity'" );
			}

			// Normalize the list by removing duplicates and sortings
			$paths = array_values( array_unique( $data[self::KEY_PATHS] ) );
			sort( $paths, SORT_STRING );
			$blob = json_encode( $paths, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

			$existingBlob = $depsBlobByEntity[$entity] ?? null;
			if ( $blob !== $existingBlob ) {
				$rows[] = [
					'md_module' => $module,
					'md_skin' => $variant,
					'md_deps' => $blob
				];
			}
		}

		// @TODO: use a single query with VALUES()/aliases support in DB wrapper
		// See https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
		foreach ( $rows as $row ) {
			$dbw->newInsertQueryBuilder()
				->insertInto( 'module_deps' )
				->row( $row )
				->onDuplicateKeyUpdate()
				->uniqueIndexFields( [ 'md_module', 'md_skin' ] )
				->set( [ 'md_deps' => $row['md_deps'] ] )
				->caller( __METHOD__ )->execute();
		}
	}

	public function remove( $type, $entities ) {
		// Avoid opening a primary DB connection when it's not needed.
		// ResourceLoader::saveModuleDependenciesInternal calls this method unconditionally
		// with empty values most of the time.
		if ( !$entities ) {
			return;
		}

		$dbw = $this->getPrimaryDb();
		$disjunctionConds = [];
		foreach ( (array)$entities as $entity ) {
			[ $module, $variant ] = $this->getEntityNameComponents( $entity );
			$disjunctionConds[] = $dbw
				->expr( 'md_skin', '=', $variant )
				->and( 'md_module', '=', $module );
		}

		if ( $disjunctionConds ) {
			$dbw->newDeleteQueryBuilder()
				->deleteFrom( 'module_deps' )
				->where( $dbw->orExpr( $disjunctionConds ) )
				->caller( __METHOD__ )->execute();
		}
	}

	/**
	 * @param string[] $entities
	 * @param IReadableDatabase $db
	 * @return string[]
	 */
	private function fetchDependencyBlobs( array $entities, IReadableDatabase $db ) {
		$modulesByVariant = [];
		foreach ( $entities as $entity ) {
			[ $module, $variant ] = $this->getEntityNameComponents( $entity );
			$modulesByVariant[$variant][] = $module;
		}

		$disjunctionConds = [];
		foreach ( $modulesByVariant as $variant => $modules ) {
			$disjunctionConds[] = $db
				->expr( 'md_skin', '=', $variant )
				->and( 'md_module', '=', $modules );
		}

		$depsBlobByEntity = [];

		if ( $disjunctionConds ) {
			$res = $db->newSelectQueryBuilder()
				->select( [ 'md_module', 'md_skin', 'md_deps' ] )
				->from( 'module_deps' )
				->where( $db->orExpr( $disjunctionConds ) )
				->caller( __METHOD__ )->fetchResultSet();

			foreach ( $res as $row ) {
				$entity = "{$row->md_module}|{$row->md_skin}";
				$depsBlobByEntity[$entity] = $row->md_deps;
			}
		}

		return $depsBlobByEntity;
	}

	/**
	 * @return IReadableDatabase
	 */
	private function getReplicaDb() {
		return $this->lb
			->getConnection( DB_REPLICA, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
	}

	/**
	 * @return IDatabase
	 */
	private function getPrimaryDb() {
		return $this->lb
			->getConnection( DB_PRIMARY, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
	}

	/**
	 * @param string $entity
	 * @return string[]
	 */
	private function getEntityNameComponents( $entity ) {
		$parts = explode( '|', $entity, 2 );
		if ( count( $parts ) !== 2 ) {
			throw new InvalidArgumentException( "Invalid module entity '$entity'" );
		}

		return $parts;
	}
}