|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Content object for wiki text pages. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License along |
|
16
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
17
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
|
19
|
|
|
* |
|
20
|
|
|
* @since 1.21 |
|
21
|
|
|
* |
|
22
|
|
|
* @file |
|
23
|
|
|
* @ingroup Content |
|
24
|
|
|
* |
|
25
|
|
|
* @author Daniel Kinzler |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Content object for wiki text pages. |
|
30
|
|
|
* |
|
31
|
|
|
* @ingroup Content |
|
32
|
|
|
*/ |
|
33
|
|
|
class WikitextContent extends TextContent { |
|
34
|
|
|
private $redirectTargetAndText = null; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( $text ) { |
|
37
|
|
|
parent::__construct( $text, CONTENT_MODEL_WIKITEXT ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string|number $sectionId |
|
42
|
|
|
* |
|
43
|
|
|
* @return Content|bool|null |
|
44
|
|
|
* |
|
45
|
|
|
* @see Content::getSection() |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getSection( $sectionId ) { |
|
48
|
|
|
global $wgParser; |
|
49
|
|
|
|
|
50
|
|
|
$text = $this->getNativeData(); |
|
51
|
|
|
$sect = $wgParser->getSection( $text, $sectionId, false ); |
|
52
|
|
|
|
|
53
|
|
|
if ( $sect === false ) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} else { |
|
56
|
|
|
return new static( $sect ); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string|number|null|bool $sectionId |
|
62
|
|
|
* @param Content $with |
|
63
|
|
|
* @param string $sectionTitle |
|
64
|
|
|
* |
|
65
|
|
|
* @throws MWException |
|
66
|
|
|
* @return Content |
|
67
|
|
|
* |
|
68
|
|
|
* @see Content::replaceSection() |
|
69
|
|
|
*/ |
|
70
|
|
|
public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) { |
|
71
|
|
|
|
|
72
|
|
|
$myModelId = $this->getModel(); |
|
73
|
|
|
$sectionModelId = $with->getModel(); |
|
74
|
|
|
|
|
75
|
|
|
if ( $sectionModelId != $myModelId ) { |
|
76
|
|
|
throw new MWException( "Incompatible content model for section: " . |
|
77
|
|
|
"document uses $myModelId but " . |
|
78
|
|
|
"section uses $sectionModelId." ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$oldtext = $this->getNativeData(); |
|
82
|
|
|
$text = $with->getNativeData(); |
|
83
|
|
|
|
|
84
|
|
|
if ( strval( $sectionId ) === '' ) { |
|
85
|
|
|
return $with; # XXX: copy first? |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ( $sectionId === 'new' ) { |
|
89
|
|
|
# Inserting a new section |
|
90
|
|
|
$subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' ) |
|
91
|
|
|
->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : ''; |
|
92
|
|
|
if ( Hooks::run( 'PlaceNewSection', [ $this, $oldtext, $subject, &$text ] ) ) { |
|
93
|
|
|
$text = strlen( trim( $oldtext ) ) > 0 |
|
94
|
|
|
? "{$oldtext}\n\n{$subject}{$text}" |
|
95
|
|
|
: "{$subject}{$text}"; |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
|
|
# Replacing an existing section; roll out the big guns |
|
99
|
|
|
global $wgParser; |
|
100
|
|
|
|
|
101
|
|
|
$text = $wgParser->replaceSection( $oldtext, $sectionId, $text ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$newContent = new static( $text ); |
|
105
|
|
|
|
|
106
|
|
|
return $newContent; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns a new WikitextContent object with the given section heading |
|
111
|
|
|
* prepended. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $header |
|
114
|
|
|
* |
|
115
|
|
|
* @return Content |
|
116
|
|
|
*/ |
|
117
|
|
|
public function addSectionHeader( $header ) { |
|
118
|
|
|
$text = wfMessage( 'newsectionheaderdefaultlevel' ) |
|
119
|
|
|
->rawParams( $header )->inContentLanguage()->text(); |
|
120
|
|
|
$text .= "\n\n"; |
|
121
|
|
|
$text .= $this->getNativeData(); |
|
122
|
|
|
|
|
123
|
|
|
return new static( $text ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns a Content object with pre-save transformations applied using |
|
128
|
|
|
* Parser::preSaveTransform(). |
|
129
|
|
|
* |
|
130
|
|
|
* @param Title $title |
|
131
|
|
|
* @param User $user |
|
132
|
|
|
* @param ParserOptions $popts |
|
133
|
|
|
* |
|
134
|
|
|
* @return Content |
|
135
|
|
|
*/ |
|
136
|
|
|
public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { |
|
137
|
|
|
global $wgParser; |
|
138
|
|
|
|
|
139
|
|
|
$text = $this->getNativeData(); |
|
140
|
|
|
$pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); |
|
141
|
|
|
|
|
142
|
|
|
return ( $text === $pst ) ? $this : new static( $pst ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns a Content object with preload transformations applied (or this |
|
147
|
|
|
* object if no transformations apply). |
|
148
|
|
|
* |
|
149
|
|
|
* @param Title $title |
|
150
|
|
|
* @param ParserOptions $popts |
|
151
|
|
|
* @param array $params |
|
152
|
|
|
* |
|
153
|
|
|
* @return Content |
|
154
|
|
|
*/ |
|
155
|
|
|
public function preloadTransform( Title $title, ParserOptions $popts, $params = [] ) { |
|
156
|
|
|
global $wgParser; |
|
157
|
|
|
|
|
158
|
|
|
$text = $this->getNativeData(); |
|
159
|
|
|
$plt = $wgParser->getPreloadText( $text, $title, $popts, $params ); |
|
160
|
|
|
|
|
161
|
|
|
return new static( $plt ); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Extract the redirect target and the remaining text on the page. |
|
166
|
|
|
* |
|
167
|
|
|
* @note migrated here from Title::newFromRedirectInternal() |
|
168
|
|
|
* |
|
169
|
|
|
* @since 1.23 |
|
170
|
|
|
* |
|
171
|
|
|
* @return array List of two elements: Title|null and string. |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function getRedirectTargetAndText() { |
|
174
|
|
|
global $wgMaxRedirects; |
|
175
|
|
|
|
|
176
|
|
|
if ( $this->redirectTargetAndText !== null ) { |
|
177
|
|
|
return $this->redirectTargetAndText; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if ( $wgMaxRedirects < 1 ) { |
|
181
|
|
|
// redirects are disabled, so quit early |
|
182
|
|
|
$this->redirectTargetAndText = [ null, $this->getNativeData() ]; |
|
183
|
|
|
return $this->redirectTargetAndText; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$redir = MagicWord::get( 'redirect' ); |
|
187
|
|
|
$text = ltrim( $this->getNativeData() ); |
|
188
|
|
|
if ( $redir->matchStartAndRemove( $text ) ) { |
|
189
|
|
|
// Extract the first link and see if it's usable |
|
190
|
|
|
// Ensure that it really does come directly after #REDIRECT |
|
191
|
|
|
// Some older redirects included a colon, so don't freak about that! |
|
192
|
|
|
$m = []; |
|
193
|
|
|
if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) { |
|
194
|
|
|
// Strip preceding colon used to "escape" categories, etc. |
|
195
|
|
|
// and URL-decode links |
|
196
|
|
|
if ( strpos( $m[1], '%' ) !== false ) { |
|
197
|
|
|
// Match behavior of inline link parsing here; |
|
198
|
|
|
$m[1] = rawurldecode( ltrim( $m[1], ':' ) ); |
|
199
|
|
|
} |
|
200
|
|
|
$title = Title::newFromText( $m[1] ); |
|
201
|
|
|
// If the title is a redirect to bad special pages or is invalid, return null |
|
202
|
|
|
if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) { |
|
203
|
|
|
$this->redirectTargetAndText = [ null, $this->getNativeData() ]; |
|
204
|
|
|
return $this->redirectTargetAndText; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$this->redirectTargetAndText = [ $title, substr( $text, strlen( $m[0] ) ) ]; |
|
208
|
|
|
return $this->redirectTargetAndText; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$this->redirectTargetAndText = [ null, $this->getNativeData() ]; |
|
213
|
|
|
return $this->redirectTargetAndText; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Implement redirect extraction for wikitext. |
|
218
|
|
|
* |
|
219
|
|
|
* @return Title|null |
|
220
|
|
|
* |
|
221
|
|
|
* @see Content::getRedirectTarget |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getRedirectTarget() { |
|
224
|
|
|
list( $title, ) = $this->getRedirectTargetAndText(); |
|
225
|
|
|
|
|
226
|
|
|
return $title; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* This implementation replaces the first link on the page with the given new target |
|
231
|
|
|
* if this Content object is a redirect. Otherwise, this method returns $this. |
|
232
|
|
|
* |
|
233
|
|
|
* @since 1.21 |
|
234
|
|
|
* |
|
235
|
|
|
* @param Title $target |
|
236
|
|
|
* |
|
237
|
|
|
* @return Content |
|
238
|
|
|
* |
|
239
|
|
|
* @see Content::updateRedirect() |
|
240
|
|
|
*/ |
|
241
|
|
|
public function updateRedirect( Title $target ) { |
|
242
|
|
|
if ( !$this->isRedirect() ) { |
|
243
|
|
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
# Fix the text |
|
247
|
|
|
# Remember that redirect pages can have categories, templates, etc., |
|
248
|
|
|
# so the regex has to be fairly general |
|
249
|
|
|
$newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x', |
|
250
|
|
|
'[[' . $target->getFullText() . ']]', |
|
251
|
|
|
$this->getNativeData(), 1 ); |
|
252
|
|
|
|
|
253
|
|
|
return new static( $newText ); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Returns true if this content is not a redirect, and this content's text |
|
258
|
|
|
* is countable according to the criteria defined by $wgArticleCountMethod. |
|
259
|
|
|
* |
|
260
|
|
|
* @param bool|null $hasLinks If it is known whether this content contains |
|
261
|
|
|
* links, provide this information here, to avoid redundant parsing to |
|
262
|
|
|
* find out (default: null). |
|
263
|
|
|
* @param Title|null $title Optional title, defaults to the title from the current main request. |
|
264
|
|
|
* |
|
265
|
|
|
* @return bool |
|
266
|
|
|
*/ |
|
267
|
|
|
public function isCountable( $hasLinks = null, Title $title = null ) { |
|
268
|
|
|
global $wgArticleCountMethod; |
|
269
|
|
|
|
|
270
|
|
|
if ( $this->isRedirect() ) { |
|
271
|
|
|
return false; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
switch ( $wgArticleCountMethod ) { |
|
275
|
|
|
case 'any': |
|
276
|
|
|
return true; |
|
277
|
|
|
case 'comma': |
|
278
|
|
|
$text = $this->getNativeData(); |
|
279
|
|
|
return strpos( $text, ',' ) !== false; |
|
280
|
|
|
case 'link': |
|
281
|
|
|
if ( $hasLinks === null ) { # not known, find out |
|
282
|
|
|
if ( !$title ) { |
|
283
|
|
|
$context = RequestContext::getMain(); |
|
284
|
|
|
$title = $context->getTitle(); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
$po = $this->getParserOutput( $title, null, null, false ); |
|
|
|
|
|
|
288
|
|
|
$links = $po->getLinks(); |
|
289
|
|
|
$hasLinks = !empty( $links ); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
return $hasLinks; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
return false; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @param int $maxlength |
|
300
|
|
|
* @return string |
|
301
|
|
|
*/ |
|
302
|
|
|
public function getTextForSummary( $maxlength = 250 ) { |
|
303
|
|
|
$truncatedtext = parent::getTextForSummary( $maxlength ); |
|
304
|
|
|
|
|
305
|
|
|
# clean up unfinished links |
|
306
|
|
|
# XXX: make this optional? wasn't there in autosummary, but required for |
|
307
|
|
|
# deletion summary. |
|
308
|
|
|
$truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext ); |
|
309
|
|
|
|
|
310
|
|
|
return $truncatedtext; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Returns a ParserOutput object resulting from parsing the content's text |
|
315
|
|
|
* using $wgParser. |
|
316
|
|
|
* |
|
317
|
|
|
* @param Title $title |
|
318
|
|
|
* @param int $revId Revision to pass to the parser (default: null) |
|
319
|
|
|
* @param ParserOptions $options (default: null) |
|
320
|
|
|
* @param bool $generateHtml (default: true) |
|
321
|
|
|
* @param ParserOutput &$output ParserOutput representing the HTML form of the text, |
|
322
|
|
|
* may be manipulated or replaced. |
|
323
|
|
|
*/ |
|
324
|
|
|
protected function fillParserOutput( Title $title, $revId, |
|
325
|
|
|
ParserOptions $options, $generateHtml, ParserOutput &$output |
|
326
|
|
|
) { |
|
327
|
|
|
global $wgParser; |
|
328
|
|
|
|
|
329
|
|
|
list( $redir, $text ) = $this->getRedirectTargetAndText(); |
|
330
|
|
|
$output = $wgParser->parse( $text, $title, $options, true, true, $revId ); |
|
331
|
|
|
|
|
332
|
|
|
// Add redirect indicator at the top |
|
333
|
|
|
if ( $redir ) { |
|
334
|
|
|
// Make sure to include the redirect link in pagelinks |
|
335
|
|
|
$output->addLink( $redir ); |
|
336
|
|
|
if ( $generateHtml ) { |
|
337
|
|
|
$chain = $this->getRedirectChain(); |
|
338
|
|
|
$output->setText( |
|
339
|
|
|
Article::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) . |
|
|
|
|
|
|
340
|
|
|
$output->getRawText() |
|
341
|
|
|
); |
|
342
|
|
|
$output->addModuleStyles( 'mediawiki.action.view.redirectPage' ); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @throws MWException |
|
349
|
|
|
*/ |
|
350
|
|
|
protected function getHtml() { |
|
351
|
|
|
throw new MWException( |
|
352
|
|
|
"getHtml() not implemented for wikitext. " |
|
353
|
|
|
. "Use getParserOutput()->getText()." |
|
354
|
|
|
); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* This implementation calls $word->match() on the this TextContent object's text. |
|
359
|
|
|
* |
|
360
|
|
|
* @param MagicWord $word |
|
361
|
|
|
* |
|
362
|
|
|
* @return bool |
|
363
|
|
|
* |
|
364
|
|
|
* @see Content::matchMagicWord() |
|
365
|
|
|
*/ |
|
366
|
|
|
public function matchMagicWord( MagicWord $word ) { |
|
367
|
|
|
return $word->match( $this->getNativeData() ); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
} |
|
371
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: