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 = strval( $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  public function getRedirectTargetAndText() {
148  $maxRedirects = MediaWikiServices::getInstance()->getMainConfig()->get( 'MaxRedirects' );
149 
150  if ( $this->redirectTargetAndText !== null ) {
152  }
153 
154  if ( $maxRedirects < 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  $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()->get( 'ArticleCountMethod' );
243 
244  if ( $this->isRedirect() ) {
245  return false;
246  }
247 
248  if ( $articleCountMethod === '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  $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
256  $po = $contentRenderer->getParserOutput( $this, $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 
291  public function matchMagicWord( MagicWord $word ) {
292  return $word->match( $this->getText() );
293  }
294 
300  public function setPreSaveTransformFlags( array $flags ) {
301  $this->preSaveTransformFlags = $flags;
302  }
303 
309  public function getPreSaveTransformFlags() {
311  }
312 }
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:377
AbstractContent\isRedirect
isRedirect()
Definition: AbstractContent.php:347
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:203
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
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1167
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:1917
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
WikitextContent\__construct
__construct( $text)
Definition: WikitextContent.php:49
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
$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:291
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:71
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:300
WikitextContent\getPreSaveTransformFlags
getPreSaveTransformFlags()
Records flags set by preSaveTransform.
Definition: WikitextContent.php:309
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
MagicWord\match
match( $text)
Returns true if the text contains the word.
Definition: MagicWord.php:246