summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Parser/RecursiveTextProcessor.php
blob: 52973d40afae936357a66a9cd869ca3925bb827e (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php

namespace SMW\Parser;

use Parser;
use ParserOptions;
use ParserOutput;
use RuntimeException;
use SMW\Localizer;
use SMW\ParserData;
use Title;

/**
 * @private
 *
 * Helper class in processing content that requires to be parsed internally and
 * recursively mostly in connection with templates.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class RecursiveTextProcessor {

	/**
	 * @see Special:ExpandTemplates
	 * @var int Maximum size in bytes to include. 50MB allows fixing those huge pages
	 */
	const MAX_INCLUDE_SIZE = 50000000;

	/**
	 * @var Parser
	 */
	private $parser;

	/**
	 * Incremented while expanding templates inserted during printout; stop
	 * expansion at some point
	 *
	 * @var integer
	 */
	private $recursionDepth = 0;

	/**
	 * @var integer
	 */
	private $maxRecursionDepth = 2;

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

	/**
	 * @var integer
	 */
	private $uniqid;

	/**
	 * @var string
	 */
	private $error = [];

	/**
	 * @since 3.0
	 *
	 * @param Parser $parser|null
	 */
	public function __construct( Parser $parser = null ) {
		$this->parser = $parser;

		if ( $this->parser === null ) {
			$this->parser = $GLOBALS['wgParser'];
		}
	}

	/**
	 * @since 3.0
	 *
	 * @return Parser
	 */
	public function getParser() {
		return $this->parser;
	}

	/**
	 * @since 3.0
	 *
	 * @return []
	 */
	public function getError() {
		return $this->error;
	}

	/**
	 * Track recursive processing
	 *
	 * @since 3.0
	 *
	 * @param string|integer|null $uniqid
	 */
	public function uniqid( $uniqid = null ) {

		if ( $uniqid === null ) {
			$uniqid = uniqid();
		}

		$this->uniqid = $uniqid;
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $maxRecursionDepth
	 */
	public function setMaxRecursionDepth( $maxRecursionDepth ) {
		$this->maxRecursionDepth = $maxRecursionDepth;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $transcludeAnnotation
	 */
	public function transcludeAnnotation( $transcludeAnnotation ) {

		if ( $this->parser->getOutput() === null || $transcludeAnnotation === true ) {
			return;
		}

		if ( $this->uniqid === null ) {
			throw new RuntimeException( "Expected a uniqid and not null." );
		}

		$parserOutput = $this->parser->getOutput();
		$track = $parserOutput->getExtensionData( ParserData::ANNOTATION_BLOCK );

		if ( $track === null ) {
			$track = [];
		}

		// Track each embedded #ask process to ensure to remove
		// blocks on the correct recursive iteration (e.g Page A containing
		// #ask is transcluded in Page B using a #ask -> is embedded ...
		// etc.)
		$track[$this->uniqid] = true;

		$parserOutput->setExtensionData( ParserData::ANNOTATION_BLOCK, $track );
	}

	/**
	 * @since 3.0
	 */
	public function releaseAnnotationBlock() {

		if ( $this->parser->getOutput() === null ) {
			return;
		}

		$parserOutput = $this->parser->getOutput();
		$track = $parserOutput->getExtensionData( ParserData::ANNOTATION_BLOCK );

		if ( $track !== [] ) {
			unset( $track[$this->uniqid] );
		}

		// No recursive tracks left, set it to false
		if ( $track === [] ) {
			$track = false;
		}

		$parserOutput->setExtensionData( ParserData::ANNOTATION_BLOCK, $track );
	}

	/**
	 * @since 3.0
	 */
	public function releaseAnyAnnotationBlock() {
		if ( $this->parser->getOutput() !== null ) {
			$this->parser->getOutput()->setExtensionData( ParserData::ANNOTATION_BLOCK, false );
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $recursiveAnnotation
	 */
	public function setRecursiveAnnotation( $recursiveAnnotation ) {
		$this->recursiveAnnotation = (bool)$recursiveAnnotation;
	}

	/**
	 * @since 3.0
	 *
	 * @param ParserData $parserData
	 */
	public function copyData( ParserData $parserData ) {
		if ( $this->recursiveAnnotation ) {
			$parserData->importFromParserOutput( $this->parser->getOutput() );
		}
	}

	/**
	 * @see Special:ExpandTemplates
	 * @since 3.0
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public function expandTemplates( $text ) {

		if ( $this->parser === null ) {
			throw new RuntimeException( 'Missing a parser instance!' );
		}

		$options = $this->parser->getOptions();

		if ( !$options instanceof ParserOptions ) {
			$options = new ParserOptions();
			$options->setRemoveComments( true );
			$options->setTidy( true );
			$options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
		}

		$title = $this->parser->getTitle();

		if ( !$title instanceof Title ) {
			$title = $GLOBALS['wgTitle'];

			if ( !$title instanceof Title ) {
				$title = Title::newFromText( 'UNKNOWN_TITLE' );
			}
		}

		$text = $this->parser->preprocess( $text, $title, $options );

		$text = str_replace(
			[ '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_', '<nowiki>', '</nowiki>' ],
			'',
			$text
		);

		return $text;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public function recursivePreprocess( $text ) {

		// not during parsing, no preprocessing needed, still protect the result
		if ( $this->parser === null || !$this->parser->getTitle() instanceof Title || !$this->parser->getOptions() instanceof ParserOptions ) {
			return $this->recursiveAnnotation ? $text : '[[SMW::off]]' . $text . '[[SMW::on]]';
		}

		$this->recursionDepth++;

		// restrict recursion
		if ( $this->recursionDepth <= $this->maxRecursionDepth && $this->recursiveAnnotation ) {
			$text =  $this->parser->recursivePreprocess( $text );
		} elseif ( $this->recursionDepth <= $this->maxRecursionDepth ) {
			$text = '[[SMW::off]]' . $this->parser->replaceVariables( $text ) . '[[SMW::on]]';
		} else {
			$this->error = [ 'smw-parser-recursion-level-exceeded', $this->maxRecursionDepth ];
			$text = '';
		}

		// During a block request remove any categories from the text since we
		// cannot block the annotation during a parse, this ensures that
		// categories don't appear in the source text and hereby in any successive
		// parse
		$this->pruneCategory( $text );

		$this->recursionDepth--;

		return $text;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public function recursiveTagParse( $text ) {

		if ( $this->parser === null ) {
			throw new RuntimeException( 'Missing a parser instance!' );
		}

		$this->recursionDepth++;
		$isValid = $this->parser->getTitle() instanceof Title && $this->parser->getOptions() instanceof ParserOptions;

		if ( $this->recursionDepth <= $this->maxRecursionDepth && $isValid ) {
				$text = $this->parser->recursiveTagParse( $text );
		} elseif ( $this->recursionDepth <= $this->maxRecursionDepth ) {
			$title = $GLOBALS['wgTitle'];

			if ( $title === null ) {
				$title = Title::newFromText( 'UNKNOWN_TITLE' );
			}

			$popt = new ParserOptions();

			// FIXME: Remove the if block once compatibility with MW <1.31 is dropped
			if ( ! defined( '\ParserOutput::SUPPORTS_STATELESS_TRANSFORMS' ) || \ParserOutput::SUPPORTS_STATELESS_TRANSFORMS !== 1 ) {
				$popt->setEditSection( false );
			}
			$parserOutput = $this->parser->parse( $text . '__NOTOC__', $title, $popt );

			// Maybe better to use Parser::recursiveTagParseFully ??

			/// NOTE: as of MW 1.14SVN, there is apparently no better way to hide the TOC
			\SMWOutputs::requireFromParserOutput( $parserOutput );
			$text = $parserOutput->getText( [ 'enableSectionEditLinks' => false ] );
		} else {
			$this->error = [ 'smw-parser-recursion-level-exceeded', $this->maxRecursionDepth ];
			$text = '';
		}

		$this->recursionDepth--;

		return $text;
	}

	private function pruneCategory( &$text ) {

		if ( $this->parser->getOutput() === null ) {
			return;
		}

		$parserOutput = $this->parser->getOutput();

		if ( ( $track = $parserOutput->getExtensionData( ParserData::ANNOTATION_BLOCK ) ) === false ) {
			return;
		}

		// Content language dep. category name
		$category = Localizer::getInstance()->getNamespaceTextById(
			NS_CATEGORY
		);

		if ( isset( $track[$this->uniqid] ) ) {
			$text = preg_replace(
				"/\[\[(Category|{$category}):(.*)\]\]/U",
				'',
				$text
			);
		}
	}

}