MediaWiki  REL1_24
TextContent.php
Go to the documentation of this file.
00001 <?php
00035 class TextContent extends AbstractContent {
00036 
00041     public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
00042         parent::__construct( $model_id );
00043 
00044         if ( $text === null || $text === false ) {
00045             wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
00046                 . "This may indicate an error in the caller's scope.", 2 );
00047 
00048             $text = '';
00049         }
00050 
00051         if ( !is_string( $text ) ) {
00052             throw new MWException( "TextContent expects a string in the constructor." );
00053         }
00054 
00055         $this->mText = $text;
00056     }
00057 
00063     public function copy() {
00064         return $this; # NOTE: this is ok since TextContent are immutable.
00065     }
00066 
00067     public function getTextForSummary( $maxlength = 250 ) {
00068         global $wgContLang;
00069 
00070         $text = $this->getNativeData();
00071 
00072         $truncatedtext = $wgContLang->truncate(
00073             preg_replace( "/[\n\r]/", ' ', $text ),
00074             max( 0, $maxlength ) );
00075 
00076         return $truncatedtext;
00077     }
00078 
00084     public function getSize() {
00085         $text = $this->getNativeData();
00086 
00087         return strlen( $text );
00088     }
00089 
00099     public function isCountable( $hasLinks = null ) {
00100         global $wgArticleCountMethod;
00101 
00102         if ( $this->isRedirect() ) {
00103             return false;
00104         }
00105 
00106         if ( $wgArticleCountMethod === 'any' ) {
00107             return true;
00108         }
00109 
00110         return false;
00111     }
00112 
00118     public function getNativeData() {
00119         return $this->mText;
00120     }
00121 
00127     public function getTextForSearchIndex() {
00128         return $this->getNativeData();
00129     }
00130 
00139     public function getWikitextForTransclusion() {
00140         $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
00141 
00142         if ( $wikitext ) {
00143             return $wikitext->getNativeData();
00144         } else {
00145             return false;
00146         }
00147     }
00148 
00159     public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
00160         $text = $this->getNativeData();
00161         $pst = rtrim( $text );
00162 
00163         return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() );
00164     }
00165 
00178     public function diff( Content $that, Language $lang = null ) {
00179         global $wgContLang;
00180 
00181         $this->checkModelID( $that->getModel() );
00182 
00183         // @todo could implement this in DifferenceEngine and just delegate here?
00184 
00185         if ( !$lang ) {
00186             $lang = $wgContLang;
00187         }
00188 
00189         $otext = $this->getNativeData();
00190         $ntext = $that->getNativeData();
00191 
00192         # Note: Use native PHP diff, external engines don't give us abstract output
00193         $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
00194         $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
00195 
00196         $diff = new Diff( $ota, $nta );
00197 
00198         return $diff;
00199     }
00200 
00218     protected function fillParserOutput( Title $title, $revId,
00219         ParserOptions $options, $generateHtml, ParserOutput &$output
00220     ) {
00221         global $wgParser, $wgTextModelsToParse;
00222 
00223         if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
00224             // parse just to get links etc into the database, HTML is replaced below.
00225             $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
00226         }
00227 
00228         if ( $generateHtml ) {
00229             $html = $this->getHtml();
00230         } else {
00231             $html = '';
00232         }
00233 
00234         $output->setText( $html );
00235     }
00236 
00250     protected function getHtml() {
00251         return $this->getHighlightHtml();
00252     }
00253 
00270     protected function getHighlightHtml() {
00271         return htmlspecialchars( $this->getNativeData() );
00272     }
00273 
00287     public function convert( $toModel, $lossy = '' ) {
00288         $converted = parent::convert( $toModel, $lossy );
00289 
00290         if ( $converted !== false ) {
00291             return $converted;
00292         }
00293 
00294         $toHandler = ContentHandler::getForModelID( $toModel );
00295 
00296         if ( $toHandler instanceof TextContentHandler ) {
00297             // NOTE: ignore content serialization format - it's just text anyway.
00298             $text = $this->getNativeData();
00299             $converted = $toHandler->unserializeContent( $text );
00300         }
00301 
00302         return $converted;
00303     }
00304 
00305 }