Test Setup Failed
Push — master ( 8f244e...58b6ad )
by
unknown
34:03
created

LingoParser::purgeCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File holding the Lingo\LingoParser class.
5
 *
6
 * This file is part of the MediaWiki extension Lingo.
7
 *
8
 * @copyright 2011 - 2018, Stephan Gambke
9
 * @license GPL-2.0-or-later
10
 *
11
 * The Lingo extension is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by the Free
13
 * Software Foundation; either version 2 of the License, or (at your option) any
14
 * later version.
15
 *
16
 * The Lingo extension is distributed in the hope that it will be useful, but
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU General Public License along
22
 * with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 * @author Stephan Gambke
25
 *
26
 * @file
27
 * @ingroup Lingo
28
 */
29
namespace Lingo;
30
31
use DOMDocument;
32
use DOMXPath;
33
use ObjectCache;
0 ignored issues
show
Bug introduced by
The type ObjectCache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use Parser;
0 ignored issues
show
Bug introduced by
The type Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use Title;
0 ignored issues
show
Bug introduced by
The type Title was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
37
/**
38
 * This class parses the given text and enriches it with definitions for defined
39
 * terms.
40
 *
41
 * Contains a static function to initiate the parsing.
42
 *
43
 * @ingroup Lingo
44
 */
45
class LingoParser {
46
47
	const WORD_VALUE = 0;
48
	const WORD_OFFSET = 1;
49
50
	private $mLingoTree = null;
51
52
	/**
53
	 * @var Backend
54
	 */
55
	private $mLingoBackend = null;
56
	private static $parserSingleton = null;
57
58
	// Api params passed in from ApiMakeParserOptions Hook
59
	private $mApiParams = null;
60
61
	// The RegEx to split a chunk of text into words
62
	public $regex = null;
63
64
	/**
65 8
	 * Lingo\LingoParser constructor.
66
	 * @param MessageLog|null $messages
67
	 */
68 8
	public function __construct( MessageLog &$messages = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $messages is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
	public function __construct( /** @scrutinizer ignore-unused */ MessageLog &$messages = null ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69 8
		// The RegEx to split a chunk of text into words
70
		// Words are: placeholders for stripped items, sequences of letters and numbers, single characters that are neither letter nor number
71
		$this->regex = '/' . preg_quote( Parser::MARKER_PREFIX, '/' ) . '.*?' . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '|[\p{L}\p{N}]+|[^\p{L}\p{N}]/u';
72
	}
73
74
	/**
75
	 * @param Parser $mwParser
76 7
	 *
77 7
	 * @return Boolean
78 3
	 */
79
	public function parse( $mwParser ) {
80
		if ( $this->shouldParse( $mwParser ) ) {
81 7
			$this->realParse( $mwParser );
82
		}
83
84
		return true;
85
	}
86
87
	/**
88
	 * @return LingoParser
89
	 * @since 2.0.1
90
	 */
91
	public static function getInstance() {
92
		if ( !self::$parserSingleton ) {
93
			self::$parserSingleton = new LingoParser();
94
95
		}
96
97
		return self::$parserSingleton;
98
	}
99
100
	/**
101
	 * @return string
102
	 */
103
	private function getCacheKey() {
104
		// FIXME: If Lingo ever stores the glossary tree per user, then the cache key also needs to include the user id (see T163608)
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "If Lingo ever stores the glossary tree per user, then the cache key also needs to include the user id (see T163608"
Loading history...
105
		return ObjectCache::getLocalClusterInstance()->makeKey( 'ext', 'lingo', 'lingotree', Tree::TREE_VERSION, get_class( $this->getBackend() ) );
106
	}
107
108
	/**
109
	 * @return Backend the backend used by the parser
110
	 * @throws \MWException
111
	 */
112
	public function getBackend() {
113
		if ( $this->mLingoBackend === null ) {
114
			throw new \MWException( 'No Lingo backend available!' );
0 ignored issues
show
Bug introduced by
The type MWException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
115
		}
116
117
		return $this->mLingoBackend;
118
	}
119
120
	/**
121
	 * Returns the list of terms in the glossary
122
	 *
123
	 * @return array an array mapping terms (keys) to descriptions (values)
124
	 */
125
	public function getLingoArray() {
126
		return $this->getLingoTree()->getTermList();
127
	}
128
129
	/**
130
	 * Returns the list of terms in the glossary as a Lingo\Tree
131
	 *
132
	 * @return Tree a Lingo\Tree mapping terms (keys) to descriptions (values)
133
	 */
134
	public function getLingoTree() {
135
		// build glossary array only once per request
136
		if ( !$this->mLingoTree ) {
137
138
			// use cache if enabled
139
			if ( $this->getBackend()->useCache() ) {
140
141
				// Try cache first
142
				global $wgexLingoCacheType;
143
				$cache = ( $wgexLingoCacheType !== null ) ? wfGetCache( $wgexLingoCacheType ) : wfGetMainCache();
0 ignored issues
show
Bug introduced by
The function wfGetMainCache was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
				$cache = ( $wgexLingoCacheType !== null ) ? wfGetCache( $wgexLingoCacheType ) : /** @scrutinizer ignore-call */ wfGetMainCache();
Loading history...
Bug introduced by
The function wfGetCache was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
				$cache = ( $wgexLingoCacheType !== null ) ? /** @scrutinizer ignore-call */ wfGetCache( $wgexLingoCacheType ) : wfGetMainCache();
Loading history...
144
				$cachekey = $this->getCacheKey();
145
				$cachedLingoTree = $cache->get( $cachekey );
146
147
				// cache hit?
148
				if ( $cachedLingoTree !== false && $cachedLingoTree !== null ) {
149
150
					wfDebug( "Cache hit: Got lingo tree from cache.\n" );
0 ignored issues
show
Bug introduced by
The function wfDebug was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
					/** @scrutinizer ignore-call */ 
151
     wfDebug( "Cache hit: Got lingo tree from cache.\n" );
Loading history...
151
					$this->mLingoTree = &$cachedLingoTree;
152
153
					wfDebug( "Re-cached lingo tree.\n" );
154
				} else {
155
156
					wfDebug( "Cache miss: Lingo tree not found in cache.\n" );
157
					$this->mLingoTree =& $this->buildLingo();
158
					wfDebug( "Cached lingo tree.\n" );
159
				}
160
161
				// Keep for one month
162
				// Limiting the cache validity will allow to purge stale cache
163
				// entries inserted by older versions after one month
164
				$cache->set( $cachekey, $this->mLingoTree, 60 * 60 * 24 * 30 );
165
166
			} else {
167
				wfDebug( "Caching of lingo tree disabled.\n" );
168
				$this->mLingoTree =& $this->buildLingo();
169
			}
170
171
		}
172
173
		return $this->mLingoTree;
174
	}
175
176
	/**
177
	 * @return Tree
178
	 */
179
	protected function &buildLingo() {
180
		$lingoTree = new Tree();
181
		$backend = &$this->mLingoBackend;
182
183
		// assemble the result array
184
		while ( $elementData = $backend->next() ) {
185
			$lingoTree->addTerm( $elementData[ Element::ELEMENT_TERM ], $elementData );
186
		}
187
188
		return $lingoTree;
189
	}
190
191
	/**
192
	 * Parses the given text and enriches applicable terms
193
	 *
194
	 * This method currently only recognizes terms consisting of max one word
195
	 *
196
	 * @param Parser $parser
197 3
	 *
198 3
	 * @return Boolean
199
	 */
200 3
	protected function realParse( &$parser ) {
201 3
		// Parse text identical to options used in includes/api/ApiParse.php
202
		$params = $this->mApiParams;
203
		$text = is_null( $params ) ? $parser->getOutput()->getText() : $parser->getOutput()->getText( [
204
			'allowTOC' => !$params['disabletoc'],
205
			'enableSectionEditLinks' => !$params['disableeditsection'],
206
			'wrapperDivClass' => $params['wrapoutputclass'],
207
			'deduplicateStyles' => !$params['disablestylededuplication'],
208
		] );
209
210
		if ( $text === null || $text === '' ) {
211
			return true;
212
		}
213
214
		// Get array of terms
215
		$glossary = $this->getLingoTree();
216
217
		if ( $glossary == null ) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
218
			return true;
219
		}
220
221
		// Parse HTML from page
222
		\MediaWiki\suppressWarnings();
0 ignored issues
show
Bug introduced by
The function suppressWarnings was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

222
		/** @scrutinizer ignore-call */ 
223
  \MediaWiki\suppressWarnings();
Loading history...
223
224
		$doc = new DOMDocument( '1.0', 'utf-8' );
225
		$doc->loadHTML( '<html><head><meta http-equiv="content-type" content="charset=utf-8"/></head><body>' . $text . '</body></html>' );
226
227
		\MediaWiki\restoreWarnings();
0 ignored issues
show
Bug introduced by
The function restoreWarnings was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
		/** @scrutinizer ignore-call */ 
228
  \MediaWiki\restoreWarnings();
Loading history...
228
229
		// Find all text in HTML.
230
		$xpath = new DOMXPath( $doc );
231
		$textElements = $xpath->query(
232
			"//*[not(ancestor-or-self::*[@class='noglossary'] or ancestor-or-self::a)][text()!=' ']/text()"
233
		);
234
235
		// Iterate all HTML text matches
236
		$numberOfTextElements = $textElements->length;
237
238
		$definitions = [];
239
240
		for ( $textElementIndex = 0; $textElementIndex < $numberOfTextElements; $textElementIndex++ ) {
241
			$textElement = $textElements->item( $textElementIndex );
242
243
			if ( strlen( $textElement->nodeValue ) < $glossary->getMinTermLength() ) {
244
				continue;
245
			}
246
247
			$matches = [];
248
			preg_match_all(
249
				$this->regex,
250
				$textElement->nodeValue,
251
				$matches,
252
				PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER
253
			);
254
255
			if ( count( $matches ) === 0 || count( $matches[ 0 ] ) === 0 ) {
256
				continue;
257
			}
258
259
			$wordDescriptors = &$matches[ 0 ]; // See __construct() for definition of "word"
260
			$numberOfWordDescriptors = count( $wordDescriptors );
261
262
			$parentNode = &$textElement->parentNode;
263
264
			$wordDescriptorIndex = 0;
265
			$changedElem = false;
266
267
			while ( $wordDescriptorIndex < $numberOfWordDescriptors ) {
268
269
				/** @var \Lingo\Element $definition */
270
				list( $skippedWords, $usedWords, $definition ) =
271
					$glossary->findNextTerm( $wordDescriptors, $wordDescriptorIndex, $numberOfWordDescriptors );
272
273
				if ( $usedWords > 0 ) { // found a term
274
275
					if ( $skippedWords > 0 ) { // skipped some text, insert it as is
276
277
						$start = $wordDescriptors[ $wordDescriptorIndex ][ self::WORD_OFFSET ];
278
						$length = $wordDescriptors[ $wordDescriptorIndex + $skippedWords ][ self::WORD_OFFSET ] - $start;
279
280
						$parentNode->insertBefore(
281
							$doc->createTextNode(
282
								substr( $textElement->nodeValue, $start, $length )
283
							),
284
							$textElement
285
						);
286
					}
287
288
					$parentNode->insertBefore( $definition->getFormattedTerm( $doc ), $textElement );
289
290
					$definitions[ $definition->getId() ] = $definition->getFormattedDefinitions();
291
292
					$changedElem = true;
293
294
				} else { // did not find any term, just use the rest of the text
295
296
					// If we found no term now and no term before, there was no
297
					// term in the whole element. Might as well not change the
298
					// element at all.
299
300
					// Only change element if found term before
301
					if ( $changedElem === true ) {
302
303
						$start = $wordDescriptors[ $wordDescriptorIndex ][ self::WORD_OFFSET ];
304
305
						$parentNode->insertBefore(
306
							$doc->createTextNode(
307
								substr( $textElement->nodeValue, $start )
308
							),
309
							$textElement
310
						);
311
312
					}
313
314
					// In principle superfluous, the loop would run out anyway. Might save a bit of time.
315
					break;
316
				}
317
318
				$wordDescriptorIndex += $usedWords + $skippedWords;
319
			}
320
321
			if ( $changedElem ) {
322
				$parentNode->removeChild( $textElement );
323
			}
324
		}
325
326
		if ( count( $definitions ) > 0 ) {
327
328
			$this->loadModules( $parser );
329
330
			// U - Ungreedy, D - dollar matches only end of string, s - dot matches newlines
331
			$text = preg_replace( '%(^.*<body>)|(</body>.*$)%UDs', '', $doc->saveHTML() );
332
			$text .= $parser->recursiveTagParseFully( implode( $definitions ) );
333
334
			$parser->getOutput()->setText( $text );
335
		}
336
337
		return true;
338
	}
339
340
	/**
341
	 * @param Parser $parser
342
	 */
343
	protected function loadModules( &$parser ) {
344
		global $wgOut;
345
346
		$parserOutput = $parser->getOutput();
347
348
		// load scripts
349
		$parserOutput->addModules( 'ext.Lingo.Scripts' );
350
351
		if ( !$wgOut->isArticle() ) {
352
			$wgOut->addModules( 'ext.Lingo.Scripts' );
353
		}
354
355
		// load styles
356
		$parserOutput->addModuleStyles( 'ext.Lingo.Styles' );
357
358
		if ( !$wgOut->isArticle() ) {
359
			$wgOut->addModuleStyles( 'ext.Lingo.Styles' );
360
		}
361
	}
362
363
	/**
364
	 * Purges the lingo tree from the cache.
365
	 *
366
	 * @deprecated 2.0.2
367
	 */
368
	public static function purgeCache() {
369
		self::getInstance()->purgeGlossaryFromCache();
370
	}
371
372
	/**
373
	 * Purges the lingo tree from the cache.
374
	 *
375
	 * @since 2.0.2
376
	 */
377
	public function purgeGlossaryFromCache() {
378
		global $wgexLingoCacheType;
379
		$cache = ( $wgexLingoCacheType !== null ) ? wfGetCache( $wgexLingoCacheType ) : wfGetMainCache();
0 ignored issues
show
Bug introduced by
The function wfGetCache was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

379
		$cache = ( $wgexLingoCacheType !== null ) ? /** @scrutinizer ignore-call */ wfGetCache( $wgexLingoCacheType ) : wfGetMainCache();
Loading history...
Bug introduced by
The function wfGetMainCache was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

379
		$cache = ( $wgexLingoCacheType !== null ) ? wfGetCache( $wgexLingoCacheType ) : /** @scrutinizer ignore-call */ wfGetMainCache();
Loading history...
380
		$cache->delete( $this->getCacheKey() );
381
	}
382
383
	/**
384
	 * @since 2.0.1
385
	 * @param Backend $backend
386 7
	 */
387 7
	public function setBackend( Backend $backend ) {
388
		$this->mLingoBackend = $backend;
389 7
		$backend->setLingoParser( $this );
390 1
	}
391
392
	/**
393 6
	 * Set parser options from API
394 1
	 *
395
	 * @param array $params
396
	 */
397 5
	public function setApiParams( array $params ) {
398
		$this->mApiParams = $params;
399 5
	}
400 1
401
	/**
402
	 * @param Parser $parser
403 4
	 * @return bool
404
	 */
405 4
	protected function shouldParse( &$parser ) {
406 1
		global $wgexLingoUseNamespaces;
407
408
		if ( !( $parser instanceof Parser ) ) {
409 3
			return false;
410
		}
411
412
		if ( isset( $parser->mDoubleUnderscores[ 'noglossary' ] ) ) { // __NOGLOSSARY__ found in wikitext
413
			return false;
414
		}
415
416
		$title = $parser->getTitle();
417
418
		if ( !( $title instanceof Title ) ) {
419
			return false;
420
		}
421
422
		$namespace = $title->getNamespace();
423
424
		if ( isset( $wgexLingoUseNamespaces[ $namespace ] ) && $wgexLingoUseNamespaces[ $namespace ] === false ) {
425
			return false;
426
		};
427
428
		return true;
429
	}
430
}
431