MediaWiki  REL1_22
TextContent.php
Go to the documentation of this file.
00001 <?php
00035 class TextContent extends AbstractContent {
00036 
00037     public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
00038         parent::__construct( $model_id );
00039 
00040         if ( $text === null || $text === false ) {
00041             wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
00042                     . "This may indicate an error in the caller's scope." );
00043 
00044             $text = '';
00045         }
00046 
00047         if ( !is_string( $text ) ) {
00048             throw new MWException( "TextContent expects a string in the constructor." );
00049         }
00050 
00051         $this->mText = $text;
00052     }
00053 
00054     public function copy() {
00055         return $this; # NOTE: this is ok since TextContent are immutable.
00056     }
00057 
00058     public function getTextForSummary( $maxlength = 250 ) {
00059         global $wgContLang;
00060 
00061         $text = $this->getNativeData();
00062 
00063         $truncatedtext = $wgContLang->truncate(
00064             preg_replace( "/[\n\r]/", ' ', $text ),
00065             max( 0, $maxlength ) );
00066 
00067         return $truncatedtext;
00068     }
00069 
00075     public function getSize() {
00076         $text = $this->getNativeData();
00077         return strlen( $text );
00078     }
00079 
00089     public function isCountable( $hasLinks = null ) {
00090         global $wgArticleCountMethod;
00091 
00092         if ( $this->isRedirect() ) {
00093             return false;
00094         }
00095 
00096         if ( $wgArticleCountMethod === 'any' ) {
00097             return true;
00098         }
00099 
00100         return false;
00101     }
00102 
00108     public function getNativeData() {
00109         $text = $this->mText;
00110         return $text;
00111     }
00112 
00118     public function getTextForSearchIndex() {
00119         return $this->getNativeData();
00120     }
00121 
00130     public function getWikitextForTransclusion() {
00131         $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
00132 
00133         if ( $wikitext ) {
00134             return $wikitext->getNativeData();
00135         } else {
00136             return false;
00137         }
00138     }
00139 
00149     public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
00150         $text = $this->getNativeData();
00151         $pst = rtrim( $text );
00152 
00153         return ( $text === $pst ) ? $this : new WikitextContent( $pst );
00154     }
00155 
00169     public function diff( Content $that, Language $lang = null ) {
00170         global $wgContLang;
00171 
00172         $this->checkModelID( $that->getModel() );
00173 
00174         // @todo could implement this in DifferenceEngine and just delegate here?
00175 
00176         if ( !$lang ) {
00177             $lang = $wgContLang;
00178         }
00179 
00180         $otext = $this->getNativeData();
00181         $ntext = $this->getNativeData();
00182 
00183         # Note: Use native PHP diff, external engines don't give us abstract output
00184         $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
00185         $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
00186 
00187         $diff = new Diff( $ota, $nta );
00188         return $diff;
00189     }
00190 
00202     public function getParserOutput( Title $title,
00203         $revId = null,
00204         ParserOptions $options = null, $generateHtml = true
00205     ) {
00206         global $wgParser, $wgTextModelsToParse;
00207 
00208         if ( !$options ) {
00209             //NOTE: use canonical options per default to produce cacheable output
00210             $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
00211         }
00212 
00213         if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
00214             // parse just to get links etc into the database
00215             $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
00216         } else {
00217             $po = new ParserOutput();
00218         }
00219 
00220         if ( $generateHtml ) {
00221             $html = $this->getHtml();
00222         } else {
00223             $html = '';
00224         }
00225 
00226         $po->setText( $html );
00227         return $po;
00228     }
00229 
00241     protected function getHtml() {
00242         return $this->getHighlightHtml();
00243     }
00244 
00251     protected function getHighlightHtml() {
00252         # TODO: make Highlighter interface, use highlighter here, if available
00253         return htmlspecialchars( $this->getNativeData() );
00254     }
00255 
00269     public function convert( $toModel, $lossy = '' ) {
00270         $converted = parent::convert( $toModel, $lossy );
00271 
00272         if ( $converted !== false ) {
00273             return $converted;
00274         }
00275 
00276         $toHandler = ContentHandler::getForModelID( $toModel );
00277 
00278         if ( $toHandler instanceof TextContentHandler ) {
00279             //NOTE: ignore content serialization format - it's just text anyway.
00280             $text = $this->getNativeData();
00281             $converted = $toHandler->unserializeContent( $text );
00282         }
00283 
00284         return $converted;
00285     }
00286 }