MediaWiki
REL1_21
|
00001 <?php 00033 class WikitextContent extends TextContent { 00034 00035 public function __construct( $text ) { 00036 parent::__construct( $text, CONTENT_MODEL_WIKITEXT ); 00037 } 00038 00042 public function getSection( $section ) { 00043 global $wgParser; 00044 00045 $text = $this->getNativeData(); 00046 $sect = $wgParser->getSection( $text, $section, false ); 00047 00048 if ( $sect === false ) { 00049 return false; 00050 } else { 00051 return new WikitextContent( $sect ); 00052 } 00053 } 00054 00058 public function replaceSection( $section, Content $with, $sectionTitle = '' ) { 00059 wfProfileIn( __METHOD__ ); 00060 00061 $myModelId = $this->getModel(); 00062 $sectionModelId = $with->getModel(); 00063 00064 if ( $sectionModelId != $myModelId ) { 00065 throw new MWException( "Incompatible content model for section: " . 00066 "document uses $myModelId but " . 00067 "section uses $sectionModelId." ); 00068 } 00069 00070 $oldtext = $this->getNativeData(); 00071 $text = $with->getNativeData(); 00072 00073 if ( $section === '' ) { 00074 wfProfileOut( __METHOD__ ); 00075 return $with; # XXX: copy first? 00076 } if ( $section == 'new' ) { 00077 # Inserting a new section 00078 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' ) 00079 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : ''; 00080 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) { 00081 $text = strlen( trim( $oldtext ) ) > 0 00082 ? "{$oldtext}\n\n{$subject}{$text}" 00083 : "{$subject}{$text}"; 00084 } 00085 } else { 00086 # Replacing an existing section; roll out the big guns 00087 global $wgParser; 00088 00089 $text = $wgParser->replaceSection( $oldtext, $section, $text ); 00090 } 00091 00092 $newContent = new WikitextContent( $text ); 00093 00094 wfProfileOut( __METHOD__ ); 00095 return $newContent; 00096 } 00097 00105 public function addSectionHeader( $header ) { 00106 $text = wfMessage( 'newsectionheaderdefaultlevel' ) 00107 ->rawParams( $header )->inContentLanguage()->text(); 00108 $text .= "\n\n"; 00109 $text .= $this->getNativeData(); 00110 00111 return new WikitextContent( $text ); 00112 } 00113 00123 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { 00124 global $wgParser; 00125 00126 $text = $this->getNativeData(); 00127 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); 00128 rtrim( $pst ); 00129 00130 return ( $text === $pst ) ? $this : new WikitextContent( $pst ); 00131 } 00132 00141 public function preloadTransform( Title $title, ParserOptions $popts ) { 00142 global $wgParser; 00143 00144 $text = $this->getNativeData(); 00145 $plt = $wgParser->getPreloadText( $text, $title, $popts ); 00146 00147 return new WikitextContent( $plt ); 00148 } 00149 00160 public function getRedirectTarget() { 00161 global $wgMaxRedirects; 00162 if ( $wgMaxRedirects < 1 ) { 00163 // redirects are disabled, so quit early 00164 return null; 00165 } 00166 $redir = MagicWord::get( 'redirect' ); 00167 $text = trim( $this->getNativeData() ); 00168 if ( $redir->matchStartAndRemove( $text ) ) { 00169 // Extract the first link and see if it's usable 00170 // Ensure that it really does come directly after #REDIRECT 00171 // Some older redirects included a colon, so don't freak about that! 00172 $m = array(); 00173 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) { 00174 // Strip preceding colon used to "escape" categories, etc. 00175 // and URL-decode links 00176 if ( strpos( $m[1], '%' ) !== false ) { 00177 // Match behavior of inline link parsing here; 00178 $m[1] = rawurldecode( ltrim( $m[1], ':' ) ); 00179 } 00180 $title = Title::newFromText( $m[1] ); 00181 // If the title is a redirect to bad special pages or is invalid, return null 00182 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) { 00183 return null; 00184 } 00185 return $title; 00186 } 00187 } 00188 return null; 00189 } 00190 00203 public function updateRedirect( Title $target ) { 00204 if ( !$this->isRedirect() ) { 00205 return $this; 00206 } 00207 00208 # Fix the text 00209 # Remember that redirect pages can have categories, templates, etc., 00210 # so the regex has to be fairly general 00211 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x', 00212 '[[' . $target->getFullText() . ']]', 00213 $this->getNativeData(), 1 ); 00214 00215 return new WikitextContent( $newText ); 00216 } 00217 00231 public function isCountable( $hasLinks = null, Title $title = null ) { 00232 global $wgArticleCountMethod; 00233 00234 if ( $this->isRedirect() ) { 00235 return false; 00236 } 00237 00238 $text = $this->getNativeData(); 00239 00240 switch ( $wgArticleCountMethod ) { 00241 case 'any': 00242 return true; 00243 case 'comma': 00244 return strpos( $text, ',' ) !== false; 00245 case 'link': 00246 if ( $hasLinks === null ) { # not known, find out 00247 if ( !$title ) { 00248 $context = RequestContext::getMain(); 00249 $title = $context->getTitle(); 00250 } 00251 00252 $po = $this->getParserOutput( $title, null, null, false ); 00253 $links = $po->getLinks(); 00254 $hasLinks = !empty( $links ); 00255 } 00256 00257 return $hasLinks; 00258 } 00259 00260 return false; 00261 } 00262 00263 public function getTextForSummary( $maxlength = 250 ) { 00264 $truncatedtext = parent::getTextForSummary( $maxlength ); 00265 00266 # clean up unfinished links 00267 # XXX: make this optional? wasn't there in autosummary, but required for 00268 # deletion summary. 00269 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext ); 00270 00271 return $truncatedtext; 00272 } 00273 00288 public function getParserOutput( Title $title, 00289 $revId = null, 00290 ParserOptions $options = null, $generateHtml = true 00291 ) { 00292 global $wgParser; 00293 00294 if ( !$options ) { 00295 //NOTE: use canonical options per default to produce cacheable output 00296 $options = $this->getContentHandler()->makeParserOptions( 'canonical' ); 00297 } 00298 00299 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId ); 00300 return $po; 00301 } 00302 00303 protected function getHtml() { 00304 throw new MWException( 00305 "getHtml() not implemented for wikitext. " 00306 . "Use getParserOutput()->getText()." 00307 ); 00308 } 00309 00319 public function matchMagicWord( MagicWord $word ) { 00320 return $word->match( $this->getNativeData() ); 00321 } 00322 }