MediaWiki  REL1_23
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         $text = $this->mText;
00120 
00121         return $text;
00122     }
00123 
00129     public function getTextForSearchIndex() {
00130         return $this->getNativeData();
00131     }
00132 
00141     public function getWikitextForTransclusion() {
00142         $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
00143 
00144         if ( $wikitext ) {
00145             return $wikitext->getNativeData();
00146         } else {
00147             return false;
00148         }
00149     }
00150 
00161     public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
00162         $text = $this->getNativeData();
00163         $pst = rtrim( $text );
00164 
00165         return ( $text === $pst ) ? $this : new static( $pst );
00166     }
00167 
00181     public function diff( Content $that, Language $lang = null ) {
00182         global $wgContLang;
00183 
00184         $this->checkModelID( $that->getModel() );
00185 
00186         // @todo could implement this in DifferenceEngine and just delegate here?
00187 
00188         if ( !$lang ) {
00189             $lang = $wgContLang;
00190         }
00191 
00192         $otext = $this->getNativeData();
00193         $ntext = $that->getNativeData();
00194 
00195         # Note: Use native PHP diff, external engines don't give us abstract output
00196         $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
00197         $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
00198 
00199         $diff = new Diff( $ota, $nta );
00200 
00201         return $diff;
00202     }
00203 
00215     public function getParserOutput( Title $title, $revId = null,
00216         ParserOptions $options = null, $generateHtml = true ) {
00217         global $wgParser, $wgTextModelsToParse;
00218 
00219         if ( !$options ) {
00220             //NOTE: use canonical options per default to produce cacheable output
00221             $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
00222         }
00223 
00224         if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
00225             // parse just to get links etc into the database
00226             $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
00227         } else {
00228             $po = new ParserOutput();
00229         }
00230 
00231         if ( $generateHtml ) {
00232             $html = $this->getHtml();
00233         } else {
00234             $html = '';
00235         }
00236 
00237         $po->setText( $html );
00238 
00239         return $po;
00240     }
00241 
00253     protected function getHtml() {
00254         return $this->getHighlightHtml();
00255     }
00256 
00263     protected function getHighlightHtml() {
00264         # TODO: make Highlighter interface, use highlighter here, if available
00265         return htmlspecialchars( $this->getNativeData() );
00266     }
00267 
00279     public function convert( $toModel, $lossy = '' ) {
00280         $converted = parent::convert( $toModel, $lossy );
00281 
00282         if ( $converted !== false ) {
00283             return $converted;
00284         }
00285 
00286         $toHandler = ContentHandler::getForModelID( $toModel );
00287 
00288         if ( $toHandler instanceof TextContentHandler ) {
00289             //NOTE: ignore content serialization format - it's just text anyway.
00290             $text = $this->getNativeData();
00291             $converted = $toHandler->unserializeContent( $text );
00292         }
00293 
00294         return $converted;
00295     }
00296 
00297 }