Merge "mw.language.convertPlural: Check if matching form exists"
[lhc/web/wiklou.git] / includes / content / TextContent.php
index a6ee7b0..d03d61e 100644 (file)
  * @ingroup Content
  */
 class TextContent extends AbstractContent {
-
        public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
                parent::__construct( $model_id );
 
                if ( $text === null || $text === false ) {
                        wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
-                                       . "This may indicate an error in the caller's scope." );
+                               . "This may indicate an error in the caller's scope." );
 
                        $text = '';
                }
@@ -74,6 +73,7 @@ class TextContent extends AbstractContent {
         */
        public function getSize() {
                $text = $this->getNativeData();
+
                return strlen( $text );
        }
 
@@ -107,6 +107,7 @@ class TextContent extends AbstractContent {
         */
        public function getNativeData() {
                $text = $this->mText;
+
                return $text;
        }
 
@@ -156,14 +157,14 @@ class TextContent extends AbstractContent {
        /**
         * Diff this content object with another content object.
         *
-        * @since 1.21diff
+        * @since 1.21
         *
         * @param $that Content: The other content object to compare this content
         * object to.
         * @param $lang Language: The language object to use for text segmentation.
         *    If not given, $wgContentLang is used.
         *
-        * @return DiffResult: A diff representing the changes that would have to be
+        * @return Diff A diff representing the changes that would have to be
         *    made to this content object to make it equal to $that.
         */
        public function diff( Content $that, Language $lang = null ) {
@@ -178,38 +179,44 @@ class TextContent extends AbstractContent {
                }
 
                $otext = $this->getNativeData();
-               $ntext = $this->getNativeData();
+               $ntext = $that->getNativeData();
 
                # Note: Use native PHP diff, external engines don't give us abstract output
                $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
                $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
 
                $diff = new Diff( $ota, $nta );
+
                return $diff;
        }
 
        /**
-        * 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.
+        * Returns a generic ParserOutput object, wrapping the HTML returned by
+        * getHtml().
         *
         * @param $title Title Context title for parsing
         * @param int|null $revId Revision ID (for {{REVISIONID}})
         * @param $options ParserOptions|null Parser options
         * @param bool $generateHtml Whether or not to generate HTML
-        * @param $output ParserOutput The output object to fill (reference).
+        *
+        * @return ParserOutput representing the HTML form of the text
         */
-       protected function fillParserOutput( Title $title, $revId,
-               ParserOptions $options, $generateHtml, ParserOutput &$output
+       public function getParserOutput( Title $title,
+               $revId = null,
+               ParserOptions $options = null, $generateHtml = true
        ) {
                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, HTML is replaced below.
-                       $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
+                       // parse just to get links etc into the database
+                       $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
+               } else {
+                       $po = new ParserOutput();
                }
 
                if ( $generateHtml ) {
@@ -218,25 +225,35 @@ class TextContent extends AbstractContent {
                        $html = '';
                }
 
-               $output->setText( $html );
+               $po->setText( $html );
+
+               return $po;
        }
 
        /**
         * Generates an HTML version of the content, for display. Used by
         * getParserOutput() to construct a ParserOutput object.
         *
-        * This default implementation runs the text returned by $this->getNativeData()
-        * through htmlspecialchars and tried to convert line breaks and indentation to HTML..
+        * 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.
         *
         * @return string An HTML representation of the content
         */
-       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;
+       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() );
        }
 
        /**
@@ -245,8 +262,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