forked from oetterer/BootstrapComponents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserOutputHelper.php
More file actions
294 lines (267 loc) · 8.52 KB
/
Copy pathParserOutputHelper.php
File metadata and controls
294 lines (267 loc) · 8.52 KB
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
<?php
/**
* Contains the class augmenting the parser output.
*
* @copyright (C) 2018, Tobias Oetterer, Paderborn University
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
*
* This file is part of the MediaWiki extension BootstrapComponents.
* The BootstrapComponents extension is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The BootstrapComponents extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @file
* @ingroup BootstrapComponents
* @author Tobias Oetterer
*/
namespace BootstrapComponents;
use \ConfigException;
use \Html;
use \MediaWiki\MediaWikiServices;
use \ParserOutput;
use \RequestContext;
use \Title;
/**
* Class ParserOutputHelper
*
* Performs all the adaptions on the ParserOutput
*
* @since 1.0
*/
class ParserOutputHelper {
/**
* @var string
*/
const EXTENSION_DATA_DEFERRED_CONTENT_KEY = 'bsc_deferredContent';
/**
* @var string
*/
const INJECTION_PREFIX = '<!-- injected by Extension:BootstrapComponents -->';
/**
* @var string
*/
const INJECTION_SUFFIX = '<!-- /injected by Extension:BootstrapComponents -->';
/**
* To make sure, we only add the tracking category once.
*
* @var bool $articleTracked
*/
private $articleTracked;
/**
* To make sure, we only add the error tracking category once.
*
* @var bool $articleTrackedOnError
*/
private $articleTrackedOnError;
/**
* Holds the name of the skin we use (or false, if there is no skin).
*
* @var string $nameOfActiveSkin
*/
private $nameOfActiveSkin;
/**
* @var \Parser $parser
*/
private $parser;
/**
* ParserOutputHelper constructor.
*
* Do not instantiate directly, but use {@see ApplicationFactory::getParserOutputHelper} instead.
*
* @param \Parser $parser
*
* @see ApplicationFactory::getParserOutputHelper
*/
public function __construct( $parser ) {
$this->articleTracked = false;
$this->articleTrackedOnError = false;
$this->parser = $parser;
$this->nameOfActiveSkin = $this->detectSkinInUse(
defined( 'MW_NO_SESSION' )
);
}
/**
* Adds the error tracking category to the current page if not done already.
*/
public function addErrorTrackingCategory() {
if ( $this->articleTrackedOnError ) {
return;
}
$this->placeTrackingCategory( 'bootstrap-components-error-tracking-category' );
$this->articleTrackedOnError = true;
}
/**
* Adds the supplied modules to the parser output.
*
* @param array $modulesToAdd
*/
public function addModules( $modulesToAdd ) {
$parserOutput = $this->getParser()->getOutput();
if ( is_a( $parserOutput, ParserOutput::class ) ) {
// Q: when do we expect \Parser->getOutput() no to be a \ParserOutput? A:During tests.
$parserOutput->addModules( $modulesToAdd );
}
}
/**
* Adds the tracking category to the current page if not done already.
*/
public function addTrackingCategory() {
if ( $this->articleTracked ) {
return;
}
$this->placeTrackingCategory( 'bootstrap-components-tracking-category' );
$this->articleTracked = true;
}
/**
* Unless I find a solution for the integration test problem, I cannot use an instance of
* ParserOutputHelper in ImageModal to ascertain this. In integration tests, "we" use a
* different parser than the InternalParseBeforeLinks-Hook. At least, after I added
* Scribunto _unit_ tests. All messes up, I'm afraid. ImageModal better use global parser, and
* for the time being this method will be
* @deprecated
*
* @return bool|null
*/
public function areImageModalsSuppressed() {
return $this->getParser()->getOutput()->getExtensionData( 'bsc_no_image_modal' );
}
/**
* Returns the raw html that is be inserted at the end of the page.
*
* @param \ParserOutput $parserOutput
*
* @return string
*/
public function getContentForLaterInjection( \ParserOutput $parserOutput ) {
$deferredContent = $parserOutput->getExtensionData( self::EXTENSION_DATA_DEFERRED_CONTENT_KEY );
if ( empty( $deferredContent ) || !is_array( $deferredContent ) ) {
return '';
}
// clearing extension data for unit and integration tests to work
$parserOutput->setExtensionData( self::EXTENSION_DATA_DEFERRED_CONTENT_KEY, null );
return self::INJECTION_PREFIX . implode( array_values( $deferredContent ) ) . self::INJECTION_SUFFIX;
}
/**
* @return string
*/
public function getNameOfActiveSkin() {
return $this->nameOfActiveSkin;
}
/**
* Allows for html fragments for a given container id to be stored, so that it can be added to the page at a later time.
*
* @param string $id
* @param string $rawHtml
*
* @return ParserOutputHelper $this (fluid)
*/
public function injectLater( $id, $rawHtml ) {
if ( empty( $this->getParser()->getOutput() ) ) {
# fix issues with PageForms file upload (issue #20)
return $this;
}
if ( !empty( $rawHtml ) ) {
$deferredContent = $this->getParser()->getOutput()->getExtensionData( self::EXTENSION_DATA_DEFERRED_CONTENT_KEY );
if ( empty( $deferredContent ) ) {
$deferredContent = [];
}
$deferredContent[$id] = $rawHtml;
$this->getParser()->getOutput()->setExtensionData( self::EXTENSION_DATA_DEFERRED_CONTENT_KEY, $deferredContent );
}
return $this;
}
/**
* Adds the bootstrap modules and styles to the page, if not done already
* @todo augment signature to include bsModule to add via BootstrapManager::getInstance()->addBootstrapModule();
*/
public function loadBootstrapModules() {
$parserOutput = $this->getParser()->getOutput();
if ( is_a( $parserOutput, ParserOutput::class ) ) {
// Q: when do we expect \Parser->getOutput() not to be a \ParserOutput? A: During tests.
$parserOutput->setExtensionData( 'bsc_load_modules', true );
if ( $this->vectorSkinInUse() ) {
$parserOutput->addModules( 'ext.bootstrapComponents.vector-fix' );
}
}
}
/**
* Formats a text as error text so it can be added to the output.
*
* @param string $errorMessageName
*
* @return string
*/
public function renderErrorMessage( $errorMessageName ) {
if ( !$errorMessageName || !trim( $errorMessageName ) ) {
return '';
}
$this->addErrorTrackingCategory();
return Html::rawElement(
'span',
[ 'class' => 'error' ],
wfMessage( trim( $errorMessageName ) )->inContentLanguage()->title( $this->parser->getTitle() )->parse()
);
}
/**
* Returns true, if active skin is vector
*
* @return bool
*/
public function vectorSkinInUse() {
return strtolower( $this->getNameOfActiveSkin() ) == 'vector';
}
/**
* @return \Parser
*/
protected function getParser() {
return $this->parser;
}
/**
* @param bool $useConfig set to true, if we can't rely on {@see \RequestContext::getSkin}
*
* @return string
*/
private function detectSkinInUse( $useConfig = false ) {
if ( !$useConfig ) {
$skin = RequestContext::getMain()->getSkin();
}
if ( !empty( $skin ) && is_a( $skin, 'Skin' ) ) {
return $skin->getSkinName();
}
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
try {
$defaultSkin = $mainConfig->get( 'DefaultSkin' );
} catch ( \ConfigException $e ) {
$defaultSkin = 'unknown';
}
return empty( $defaultSkin ) ? 'unknown' : $defaultSkin;
}
/**
* Adds current page to the indicated tracking category, if not done already.
*
* @param String $trackingCategoryMessageName name of the message, containing the tracking category
*/
private function placeTrackingCategory( $trackingCategoryMessageName ) {
$categoryMessage = wfMessage( $trackingCategoryMessageName )->inContentLanguage();
$parserOutput = $this->parser->getOutput();
if ( !$categoryMessage->isDisabled() && is_a( $parserOutput, ParserOutput::class ) ) {
// Q: when do we expect \Parser->getOutput() no to be a \ParserOutput? A:During tests.
$cat = Title::makeTitleSafe( NS_CATEGORY, $categoryMessage->text() );
if ( $cat ) {
$sort = (string) $parserOutput->getProperty( 'defaultsort' );
$parserOutput->addCategory( $cat->getDBkey(), $sort );
} else {
wfDebug( __METHOD__ . ": [[MediaWiki:{$trackingCategoryMessageName}]] is not a valid title!\n" );
}
}
}
}