a17bf319a7dd53399f28c2a7e7ae99c3bcb16dbe
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 if ( $sect === false ) {
23 return new WikitextContent( $sect );
28 * @see Content::replaceSection()
30 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
31 wfProfileIn( __METHOD__
);
33 $myModelId = $this->getModel();
34 $sectionModelId = $with->getModel();
36 if ( $sectionModelId != $myModelId ) {
37 throw new MWException( "Incompatible content model for section: " .
38 "document uses $myModelId but " .
39 "section uses $sectionModelId." );
42 $oldtext = $this->getNativeData();
43 $text = $with->getNativeData();
45 if ( $section === '' ) {
46 return $with; # XXX: copy first?
47 } if ( $section == 'new' ) {
48 # Inserting a new section
49 $subject = $sectionTitle ?
wfMessage( 'newsectionheaderdefaultlevel' )
50 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
51 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
52 $text = strlen( trim( $oldtext ) ) > 0
53 ?
"{$oldtext}\n\n{$subject}{$text}"
54 : "{$subject}{$text}";
57 # Replacing an existing section; roll out the big guns
60 $text = $wgParser->replaceSection( $oldtext, $section, $text );
63 $newContent = new WikitextContent( $text );
65 wfProfileOut( __METHOD__
);
70 * Returns a new WikitextContent object with the given section heading
73 * @param $header string
76 public function addSectionHeader( $header ) {
77 $text = wfMessage( 'newsectionheaderdefaultlevel' )
78 ->rawParams( $header )->inContentLanguage()->text();
80 $text .= $this->getNativeData();
82 return new WikitextContent( $text );
86 * Returns a Content object with pre-save transformations applied using
87 * Parser::preSaveTransform().
91 * @param $popts ParserOptions
94 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
97 $text = $this->getNativeData();
98 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
100 return new WikitextContent( $pst );
104 * Returns a Content object with preload transformations applied (or this
105 * object if no transformations apply).
107 * @param $title Title
108 * @param $popts ParserOptions
111 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
114 $text = $this->getNativeData();
115 $plt = $wgParser->getPreloadText( $text, $title, $popts );
117 return new WikitextContent( $plt );
121 * Implement redirect extraction for wikitext.
125 * @note: migrated here from Title::newFromRedirectInternal()
127 * @see Content::getRedirectTarget
128 * @see AbstractContent::getRedirectTarget
130 public function getRedirectTarget() {
131 global $wgMaxRedirects;
132 if ( $wgMaxRedirects < 1 ) {
133 // redirects are disabled, so quit early
136 $redir = MagicWord
::get( 'redirect' );
137 $text = trim( $this->getNativeData() );
138 if ( $redir->matchStartAndRemove( $text ) ) {
139 // Extract the first link and see if it's usable
140 // Ensure that it really does come directly after #REDIRECT
141 // Some older redirects included a colon, so don't freak about that!
143 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
144 // Strip preceding colon used to "escape" categories, etc.
145 // and URL-decode links
146 if ( strpos( $m[1], '%' ) !== false ) {
147 // Match behavior of inline link parsing here;
148 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
150 $title = Title
::newFromText( $m[1] );
151 // If the title is a redirect to bad special pages or is invalid, return null
152 if ( !$title instanceof Title ||
!$title->isValidRedirectTarget() ) {
162 * @see Content::updateRedirect()
164 * This implementation replaces the first link on the page with the given new target
165 * if this Content object is a redirect. Otherwise, this method returns $this.
169 * @param Title $target
171 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
173 public function updateRedirect( Title
$target ) {
174 if ( !$this->isRedirect() ) {
179 # Remember that redirect pages can have categories, templates, etc.,
180 # so the regex has to be fairly general
181 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
182 '[[' . $target->getFullText() . ']]',
183 $this->getNativeData(), 1 );
185 return new WikitextContent( $newText );
189 * Returns true if this content is not a redirect, and this content's text
190 * is countable according to the criteria defined by $wgArticleCountMethod.
192 * @param $hasLinks Bool if it is known whether this content contains
193 * links, provide this information here, to avoid redundant parsing to
195 * @param $title null|\Title
197 * @internal param \IContextSource $context context for parsing if necessary
199 * @return bool True if the content is countable
201 public function isCountable( $hasLinks = null, Title
$title = null ) {
202 global $wgArticleCountMethod;
204 if ( $this->isRedirect( ) ) {
208 $text = $this->getNativeData();
210 switch ( $wgArticleCountMethod ) {
214 return strpos( $text, ',' ) !== false;
216 if ( $hasLinks === null ) { # not known, find out
218 $context = RequestContext
::getMain();
219 $title = $context->getTitle();
222 $po = $this->getParserOutput( $title, null, null, false );
223 $links = $po->getLinks();
224 $hasLinks = !empty( $links );
233 public function getTextForSummary( $maxlength = 250 ) {
234 $truncatedtext = parent
::getTextForSummary( $maxlength );
236 # clean up unfinished links
237 # XXX: make this optional? wasn't there in autosummary, but required for
239 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
241 return $truncatedtext;
245 * Returns a ParserOutput object resulting from parsing the content's text
250 * @param $content Content the content to render
251 * @param $title \Title
253 * @param $options null|ParserOptions
254 * @param $generateHtml bool
256 * @internal param \IContextSource|null $context
257 * @return ParserOutput representing the HTML form of the text
259 public function getParserOutput( Title
$title,
261 ParserOptions
$options = null, $generateHtml = true
266 //NOTE: use canonical options per default to produce cacheable output
267 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
270 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
274 protected function getHtml() {
275 throw new MWException(
276 "getHtml() not implemented for wikitext. "
277 . "Use getParserOutput()->getText()."
282 * @see Content::matchMagicWord()
284 * This implementation calls $word->match() on the this TextContent object's text.
286 * @param MagicWord $word
288 * @return bool whether this Content object matches the given magic word.
290 public function matchMagicWord( MagicWord
$word ) {
291 return $word->match( $this->getNativeData() );