MediaWiki
REL1_23
|
00001 <?php 00033 class WikitextContent extends TextContent { 00034 00035 public function __construct( $text ) { 00036 parent::__construct( $text, CONTENT_MODEL_WIKITEXT ); 00037 } 00038 00046 public function getSection( $section ) { 00047 global $wgParser; 00048 00049 $text = $this->getNativeData(); 00050 $sect = $wgParser->getSection( $text, $section, false ); 00051 00052 if ( $sect === false ) { 00053 return false; 00054 } else { 00055 return new WikitextContent( $sect ); 00056 } 00057 } 00058 00069 public function replaceSection( $section, Content $with, $sectionTitle = '' ) { 00070 wfProfileIn( __METHOD__ ); 00071 00072 $myModelId = $this->getModel(); 00073 $sectionModelId = $with->getModel(); 00074 00075 if ( $sectionModelId != $myModelId ) { 00076 wfProfileOut( __METHOD__ ); 00077 throw new MWException( "Incompatible content model for section: " . 00078 "document uses $myModelId but " . 00079 "section uses $sectionModelId." ); 00080 } 00081 00082 $oldtext = $this->getNativeData(); 00083 $text = $with->getNativeData(); 00084 00085 if ( $section === '' ) { 00086 wfProfileOut( __METHOD__ ); 00087 00088 return $with; # XXX: copy first? 00089 } 00090 00091 if ( $section == 'new' ) { 00092 # Inserting a new section 00093 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' ) 00094 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : ''; 00095 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) { 00096 $text = strlen( trim( $oldtext ) ) > 0 00097 ? "{$oldtext}\n\n{$subject}{$text}" 00098 : "{$subject}{$text}"; 00099 } 00100 } else { 00101 # Replacing an existing section; roll out the big guns 00102 global $wgParser; 00103 00104 $text = $wgParser->replaceSection( $oldtext, $section, $text ); 00105 } 00106 00107 $newContent = new WikitextContent( $text ); 00108 00109 wfProfileOut( __METHOD__ ); 00110 00111 return $newContent; 00112 } 00113 00122 public function addSectionHeader( $header ) { 00123 $text = wfMessage( 'newsectionheaderdefaultlevel' ) 00124 ->rawParams( $header )->inContentLanguage()->text(); 00125 $text .= "\n\n"; 00126 $text .= $this->getNativeData(); 00127 00128 return new WikitextContent( $text ); 00129 } 00130 00141 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { 00142 global $wgParser; 00143 00144 $text = $this->getNativeData(); 00145 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); 00146 rtrim( $pst ); 00147 00148 return ( $text === $pst ) ? $this : new WikitextContent( $pst ); 00149 } 00150 00161 public function preloadTransform( Title $title, ParserOptions $popts, $params = array() ) { 00162 global $wgParser; 00163 00164 $text = $this->getNativeData(); 00165 $plt = $wgParser->getPreloadText( $text, $title, $popts, $params ); 00166 00167 return new WikitextContent( $plt ); 00168 } 00169 00180 public function getRedirectTarget() { 00181 global $wgMaxRedirects; 00182 if ( $wgMaxRedirects < 1 ) { 00183 // redirects are disabled, so quit early 00184 return null; 00185 } 00186 $redir = MagicWord::get( 'redirect' ); 00187 $text = trim( $this->getNativeData() ); 00188 if ( $redir->matchStartAndRemove( $text ) ) { 00189 // Extract the first link and see if it's usable 00190 // Ensure that it really does come directly after #REDIRECT 00191 // Some older redirects included a colon, so don't freak about that! 00192 $m = array(); 00193 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) { 00194 // Strip preceding colon used to "escape" categories, etc. 00195 // and URL-decode links 00196 if ( strpos( $m[1], '%' ) !== false ) { 00197 // Match behavior of inline link parsing here; 00198 $m[1] = rawurldecode( ltrim( $m[1], ':' ) ); 00199 } 00200 $title = Title::newFromText( $m[1] ); 00201 // If the title is a redirect to bad special pages or is invalid, return null 00202 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) { 00203 return null; 00204 } 00205 00206 return $title; 00207 } 00208 } 00209 00210 return null; 00211 } 00212 00225 public function updateRedirect( Title $target ) { 00226 if ( !$this->isRedirect() ) { 00227 return $this; 00228 } 00229 00230 # Fix the text 00231 # Remember that redirect pages can have categories, templates, etc., 00232 # so the regex has to be fairly general 00233 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x', 00234 '[[' . $target->getFullText() . ']]', 00235 $this->getNativeData(), 1 ); 00236 00237 return new WikitextContent( $newText ); 00238 } 00239 00253 public function isCountable( $hasLinks = null, Title $title = null ) { 00254 global $wgArticleCountMethod; 00255 00256 if ( $this->isRedirect() ) { 00257 return false; 00258 } 00259 00260 $text = $this->getNativeData(); 00261 00262 switch ( $wgArticleCountMethod ) { 00263 case 'any': 00264 return true; 00265 case 'comma': 00266 return strpos( $text, ',' ) !== false; 00267 case 'link': 00268 if ( $hasLinks === null ) { # not known, find out 00269 if ( !$title ) { 00270 $context = RequestContext::getMain(); 00271 $title = $context->getTitle(); 00272 } 00273 00274 $po = $this->getParserOutput( $title, null, null, false ); 00275 $links = $po->getLinks(); 00276 $hasLinks = !empty( $links ); 00277 } 00278 00279 return $hasLinks; 00280 } 00281 00282 return false; 00283 } 00284 00289 public function getTextForSummary( $maxlength = 250 ) { 00290 $truncatedtext = parent::getTextForSummary( $maxlength ); 00291 00292 # clean up unfinished links 00293 # XXX: make this optional? wasn't there in autosummary, but required for 00294 # deletion summary. 00295 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext ); 00296 00297 return $truncatedtext; 00298 } 00299 00313 public function getParserOutput( Title $title, $revId = null, 00314 ParserOptions $options = null, $generateHtml = true ) { 00315 global $wgParser; 00316 00317 if ( !$options ) { 00318 //NOTE: use canonical options per default to produce cacheable output 00319 $options = $this->getContentHandler()->makeParserOptions( 'canonical' ); 00320 } 00321 00322 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId ); 00323 00324 return $po; 00325 } 00326 00330 protected function getHtml() { 00331 throw new MWException( 00332 "getHtml() not implemented for wikitext. " 00333 . "Use getParserOutput()->getText()." 00334 ); 00335 } 00336 00346 public function matchMagicWord( MagicWord $word ) { 00347 return $word->match( $this->getNativeData() ); 00348 } 00349 00350 }