summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/ContentParser.php
blob: 3fda57a91a0ecac60232817794c93823681d8647 (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
<?php

namespace SMW;

use Parser;
use ParserOptions;
use Revision;
use Title;
use User;

/**
 * Fetches the ParserOutput either by parsing an invoked text component,
 * re-parsing a text revision, or accessing the ContentHandler to generate a
 * ParserOutput object
 *
 * @ingroup SMW
 *
 * @licence GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class ContentParser {

	/** @var Title */
	protected $title;

	/** @var Parser */
	protected $parser = null;

	/** @var ParserOutput */
	protected $parserOutput = null;

	/** @var Revision */
	protected $revision = null;

	/** @var array */
	protected $errors = [];

	/**
	 * @var boolean
	 */
	private $enabledToUseContentHandler = true;

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

	/**
	 * @note Injecting new Parser() alone will not yield an expected result and
	 * doing new Parser( $GLOBALS['wgParserConf'] brings no benefits therefore
	 * we stick to the GLOBAL as fallback if no parser is injected.
	 *
	 * @since 1.9
	 *
	 * @param Title $title
	 * @param Parser|null $parser
	 */
	public function __construct( Title $title, Parser $parser = null ) {
		$this->title  = $title;
		$this->parser = $parser;

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

	/**
	 * @since 2.3
	 *
	 * @return Parser $parser
	 */
	public function setParser( Parser $parser ) {
		$this->parser = $parser;
	}

	/**
	 * @since 1.9.1
	 *
	 * @return ContentParser
	 */
	public function setRevision( Revision $revision = null ) {
		$this->revision = $revision;
		return $this;
	}

	/**
	 * @bug 62856 and #212
	 *
	 * @since 2.0
	 */
	public function forceToUseParser() {
		$this->enabledToUseContentHandler = false;
	}

	/**
	 * @since 1.9
	 *
	 * @return Title
	 */
	public function getTitle() {
		return $this->title;
	}

	/**
	 * @since 1.9
	 *
	 * @return ParserOutput|null
	 */
	public function getOutput() {
		return $this->parserOutput;
	}

	/**
	 * @since 1.9
	 *
	 * @return array
	 */
	public function getErrors() {
		return $this->errors;
	}

	public function skipInTextAnnotationParser() {
		return $this->skipInTextAnnotationParser = true;
	}

	/**
	 * Generates or fetches the ParserOutput object from an appropriate source
	 *
	 * @since 1.9
	 *
	 * @param string|null $text
	 *
	 * @return ContentParser
	 */
	public function parse( $text = null ) {

		if ( $text !== null ) {
			return $this->parseText( $text );
		}

		if ( $this->hasContentHandler() && $this->enabledToUseContentHandler ) {
			return $this->fetchFromContent();
		}

		return $this->fetchFromParser();
	}

	protected function parseText( $text ) {

		$this->parserOutput = $this->parser->parse(
			$text,
			$this->getTitle(),
			$this->makeParserOptions()
		);

		return $this;
	}

	/**
	 * @note Revision ID must be passed to the parser output to
	 * get revision variables correct
	 *
	 * @note If no content is available create an empty object
	 */
	protected function fetchFromContent() {

		if ( $this->getRevision() === null ) {
			return $this->msgForNullRevision();
		}

		$content = $this->getRevision()->getContent( Revision::RAW );

		if ( !$content ) {
			$content = $this->getRevision()->getContentHandler()->makeEmptyContent();
		}

		$this->parserOutput = $content->getParserOutput(
			$this->getTitle(),
			$this->getRevision()->getId(),
			null,
			true
		);

		return $this;
	}

	protected function fetchFromParser() {

		if ( $this->getRevision() === null ) {
			return $this->msgForNullRevision();
		}

		$this->parserOutput = $this->parser->parse(
			$this->getRevision()->getText(),
			$this->getTitle(),
			$this->makeParserOptions(),
			true,
			true,
			$this->getRevision()->getID()
		);

		return $this;
	}

	protected function msgForNullRevision( $fname = __METHOD__ ) {
		$this->errors = [ $fname . " No revision available for {$this->getTitle()->getPrefixedDBkey()}" ];
		return $this;
	}

	protected function makeParserOptions() {

		$user = null;

		if ( $this->getRevision() !== null ) {
			$user = User::newFromId( $this->getRevision()->getUser() );
		}

		$parserOptions = new ParserOptions( $user );

		// Use the InterfaceMessage marker to skip InTextAnnotationParser
		// processing
		$parserOptions->setInterfaceMessage( $this->skipInTextAnnotationParser );

		return $parserOptions;
	}

	protected function getRevision() {

		if ( $this->revision instanceof Revision ) {
			return $this->revision;
		}

		// Revision::READ_NORMAL is not specified in MW 1.19
		if ( defined( 'Revision::READ_NORMAL' ) ) {
			$this->revision = Revision::newFromTitle( $this->getTitle(), false, Revision::READ_NORMAL );
		} else {
			$this->revision = Revision::newFromTitle( $this->getTitle() );
		}

		\Hooks::run( 'SMW::Parser::ChangeRevision', [ $this->getTitle(), &$this->revision ] );

		return $this->revision;
	}

	protected function hasContentHandler() {
		return defined( 'CONTENT_MODEL_WIKITEXT' );
	}

}