MediaWiki  master
TextContent.php
Go to the documentation of this file.
00001 <?php
00002 
00030 class TextContent extends AbstractContent {
00031 
00032         public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
00033                 parent::__construct( $model_id );
00034 
00035                 if ( $text === null || $text === false ) {
00036                         wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
00037                                         . "This may indicate an error in the caller's scope." );
00038 
00039                         $text = '';
00040                 }
00041 
00042                 if ( !is_string( $text ) ) {
00043                         throw new MWException( "TextContent expects a string in the constructor." );
00044                 }
00045 
00046                 $this->mText = $text;
00047         }
00048 
00049         public function copy() {
00050                 return $this; # NOTE: this is ok since TextContent are immutable.
00051         }
00052 
00053         public function getTextForSummary( $maxlength = 250 ) {
00054                 global $wgContLang;
00055 
00056                 $text = $this->getNativeData();
00057 
00058                 $truncatedtext = $wgContLang->truncate(
00059                         preg_replace( "/[\n\r]/", ' ', $text ),
00060                         max( 0, $maxlength ) );
00061 
00062                 return $truncatedtext;
00063         }
00064 
00070         public function getSize( ) {
00071                 $text = $this->getNativeData( );
00072                 return strlen( $text );
00073         }
00074 
00084         public function isCountable( $hasLinks = null ) {
00085                 global $wgArticleCountMethod;
00086 
00087                 if ( $this->isRedirect( ) ) {
00088                         return false;
00089                 }
00090 
00091                 if ( $wgArticleCountMethod === 'any' ) {
00092                         return true;
00093                 }
00094 
00095                 return false;
00096         }
00097 
00103         public function getNativeData( ) {
00104                 $text = $this->mText;
00105                 return $text;
00106         }
00107 
00113         public function getTextForSearchIndex( ) {
00114                 return $this->getNativeData();
00115         }
00116 
00125         public function getWikitextForTransclusion( ) {
00126                 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
00127 
00128                 if ( $wikitext ) {
00129                         return $wikitext->getNativeData();
00130                 } else {
00131                         return false;
00132                 }
00133         }
00134 
00144         public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
00145                 $text = $this->getNativeData();
00146                 $pst = rtrim( $text );
00147 
00148                 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
00149         }
00150 
00164         public function diff( Content $that, Language $lang = null ) {
00165                 global $wgContLang;
00166 
00167                 $this->checkModelID( $that->getModel() );
00168 
00169                 # @todo: could implement this in DifferenceEngine and just delegate here?
00170 
00171                 if ( !$lang ) $lang = $wgContLang;
00172 
00173                 $otext = $this->getNativeData();
00174                 $ntext = $this->getNativeData();
00175 
00176                 # Note: Use native PHP diff, external engines don't give us abstract output
00177                 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
00178                 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
00179 
00180                 $diff = new Diff( $ota, $nta );
00181                 return $diff;
00182         }
00183 
00184 
00196         public function getParserOutput( Title $title,
00197                 $revId = null,
00198                 ParserOptions $options = null, $generateHtml = true
00199         ) {
00200                 global $wgParser, $wgTextModelsToParse;
00201 
00202                 if ( !$options ) {
00203                         //NOTE: use canonical options per default to produce cacheable output
00204                         $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
00205                 }
00206 
00207                 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
00208                         // parse just to get links etc into the database
00209                         $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
00210                 } else {
00211                         $po = new ParserOutput();
00212                 }
00213 
00214                 if ( $generateHtml ) {
00215                         $html = $this->getHtml();
00216                 } else {
00217                         $html = '';
00218                 }
00219 
00220                 $po->setText( $html );
00221                 return $po;
00222         }
00223 
00235         protected function getHtml() {
00236                 return $this->getHighlightHtml();
00237         }
00238 
00245         protected function getHighlightHtml( ) {
00246                 # TODO: make Highlighter interface, use highlighter here, if available
00247                 return htmlspecialchars( $this->getNativeData() );
00248         }
00249 
00263         public function convert( $toModel, $lossy = '' ) {
00264                 $converted = parent::convert( $toModel, $lossy );
00265 
00266                 if ( $converted !== false ) {
00267                         return $converted;
00268                 }
00269 
00270                 $toHandler = ContentHandler::getForModelID( $toModel );
00271 
00272                 if ( $toHandler instanceof TextContentHandler ) {
00273                         //NOTE: ignore content serialization format - it's just text anyway.
00274                         $text = $this->getNativeData();
00275                         $converted = $toHandler->unserializeContent( $text );
00276                 }
00277 
00278                 return $converted;
00279         }
00280 }