Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
6 / 8 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CacheKeyHelper | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
6.10 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getKeyForPage | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Page; |
| 8 | |
| 9 | use LogicException; |
| 10 | use MediaWiki\DAO\WikiAwareEntity; |
| 11 | use Wikimedia\Parsoid\Core\LinkTarget; |
| 12 | |
| 13 | /** |
| 14 | * Helper class for mapping page value objects to a string key. |
| 15 | * |
| 16 | * This logic should not reside in a class like PageStoreRecord or Title, because: |
| 17 | * |
| 18 | * 1. These value object should not contain care about caching in other classes. |
| 19 | * |
| 20 | * 2. We type against interfaces like PageReference and LinkTarget, and for |
| 21 | * caching to work, all implementations would have to derive cache keys in the |
| 22 | * exact same way. Otherwise, caches will break when different implementations |
| 23 | * are passed to the same cache. |
| 24 | * |
| 25 | * Furthermore, the logic for deriving cache keys also should not reside in a service class, |
| 26 | * because there must only ever be one implementation, it must not depend on configuration, |
| 27 | * and it may never change. |
| 28 | * |
| 29 | * @since 1.37 |
| 30 | * @ingroup Page |
| 31 | */ |
| 32 | abstract class CacheKeyHelper { |
| 33 | |
| 34 | /** |
| 35 | * @return never |
| 36 | */ |
| 37 | private function __construct() { |
| 38 | throw new LogicException( 'May not be instantiated' ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns a stable key for identifying the given page in a cache. |
| 43 | * The return value takes into account the page's DB key, namespace and |
| 44 | * wiki ID or interwiki prefix. It is suitable for use with |
| 45 | * BagOStuff::makeKey and BagOStuff::makeGlobalKey. |
| 46 | * |
| 47 | * @note The key that this method returns for a given page should never change. |
| 48 | * Changing the return value may have sever impact on deployment, as it would |
| 49 | * cause caches that rely on this method to become effectively "cold" (empty). |
| 50 | * |
| 51 | * @param LinkTarget|PageReference $page |
| 52 | * @return string A string suitable for identifying the given page. Callers |
| 53 | * should not attempt to extract information from this value, it |
| 54 | * should be treated as opaque (but stable). |
| 55 | */ |
| 56 | public static function getKeyForPage( $page ): string { |
| 57 | $prefix = 'ns' . $page->getNamespace(); |
| 58 | |
| 59 | if ( $page instanceof WikiAwareEntity && $page->getWikiId() !== WikiAwareEntity::LOCAL ) { |
| 60 | $prefix .= '@id@' . $page->getWikiId(); |
| 61 | } elseif ( $page instanceof LinkTarget && $page->getInterwiki() !== '' ) { |
| 62 | $prefix .= '@iw@' . $page->getInterwiki(); |
| 63 | } |
| 64 | |
| 65 | return $prefix . ':' . $page->getDBkey(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** @deprecated class alias since 1.45 */ |
| 70 | class_alias( CacheKeyHelper::class, 'MediaWiki\Cache\CacheKeyHelper' ); |