5 class WikitextContent
extends TextContent
{
7 public function __construct( $text ) {
8 parent
::__construct( $text, CONTENT_MODEL_WIKITEXT
);
12 * @see Content::getSection()
14 public function getSection( $section ) {
17 $text = $this->getNativeData();
18 $sect = $wgParser->getSection( $text, $section, false );
20 return new WikitextContent( $sect );
24 * @see Content::replaceSection()
26 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
27 wfProfileIn( __METHOD__
);
29 $myModelId = $this->getModel();
30 $sectionModelId = $with->getModel();
32 if ( $sectionModelId != $myModelId ) {
33 throw new MWException( "Incompatible content model for section: " .
34 "document uses $myModelId but " .
35 "section uses $sectionModelId." );
38 $oldtext = $this->getNativeData();
39 $text = $with->getNativeData();
41 if ( $section === '' ) {
42 return $with; # XXX: copy first?
43 } if ( $section == 'new' ) {
44 # Inserting a new section
45 $subject = $sectionTitle ?
wfMessage( 'newsectionheaderdefaultlevel' )
46 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
47 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
48 $text = strlen( trim( $oldtext ) ) > 0
49 ?
"{$oldtext}\n\n{$subject}{$text}"
50 : "{$subject}{$text}";
53 # Replacing an existing section; roll out the big guns
56 $text = $wgParser->replaceSection( $oldtext, $section, $text );
59 $newContent = new WikitextContent( $text );
61 wfProfileOut( __METHOD__
);
66 * Returns a new WikitextContent object with the given section heading
69 * @param $header string
72 public function addSectionHeader( $header ) {
73 $text = wfMessage( 'newsectionheaderdefaultlevel' )
74 ->rawParams( $header )->inContentLanguage()->text();
76 $text .= $this->getNativeData();
78 return new WikitextContent( $text );
82 * Returns a Content object with pre-save transformations applied using
83 * Parser::preSaveTransform().
87 * @param $popts ParserOptions
90 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
93 $text = $this->getNativeData();
94 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
96 return new WikitextContent( $pst );
100 * Returns a Content object with preload transformations applied (or this
101 * object if no transformations apply).
103 * @param $title Title
104 * @param $popts ParserOptions
107 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
110 $text = $this->getNativeData();
111 $plt = $wgParser->getPreloadText( $text, $title, $popts );
113 return new WikitextContent( $plt );
117 * Implement redirect extraction for wikitext.
121 * @note: migrated here from Title::newFromRedirectInternal()
123 * @see Content::getRedirectTarget
124 * @see AbstractContent::getRedirectTarget
126 public function getRedirectTarget() {
127 global $wgMaxRedirects;
128 if ( $wgMaxRedirects < 1 ) {
129 // redirects are disabled, so quit early
132 $redir = MagicWord
::get( 'redirect' );
133 $text = trim( $this->getNativeData() );
134 if ( $redir->matchStartAndRemove( $text ) ) {
135 // Extract the first link and see if it's usable
136 // Ensure that it really does come directly after #REDIRECT
137 // Some older redirects included a colon, so don't freak about that!
139 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
140 // Strip preceding colon used to "escape" categories, etc.
141 // and URL-decode links
142 if ( strpos( $m[1], '%' ) !== false ) {
143 // Match behavior of inline link parsing here;
144 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
146 $title = Title
::newFromText( $m[1] );
147 // If the title is a redirect to bad special pages or is invalid, return null
148 if ( !$title instanceof Title ||
!$title->isValidRedirectTarget() ) {
158 * @see Content::updateRedirect()
160 * This implementation replaces the first link on the page with the given new target
161 * if this Content object is a redirect. Otherwise, this method returns $this.
165 * @param Title $target
167 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
169 public function updateRedirect( Title
$target ) {
170 if ( !$this->isRedirect() ) {
175 # Remember that redirect pages can have categories, templates, etc.,
176 # so the regex has to be fairly general
177 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
178 '[[' . $target->getFullText() . ']]',
179 $this->getNativeData(), 1 );
181 return new WikitextContent( $newText );
185 * Returns true if this content is not a redirect, and this content's text
186 * is countable according to the criteria defined by $wgArticleCountMethod.
188 * @param $hasLinks Bool if it is known whether this content contains
189 * links, provide this information here, to avoid redundant parsing to
191 * @param $title null|\Title
193 * @internal param \IContextSource $context context for parsing if necessary
195 * @return bool True if the content is countable
197 public function isCountable( $hasLinks = null, Title
$title = null ) {
198 global $wgArticleCountMethod;
200 if ( $this->isRedirect( ) ) {
204 $text = $this->getNativeData();
206 switch ( $wgArticleCountMethod ) {
210 return strpos( $text, ',' ) !== false;
212 if ( $hasLinks === null ) { # not known, find out
214 $context = RequestContext
::getMain();
215 $title = $context->getTitle();
218 $po = $this->getParserOutput( $title, null, null, false );
219 $links = $po->getLinks();
220 $hasLinks = !empty( $links );
229 public function getTextForSummary( $maxlength = 250 ) {
230 $truncatedtext = parent
::getTextForSummary( $maxlength );
232 # clean up unfinished links
233 # XXX: make this optional? wasn't there in autosummary, but required for
235 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
237 return $truncatedtext;
241 * Returns a ParserOutput object resulting from parsing the content's text
246 * @param $content Content the content to render
247 * @param $title \Title
249 * @param $options null|ParserOptions
250 * @param $generateHtml bool
252 * @internal param \IContextSource|null $context
253 * @return ParserOutput representing the HTML form of the text
255 public function getParserOutput( Title
$title,
257 ParserOptions
$options = null, $generateHtml = true
262 //NOTE: use canonical options per default to produce cacheable output
263 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
266 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
270 protected function getHtml() {
271 throw new MWException(
272 "getHtml() not implemented for wikitext. "
273 . "Use getParserOutput()->getText()."
278 * @see Content::matchMagicWord()
280 * This implementation calls $word->match() on the this TextContent object's text.
282 * @param MagicWord $word
284 * @return bool whether this Content object matches the given magic word.
286 public function matchMagicWord( MagicWord
$word ) {
287 return $word->match( $this->getNativeData() );