Introducing ContentGetParserOutput hook.
[lhc/web/wiklou.git] / includes / content / TextContent.php
index 0f7a531..a6ee7b0 100644 (file)
@@ -72,8 +72,8 @@ class TextContent extends AbstractContent {
         *
         * @return int The size
         */
-       public function getSize( ) {
-               $text = $this->getNativeData( );
+       public function getSize() {
+               $text = $this->getNativeData();
                return strlen( $text );
        }
 
@@ -81,7 +81,7 @@ class TextContent extends AbstractContent {
         * Returns true if this content is not a redirect, and $wgArticleCountMethod
         * is "any".
         *
-        * @param $hasLinks Bool: if it is known whether this content contains links,
+        * @param bool $hasLinks if it is known whether this content contains links,
         * provide this information here, to avoid redundant parsing to find out.
         *
         * @return bool True if the content is countable
@@ -89,7 +89,7 @@ class TextContent extends AbstractContent {
        public function isCountable( $hasLinks = null ) {
                global $wgArticleCountMethod;
 
-               if ( $this->isRedirect( ) ) {
+               if ( $this->isRedirect() ) {
                        return false;
                }
 
@@ -105,7 +105,7 @@ class TextContent extends AbstractContent {
         *
         * @return string: the raw text
         */
-       public function getNativeData( ) {
+       public function getNativeData() {
                $text = $this->mText;
                return $text;
        }
@@ -115,7 +115,7 @@ class TextContent extends AbstractContent {
         *
         * @return string: the raw text
         */
-       public function getTextForSearchIndex( ) {
+       public function getTextForSearchIndex() {
                return $this->getNativeData();
        }
 
@@ -127,7 +127,7 @@ class TextContent extends AbstractContent {
         *
         * @return string|false: the raw text, or null if the conversion failed
         */
-       public function getWikitextForTransclusion( ) {
+       public function getWikitextForTransclusion() {
                $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
 
                if ( $wikitext ) {
@@ -171,49 +171,45 @@ class TextContent extends AbstractContent {
 
                $this->checkModelID( $that->getModel() );
 
-               # @todo: could implement this in DifferenceEngine and just delegate here?
+               // @todo could implement this in DifferenceEngine and just delegate here?
 
-               if ( !$lang ) $lang = $wgContLang;
+               if ( !$lang ) {
+                       $lang = $wgContLang;
+               }
 
                $otext = $this->getNativeData();
                $ntext = $this->getNativeData();
 
                # Note: Use native PHP diff, external engines don't give us abstract output
-               $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
-               $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
+               $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
+               $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
 
                $diff = new Diff( $ota, $nta );
                return $diff;
        }
 
-
        /**
-        * Returns a generic ParserOutput object, wrapping the HTML returned by
-        * getHtml().
+        * Fills the provided ParserOutput object with the HTML returned by getHtml().
+        *
+        * Content models in $wgTextModelsToParse will be parsed as wikitext to process links,
+        * magic words, etc.
+        *
+        * Subclasses may override this to provide custom rendering.
         *
         * @param $title Title Context title for parsing
-        * @param $revId int|null Revision ID (for {{REVISIONID}})
+        * @param int|null $revId Revision ID (for {{REVISIONID}})
         * @param $options ParserOptions|null Parser options
-        * @param $generateHtml bool Whether or not to generate HTML
-        *
-        * @return ParserOutput representing the HTML form of the text
+        * @param bool $generateHtml Whether or not to generate HTML
+        * @param $output ParserOutput The output object to fill (reference).
         */
-       public function getParserOutput( Title $title,
-               $revId = null,
-               ParserOptions $options = null, $generateHtml = true
+       protected function fillParserOutput( Title $title, $revId,
+               ParserOptions $options, $generateHtml, ParserOutput &$output
        ) {
                global $wgParser, $wgTextModelsToParse;
 
-               if ( !$options ) {
-                       //NOTE: use canonical options per default to produce cacheable output
-                       $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
-               }
-
                if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
-                       // parse just to get links etc into the database
-                       $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
-               } else {
-                       $po = new ParserOutput();
+                       // parse just to get links etc into the database, HTML is replaced below.
+                       $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
                }
 
                if ( $generateHtml ) {
@@ -222,34 +218,25 @@ class TextContent extends AbstractContent {
                        $html = '';
                }
 
-               $po->setText( $html );
-               return $po;
+               $output->setText( $html );
        }
 
        /**
         * Generates an HTML version of the content, for display. Used by
         * getParserOutput() to construct a ParserOutput object.
         *
-        * This default implementation just calls getHighlightHtml(). Content
-        * models that have another mapping to HTML (as is the case for markup
-        * languages like wikitext) should override this method to generate the
-        * appropriate HTML.
+        * This default implementation runs the text returned by $this->getNativeData()
+        * through htmlspecialchars and tried to convert line breaks and indentation to HTML..
         *
         * @return string An HTML representation of the content
         */
-       protected function getHtml() {
-               return $this->getHighlightHtml();
-       }
-
-       /**
-        * Generates a syntax-highlighted version of the content, as HTML.
-        * Used by the default implementation of getHtml().
-        *
-        * @return string an HTML representation of the content's markup
-        */
-       protected function getHighlightHtml( ) {
-               # TODO: make Highlighter interface, use highlighter here, if available
-               return htmlspecialchars( $this->getNativeData() );
+       public static function convertWhiteSpaceToHTML( $msg ) {
+               $msg = htmlspecialchars( $msg );
+               $msg = preg_replace( '/^ /m', ' ', $msg );
+               $msg = preg_replace( '/ $/m', ' ', $msg );
+               $msg = preg_replace( '/  /', '  ', $msg );
+               $msg = str_replace( "\n", '<br />', $msg );
+               return $msg;
        }
 
        /**
@@ -258,8 +245,8 @@ class TextContent extends AbstractContent {
         * This implementation provides lossless conversion between content models based
         * on TextContent.
         *
-        * @param String  $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
-        * @param String  $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
+        * @param string  $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
+        * @param string  $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
         * not allowed, full round-trip conversion is expected to work without losing information.
         *
         * @return Content|bool A content object with the content model $toModel, or false if