CacheHelper::newFromOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 21
ccs 14
cts 14
cp 1
crap 1
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace SUC;
4
5
use ObjectCache;
6
use Onoi\Cache\CacheFactory as OnoiCacheFactory;
7
use Onoi\BlobStore\BlobStore;
8
use ApiParse;
9
use ApiBase;
10
use Title;
11
use Revision;
12
13
/**
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class CacheHelper {
20
21
	const VERSION = '0.1';
22
23
	/**
24
	 * @var BlobStore
25
	 */
26
	private $blobStore;
27
28
	/**
29
	 * @var Options
30
	 */
31
	private $options;
32
33
	/**
34
	 * @since 1.0
35
	 *
36
	 * @param BlobStore $blobStore
37
	 * @param Options $options
38
	 */
39 5
	public function __construct( BlobStore $blobStore, Options $options ) {
40 5
		$this->blobStore = $blobStore;
41 5
		$this->options = $options;
42 5
	}
43
44
	/**
45
	 * @since 1.0
46
	 *
47
	 * @param Options $options
48
	 *
49
	 * @return CacheHelper
50
	 */
51 1
	public static function newFromOptions( Options $options ) {
52
53 1
		$cache = OnoiCacheFactory::getInstance()->newMediaWikiCache(
54 1
			ObjectCache::getInstance( $options->get( 'backendParserCacheType' ) )
55 1
		);
56
57 1
		$blobStore = new BlobStore(
58 1
			'summarycards:store',
59
			$cache
60 1
		);
61
62 1
		$blobStore->setNamespacePrefix(
63 1
			$options->get( 'cachePrefix' )
64 1
		);
65
66 1
		$blobStore->setExpiryInSeconds(
67 1
			$options->get( 'backendParserCacheLifetime' )
68 1
		);
69
70 1
		return new self( $blobStore, $options );
71
	}
72
73
	/**
74
	 * @since 1.0
75
	 *
76
	 * @param Title $title
77
	 *
78
	 * @return string
79
	 */
80 3
	public function getHashFrom( Title $title = null ) {
81
82 3
		$enabledNamespaceWithTemplate = $this->options->get( 'enabledNamespaceWithTemplate' );
83
84 3
		if ( $title === null || !isset( $enabledNamespaceWithTemplate[$title->getNamespace()] ) ) {
85 2
			return '';
86
		}
87
88 1
		return md5(
89 1
			self::VERSION .
90 1
			$title->getPrefixedDBKey() .
91 1
			$enabledNamespaceWithTemplate[$title->getNamespace()] .
92 1
			$this->options->get( 'backendParserCacheLifetime' )
93 1
		);
94
	}
95
96
	/**
97
	 * @since 1.0
98
	 *
99
	 * @param string $title
100
	 *
101
	 * @return Title|null
102
	 */
103 1
	public function newTitleFromText( $title ) {
104
105 1
		$title = Title::newFromText( $title );
106
107 1
		if ( $title === null || $title->hasFragment() ) {
108
			return $title;
109
		}
110
111
		# Get the article text
112 1
		$rev = Revision::newFromTitle( $title, false, Revision::READ_LATEST );
113
114 1
		if ( !is_object( $rev ) ) {
115 1
			return $title;
116
		}
117
118
		$content = $rev->getContent();
119
		# Does the redirect point to the source?
120
		# Or is it a broken self-redirect, usually caused by namespace collisions?
121
		return $content && $content->getRedirectTarget() !== null ? $content->getRedirectTarget() : $title;
122
	}
123
124
	/**
125
	 * @since 1.0
126
	 *
127
	 * @return BlobStore
128
	 */
129 1
	public function getBlobStore() {
130 1
		return $this->blobStore;
131
	}
132
133
	/**
134
	 * @since 1.0
135
	 *
136
	 * @param Title $title
137
	 */
138 1
	public function invalidateCache( Title $title ) {
139 1
		$this->blobStore->delete( $this->getHashFrom( $title ) );
140 1
		return true;
141
	}
142
143
}
144