MediaWiki  1.23.1
WikitextContent.php
Go to the documentation of this file.
1 <?php
34 
35  public function __construct( $text ) {
36  parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
37  }
38 
46  public function getSection( $section ) {
48 
49  $text = $this->getNativeData();
50  $sect = $wgParser->getSection( $text, $section, false );
51 
52  if ( $sect === false ) {
53  return false;
54  } else {
55  return new WikitextContent( $sect );
56  }
57  }
58 
69  public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
70  wfProfileIn( __METHOD__ );
71 
72  $myModelId = $this->getModel();
73  $sectionModelId = $with->getModel();
74 
75  if ( $sectionModelId != $myModelId ) {
76  wfProfileOut( __METHOD__ );
77  throw new MWException( "Incompatible content model for section: " .
78  "document uses $myModelId but " .
79  "section uses $sectionModelId." );
80  }
81 
82  $oldtext = $this->getNativeData();
83  $text = $with->getNativeData();
84 
85  if ( $section === '' ) {
86  wfProfileOut( __METHOD__ );
87 
88  return $with; # XXX: copy first?
89  }
90 
91  if ( $section == 'new' ) {
92  # Inserting a new section
93  $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
94  ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
95  if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
96  $text = strlen( trim( $oldtext ) ) > 0
97  ? "{$oldtext}\n\n{$subject}{$text}"
98  : "{$subject}{$text}";
99  }
100  } else {
101  # Replacing an existing section; roll out the big guns
103 
104  $text = $wgParser->replaceSection( $oldtext, $section, $text );
105  }
106 
107  $newContent = new WikitextContent( $text );
108 
109  wfProfileOut( __METHOD__ );
110 
111  return $newContent;
112  }
113 
122  public function addSectionHeader( $header ) {
123  $text = wfMessage( 'newsectionheaderdefaultlevel' )
124  ->rawParams( $header )->inContentLanguage()->text();
125  $text .= "\n\n";
126  $text .= $this->getNativeData();
127 
128  return new WikitextContent( $text );
129  }
130 
141  public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
143 
144  $text = $this->getNativeData();
145  $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
146  rtrim( $pst );
147 
148  return ( $text === $pst ) ? $this : new WikitextContent( $pst );
149  }
150 
161  public function preloadTransform( Title $title, ParserOptions $popts, $params = array() ) {
163 
164  $text = $this->getNativeData();
165  $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
166 
167  return new WikitextContent( $plt );
168  }
169 
180  public function getRedirectTarget() {
181  global $wgMaxRedirects;
182  if ( $wgMaxRedirects < 1 ) {
183  // redirects are disabled, so quit early
184  return null;
185  }
186  $redir = MagicWord::get( 'redirect' );
187  $text = trim( $this->getNativeData() );
188  if ( $redir->matchStartAndRemove( $text ) ) {
189  // Extract the first link and see if it's usable
190  // Ensure that it really does come directly after #REDIRECT
191  // Some older redirects included a colon, so don't freak about that!
192  $m = array();
193  if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
194  // Strip preceding colon used to "escape" categories, etc.
195  // and URL-decode links
196  if ( strpos( $m[1], '%' ) !== false ) {
197  // Match behavior of inline link parsing here;
198  $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
199  }
200  $title = Title::newFromText( $m[1] );
201  // If the title is a redirect to bad special pages or is invalid, return null
202  if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
203  return null;
204  }
205 
206  return $title;
207  }
208  }
209 
210  return null;
211  }
212 
225  public function updateRedirect( Title $target ) {
226  if ( !$this->isRedirect() ) {
227  return $this;
228  }
229 
230  # Fix the text
231  # Remember that redirect pages can have categories, templates, etc.,
232  # so the regex has to be fairly general
233  $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
234  '[[' . $target->getFullText() . ']]',
235  $this->getNativeData(), 1 );
236 
237  return new WikitextContent( $newText );
238  }
239 
253  public function isCountable( $hasLinks = null, Title $title = null ) {
254  global $wgArticleCountMethod;
255 
256  if ( $this->isRedirect() ) {
257  return false;
258  }
259 
260  $text = $this->getNativeData();
261 
262  switch ( $wgArticleCountMethod ) {
263  case 'any':
264  return true;
265  case 'comma':
266  return strpos( $text, ',' ) !== false;
267  case 'link':
268  if ( $hasLinks === null ) { # not known, find out
269  if ( !$title ) {
270  $context = RequestContext::getMain();
271  $title = $context->getTitle();
272  }
273 
274  $po = $this->getParserOutput( $title, null, null, false );
275  $links = $po->getLinks();
276  $hasLinks = !empty( $links );
277  }
278 
279  return $hasLinks;
280  }
281 
282  return false;
283  }
284 
289  public function getTextForSummary( $maxlength = 250 ) {
290  $truncatedtext = parent::getTextForSummary( $maxlength );
291 
292  # clean up unfinished links
293  # XXX: make this optional? wasn't there in autosummary, but required for
294  # deletion summary.
295  $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
296 
297  return $truncatedtext;
298  }
299 
313  public function getParserOutput( Title $title, $revId = null,
314  ParserOptions $options = null, $generateHtml = true ) {
316 
317  if ( !$options ) {
318  //NOTE: use canonical options per default to produce cacheable output
319  $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
320  }
321 
322  $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
323 
324  return $po;
325  }
326 
330  protected function getHtml() {
331  throw new MWException(
332  "getHtml() not implemented for wikitext. "
333  . "Use getParserOutput()->getText()."
334  );
335  }
336 
346  public function matchMagicWord( MagicWord $word ) {
347  return $word->match( $this->getNativeData() );
348  }
349 
350 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:31
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:189
AbstractContent\isRedirect
isRedirect()
Definition: AbstractContent.php:306
WikitextContent\getRedirectTarget
getRedirectTarget()
Implement redirect extraction for wikitext.
Definition: WikitextContent.php:180
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
WikitextContent\preloadTransform
preloadTransform(Title $title, ParserOptions $popts, $params=array())
Returns a Content object with preload transformations applied (or this object if no transformations a...
Definition: WikitextContent.php:161
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
MagicWord\get
static & get( $id)
Factory: creates an object representing an ID.
Definition: MagicWord.php:238
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:225
$params
$params
Definition: styleTest.css.php:40
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:283
WikitextContent\replaceSection
replaceSection( $section, Content $with, $sectionTitle='')
Definition: WikitextContent.php:69
MWException
MediaWiki exception.
Definition: MWException.php:26
AbstractContent\getContentHandler
getContentHandler()
Definition: AbstractContent.php:85
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
Title\getFullText
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1393
TextContent\copy
copy()
Definition: TextContent.php:63
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
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:253
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
WikitextContent\__construct
__construct( $text)
Definition: WikitextContent.php:35
MagicWord
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition: MagicWord.php:61
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:33
WikitextContent\preSaveTransform
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Returns a Content object with pre-save transformations applied using Parser::preSaveTransform().
Definition: WikitextContent.php:141
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
$section
$section
Definition: Utf8Test.php:88
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
WikitextContent\matchMagicWord
matchMagicWord(MagicWord $word)
This implementation calls $word->match() on the this TextContent object's text.
Definition: WikitextContent.php:346
Content\getNativeData
getNativeData()
Returns native representation of the data.
WikitextContent\getTextForSummary
getTextForSummary( $maxlength=250)
Definition: WikitextContent.php:289
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
Content
Base interface for content objects.
Definition: Content.php:34
AbstractContent\getModel
getModel()
Definition: AbstractContent.php:58
WikitextContent\getSection
getSection( $section)
Definition: WikitextContent.php:46
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$wgParser
$wgParser
Definition: Setup.php:567
WikitextContent\addSectionHeader
addSectionHeader( $header)
Returns a new WikitextContent object with the given section heading prepended.
Definition: WikitextContent.php:122
Content\getModel
getModel()
Returns the ID of the content model used by this Content object.
WikitextContent\getParserOutput
getParserOutput(Title $title, $revId=null, ParserOptions $options=null, $generateHtml=true)
Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
Definition: WikitextContent.php:313
WikitextContent\getHtml
getHtml()
Definition: WikitextContent.php:330
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
MagicWord\match
match( $text)
Returns true if the text contains the word.
Definition: MagicWord.php:442
TextContent\getNativeData
getNativeData()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:118