3 * Content object for wiki text pages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * @author Daniel Kinzler
29 * Content object for wiki text pages.
33 class WikitextContent
extends TextContent
{
34 public function __construct( $text ) {
35 parent
::__construct( $text, CONTENT_MODEL_WIKITEXT
);
39 * @see Content::getSection()
41 public function getSection( $section ) {
44 $text = $this->getNativeData();
45 $sect = $wgParser->getSection( $text, $section, false );
47 if ( $sect === false ) {
50 return new WikitextContent( $sect );
55 * @see Content::replaceSection()
57 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
58 wfProfileIn( __METHOD__
);
60 $myModelId = $this->getModel();
61 $sectionModelId = $with->getModel();
63 if ( $sectionModelId != $myModelId ) {
64 wfProfileOut( __METHOD__
);
65 throw new MWException( "Incompatible content model for section: " .
66 "document uses $myModelId but " .
67 "section uses $sectionModelId." );
70 $oldtext = $this->getNativeData();
71 $text = $with->getNativeData();
73 if ( $section === '' ) {
74 wfProfileOut( __METHOD__
);
76 return $with; # XXX: copy first?
79 if ( $section == 'new' ) {
80 # Inserting a new section
81 $subject = $sectionTitle ?
wfMessage( 'newsectionheaderdefaultlevel' )
82 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
83 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
84 $text = strlen( trim( $oldtext ) ) > 0
85 ?
"{$oldtext}\n\n{$subject}{$text}"
86 : "{$subject}{$text}";
89 # Replacing an existing section; roll out the big guns
92 $text = $wgParser->replaceSection( $oldtext, $section, $text );
95 $newContent = new WikitextContent( $text );
97 wfProfileOut( __METHOD__
);
103 * Returns a new WikitextContent object with the given section heading
106 * @param $header string
109 public function addSectionHeader( $header ) {
110 $text = wfMessage( 'newsectionheaderdefaultlevel' )
111 ->rawParams( $header )->inContentLanguage()->text();
113 $text .= $this->getNativeData();
115 return new WikitextContent( $text );
119 * Returns a Content object with pre-save transformations applied using
120 * Parser::preSaveTransform().
122 * @param $title Title
124 * @param $popts ParserOptions
127 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
130 $text = $this->getNativeData();
131 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
134 return ( $text === $pst ) ?
$this : new WikitextContent( $pst );
138 * Returns a Content object with preload transformations applied (or this
139 * object if no transformations apply).
141 * @param $title Title
142 * @param $popts ParserOptions
145 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
148 $text = $this->getNativeData();
149 $plt = $wgParser->getPreloadText( $text, $title, $popts );
151 return new WikitextContent( $plt );
155 * Extract the redirect target and the remaining text on the page.
157 * @note: migrated here from Title::newFromRedirectInternal()
160 * @return array 2 elements: Title|null and string
162 protected function getRedirectTargetAndText() {
163 global $wgMaxRedirects;
164 if ( $wgMaxRedirects < 1 ) {
165 // redirects are disabled, so quit early
166 return array( null, $this->getNativeData() );
168 $redir = MagicWord
::get( 'redirect' );
169 $text = ltrim( $this->getNativeData() );
170 if ( $redir->matchStartAndRemove( $text ) ) {
171 // Extract the first link and see if it's usable
172 // Ensure that it really does come directly after #REDIRECT
173 // Some older redirects included a colon, so don't freak about that!
175 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
176 // Strip preceding colon used to "escape" categories, etc.
177 // and URL-decode links
178 if ( strpos( $m[1], '%' ) !== false ) {
179 // Match behavior of inline link parsing here;
180 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
182 $title = Title
::newFromText( $m[1] );
183 // If the title is a redirect to bad special pages or is invalid, return null
184 if ( !$title instanceof Title ||
!$title->isValidRedirectTarget() ) {
185 return array( null, $this->getNativeData() );
188 return array( $title, substr( $text, strlen( $m[0] ) ) );
192 return array( null, $this->getNativeData() );
196 * Implement redirect extraction for wikitext.
200 * @see Content::getRedirectTarget
201 * @see AbstractContent::getRedirectTarget
203 public function getRedirectTarget() {
204 list( $title, ) = $this->getRedirectTargetAndText();
210 * @see Content::updateRedirect()
212 * This implementation replaces the first link on the page with the given new target
213 * if this Content object is a redirect. Otherwise, this method returns $this.
217 * @param Title $target
219 * @return Content a new Content object with the updated redirect (or $this
220 * if this Content object isn't a redirect)
222 public function updateRedirect( Title
$target ) {
223 if ( !$this->isRedirect() ) {
228 # Remember that redirect pages can have categories, templates, etc.,
229 # so the regex has to be fairly general
230 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
231 '[[' . $target->getFullText() . ']]',
232 $this->getNativeData(), 1 );
234 return new WikitextContent( $newText );
238 * Returns true if this content is not a redirect, and this content's text
239 * is countable according to the criteria defined by $wgArticleCountMethod.
241 * @param bool $hasLinks if it is known whether this content contains
242 * links, provide this information here, to avoid redundant parsing to
243 * find out (default: null).
244 * @param $title Title: (default: null)
246 * @internal param \IContextSource $context context for parsing if necessary
248 * @return bool True if the content is countable
250 public function isCountable( $hasLinks = null, Title
$title = null ) {
251 global $wgArticleCountMethod;
253 if ( $this->isRedirect() ) {
257 $text = $this->getNativeData();
259 switch ( $wgArticleCountMethod ) {
263 return strpos( $text, ',' ) !== false;
265 if ( $hasLinks === null ) { # not known, find out
267 $context = RequestContext
::getMain();
268 $title = $context->getTitle();
271 $po = $this->getParserOutput( $title, null, null, false );
272 $links = $po->getLinks();
273 $hasLinks = !empty( $links );
282 public function getTextForSummary( $maxlength = 250 ) {
283 $truncatedtext = parent
::getTextForSummary( $maxlength );
285 # clean up unfinished links
286 # XXX: make this optional? wasn't there in autosummary, but required for
288 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
290 return $truncatedtext;
294 * Returns a ParserOutput object resulting from parsing the content's text
299 * @param $title Title
300 * @param int $revId Revision to pass to the parser (default: null)
301 * @param $options ParserOptions (default: null)
302 * @param bool $generateHtml (default: false)
304 * @internal param \IContextSource|null $context
305 * @return ParserOutput representing the HTML form of the text
307 public function getParserOutput( Title
$title,
309 ParserOptions
$options = null, $generateHtml = true
314 //NOTE: use canonical options per default to produce cacheable output
315 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
318 list( $redir, $text ) = $this->getRedirectTargetAndText();
319 $po = $wgParser->parse( $text, $title, $options, true, true, $revId );
321 // Add redirect indicator at the top
323 // Make sure to include the redirect link in pagelinks
324 $po->addLink( $redir );
325 if ( $generateHtml ) {
326 $chain = $this->getRedirectChain();
328 Article
::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
337 protected function getHtml() {
338 throw new MWException(
339 "getHtml() not implemented for wikitext. "
340 . "Use getParserOutput()->getText()."
345 * @see Content::matchMagicWord()
347 * This implementation calls $word->match() on the this TextContent object's text.
349 * @param MagicWord $word
351 * @return bool whether this Content object matches the given magic word.
353 public function matchMagicWord( MagicWord
$word ) {
354 return $word->match( $this->getNativeData() );