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
{
35 public function __construct( $text ) {
36 parent
::__construct( $text, CONTENT_MODEL_WIKITEXT
);
40 * @see Content::getSection()
42 public function getSection( $section ) {
45 $text = $this->getNativeData();
46 $sect = $wgParser->getSection( $text, $section, false );
48 if ( $sect === false ) {
51 return new WikitextContent( $sect );
56 * @see Content::replaceSection()
58 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
59 wfProfileIn( __METHOD__
);
61 $myModelId = $this->getModel();
62 $sectionModelId = $with->getModel();
64 if ( $sectionModelId != $myModelId ) {
65 wfProfileOut( __METHOD__
);
66 throw new MWException( "Incompatible content model for section: " .
67 "document uses $myModelId but " .
68 "section uses $sectionModelId." );
71 $oldtext = $this->getNativeData();
72 $text = $with->getNativeData();
74 if ( $section === '' ) {
75 wfProfileOut( __METHOD__
);
76 return $with; # XXX: copy first?
77 } if ( $section == 'new' ) {
78 # Inserting a new section
79 $subject = $sectionTitle ?
wfMessage( 'newsectionheaderdefaultlevel' )
80 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
81 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
82 $text = strlen( trim( $oldtext ) ) > 0
83 ?
"{$oldtext}\n\n{$subject}{$text}"
84 : "{$subject}{$text}";
87 # Replacing an existing section; roll out the big guns
90 $text = $wgParser->replaceSection( $oldtext, $section, $text );
93 $newContent = new WikitextContent( $text );
95 wfProfileOut( __METHOD__
);
100 * Returns a new WikitextContent object with the given section heading
103 * @param $header string
106 public function addSectionHeader( $header ) {
107 $text = wfMessage( 'newsectionheaderdefaultlevel' )
108 ->rawParams( $header )->inContentLanguage()->text();
110 $text .= $this->getNativeData();
112 return new WikitextContent( $text );
116 * Returns a Content object with pre-save transformations applied using
117 * Parser::preSaveTransform().
119 * @param $title Title
121 * @param $popts ParserOptions
124 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
127 $text = $this->getNativeData();
128 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
131 return ( $text === $pst ) ?
$this : new WikitextContent( $pst );
135 * Returns a Content object with preload transformations applied (or this
136 * object if no transformations apply).
138 * @param $title Title
139 * @param $popts ParserOptions
142 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
145 $text = $this->getNativeData();
146 $plt = $wgParser->getPreloadText( $text, $title, $popts );
148 return new WikitextContent( $plt );
152 * Implement redirect extraction for wikitext.
156 * @note: migrated here from Title::newFromRedirectInternal()
158 * @see Content::getRedirectTarget
159 * @see AbstractContent::getRedirectTarget
161 public function getRedirectTarget() {
162 global $wgMaxRedirects;
163 if ( $wgMaxRedirects < 1 ) {
164 // redirects are disabled, so quit early
167 $redir = MagicWord
::get( 'redirect' );
168 $text = trim( $this->getNativeData() );
169 if ( $redir->matchStartAndRemove( $text ) ) {
170 // Extract the first link and see if it's usable
171 // Ensure that it really does come directly after #REDIRECT
172 // Some older redirects included a colon, so don't freak about that!
174 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
175 // Strip preceding colon used to "escape" categories, etc.
176 // and URL-decode links
177 if ( strpos( $m[1], '%' ) !== false ) {
178 // Match behavior of inline link parsing here;
179 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
181 $title = Title
::newFromText( $m[1] );
182 // If the title is a redirect to bad special pages or is invalid, return null
183 if ( !$title instanceof Title ||
!$title->isValidRedirectTarget() ) {
193 * @see Content::updateRedirect()
195 * This implementation replaces the first link on the page with the given new target
196 * if this Content object is a redirect. Otherwise, this method returns $this.
200 * @param Title $target
202 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
204 public function updateRedirect( Title
$target ) {
205 if ( !$this->isRedirect() ) {
210 # Remember that redirect pages can have categories, templates, etc.,
211 # so the regex has to be fairly general
212 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
213 '[[' . $target->getFullText() . ']]',
214 $this->getNativeData(), 1 );
216 return new WikitextContent( $newText );
220 * Returns true if this content is not a redirect, and this content's text
221 * is countable according to the criteria defined by $wgArticleCountMethod.
223 * @param bool $hasLinks if it is known whether this content contains
224 * links, provide this information here, to avoid redundant parsing to
225 * find out (default: null).
226 * @param $title Title: (default: null)
228 * @internal param \IContextSource $context context for parsing if necessary
230 * @return bool True if the content is countable
232 public function isCountable( $hasLinks = null, Title
$title = null ) {
233 global $wgArticleCountMethod;
235 if ( $this->isRedirect() ) {
239 $text = $this->getNativeData();
241 switch ( $wgArticleCountMethod ) {
245 return strpos( $text, ',' ) !== false;
247 if ( $hasLinks === null ) { # not known, find out
249 $context = RequestContext
::getMain();
250 $title = $context->getTitle();
253 $po = $this->getParserOutput( $title, null, null, false );
254 $links = $po->getLinks();
255 $hasLinks = !empty( $links );
264 public function getTextForSummary( $maxlength = 250 ) {
265 $truncatedtext = parent
::getTextForSummary( $maxlength );
267 # clean up unfinished links
268 # XXX: make this optional? wasn't there in autosummary, but required for
270 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
272 return $truncatedtext;
276 * Returns a ParserOutput object resulting from parsing the content's text
281 * @param $title Title
282 * @param int $revId Revision to pass to the parser (default: null)
283 * @param $options ParserOptions (default: null)
284 * @param bool $generateHtml (default: false)
286 * @internal param \IContextSource|null $context
287 * @return ParserOutput representing the HTML form of the text
289 public function getParserOutput( Title
$title,
291 ParserOptions
$options = null, $generateHtml = true
296 //NOTE: use canonical options per default to produce cacheable output
297 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
300 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
304 protected function getHtml() {
305 throw new MWException(
306 "getHtml() not implemented for wikitext. "
307 . "Use getParserOutput()->getText()."
312 * @see Content::matchMagicWord()
314 * This implementation calls $word->match() on the this TextContent object's text.
316 * @param MagicWord $word
318 * @return bool whether this Content object matches the given magic word.
320 public function matchMagicWord( MagicWord
$word ) {
321 return $word->match( $this->getNativeData() );