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
|
<?php
/**
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Content;
use InvalidArgumentException;
use MediaWiki\Context\RequestContext;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Parser\MagicWord;
use MediaWiki\Title\Title;
/**
* Content object for wiki text pages.
*
* @newable
* @since 1.21
* @ingroup Content
* @author Daniel Kinzler
*/
class WikitextContent extends TextContent {
/**
* @var string[] flags set by PST
*/
private $preSaveTransformFlags = [];
/**
* @stable to call
*
* @param string $text
*/
public function __construct( $text ) {
parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
}
/**
* @param string|int $sectionId
*
* @return Content|false|null
*
* @see Content::getSection()
*/
public function getSection( $sectionId ) {
$text = $this->getText();
$sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
->getSection( $text, $sectionId, false );
if ( $sect === false ) {
return false;
} else {
return new static( $sect );
}
}
/**
* @param string|int|null|false $sectionId
* @param Content $with New section content, must have the same content model as $this.
* @param string $sectionTitle
*
* @return Content
*
* @see Content::replaceSection()
*/
public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
// @phan-suppress-previous-line PhanParamSignatureMismatch False positive
$myModelId = $this->getModel();
$sectionModelId = $with->getModel();
if ( $sectionModelId != $myModelId ) {
throw new InvalidArgumentException( "Incompatible content model for section: " .
"document uses $myModelId but " .
"section uses $sectionModelId." );
}
/** @var self $with $oldtext */
'@phan-var self $with';
$oldtext = $this->getText();
$text = $with->getText();
if ( strval( $sectionId ) === '' ) {
return $with; # XXX: copy first?
}
if ( $sectionId === 'new' ) {
# Inserting a new section
$subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
$hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) );
if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
$text = trim( $oldtext ) !== ''
? "{$oldtext}\n\n{$subject}{$text}"
: "{$subject}{$text}";
}
} else {
# Replacing an existing section; roll out the big guns
$text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
->replaceSection( $oldtext, $sectionId, $text );
}
$newContent = new static( $text );
return $newContent;
}
/**
* Returns a new WikitextContent object with the given section heading
* prepended.
*
* @param string $header
*
* @return Content
*/
public function addSectionHeader( $header ) {
$text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
$text .= $this->getText();
return new static( $text );
}
/**
* Extract the redirect target and the remaining text on the page.
*
* @since 1.23
* @deprecated since 1.41, use WikitextContentHandler::getRedirectTargetAndText
*
* @return array List of two elements: Title|null and string.
*/
public function getRedirectTargetAndText() {
wfDeprecated( __METHOD__, '1.41' );
$handler = $this->getContentHandler();
[ $target, $content ] = $handler->extractRedirectTargetAndText( $this );
return [ Title::castFromLinkTarget( $target ), $content->getText() ];
}
/**
* Implement redirect extraction for wikitext.
*
* @return Title|null
*
* @see Content::getRedirectTarget
*/
public function getRedirectTarget() {
// TODO: The redirect target should be injected on construction.
// But that only works if the object is created by WikitextContentHandler.
$handler = $this->getContentHandler();
[ $target, ] = $handler->extractRedirectTargetAndText( $this );
return Title::castFromLinkTarget( $target );
}
/**
* This implementation replaces the first link on the page with the given new target
* if this Content object is a redirect. Otherwise, this method returns $this.
*
* @since 1.21
*
* @param Title $target
*
* @return Content
*
* @see Content::updateRedirect()
*/
public function updateRedirect( Title $target ) {
if ( !$this->isRedirect() ) {
return $this;
}
# Fix the text
# Remember that redirect pages can have categories, templates, etc.,
# so the regex has to be fairly general
$newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
'[[' . $target->getFullText() . ']]',
$this->getText(), 1 );
return new static( $newText );
}
/**
* Returns true if this content is not a redirect, and this content's text
* is countable according to the criteria defined by $wgArticleCountMethod.
*
* @param bool|null $hasLinks If it is known whether this content contains
* links, provide this information here, to avoid redundant parsing to
* find out (default: null).
* @param Title|null $title Optional title, defaults to the title from the current main request.
*
* @return bool
*/
public function isCountable( $hasLinks = null, ?Title $title = null ) {
$articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
->get( MainConfigNames::ArticleCountMethod );
if ( $this->isRedirect() ) {
return false;
}
if ( $articleCountMethod === 'link' ) {
if ( $hasLinks === null ) { # not known, find out
// @TODO: require an injected title
if ( !$title ) {
$context = RequestContext::getMain();
$title = $context->getTitle();
}
$contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here
$po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
$hasLinks = $po->hasLinks();
}
return $hasLinks;
}
return true;
}
/**
* @param int $maxlength
*
* @return string
*/
public function getTextForSummary( $maxlength = 250 ) {
$truncatedtext = parent::getTextForSummary( $maxlength );
# clean up unfinished links
# XXX: make this optional? wasn't there in autosummary, but required for
# deletion summary.
$truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
return $truncatedtext;
}
/**
* This implementation calls $word->match() on the this TextContent object's text.
*
* @param MagicWord $word
*
* @return bool
*
* @see Content::matchMagicWord()
*/
public function matchMagicWord( MagicWord $word ) {
return $word->match( $this->getText() );
}
/**
* Records flags set by preSaveTransform
*
* @internal for use by WikitextContentHandler
*
* @param string[] $flags
*/
public function setPreSaveTransformFlags( array $flags ) {
$this->preSaveTransformFlags = $flags;
}
/**
* Records flags set by preSaveTransform
*
* @internal for use by WikitextContentHandler
* @return string[]
*/
public function getPreSaveTransformFlags() {
return $this->preSaveTransformFlags;
}
public function getContentHandler(): WikitextContentHandler {
$handler = parent::getContentHandler();
'@phan-var WikitextContentHandler $handler';
return $handler;
}
}
/** @deprecated class alias since 1.43 */
class_alias( WikitextContent::class, 'WikitextContent' );
|