MediaWiki  master
WikitextContent.php
Go to the documentation of this file.
1 <?php
32 
40  private $redirectTargetAndText = null;
41 
45  private $preSaveTransformFlags = [];
46 
52  public function __construct( $text ) {
53  parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
54  }
55 
63  public function getSection( $sectionId ) {
64  $text = $this->getText();
65  $sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
66  ->getSection( $text, $sectionId, false );
67 
68  if ( $sect === false ) {
69  return false;
70  } else {
71  return new static( $sect );
72  }
73  }
74 
85  public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
86  // @phan-suppress-previous-line PhanParamSignatureMismatch False positive
87  $myModelId = $this->getModel();
88  $sectionModelId = $with->getModel();
89 
90  if ( $sectionModelId != $myModelId ) {
91  throw new MWException( "Incompatible content model for section: " .
92  "document uses $myModelId but " .
93  "section uses $sectionModelId." );
94  }
96  '@phan-var self $with';
97 
98  $oldtext = $this->getText();
99  $text = $with->getText();
100 
101  if ( strval( $sectionId ) === '' ) {
102  return $with; # XXX: copy first?
103  }
104 
105  if ( $sectionId === 'new' ) {
106  # Inserting a new section
107  $subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
108  ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
109  if ( Hooks::runner()->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
110  $text = strlen( trim( $oldtext ) ) > 0
111  ? "{$oldtext}\n\n{$subject}{$text}"
112  : "{$subject}{$text}";
113  }
114  } else {
115  # Replacing an existing section; roll out the big guns
116  $text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
117  ->replaceSection( $oldtext, $sectionId, $text );
118  }
119 
120  $newContent = new static( $text );
121 
122  return $newContent;
123  }
124 
133  public function addSectionHeader( $header ) {
134  $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
135  ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
136  $text .= $this->getText();
137 
138  return new static( $text );
139  }
140 
150  public function getRedirectTargetAndText() {
151  if ( $this->redirectTargetAndText !== null ) {
152  return $this->redirectTargetAndText;
153  }
154 
155  $redir = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'redirect' );
156  $text = ltrim( $this->getText() );
157  if ( $redir->matchStartAndRemove( $text ) ) {
158  // Extract the first link and see if it's usable
159  // Ensure that it really does come directly after #REDIRECT
160  // Some older redirects included a colon, so don't freak about that!
161  $m = [];
162  if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
163  // Strip preceding colon used to "escape" categories, etc.
164  // and URL-decode links
165  if ( strpos( $m[1], '%' ) !== false ) {
166  // Match behavior of inline link parsing here;
167  $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
168  }
169  $title = Title::newFromText( $m[1] );
170  // If the title is a redirect to bad special pages or is invalid, return null
171  if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
172  $this->redirectTargetAndText = [ null, $this->getText() ];
173  return $this->redirectTargetAndText;
174  }
175 
176  $this->redirectTargetAndText = [ $title, substr( $text, strlen( $m[0] ) ) ];
177  return $this->redirectTargetAndText;
178  }
179  }
180 
181  $this->redirectTargetAndText = [ null, $this->getText() ];
182  return $this->redirectTargetAndText;
183  }
184 
192  public function getRedirectTarget() {
193  [ $title, ] = $this->getRedirectTargetAndText();
194 
195  return $title;
196  }
197 
210  public function updateRedirect( Title $target ) {
211  if ( !$this->isRedirect() ) {
212  return $this;
213  }
214 
215  # Fix the text
216  # Remember that redirect pages can have categories, templates, etc.,
217  # so the regex has to be fairly general
218  $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
219  '[[' . $target->getFullText() . ']]',
220  $this->getText(), 1 );
221 
222  return new static( $newText );
223  }
224 
236  public function isCountable( $hasLinks = null, Title $title = null ) {
237  $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
238  ->get( MainConfigNames::ArticleCountMethod );
239 
240  if ( $this->isRedirect() ) {
241  return false;
242  }
243 
244  if ( $articleCountMethod === 'link' ) {
245  if ( $hasLinks === null ) { # not known, find out
246  // @TODO: require an injected title
247  if ( !$title ) {
248  $context = RequestContext::getMain();
249  $title = $context->getTitle();
250  }
251  $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
252  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here
253  $po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
254  $links = $po->getLinks();
255  $hasLinks = !empty( $links );
256  }
257 
258  return $hasLinks;
259  }
260 
261  return true;
262  }
263 
268  public function getTextForSummary( $maxlength = 250 ) {
269  $truncatedtext = parent::getTextForSummary( $maxlength );
270 
271  # clean up unfinished links
272  # XXX: make this optional? wasn't there in autosummary, but required for
273  # deletion summary.
274  $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
275 
276  return $truncatedtext;
277  }
278 
288  public function matchMagicWord( MagicWord $word ) {
289  return $word->match( $this->getText() );
290  }
291 
297  public function setPreSaveTransformFlags( array $flags ) {
298  $this->preSaveTransformFlags = $flags;
299  }
300 
306  public function getPreSaveTransformFlags() {
307  return $this->preSaveTransformFlags;
308  }
309 }
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:211
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
MediaWiki exception.
Definition: MWException.php:32
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition: MagicWord.php:65
match( $text)
Returns true if the text contains the word.
Definition: MagicWord.php:254
Represents a title within MediaWiki.
Definition: Title.php:82
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1951
static getMain()
Get the RequestContext object associated with the main request.
Content object implementation for representing flat text.
Definition: TextContent.php:40
getText()
Returns the text represented by this Content object, as a string.
Content object for wiki text pages.
updateRedirect(Title $target)
This implementation replaces the first link on the page with the given new target if this Content obj...
setPreSaveTransformFlags(array $flags)
Records flags set by preSaveTransform.
getRedirectTarget()
Implement redirect extraction for wikitext.
getTextForSummary( $maxlength=250)
getPreSaveTransformFlags()
Records flags set by preSaveTransform.
getRedirectTargetAndText()
Extract the redirect target and the remaining text on the page.
addSectionHeader( $header)
Returns a new WikitextContent object with the given section heading prepended.
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...
getSection( $sectionId)
matchMagicWord(MagicWord $word)
This implementation calls $word->match() on the this TextContent object's text.
replaceSection( $sectionId, Content $with, $sectionTitle='')
Base interface for representing page content.
Definition: Content.php:37
getModel()
Returns the ID of the content model used by this Content object.
$header