Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.96% |
60 / 69 |
|
76.92% |
10 / 13 |
CRAP | |
0.00% |
0 / 1 |
WikitextContent | |
88.24% |
60 / 68 |
|
76.92% |
10 / 13 |
27.10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSection | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
replaceSection | |
86.36% |
19 / 22 |
|
0.00% |
0 / 1 |
7.12 | |||
addSectionHeader | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getRedirectTargetAndText | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getRedirectTarget | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
updateRedirect | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
isCountable | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
getTextForSummary | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
matchMagicWord | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setPreSaveTransformFlags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPreSaveTransformFlags | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getContentHandler | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
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 | namespace MediaWiki\Content; |
29 | |
30 | use InvalidArgumentException; |
31 | use MediaWiki\Context\RequestContext; |
32 | use MediaWiki\HookContainer\HookRunner; |
33 | use MediaWiki\MainConfigNames; |
34 | use MediaWiki\MediaWikiServices; |
35 | use MediaWiki\Parser\MagicWord; |
36 | use MediaWiki\Title\Title; |
37 | |
38 | /** |
39 | * Content object for wiki text pages. |
40 | * |
41 | * @newable |
42 | * @ingroup Content |
43 | */ |
44 | class WikitextContent extends TextContent { |
45 | |
46 | /** |
47 | * @var string[] flags set by PST |
48 | */ |
49 | private $preSaveTransformFlags = []; |
50 | |
51 | /** |
52 | * @stable to call |
53 | * |
54 | * @param string $text |
55 | */ |
56 | public function __construct( $text ) { |
57 | parent::__construct( $text, CONTENT_MODEL_WIKITEXT ); |
58 | } |
59 | |
60 | /** |
61 | * @param string|int $sectionId |
62 | * |
63 | * @return Content|false|null |
64 | * |
65 | * @see Content::getSection() |
66 | */ |
67 | public function getSection( $sectionId ) { |
68 | $text = $this->getText(); |
69 | $sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance() |
70 | ->getSection( $text, $sectionId, false ); |
71 | |
72 | if ( $sect === false ) { |
73 | return false; |
74 | } else { |
75 | return new static( $sect ); |
76 | } |
77 | } |
78 | |
79 | /** |
80 | * @param string|int|null|false $sectionId |
81 | * @param Content $with New section content, must have the same content model as $this. |
82 | * @param string $sectionTitle |
83 | * |
84 | * @return Content |
85 | * |
86 | * @see Content::replaceSection() |
87 | */ |
88 | public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) { |
89 | // @phan-suppress-previous-line PhanParamSignatureMismatch False positive |
90 | $myModelId = $this->getModel(); |
91 | $sectionModelId = $with->getModel(); |
92 | |
93 | if ( $sectionModelId != $myModelId ) { |
94 | throw new InvalidArgumentException( "Incompatible content model for section: " . |
95 | "document uses $myModelId but " . |
96 | "section uses $sectionModelId." ); |
97 | } |
98 | /** @var self $with $oldtext */ |
99 | '@phan-var self $with'; |
100 | |
101 | $oldtext = $this->getText(); |
102 | $text = $with->getText(); |
103 | |
104 | if ( strval( $sectionId ) === '' ) { |
105 | return $with; # XXX: copy first? |
106 | } |
107 | |
108 | if ( $sectionId === 'new' ) { |
109 | # Inserting a new section |
110 | $subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' ) |
111 | ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : ''; |
112 | $hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ); |
113 | if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) { |
114 | $text = strlen( trim( $oldtext ) ) > 0 |
115 | ? "{$oldtext}\n\n{$subject}{$text}" |
116 | : "{$subject}{$text}"; |
117 | } |
118 | } else { |
119 | # Replacing an existing section; roll out the big guns |
120 | $text = MediaWikiServices::getInstance()->getParserFactory()->getInstance() |
121 | ->replaceSection( $oldtext, $sectionId, $text ); |
122 | } |
123 | |
124 | $newContent = new static( $text ); |
125 | |
126 | return $newContent; |
127 | } |
128 | |
129 | /** |
130 | * Returns a new WikitextContent object with the given section heading |
131 | * prepended. |
132 | * |
133 | * @param string $header |
134 | * |
135 | * @return Content |
136 | */ |
137 | public function addSectionHeader( $header ) { |
138 | $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' ) |
139 | ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : ''; |
140 | $text .= $this->getText(); |
141 | |
142 | return new static( $text ); |
143 | } |
144 | |
145 | /** |
146 | * Extract the redirect target and the remaining text on the page. |
147 | * |
148 | * @since 1.23 |
149 | * @deprecated since 1.41, use WikitextContentHandler::getRedirectTargetAndText |
150 | * |
151 | * @return array List of two elements: Title|null and string. |
152 | */ |
153 | public function getRedirectTargetAndText() { |
154 | wfDeprecated( __METHOD__, '1.41' ); |
155 | |
156 | $handler = $this->getContentHandler(); |
157 | [ $target, $content ] = $handler->extractRedirectTargetAndText( $this ); |
158 | |
159 | return [ Title::castFromLinkTarget( $target ), $content->getText() ]; |
160 | } |
161 | |
162 | /** |
163 | * Implement redirect extraction for wikitext. |
164 | * |
165 | * @return Title|null |
166 | * |
167 | * @see Content::getRedirectTarget |
168 | */ |
169 | public function getRedirectTarget() { |
170 | // TODO: The redirect target should be injected on construction. |
171 | // But that only works if the object is created by WikitextContentHandler. |
172 | |
173 | $handler = $this->getContentHandler(); |
174 | [ $target, ] = $handler->extractRedirectTargetAndText( $this ); |
175 | |
176 | return Title::castFromLinkTarget( $target ); |
177 | } |
178 | |
179 | /** |
180 | * This implementation replaces the first link on the page with the given new target |
181 | * if this Content object is a redirect. Otherwise, this method returns $this. |
182 | * |
183 | * @since 1.21 |
184 | * |
185 | * @param Title $target |
186 | * |
187 | * @return Content |
188 | * |
189 | * @see Content::updateRedirect() |
190 | */ |
191 | public function updateRedirect( Title $target ) { |
192 | if ( !$this->isRedirect() ) { |
193 | return $this; |
194 | } |
195 | |
196 | # Fix the text |
197 | # Remember that redirect pages can have categories, templates, etc., |
198 | # so the regex has to be fairly general |
199 | $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x', |
200 | '[[' . $target->getFullText() . ']]', |
201 | $this->getText(), 1 ); |
202 | |
203 | return new static( $newText ); |
204 | } |
205 | |
206 | /** |
207 | * Returns true if this content is not a redirect, and this content's text |
208 | * is countable according to the criteria defined by $wgArticleCountMethod. |
209 | * |
210 | * @param bool|null $hasLinks If it is known whether this content contains |
211 | * links, provide this information here, to avoid redundant parsing to |
212 | * find out (default: null). |
213 | * @param Title|null $title Optional title, defaults to the title from the current main request. |
214 | * |
215 | * @return bool |
216 | */ |
217 | public function isCountable( $hasLinks = null, ?Title $title = null ) { |
218 | $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig() |
219 | ->get( MainConfigNames::ArticleCountMethod ); |
220 | |
221 | if ( $this->isRedirect() ) { |
222 | return false; |
223 | } |
224 | |
225 | if ( $articleCountMethod === 'link' ) { |
226 | if ( $hasLinks === null ) { # not known, find out |
227 | // @TODO: require an injected title |
228 | if ( !$title ) { |
229 | $context = RequestContext::getMain(); |
230 | $title = $context->getTitle(); |
231 | } |
232 | $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer(); |
233 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here |
234 | $po = $contentRenderer->getParserOutput( $this, $title, null, null, false ); |
235 | $hasLinks = $po->hasLinks(); |
236 | } |
237 | |
238 | return $hasLinks; |
239 | } |
240 | |
241 | return true; |
242 | } |
243 | |
244 | /** |
245 | * @param int $maxlength |
246 | * |
247 | * @return string |
248 | */ |
249 | public function getTextForSummary( $maxlength = 250 ) { |
250 | $truncatedtext = parent::getTextForSummary( $maxlength ); |
251 | |
252 | # clean up unfinished links |
253 | # XXX: make this optional? wasn't there in autosummary, but required for |
254 | # deletion summary. |
255 | $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext ); |
256 | |
257 | return $truncatedtext; |
258 | } |
259 | |
260 | /** |
261 | * This implementation calls $word->match() on the this TextContent object's text. |
262 | * |
263 | * @param MagicWord $word |
264 | * |
265 | * @return bool |
266 | * |
267 | * @see Content::matchMagicWord() |
268 | */ |
269 | public function matchMagicWord( MagicWord $word ) { |
270 | return $word->match( $this->getText() ); |
271 | } |
272 | |
273 | /** |
274 | * Records flags set by preSaveTransform |
275 | * |
276 | * @internal for use by WikitextContentHandler |
277 | * |
278 | * @param string[] $flags |
279 | */ |
280 | public function setPreSaveTransformFlags( array $flags ) { |
281 | $this->preSaveTransformFlags = $flags; |
282 | } |
283 | |
284 | /** |
285 | * Records flags set by preSaveTransform |
286 | * |
287 | * @internal for use by WikitextContentHandler |
288 | * @return string[] |
289 | */ |
290 | public function getPreSaveTransformFlags() { |
291 | return $this->preSaveTransformFlags; |
292 | } |
293 | |
294 | public function getContentHandler(): WikitextContentHandler { |
295 | $handler = parent::getContentHandler(); |
296 | '@phan-var WikitextContentHandler $handler'; |
297 | |
298 | return $handler; |
299 | } |
300 | } |
301 | |
302 | /** @deprecated class alias since 1.43 */ |
303 | class_alias( WikitextContent::class, 'WikitextContent' ); |