summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Lang/JsonContentsFileReader.php
blob: 18755f60f3dc8ba07d4843996d5a14a9899d9094 (plain)
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
<?php

namespace SMW\Lang;

use Onoi\Cache\Cache;
use Onoi\Cache\NullCache;
use RuntimeException;
use SMW\Utils\ErrorCodeFormatter;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class JsonContentsFileReader {

	/**
	 * @var array
	 */
	private static $contents = [];

	/**
	 * @var string
	 */
	private $languageFileDir = '';

	/**
	 * @var Cache
	 */
	private $cache;

	/**
	 * @var boolean
	 */
	private $skipCache = false;

	/**
	 * @var integer
	 */
	private $ttl = 604800; // 7 * 24 * 3600

	/**
	 * @since 2.5
	 *
	 * @param Cache|null $cache
	 * @param string $languageFileDir
	 */
	public function __construct( Cache $cache = null, $languageFileDir = '' ) {
		$this->cache = $cache;
		$this->languageFileDir = $languageFileDir;

		if ( $this->cache === null ) {
			$this->cache = new NullCache();
		}

		if ( $this->languageFileDir === '' ) {
			$this->languageFileDir = $GLOBALS['smwgExtraneousLanguageFileDir'];
		}
	}

	/**
	 * @since 2.5
	 */
	public static function clear() {
		self::$contents = [];
	}

	/**
	 * @since 2.5
	 */
	public function skipCache() {
		$this->skipCache = true;
	}

	/**
	 * @since 1.2.0
	 *
	 * @return integer
	 */
	public function getFileModificationTime( $languageCode ) {
		return filemtime( $this->getLanguageFile( $languageCode ) );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $languageCode
	 * @param boolean $readFromFile
	 *
	 * @return boolean
	 */
	public function canReadByLanguageCode( $languageCode ) {

		$canReadByLanguageCode = '';

		try {
			$canReadByLanguageCode = $this->getLanguageFile( $languageCode );
		} catch ( \Exception $e ) {
			$canReadByLanguageCode = '';
		}

		return $canReadByLanguageCode !== '';
	}

	/**
	 * @since 2.5
	 *
	 * @param string $languageCode
	 * @param array $contents
	 */
	public function writeByLanguageCode( $languageCode, $contents ) {

		$languageCode = strtolower( trim( $languageCode ) );

		file_put_contents(
			$this->getLanguageFile( $languageCode ),
			json_encode( $contents, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
		);
	}

	/**
	 * @since 2.5
	 *
	 * @param string $languageCode
	 * @param boolean $readFromFile
	 *
	 * @return array
	 * @throws RuntimeException
	 */
	public function readByLanguageCode( $languageCode, $readFromFile = false ) {

		$languageCode = strtolower( trim( $languageCode ) );

		if ( !$readFromFile && isset( self::$contents[$languageCode] ) ) {
			return self::$contents[$languageCode];
		}

		$cacheKey = smwfCacheKey(
			'smw:lang',
			[
				$languageCode,
				$this->getFileModificationTime( $languageCode ),
				$this->ttl
			]
		);

		if ( !$readFromFile && !$this->skipCache && !isset( self::$contents[$languageCode] ) && $this->cache->contains( $cacheKey ) ) {
			self::$contents[$languageCode] = $this->cache->fetch( $cacheKey );
		}

		if ( $readFromFile || !isset( self::$contents[$languageCode] ) ) {
			self::$contents[$languageCode] = $this->readJSONFile( $languageCode, $cacheKey );
		}

		return self::$contents[$languageCode];
	}

	protected function readJSONFile( $languageCode, $cacheKey ) {

		$contents = json_decode(
			file_get_contents( $this->getLanguageFile( $languageCode ) ),
			true
		);

		if ( $contents !== null && json_last_error() === JSON_ERROR_NONE ) {
			$this->cache->save( $cacheKey, $contents, $this->ttl );
			return $contents;
		}

		throw new RuntimeException( ErrorCodeFormatter::getMessageFromJsonErrorCode( json_last_error() ) );
	}

	private function getLanguageFile( $languageCode ) {

		$file = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $this->languageFileDir . '/' . $languageCode . '.json' );

		if ( is_readable( $file ) ) {
			return $file;
		}

		throw new RuntimeException( "Expected a {$file} file" );
	}

}