MediaWiki  master
WikitextContent.php
Go to the documentation of this file.
1 <?php
29 
37  private $redirectTargetAndText = null;
38 
42  private $preSaveTransformFlags = [];
43 
49  public function __construct( $text ) {
50  parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
51  }
52 
60  public function getSection( $sectionId ) {
61  $text = $this->getText();
62  $sect = MediaWikiServices::getInstance()->getParser()
63  ->getSection( $text, $sectionId, false );
64 
65  if ( $sect === false ) {
66  return false;
67  } else {
68  return new static( $sect );
69  }
70  }
71 
82  public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
83  $myModelId = $this->getModel();
84  $sectionModelId = $with->getModel();
85 
86  if ( $sectionModelId != $myModelId ) {
87  throw new MWException( "Incompatible content model for section: " .
88  "document uses $myModelId but " .
89  "section uses $sectionModelId." );
90  }
92  '@phan-var self $with';
93 
94  $oldtext = $this->getText();
95  $text = $with->getText();
96 
97  if ( strval( $sectionId ) === '' ) {
98  return $with; # XXX: copy first?
99  }
100 
101  if ( $sectionId === 'new' ) {
102  # Inserting a new section
103  $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
104  ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
105  if ( Hooks::runner()->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
106  $text = strlen( trim( $oldtext ) ) > 0
107  ? "{$oldtext}\n\n{$subject}{$text}"
108  : "{$subject}{$text}";
109  }
110  } else {
111  # Replacing an existing section; roll out the big guns
112  $text = MediaWikiServices::getInstance()->getParser()
113  ->replaceSection( $oldtext, $sectionId, $text );
114  }
115 
116  $newContent = new static( $text );
117 
118  return $newContent;
119  }
120 
129  public function addSectionHeader( $header ) {
130  $text = wfMessage( 'newsectionheaderdefaultlevel' )
131  ->rawParams( $header )->inContentLanguage()->text();
132  $text .= "\n\n";
133  $text .= $this->getText();
134 
135  return new static( $text );
136  }
137 
147  protected function getRedirectTargetAndText() {
148  global $wgMaxRedirects;
149 
150  if ( $this->redirectTargetAndText !== null ) {
152  }
153 
154  if ( $wgMaxRedirects < 1 ) {
155  // redirects are disabled, so quit early
156  $this->redirectTargetAndText = [ null, $this->getText() ];
158  }
159 
160  $redir = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'redirect' );
161  $text = ltrim( $this->getText() );
162  if ( $redir->matchStartAndRemove( $text ) ) {
163  // Extract the first link and see if it's usable
164  // Ensure that it really does come directly after #REDIRECT
165  // Some older redirects included a colon, so don't freak about that!
166  $m = [];
167  if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
168  // Strip preceding colon used to "escape" categories, etc.
169  // and URL-decode links
170  if ( strpos( $m[1], '%' ) !== false ) {
171  // Match behavior of inline link parsing here;
172  $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
173  }
174  $title = Title::newFromText( $m[1] );
175  // If the title is a redirect to bad special pages or is invalid, return null
176  if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
177  $this->redirectTargetAndText = [ null, $this->getText() ];
179  }
180 
181  $this->redirectTargetAndText = [ $title, substr( $text, strlen( $m[0] ) ) ];
183  }
184  }
185 
186  $this->redirectTargetAndText = [ null, $this->getText() ];
188  }
189 
197  public function getRedirectTarget() {
198  list( $title, ) = $this->getRedirectTargetAndText();
199 
200  return $title;
201  }
202 
215  public function updateRedirect( Title $target ) {
216  if ( !$this->isRedirect() ) {
217  return $this;
218  }
219 
220  # Fix the text
221  # Remember that redirect pages can have categories, templates, etc.,
222  # so the regex has to be fairly general
223  $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
224  '[[' . $target->getFullText() . ']]',
225  $this->getText(), 1 );
226 
227  return new static( $newText );
228  }
229 
241  public function isCountable( $hasLinks = null, Title $title = null ) {
242  global $wgArticleCountMethod;
243 
244  if ( $this->isRedirect() ) {
245  return false;
246  }
247 
248  if ( $wgArticleCountMethod === 'link' ) {
249  if ( $hasLinks === null ) { # not known, find out
250  // @TODO: require an injected title
251  if ( !$title ) {
252  $context = RequestContext::getMain();
253  $title = $context->getTitle();
254  }
255 
256  $po = $this->getParserOutput( $title, null, null, false );
257  $links = $po->getLinks();
258  $hasLinks = !empty( $links );
259  }
260 
261  return $hasLinks;
262  }
263 
264  return true;
265  }
266 
271  public function getTextForSummary( $maxlength = 250 ) {
272  $truncatedtext = parent::getTextForSummary( $maxlength );
273 
274  # clean up unfinished links
275  # XXX: make this optional? wasn't there in autosummary, but required for
276  # deletion summary.
277  $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
278 
279  return $truncatedtext;
280  }
281 
294  protected function fillParserOutput( Title $title, $revId,
295  ParserOptions $options, $generateHtml, ParserOutput &$output
296  ) {
297  list( $redir, $text ) = $this->getRedirectTargetAndText();
298  $output = MediaWikiServices::getInstance()->getParser()
299  ->parse( $text, $title, $options, true, true, $revId );
300 
301  // Add redirect indicator at the top
302  if ( $redir ) {
303  // Make sure to include the redirect link in pagelinks
304  $output->addLink( $redir );
305  if ( $generateHtml ) {
306  $chain = $this->getRedirectChain();
307  $output->setText(
308  Article::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
309  $output->getRawText()
310  );
311  $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
312  }
313  }
314 
315  // Pass along user-signature flag
316  if ( in_array( 'user-signature', $this->preSaveTransformFlags ) ) {
317  $output->setFlag( 'user-signature' );
318  }
319  }
320 
324  protected function getHtml() {
325  // @phan-suppress-previous-line PhanPluginNeverReturnMethod
326  throw new MWException(
327  "getHtml() not implemented for wikitext. "
328  . "Use getParserOutput()->getText()."
329  );
330  }
331 
341  public function matchMagicWord( MagicWord $word ) {
342  return $word->match( $this->getText() );
343  }
344 
350  public function setPreSaveTransformFlags( array $flags ) {
351  $this->preSaveTransformFlags = $flags;
352  }
353 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:45
WikitextContent\$redirectTargetAndText
$redirectTargetAndText
Definition: WikitextContent.php:37
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:382
ParserOutput
Definition: ParserOutput.php:32
AbstractContent\isRedirect
isRedirect()
Definition: AbstractContent.php:341
WikitextContent\getRedirectTarget
getRedirectTarget()
Implement redirect extraction for wikitext.
Definition: WikitextContent.php:197
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
AbstractContent\getParserOutput
getParserOutput(Title $title, $revId=null, ParserOptions $options=null, $generateHtml=true)
Returns a ParserOutput object containing information derived from this content.
Definition: AbstractContent.php:531
WikitextContent\updateRedirect
updateRedirect(Title $target)
This implementation replaces the first link on the page with the given new target if this Content obj...
Definition: WikitextContent.php:215
ParserOutput\setFlag
setFlag( $flag)
Attach a flag to the output so that it can be checked later to handle special cases.
Definition: ParserOutput.php:1086
ParserOutput\addLink
addLink(Title $title, $id=null)
Record a local or interwiki inline link for saving in future link tables.
Definition: ParserOutput.php:867
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1186
AbstractContent\getRedirectChain
getRedirectChain()
Definition: AbstractContent.php:274
WikitextContent\$preSaveTransformFlags
string[] $preSaveTransformFlags
flags set by PST
Definition: WikitextContent.php:42
WikitextContent\getRedirectTargetAndText
getRedirectTargetAndText()
Extract the redirect target and the remaining text on the page.
Definition: WikitextContent.php:147
TextContent\getText
getText()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:153
MWException
MediaWiki exception.
Definition: MWException.php:29
Title\getFullText
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1935
TextContent\copy
copy()
Definition: TextContent.php:74
$title
$title
Definition: testCompression.php:38
WikitextContent\isCountable
isCountable( $hasLinks=null, Title $title=null)
Returns true if this content is not a redirect, and this content's text is countable according to the...
Definition: WikitextContent.php:241
ParserOutput\addModuleStyles
addModuleStyles( $modules)
Definition: ParserOutput.php:970
WikitextContent\__construct
__construct( $text)
Definition: WikitextContent.php:49
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:742
MagicWord
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition: MagicWord.php:60
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
WikitextContent\getSection
getSection( $sectionId)
Definition: WikitextContent.php:60
WikitextContent\fillParserOutput
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Returns a ParserOutput object resulting from parsing the content's text using the global Parser servi...
Definition: WikitextContent.php:294
$header
$header
Definition: updateCredits.php:37
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
WikitextContent\matchMagicWord
matchMagicWord(MagicWord $word)
This implementation calls $word->match() on the this TextContent object's text.
Definition: WikitextContent.php:341
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:208
WikitextContent\getTextForSummary
getTextForSummary( $maxlength=250)
Definition: WikitextContent.php:271
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:39
Content
Base interface for content objects.
Definition: Content.php:35
AbstractContent\getModel
getModel()
Definition: AbstractContent.php:69
Title
Represents a title within MediaWiki.
Definition: Title.php:47
WikitextContent\addSectionHeader
addSectionHeader( $header)
Returns a new WikitextContent object with the given section heading prepended.
Definition: WikitextContent.php:129
WikitextContent\setPreSaveTransformFlags
setPreSaveTransformFlags(array $flags)
Records flags set by preSaveTransform.
Definition: WikitextContent.php:350
ParserOutput\getRawText
getRawText()
Get the cacheable text with <mw:editsection> markers still in it.
Definition: ParserOutput.php:317
Content\getModel
getModel()
Returns the ID of the content model used by this Content object.
WikitextContent\replaceSection
replaceSection( $sectionId, Content $with, $sectionTitle='')
Definition: WikitextContent.php:82
$wgMaxRedirects
$wgMaxRedirects
Max number of redirects to follow when resolving redirects.
Definition: DefaultSettings.php:4732
$wgArticleCountMethod
$wgArticleCountMethod
Method used to determine if a page in a content namespace should be counted as a valid article.
Definition: DefaultSettings.php:5106
WikitextContent\getHtml
getHtml()
Definition: WikitextContent.php:324
MagicWord\match
match( $text)
Returns true if the text contains the word.
Definition: MagicWord.php:246
Article\getRedirectHeaderHtml
static getRedirectHeaderHtml(Language $lang, $target, $forceKnown=false)
Return the HTML for the top of a redirect page.
Definition: Article.php:1710