Debian Codegraph @

includes/content/WikitextContent.php

raw symbols SCIP
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 $links = $po->getLinks();
236 $hasLinks = $links !== [];
237 }
238
239 return $hasLinks;
240 }
241
242 return true;
243 }
244
245 /**
246 * @param int $maxlength
247 *
248 * @return string
249 */
250 public function getTextForSummary( $maxlength = 250 ) {
251 $truncatedtext = parent::getTextForSummary( $maxlength );
252
253 # clean up unfinished links
254 # XXX: make this optional? wasn't there in autosummary, but required for
255 # deletion summary.
256 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
257
258 return $truncatedtext;
259 }
260
261 /**
262 * This implementation calls $word->match() on the this TextContent object's text.
263 *
264 * @param MagicWord $word
265 *
266 * @return bool
267 *
268 * @see Content::matchMagicWord()
269 */
270 public function matchMagicWord( MagicWord $word ) {
271 return $word->match( $this->getText() );
272 }
273
274 /**
275 * Records flags set by preSaveTransform
276 *
277 * @internal for use by WikitextContentHandler
278 *
279 * @param string[] $flags
280 */
281 public function setPreSaveTransformFlags( array $flags ) {
282 $this->preSaveTransformFlags = $flags;
283 }
284
285 /**
286 * Records flags set by preSaveTransform
287 *
288 * @internal for use by WikitextContentHandler
289 * @return string[]
290 */
291 public function getPreSaveTransformFlags() {
292 return $this->preSaveTransformFlags;
293 }
294
295 public function getContentHandler(): WikitextContentHandler {
296 $handler = parent::getContentHandler();
297 '@phan-var WikitextContentHandler $handler';
298
299 return $handler;
300 }
301 }
302
303 /** @deprecated class alias since 1.43 */
304 class_alias( WikitextContent::class, 'WikitextContent' );