Total Complexity | 50 |
Total Lines | 394 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Repository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Repository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | abstract class Repository |
||
26 | { |
||
27 | protected CacheItemPoolInterface $cache; |
||
28 | protected ContainerInterface $container; |
||
29 | protected Client $guzzle; |
||
30 | protected LoggerInterface $logger; |
||
31 | |||
32 | /** @var Connection The database connection to the meta database. */ |
||
33 | private $metaConnection; |
||
34 | |||
35 | /** @var Connection The database connection to other tools' databases. */ |
||
36 | private $toolsConnection; |
||
37 | |||
38 | /** @var bool Whether this is configured as a WMF installation. */ |
||
39 | protected $isWMF; |
||
40 | |||
41 | /** @var int */ |
||
42 | protected $queryTimeout; |
||
43 | |||
44 | /** @var string Prefix URL for where the dblists live. Will be followed by i.e. 's1.dblist' */ |
||
45 | public const DBLISTS_URL = 'https://noc.wikimedia.org/conf/dblists/'; |
||
46 | |||
47 | /** |
||
48 | * Create a new Repository. |
||
49 | * @param ContainerInterface $container |
||
50 | * @param CacheItemPoolInterface $cache |
||
51 | * @param Client $guzzle |
||
52 | */ |
||
53 | public function __construct( |
||
54 | ContainerInterface $container, |
||
55 | CacheItemPoolInterface $cache, |
||
56 | Client $guzzle, |
||
57 | LoggerInterface $logger, |
||
58 | bool $isWMF, |
||
59 | int $queryTimeout |
||
60 | ) { |
||
61 | $this->container = $container; |
||
62 | $this->cache = $cache; |
||
63 | $this->guzzle = $guzzle; |
||
64 | $this->logger = $logger; |
||
65 | $this->isWMF = $isWMF; |
||
66 | $this->queryTimeout = $queryTimeout; |
||
67 | } |
||
68 | |||
69 | /*************** |
||
70 | * CONNECTIONS * |
||
71 | ***************/ |
||
72 | |||
73 | /** |
||
74 | * Get the database connection for the 'meta' database. |
||
75 | * @return Connection |
||
76 | * @codeCoverageIgnore |
||
77 | */ |
||
78 | protected function getMetaConnection(): Connection |
||
79 | { |
||
80 | if (!$this->metaConnection instanceof Connection) { |
||
|
|||
81 | $this->metaConnection = $this->getProjectsConnection('meta'); |
||
82 | } |
||
83 | return $this->metaConnection; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Get a database connection for the given database. |
||
88 | * @param Project|string $project Project instance, database name (i.e. 'enwiki'), or slice (i.e. 's1'). |
||
89 | * @return Connection |
||
90 | * @codeCoverageIgnore |
||
91 | */ |
||
92 | protected function getProjectsConnection($project): Connection |
||
93 | { |
||
94 | if (is_string($project)) { |
||
95 | if (1 === preg_match('/^s\d+$/', $project)) { |
||
96 | $slice = $project; |
||
97 | } else { |
||
98 | // Assume database name. Remove _p if given. |
||
99 | $db = str_replace('_p', '', $project); |
||
100 | $slice = $this->getDbList()[$db]; |
||
101 | } |
||
102 | } else { |
||
103 | $slice = $this->getDbList()[$project->getDatabaseName()]; |
||
104 | } |
||
105 | |||
106 | return $this->container->get('doctrine') |
||
107 | ->getConnection('toolforge_'.$slice); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get the database connection for the 'tools' database (the one that other tools store data in). |
||
112 | * @return Connection |
||
113 | * @codeCoverageIgnore |
||
114 | */ |
||
115 | protected function getToolsConnection(): Connection |
||
116 | { |
||
117 | if (!$this->toolsConnection instanceof Connection) { |
||
118 | $this->toolsConnection = $this->container |
||
119 | ->get('doctrine') |
||
120 | ->getManager('toolsdb') |
||
121 | ->getConnection(); |
||
122 | } |
||
123 | return $this->toolsConnection; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Fetch and concatenate all the dblists into one array. |
||
128 | * Based on ToolforgeBundle https://github.com/wikimedia/ToolforgeBundle/blob/master/Service/ReplicasClient.php |
||
129 | * License: GPL 3.0 or later |
||
130 | * @return string[] Keys are database names (i.e. 'enwiki'), values are the slices (i.e. 's1'). |
||
131 | */ |
||
132 | protected function getDbList(): array |
||
133 | { |
||
134 | $cacheKey = 'dblists'; |
||
135 | if ($this->cache->hasItem($cacheKey)) { |
||
136 | return $this->cache->getItem($cacheKey)->get(); |
||
137 | } |
||
138 | |||
139 | $dbList = []; |
||
140 | $exists = true; |
||
141 | $i = 0; |
||
142 | |||
143 | while ($exists) { |
||
144 | $i += 1; |
||
145 | $response = $this->guzzle->request('GET', self::DBLISTS_URL."s$i.dblist", ['http_errors' => false]); |
||
146 | $exists = in_array( |
||
147 | $response->getStatusCode(), |
||
148 | [Response::HTTP_OK, Response::HTTP_NOT_MODIFIED] |
||
149 | ) && $i < 50; // Safeguard |
||
150 | |||
151 | if (!$exists) { |
||
152 | break; |
||
153 | } |
||
154 | |||
155 | $lines = explode("\n", $response->getBody()->getContents()); |
||
156 | foreach ($lines as $line) { |
||
157 | $line = trim($line); |
||
158 | if (1 !== preg_match('/^#/', $line) && '' !== $line) { |
||
159 | // Skip comments and blank lines. |
||
160 | $dbList[$line] = "s$i"; |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // Manually add the meta and centralauth databases. |
||
166 | $dbList['meta'] = 's7'; |
||
167 | $dbList['centralauth'] = 's7'; |
||
168 | |||
169 | // Cache for one week. |
||
170 | return $this->setCache($cacheKey, $dbList, 'P1W'); |
||
171 | } |
||
172 | |||
173 | /***************** |
||
174 | * QUERY HELPERS * |
||
175 | *****************/ |
||
176 | |||
177 | /** |
||
178 | * Make a request to the MediaWiki API. |
||
179 | * @param Project $project |
||
180 | * @param array $params |
||
181 | * @return array |
||
182 | */ |
||
183 | public function executeApiRequest(Project $project, array $params): array |
||
184 | { |
||
185 | return json_decode($this->guzzle->request('GET', $project->getApiUrl(), [ |
||
186 | 'query' => array_merge([ |
||
187 | 'action' => 'query', |
||
188 | 'format' => 'json', |
||
189 | ], $params), |
||
190 | ])->getBody()->getContents(), true); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Normalize and quote a table name for use in SQL. |
||
195 | * @param string $databaseName |
||
196 | * @param string $tableName |
||
197 | * @param string|null $tableExtension Optional table extension, which will only get used if we're on labs. |
||
198 | * If null, table extensions are added as defined in table_map.yml. If a blank string, no extension is added. |
||
199 | * @return string Fully-qualified and quoted table name. |
||
200 | */ |
||
201 | public function getTableName(string $databaseName, string $tableName, ?string $tableExtension = null): string |
||
202 | { |
||
203 | $mapped = false; |
||
204 | |||
205 | // This is a workaround for a one-to-many mapping |
||
206 | // as required by Labs. We combine $tableName with |
||
207 | // $tableExtension in order to generate the new table name |
||
208 | if ($this->isWMF && null !== $tableExtension) { |
||
209 | $mapped = true; |
||
210 | $tableName .=('' === $tableExtension ? '' : '_'.$tableExtension); |
||
211 | } elseif ($this->container->hasParameter("app.table.$tableName")) { |
||
212 | // Use the table specified in the table mapping configuration, if present. |
||
213 | $mapped = true; |
||
214 | $tableName = $this->container->getParameter("app.table.$tableName"); |
||
215 | } |
||
216 | |||
217 | // For 'revision' and 'logging' tables (actually views) on Labs, use the indexed versions |
||
218 | // (that have some rows hidden, e.g. for revdeleted users). |
||
219 | // This is a safeguard in case table mapping isn't properly set up. |
||
220 | $isLoggingOrRevision = in_array($tableName, ['revision', 'logging', 'archive']); |
||
221 | if (!$mapped && $isLoggingOrRevision && $this->isWMF) { |
||
222 | $tableName .="_userindex"; |
||
223 | } |
||
224 | |||
225 | // Figure out database name. |
||
226 | // Use class variable for the database name if not set via function parameter. |
||
227 | if ($this->isWMF && '_p' != substr($databaseName, -2)) { |
||
228 | // Append '_p' if this is labs. |
||
229 | $databaseName .= '_p'; |
||
230 | } |
||
231 | |||
232 | return "`$databaseName`.`$tableName`"; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Get a unique cache key for the given list of arguments. Assuming each argument of |
||
237 | * your function should be accounted for, you can pass in them all with func_get_args: |
||
238 | * $this->getCacheKey(func_get_args(), 'unique key for function'); |
||
239 | * Arguments that are a model should implement their own getCacheKey() that returns |
||
240 | * a unique identifier for an instance of that model. See User::getCacheKey() for example. |
||
241 | * @param array|mixed $args Array of arguments or a single argument. |
||
242 | * @param string|null $key Unique key for this function. If omitted the function name itself |
||
243 | * is used, which is determined using `debug_backtrace`. |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getCacheKey($args, ?string $key = null): string |
||
247 | { |
||
248 | if (null === $key) { |
||
249 | $key = debug_backtrace()[1]['function']; |
||
250 | } |
||
251 | |||
252 | if (!is_array($args)) { |
||
253 | $args = [$args]; |
||
254 | } |
||
255 | |||
256 | // Start with base key. |
||
257 | $cacheKey = $key; |
||
258 | |||
259 | // Loop through and determine what values to use based on type of object. |
||
260 | foreach ($args as $arg) { |
||
261 | // Zero is an acceptable value. |
||
262 | if ('' === $arg || null === $arg) { |
||
263 | continue; |
||
264 | } |
||
265 | |||
266 | $cacheKey .= $this->getCacheKeyFromArg($arg); |
||
267 | } |
||
268 | |||
269 | // Remove reserved characters. |
||
270 | return preg_replace('/[{}()\/\@\:"]/', '', $cacheKey); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get a cache-friendly string given an argument. |
||
275 | * @param mixed $arg |
||
276 | * @return string |
||
277 | */ |
||
278 | private function getCacheKeyFromArg($arg): string |
||
279 | { |
||
280 | if (is_object($arg) && method_exists($arg, 'getCacheKey')) { |
||
281 | return '.'.$arg->getCacheKey(); |
||
282 | } elseif (is_array($arg)) { |
||
283 | // Assumed to be an array of objects that can be parsed into a string. |
||
284 | return '.'.md5(implode('', $arg)); |
||
285 | } else { |
||
286 | // Assumed to be a string, number or boolean. |
||
287 | return '.'.md5((string)$arg); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Set the cache with given options. |
||
293 | * @param string $cacheKey |
||
294 | * @param mixed $value |
||
295 | * @param string $duration Valid DateInterval string. |
||
296 | * @return mixed The given $value. |
||
297 | */ |
||
298 | public function setCache(string $cacheKey, $value, string $duration = 'PT20M') |
||
299 | { |
||
300 | $cacheItem = $this->cache |
||
301 | ->getItem($cacheKey) |
||
302 | ->set($value) |
||
303 | ->expiresAfter(new DateInterval($duration)); |
||
304 | $this->cache->save($cacheItem); |
||
305 | return $value; |
||
306 | } |
||
307 | |||
308 | /******************************** |
||
309 | * DATABASE INTERACTION HELPERS * |
||
310 | ********************************/ |
||
311 | |||
312 | /** |
||
313 | * Creates WHERE conditions with date range to be put in query. |
||
314 | * @param false|int $start Unix timestamp. |
||
315 | * @param false|int $end Unix timestamp. |
||
316 | * @param false|int $offset Unix timestamp. Used for pagination, will end up replacing $end. |
||
317 | * @param string $tableAlias Alias of table FOLLOWED BY DOT. |
||
318 | * @param string $field |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getDateConditions( |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Execute a query using the projects connection, handling certain Exceptions. |
||
350 | * @param Project|string $project Project instance, database name (i.e. 'enwiki'), or slice (i.e. 's1'). |
||
351 | * @param string $sql |
||
352 | * @param array $params Parameters to bound to the prepared query. |
||
353 | * @param int|null $timeout Maximum statement time in seconds. null will use the |
||
354 | * default specified by the app.query_timeout config parameter. |
||
355 | * @return ResultStatement |
||
356 | * @throws DriverException |
||
357 | * @throws DBALException |
||
358 | * @codeCoverageIgnore |
||
359 | */ |
||
360 | public function executeProjectsQuery( |
||
361 | $project, |
||
362 | string $sql, |
||
363 | array $params = [], |
||
364 | ?int $timeout = null |
||
365 | ): ResultStatement { |
||
366 | try { |
||
367 | $timeout = $timeout ?? $this->queryTimeout; |
||
368 | $sql = "SET STATEMENT max_statement_time = $timeout FOR\n".$sql; |
||
369 | |||
370 | return $this->getProjectsConnection($project)->executeQuery($sql, $params); |
||
371 | } catch (DriverException $e) { |
||
372 | $this->handleDriverError($e, $timeout); |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Execute a query using the projects connection, handling certain Exceptions. |
||
378 | * @param QueryBuilder $qb |
||
379 | * @param int|null $timeout Maximum statement time in seconds. null will use the |
||
380 | * default specified by the app.query_timeout config parameter. |
||
381 | * @return ResultStatement |
||
382 | * @throws HttpException |
||
383 | * @throws DriverException |
||
384 | * @codeCoverageIgnore |
||
385 | */ |
||
386 | public function executeQueryBuilder(QueryBuilder $qb, ?int $timeout = null): ResultStatement |
||
387 | { |
||
388 | try { |
||
389 | $timeout = $timeout ?? $this->container->getParameter('app.query_timeout'); |
||
390 | $sql = "SET STATEMENT max_statement_time = $timeout FOR\n".$qb->getSQL(); |
||
391 | return $qb->getConnection()->executeQuery($sql, $qb->getParameters(), $qb->getParameterTypes()); |
||
392 | } catch (DriverException $e) { |
||
393 | $this->handleDriverError($e, $timeout); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Special handling of some DriverExceptions, otherwise original Exception is thrown. |
||
399 | * @param DriverException $e |
||
400 | * @param int|null $timeout Timeout value, if applicable. This is passed to the i18n message. |
||
401 | * @throws HttpException |
||
402 | * @throws DriverException |
||
403 | * @codeCoverageIgnore |
||
404 | */ |
||
405 | private function handleDriverError(DriverException $e, ?int $timeout): void |
||
419 | } |
||
420 | } |
||
421 | } |
||
422 |