Call new Content::prepareSave from WikiPage::doEditContent.
[lhc/web/wiklou.git] / includes / Content.php
index 8c9686f..f7f0346 100644 (file)
 <?php
+/**
+ * A content object represents page content, e.g. the text to show on a page.
+ * Content objects have no knowledge about how they relate to wiki pages.
+ *
+ * @since 1.WD
+ */
+interface Content {
+
+       /**
+        * @since WD.1
+        *
+        * @return string A string representing the content in a way useful for
+        *   building a full text search index. If no useful representation exists,
+        *   this method returns an empty string.
+        *
+        * @todo: test that this actually works
+        * @todo: make sure this also works with LuceneSearch / WikiSearch
+        */
+       public function getTextForSearchIndex( );
+
+       /**
+        * @since WD.1
+        *
+        * @return string The wikitext to include when another page includes this
+        * content, or false if the content is not includable in a wikitext page.
+        *
+        * @TODO: allow native handling, bypassing wikitext representation, like
+        *    for includable special pages.
+        * @TODO: allow transclusion into other content models than Wikitext!
+        * @TODO: used in WikiPage and MessageCache to get message text. Not so
+        *    nice. What should we use instead?!
+        */
+       public function getWikitextForTransclusion( );
+
+       /**
+        * Returns a textual representation of the content suitable for use in edit
+        * summaries and log messages.
+        *
+        * @since WD.1
+        *
+        * @param $maxlength int Maximum length of the summary text
+        * @return   The summary text
+        */
+       public function getTextForSummary( $maxlength = 250 );
+
+       /**
+        * Returns native representation of the data. Interpretation depends on
+        * the data model used, as given by getDataModel().
+        *
+        * @since WD.1
+        *
+        * @return mixed The native representation of the content. Could be a
+        *    string, a nested array structure, an object, a binary blob...
+        *    anything, really.
+        *
+        * @NOTE: review all calls carefully, caller must be aware of content model!
+        */
+       public function getNativeData( );
+
+       /**
+        * Returns the content's nominal size in bogo-bytes.
+        *
+        * @return int
+        */
+       public function getSize( );
+
+       /**
+        * Returns the ID of the content model used by this Content object.
+        * Corresponds to the CONTENT_MODEL_XXX constants.
+        *
+        * @since WD.1
+        *
+        * @return String The model id
+        */
+       public function getModel();
+
+       /**
+        * Convenience method that returns the ContentHandler singleton for handling
+        * the content model that this Content object uses.
+        *
+        * Shorthand for ContentHandler::getForContent( $this )
+        *
+        * @since WD.1
+        *
+        * @return ContentHandler
+        */
+       public function getContentHandler();
+
+       /**
+        * Convenience method that returns the default serialization format for the
+        * content model that this Content object uses.
+        *
+        * Shorthand for $this->getContentHandler()->getDefaultFormat()
+        *
+        * @since WD.1
+        *
+        * @return String
+        */
+       public function getDefaultFormat();
+
+       /**
+        * Convenience method that returns the list of serialization formats
+        * supported for the content model that this Content object uses.
+        *
+        * Shorthand for $this->getContentHandler()->getSupportedFormats()
+        *
+        * @since WD.1
+        *
+        * @return Array of supported serialization formats
+        */
+       public function getSupportedFormats();
+
+       /**
+        * Returns true if $format is a supported serialization format for this
+        * Content object, false if it isn't.
+        *
+        * Note that this should always return true if $format is null, because null
+        * stands for the default serialization.
+        *
+        * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
+        *
+        * @since WD.1
+        *
+        * @param $format string The format to check
+        * @return bool Whether the format is supported
+        */
+       public function isSupportedFormat( $format );
+
+       /**
+        * Convenience method for serializing this Content object.
+        *
+        * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
+        *
+        * @since WD.1
+        *
+        * @param $format null|string The desired serialization format (or null for
+        *    the default format).
+        * @return string Serialized form of this Content object
+        */
+       public function serialize( $format = null );
+
+       /**
+        * Returns true if this Content object represents empty content.
+        *
+        * @since WD.1
+        *
+        * @return bool Whether this Content object is empty
+        */
+       public function isEmpty();
+
+       /**
+        * Returns whether the content is valid. This is intended for local validity
+        * checks, not considering global consistency.
+        *
+        * Content needs to be valid before it can be saved.
+        *
+        * This default implementation always returns true.
+        *
+        * @since WD.1
+        *
+        * @return boolean
+        */
+       public function isValid();
+
+       /**
+        * Returns true if this Content objects is conceptually equivalent to the
+        * given Content object.
+        *
+        * Contract:
+        *
+        * - Will return false if $that is null.
+        * - Will return true if $that === $this.
+        * - Will return false if $that->getModelName() != $this->getModel().
+        * - Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
+        *   where the meaning of "equal" depends on the actual data model.
+        *
+        * Implementations should be careful to make equals() transitive and reflexive:
+        *
+        * - $a->equals( $b ) <=> $b->equals( $a )
+        * - $a->equals( $b ) &&  $b->equals( $c ) ==> $a->equals( $c )
+        *
+        * @since WD.1
+        *
+        * @param $that Content The Content object to compare to
+        * @return bool True if this Content object is equal to $that, false otherwise.
+        */
+       public function equals( Content $that = null );
+
+       /**
+        * Return a copy of this Content object. The following must be true for the
+        * object returned:
+        *
+        * if $copy = $original->copy()
+        *
+        * - get_class($original) === get_class($copy)
+        * - $original->getModel() === $copy->getModel()
+        * - $original->equals( $copy )
+        *
+        * If and only if the Content object is immutable, the copy() method can and
+        * should return $this. That is,  $copy === $original may be true, but only
+        * for immutable content objects.
+        *
+        * @since WD.1
+        *
+        * @return Content. A copy of this object
+        */
+       public function copy( );
+
+       /**
+        * Returns true if this content is countable as a "real" wiki page, provided
+        * that it's also in a countable location (e.g. a current revision in the
+        * main namespace).
+        *
+        * @since WD.1
+        *
+        * @param $hasLinks Bool: If it is known whether this content contains
+        *    links, provide this information here, to avoid redundant parsing to
+        *    find out.
+        * @return boolean
+        */
+       public function isCountable( $hasLinks = null ) ;
+
+       /**
+        * Convenience method, shorthand for
+        * $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml )
+        *
+        * @note: subclasses should NOT override this to provide custom rendering.
+        *        Override ContentHandler::getParserOutput() instead!
+        *
+        * @param $title Title
+        * @param $revId null
+        * @param $options null|ParserOptions
+        * @param $generateHtml Boolean Whether to generate HTML (default: true).
+        *    If false, the result of calling getText() on the ParserOutput object
+        *    returned by this method is undefined.
+        *
+        * @since WD.1
+        *
+        * @return ParserOutput
+        */
+       public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, 
+               $generateHtml = true );
+
+       /**
+        * Construct the redirect destination from this content and return an
+        * array of Titles, or null if this content doesn't represent a redirect.
+        * The last element in the array is the final destination after all redirects
+        * have been resolved (up to $wgMaxRedirects times).
+        *
+        * @since WD.1
+        *
+        * @return Array of Titles, with the destination last
+        */
+       public function getRedirectChain();
+
+       /**
+        * Construct the redirect destination from this content and return a Title,
+        * or null if this content doesn't represent a redirect.
+        * This will only return the immediate redirect target, useful for
+        * the redirect table and other checks that don't need full recursion.
+        *
+        * @since WD.1
+        *
+        * @return Title: The corresponding Title
+        */
+       public function getRedirectTarget();
+
+       /**
+        * Construct the redirect destination from this content and return the
+        * Title, or null if this content doesn't represent a redirect.
+        *
+        * This will recurse down $wgMaxRedirects times or until a non-redirect
+        * target is hit in order to provide (hopefully) the Title of the final
+        * destination instead of another redirect.
+        *
+        * There is usually no need to override the default behaviour, subclasses that
+        * want to implement redirects should override getRedirectTarget().
+        * 
+        * @since WD.1
+        *
+        * @return Title
+        */
+       public function getUltimateRedirectTarget();
+
+       /**
+        * Returns whether this Content represents a redirect.
+        * Shorthand for getRedirectTarget() !== null.
+        *
+        * @since WD.1
+        *
+        * @return bool
+        */
+       public function isRedirect();
+
+       /**
+        * Returns the section with the given ID.
+        *
+        * @since WD.1
+        *
+        * @param $sectionId string The section's ID, given as a numeric string.
+        *    The ID "0" retrieves the section before the first heading, "1" the
+        *    text between the first heading (included) and the second heading
+        *    (excluded), etc.
+        * @return Content|Boolean|null The section, or false if no such section
+        *    exist, or null if sections are not supported.
+        */
+       public function getSection( $sectionId );
+
+       /**
+        * Replaces a section of the content and returns a Content object with the
+        * section replaced.
+        *
+        * @since WD.1
+        *
+        * @param $section Empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
+        * @param $with Content: new content of the section
+        * @param $sectionTitle String: new section's subject, only if $section is 'new'
+        * @return string Complete article text, or null if error
+        */
+       public function replaceSection( $section, Content $with, $sectionTitle = ''  );
+
+       /**
+        * Returns a Content object with pre-save transformations applied (or this
+        * object if no transformations apply).
+        *
+        * @since WD.1
+        *
+        * @param $title Title
+        * @param $user User
+        * @param $popts null|ParserOptions
+        * @return Content
+        */
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts );
+
+       /**
+        * Returns a new WikitextContent object with the given section heading
+        * prepended, if supported. The default implementation just returns this
+        * Content object unmodified, ignoring the section header.
+        *
+        * @since WD.1
+        *
+        * @param $header string
+        * @return Content
+        */
+       public function addSectionHeader( $header );
+
+       /**
+        * Returns a Content object with preload transformations applied (or this
+        * object if no transformations apply).
+        *
+        * @since WD.1
+        *
+        * @param $title Title
+        * @param $popts null|ParserOptions
+        * @return Content
+        */
+       public function preloadTransform( Title $title, ParserOptions $popts );
+
+       /**
+        * Prepare Content for saving. Called before Content is saved by WikiPage::doEditContent().
+        * This may be used to store additional information in the database, or check the content's
+        * consistency with global state.
+        *
+        * Note that this method will be called inside the same transaction bracket that will be used
+        * to save the new revision.
+        *
+        * @param WikiPage $page The page to be saved.
+        * @param int      $flags bitfield for use with EDIT_XXX constants, see WikiPage::doEditContent()
+        * @param int      $baseRevId the ID of the current revision
+        * @param User     $user
+        *
+        * @return Status A status object indicating whether the content was successfully prepared for saving.
+        *                If the returned status indicates an error, a rollback will be performed and the
+        *                transaction aborted.
+        *
+        * @see see WikiPage::doEditContent()
+        */
+       public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user );
+
+       # TODO: handle ImagePage and CategoryPage
+       # TODO: make sure we cover lucene search / wikisearch.
+       # TODO: make sure ReplaceTemplates still works
+       # FUTURE: nice&sane integration of GeSHi syntax highlighting
+       #   [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a 
+       #   config to set the class which handles syntax highlighting
+       #   [12:00] <vvv> And default it to a DummyHighlighter
+
+       # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
+
+       # TODO: tie into API to provide contentModel for Revisions
+       # TODO: tie into API to provide serialized version and contentFormat for Revisions
+       # TODO: tie into API edit interface
+       # FUTURE: make EditForm plugin for EditPage
+
+       # FUTURE: special type for redirects?!
+       # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
+       # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
+}
+
 
 /**
  * A content object represents page content, e.g. the text to show on a page.
  * Content objects have no knowledge about how they relate to Wiki pages.
- * Content objects are imutable.
  *
+ * @since 1.WD
  */
-abstract class Content {
-
-       // TODO: create actual fields and document them
-
-    /**
-     * @return String a string representing the content in a way useful for building a full text search index.
-     *         If no useful representation exists, this method returns an empty string.
-     */
-    public abstract function getTextForSearchIndex( );
-
-    /**
-     * @return String the wikitext to include when another page includes this  content, or false if the content is not
-     *         includable in a wikitext page.
-     */
-    #TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
-    public abstract function getWikitextForTransclusion( ); #FIXME: use in parser, etc!
-
-    /**
-     * Returns a textual representation of the content suitable for use in edit summaries and log messages.
-     *
-     * @param int $maxlength maximum length of the summary text
-     * @return String the summary text
-     */
-    public abstract function getTextForSummary( $maxlength = 250 );
-
-    /**
-     * Returns native represenation of the data. Interpretation depends on the data model used,
-     * as given by getDataModel().
-     *
-     * @return mixed the native representation of the content. Could be a string, a nested array
-     *         structure, an object, a binary blob... anything, really.
-     */
-    public abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
-
-    /**
-     * returns the content's nominal size in bogo-bytes.
-     *
-     * @return int
-     */
-    public abstract function getSize( );
-
-       /**
-        * TODO: do we really need to pass a $modelName here?
-        * Seems odd and makes lots of stuff hard (ie having a newEmpty static method in TextContent)
-        *
-        * @param $modelName
-        */
-       public function __construct( $modelName = null ) {
-               $this->mModelName = $modelName;
-       }
-
-    /**
-     * Returns the name of the content model used by this content objects.
-     * Corresponds to the CONTENT_MODEL_XXX constants.
-     *
-     * @return String the model name
-     */
-       public function getModelName() {
-               return $this->mModelName;
-       }
-
-    /**
-     * Throws an MWException if $modelName is not the name of the content model
-     * supported by this Content object.
-     */
-       protected function checkModelName( $modelName ) {
-               if ( $modelName !== $this->mModelName ) {
-                       throw new MWException( "Bad content model: expected " . $this->mModelName . " but got found " . $modelName );
+abstract class AbstractContent implements Content {
+
+       /**
+        * Name of the content model this Content object represents.
+        * Use with CONTENT_MODEL_XXX constants
+        *
+        * @var string $model_id
+        */
+       protected $model_id;
+
+       /**
+        * @param String $model_id
+        */
+       public function __construct( $model_id = null ) {
+               $this->model_id = $model_id;
+       }
+
+       /**
+        * @see Content::getModel()
+        */
+       public function getModel() {
+               return $this->model_id;
+       }
+
+       /**
+        * Throws an MWException if $model_id is not the id of the content model
+        * supported by this Content object.
+        *
+        * @param $model_id int the model to check
+        *
+        * @throws MWException
+        */
+       protected function checkModelID( $model_id ) {
+               if ( $model_id !== $this->model_id ) {
+                       throw new MWException( "Bad content model: " .
+                               "expected {$this->model_id}  " .
+                               "but got $model_id." );
                }
        }
 
-    /**
-     * Conveniance method that returns the ContentHandler singleton for handling the content
-     * model this Content object uses.
-     *
-     * Shorthand for ContentHandler::getForContent( $this )
-     *
-     * @return ContentHandler
-     */
+       /**
+        * @see Content::getContentHandler()
+        */
        public function getContentHandler() {
                return ContentHandler::getForContent( $this );
        }
 
-    /**
-     * Conveniance method that returns the default serialization format for the content model
-     * model this Content object uses.
-     *
-     * Shorthand for $this->getContentHandler()->getDefaultFormat()
-     *
-     * @return ContentHandler
-     */
+       /**
+        * @see Content::getDefaultFormat()
+        */
        public function getDefaultFormat() {
                return $this->getContentHandler()->getDefaultFormat();
        }
 
-    /**
-     * Conveniance method that returns the list of serialization formats supported
-     * for the content model model this Content object uses.
-     *
-     * Shorthand for $this->getContentHandler()->getSupportedFormats()
-     *
-     * @return array of supported serialization formats
-     */
+       /**
+        * @see Content::getSupportedFormats()
+        */
        public function getSupportedFormats() {
                return $this->getContentHandler()->getSupportedFormats();
        }
 
-    /**
-     * Returns true if $format is a supported serialization format for this Content object,
-     * false if it isn't.
-     *
-     * Note that this will always return true if $format is null, because null stands for the
-     * default serialization.
-     *
-     * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
-     *
-     * @param String $format the format to check
-     * @return bool whether the format is supported
-     */
+       /**
+        * @see Content::isSupportedFormat()
+        */
        public function isSupportedFormat( $format ) {
                if ( !$format ) {
                        return true; // this means "use the default"
@@ -133,597 +477,619 @@ abstract class Content {
                return $this->getContentHandler()->isSupportedFormat( $format );
        }
 
-    /**
-     * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
-     *
-     * @param $format
-     * @throws MWException
-     */
+       /**
+        * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
+        * return true.
+        *
+        * @param $format
+        * @throws MWException
+        */
        protected function checkFormat( $format ) {
                if ( !$this->isSupportedFormat( $format ) ) {
-                       throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
+                       throw new MWException( "Format $format is not supported for content model " . 
+                               $this->getModel() );
                }
        }
 
-    /**
-     * Conveniance method for serializing this Content object.
-     *
-     * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
-     *
-     * @param null|String $format the desired serialization format (or null for the default format).
-     * @return String serialized form of this Content object
-     */
+       /**
+        * @see Content::serialize
+        */
        public function serialize( $format = null ) {
                return $this->getContentHandler()->serializeContent( $this, $format );
        }
 
-    /**
-     * Returns true if this Content object represents empty content.
-     *
-     * @return bool whether this Content object is empty
-     */
-    public function isEmpty() {
-        return $this->getSize() == 0;
-    }
-
-    /**
-     * Returns true if this Content objects is conceptually equivalent to the given Content object.
-     *
-     * Will returns false if $that is null.
-     * Will return true if $that === $this.
-     *
-     * Returns false if this Content object uses a different content model than the
-     *
-     * @param Content $that the Content object to compare to
-     * @return bool true if this Content object is euzqla to $that, false otherwise.
-     */
-    public function equals( Content $that = null ) {
-        if ( empty( $that ) ){ // FIXME: empty on an object?
+       /**
+        * @see Content::isEmpty()
+        */
+       public function isEmpty() {
+               return $this->getSize() == 0;
+       }
+
+       /**
+        * @see Content::isValid()
+        */
+       public function isValid() {
+               return true;
+       }
+
+       /**
+        * @see Content::equals()
+        */
+       public function equals( Content $that = null ) {
+               if ( is_null( $that ) ) {
                        return false;
                }
 
-               return false;
-               // FIXME: something is doing wrong here, causing the compared objects to always be the same.
-               // Hence returning false for now, so changes can actually be saved...
-
-        if ( $that === $this ) {
+               if ( $that === $this ) {
                        return true;
                }
 
-        if ( $that->getModelName() !== $this->getModelName() ) {
+               if ( $that->getModel() !== $this->getModel() ) {
                        return false;
                }
 
-        return $this->getNativeData() === $that->getNativeData();
-    }
-
-    /**
-     * Returns true if this content is countable as a "real" wiki page, provided
-     * that it's also in a countable location (e.g. a current revision in the main namespace).
-     *
-     * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
-     *                        to avoid redundant parsing to find out.
-     */
-    public abstract function isCountable( $hasLinks = null ) ;
-
-    /**
-     * @param IContextSource $context
-     * @param null $revId
-     * @param null|ParserOptions $options
-     * @param Boolean $generateHtml whether to generate Html (default: true). If false,
-     *        the result of calling getText() on the ParserOutput object returned by
-     *        this method is undefined.
-     *
-     * @return ParserOutput
-     */
-    public abstract function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true );
-
-    /**
-     * Construct the redirect destination from this content and return an
-     * array of Titles, or null if this content doesn't represent a redirect.
-     * The last element in the array is the final destination after all redirects
-     * have been resolved (up to $wgMaxRedirects times).
-     *
-     * @return Array of Titles, with the destination last
-     */
-    public function getRedirectChain() {
-        return null;
-    }
-
-    /**
-     * Construct the redirect destination from this content and return an
-     * array of Titles, or null if this content doesn't represent a redirect.
-     * This will only return the immediate redirect target, useful for
-     * the redirect table and other checks that don't need full recursion.
-     *
-     * @return Title: The corresponding Title
-     */
-    public function getRedirectTarget() {
-        return null;
-    }
-
-    /**
-     * Construct the redirect destination from this content and return the
-     * Title, or null if this content doesn't represent a redirect.
-     * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
-     * in order to provide (hopefully) the Title of the final destination instead of another redirect.
-     *
-     * @return Title
-     */
-    public function getUltimateRedirectTarget() {
-        return null;
-    }
-
-    public function isRedirect() {
-        return $this->getRedirectTarget() != null;
-    }
-
-    /**
-     * Returns the section with the given id.
-     *
-     * The default implementation returns null.
-     *
-     * @param String $sectionId the section's id
-     * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
-     */
-    public function getSection( $sectionId ) {
-        return null;
-    }
-
-    /**
-     * Replaces a section of the content and returns a Content object with the section replaced.
-     *
-     * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
-     * @param $with Content: new content of the section
-     * @param $sectionTitle String: new section's subject, only if $section is 'new'
-     * @return string Complete article text, or null if error
-     */
-    public function replaceSection( $section, Content $with, $sectionTitle = ''  ) {
-        return $this;
-    }
-
-    /**
-     * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
-     *
-     * @param Title $title
-     * @param User $user
-     * @param null|ParserOptions $popts
-     * @return Content
-     */
-    public function preSaveTransform( Title $title, User $user, ParserOptions $popts = null ) {
-        return $this;
-    }
-
-    /**
-     * Returns a new WikitextContent object with the given section heading prepended, if supported.
-     * The default implementation just returns this Content object unmodified, ignoring the section header.
-     *
-     * @param $header String
-     * @return Content
-     */
-    public function addSectionHeader( $header ) {
-        return $this;
-    }
-
-    /**
-     * Returns a Content object with preload transformations applied (or this object if no transformations apply).
-     *
-     * @param Title $title
-     * @param null|ParserOptions $popts
-     * @return Content
-     */
-    public function preloadTransform( Title $title, ParserOptions $popts = null ) {
-        return $this;
-    }
-
-    # TODO: minimize special cases for CSS/JS; how to handle extra message for JS/CSS previews??
-    # TODO: handle ImagePage and CategoryPage
-    # TODO: hook into dump generation to serialize and record model and format!
-
-    # TODO: make sure we cover lucene search / wikisearch.
-    # TODO: make sure ReplaceTemplates still works
-    # TODO: nice&sane integration of GeSHi syntax highlighting
-    #   [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
-    #   [12:00] <vvv> And default it to a DummyHighlighter
-
-    # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
-
-    # TODO: tie into API to provide contentModel for Revisions
-    # TODO: tie into API to provide serialized version and contentFormat for Revisions
-    # TODO: tie into API edit interface
-    # TODO: make EditForm plugin for EditPage
-
-    # XXX: isCacheable( ) # can/should we do this here?
+               return $this->getNativeData() === $that->getNativeData();
+       }
+
+       /**
+        * @see Content::getParserOutput()
+        */
+       public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, 
+               $generateHtml = true ) 
+       {
+               return $this->getContentHandler()->getParserOutput( 
+                       $this, $title, $revId, $options, $generateHtml );
+       }
+
+       /**
+        * @see Content::getRedirectChain()
+        */
+       public function getRedirectChain() {
+               global $wgMaxRedirects;
+               $title = $this->getRedirectTarget();
+               if ( is_null( $title ) ) {
+                       return null;
+               }
+               // recursive check to follow double redirects
+               $recurse = $wgMaxRedirects;
+               $titles = array( $title );
+               while ( --$recurse > 0 ) {
+                       if ( $title->isRedirect() ) {
+                               $page = WikiPage::factory( $title );
+                               $newtitle = $page->getRedirectTarget();
+                       } else {
+                               break;
+                       }
+                       // Redirects to some special pages are not permitted
+                       if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
+                               // The new title passes the checks, so make that our current
+                               // title so that further recursion can be checked
+                               $title = $newtitle;
+                               $titles[] = $newtitle;
+                       } else {
+                               break;
+                       }
+               }
+               return $titles;
+       }
+
+       /**
+        * @see Content::getRedirectTarget()
+        */
+       public function getRedirectTarget() {
+               return null;
+       }
+
+       /**
+        * @see Content::getUltimateRedirectTarget()
+        * @note: migrated here from Title::newFromRedirectRecurse
+        */
+       public function getUltimateRedirectTarget() {
+               $titles = $this->getRedirectChain();
+               return $titles ? array_pop( $titles ) : null;
+       }
+
+       /**
+        * @since WD.1
+        *
+        * @return bool
+        */
+       public function isRedirect() {
+               return $this->getRedirectTarget() !== null;
+       }
+
+       /**
+        * @see Content::getSection()
+        */
+       public function getSection( $sectionId ) {
+               return null;
+       }
+
+       /**
+        * @see Content::replaceSection()
+        */
+       public function replaceSection( $section, Content $with, $sectionTitle = ''  ) {
+               return null;
+       }
+
+       /**
+        * @see Content::preSaveTransform()
+        */
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
+               return $this;
+       }
+
+       /**
+        * @see Content::addSectionHeader()
+        */
+       public function addSectionHeader( $header ) {
+               return $this;
+       }
+
+       /**
+        * @see Content::preloadTransform()
+        */
+       public function preloadTransform( Title $title, ParserOptions $popts ) {
+               return $this;
+       }
+
+       /**
+        * @see  Content::prepareSave()
+        */
+       public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
+               if ( $this->isValid() ) {
+                       return Status::newGood();
+               } else {
+                       return Status::newFatal( "invalid-content-data" );
+               }
+       }
 }
 
 /**
- * Content object implementation for representing flat text. The
+ * Content object implementation for representing flat text.
+ *
+ * TextContent instances are immutable
+ *
+ * @since WD.1
  */
-abstract class TextContent extends Content {
-
-    public function __construct( $text, $modelName = null ) {
-        parent::__construct( $modelName );
-
-        $this->mText = $text;
-    }
-
-    public function getTextForSummary( $maxlength = 250 ) {
-        global $wgContLang;
-
-        $text = $this->getNativeData();
-
-        $truncatedtext = $wgContLang->truncate(
-            preg_replace( "/[\n\r]/", ' ', $text ),
-            max( 0, $maxlength ) );
-
-        return $truncatedtext;
-    }
-
-    /**
-     * returns the content's nominal size in bogo-bytes.
-     */
-    public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
-        $text = $this->getNativeData( );
-        return strlen( $text );
-    }
-
-    /**
-     * 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, provide this information here,
-     *                        to avoid redundant parsing to find out.
-     */
-    public function isCountable( $hasLinks = null ) {
-        global $wgArticleCountMethod;
-
-        if ( $this->isRedirect( ) ) {
-            return false;
-        }
-
-        if (  $wgArticleCountMethod === 'any' ) {
-            return true;
-        }
-
-        return false;
-    }
-
-    /**
-     * Returns the text represented by this Content object, as a string.
-     *
-     * @return String the raw text
-     */
-    public function getNativeData( ) {
-        $text = $this->mText;
-        return $text;
-    }
-
-    /**
-     * Returns the text represented by this Content object, as a string.
-     *
-     * @return String the raw text
-     */
-    public function getTextForSearchIndex( ) { #FIXME: use!
-        return $this->getNativeData();
-    }
-
-    /**
-     * Returns the text represented by this Content object, as a string.
-     *
-     * @return String the raw text
-     */
-    public function getWikitextForTransclusion( ) { #FIXME: use!
-        return $this->getNativeData();
-    }
-
-    /**
-     * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
-     *
-     * @return ParserOutput representing the HTML form of the text
-     */
-    public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
-        # generic implementation, relying on $this->getHtml()
-
-        if ( $generateHtml ) $html = $this->getHtml( $options );
-        else $html = '';
-
-        $po = new ParserOutput( $html );
-
-        return $po;
-    }
-
-    protected abstract function getHtml( );
+abstract class TextContent extends AbstractContent {
+
+       public function __construct( $text, $model_id = null ) {
+               parent::__construct( $model_id );
+
+               $this->mText = $text;
+       }
+
+       public function copy() {
+               return $this; # NOTE: this is ok since TextContent are immutable.
+       }
+
+       public function getTextForSummary( $maxlength = 250 ) {
+               global $wgContLang;
+
+               $text = $this->getNativeData();
+
+               $truncatedtext = $wgContLang->truncate(
+                       preg_replace( "/[\n\r]/", ' ', $text ),
+                       max( 0, $maxlength ) );
+
+               return $truncatedtext;
+       }
+
+       /**
+        * returns the text's size in bytes.
+        *
+        * @return int The size
+        */
+       public function getSize( ) {
+               $text = $this->getNativeData( );
+               return strlen( $text );
+       }
+
+       /**
+        * 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,
+        * provide this information here, to avoid redundant parsing to find out.
+        *
+        * @return bool True if the content is countable
+        */
+       public function isCountable( $hasLinks = null ) {
+               global $wgArticleCountMethod;
+
+               if ( $this->isRedirect( ) ) {
+                       return false;
+               }
+
+               if (  $wgArticleCountMethod === 'any' ) {
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * Returns the text represented by this Content object, as a string.
+        *
+        * @param   the raw text
+        */
+       public function getNativeData( ) {
+               $text = $this->mText;
+               return $text;
+       }
+
+       /**
+        * Returns the text represented by this Content object, as a string.
+        *
+        * @param   the raw text
+        */
+       public function getTextForSearchIndex( ) {
+               return $this->getNativeData();
+       }
+
+       /**
+        * Returns the text represented by this Content object, as a string.
+        *
+        * @param   the raw text
+        */
+       public function getWikitextForTransclusion( ) {
+               return $this->getNativeData();
+       }
+
+       /**
+        * Diff this content object with another content object..
+        *
+        * @since WD.diff
+        *
+        * @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
+        *    made to this content object to make it equal to $that.
+        */
+       public function diff( Content $that, Language $lang = null ) {
+               global $wgContLang;
+
+               $this->checkModelID( $that->getModel() );
+
+               # @todo: could implement this in DifferenceEngine and just delegate here?
+
+               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 ) );
+
+               $diff = new Diff( $ota, $nta );
+               return $diff;
+       }
+
 
 }
 
+/**
+ * @since WD.1
+ */
 class WikitextContent extends TextContent {
 
-    public function __construct( $text ) {
-        parent::__construct($text, CONTENT_MODEL_WIKITEXT);
+       public function __construct( $text ) {
+               parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
+       }
+
+       /**
+        * @see Content::getSection()
+        */
+       public function getSection( $section ) {
+               global $wgParser;
+
+               $text = $this->getNativeData();
+               $sect = $wgParser->getSection( $text, $section, false );
 
-        $this->mDefaultParserOptions = null; #TODO: use per-class static member?!
-    }
+               return  new WikitextContent( $sect );
+       }
+
+       /**
+        * @see Content::replaceSection()
+        */
+       public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
+               wfProfileIn( __METHOD__ );
 
-    protected function getHtml( ) {
-        throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
-    }
+               $myModelId = $this->getModel();
+               $sectionModelId = $with->getModel();
 
-    public function getDefaultParserOptions() {
-        global $wgUser, $wgContLang;
+               if ( $sectionModelId != $myModelId  ) {
+                       throw new MWException( "Incompatible content model for section: " .
+                               "document uses $myModelId but " .
+                               "section uses $sectionModelId." );
+               }
+
+               $oldtext = $this->getNativeData();
+               $text = $with->getNativeData();
+
+               if ( $section === '' ) {
+                       return $with; # XXX: copy first?
+               } if ( $section == 'new' ) {
+                       # Inserting a new section
+                       if ( $sectionTitle ) {
+                               $subject = wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n";
+                       } else {
+                               $subject = '';
+                       }
+                       if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
+                               $text = strlen( trim( $oldtext ) ) > 0
+                                       ? "{$oldtext}\n\n{$subject}{$text}"
+                                       : "{$subject}{$text}";
+                       }
+               } else {
+                       # Replacing an existing section; roll out the big guns
+                       global $wgParser;
+
+                       $text = $wgParser->replaceSection( $oldtext, $section, $text );
+               }
 
-        if ( !$this->mDefaultParserOptions ) { #TODO: use per-class static member?!
-            $this->mDefaultParserOptions = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
-        }
-
-        return $this->mDefaultParserOptions;
-    }
-
-    /**
-        * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
-        *
-        * @since WikiData1
-        *
-        * @param IContextSource|null $context
-        * @param null $revId
-        * @param null|ParserOptions $options
-        * @param bool $generateHtml
-        *
-        * @return ParserOutput representing the HTML form of the text
-        */
-    public function getParserOutput( IContextSource $context = null, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
-        global $wgParser;
-
-        if ( !$options ) {
-            $options = $this->getDefaultParserOptions();
-        }
-
-        $po = $wgParser->parse( $this->mText, $context->getTitle(), $options, true, true, $revId );
-
-        return $po;
-    }
-
-    /**
-     * Returns the section with the given id.
-     *
-     * @param String $sectionId the section's id
-     * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
-     */
-    public function getSection( $section ) {
-        global $wgParser;
-
-        $text = $this->getNativeData();
-        $sect = $wgParser->getSection( $text, $section, false );
-
-        return  new WikitextContent( $sect );
-    }
-
-    /**
-     * Replaces a section in the wikitext
-     *
-     * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
-     * @param $with Content: new content of the section
-     * @param $sectionTitle String: new section's subject, only if $section is 'new'
-     * @return string Complete article text, or null if error
-     */
-    public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
-        wfProfileIn( __METHOD__ );
-
-        $myModelName = $this->getModelName();
-        $sectionModelName = $with->getModelName();
-
-        if ( $sectionModelName != $myModelName  ) {
-            throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
-        }
-
-        $oldtext = $this->getNativeData();
-        $text = $with->getNativeData();
-
-        if ( $section == 'new' ) {
-            # Inserting a new section
-            $subject = $sectionTitle ? wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
-            if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
-                $text = strlen( trim( $oldtext ) ) > 0
-                    ? "{$oldtext}\n\n{$subject}{$text}"
-                    : "{$subject}{$text}";
-            }
-        } else {
-            # Replacing an existing section; roll out the big guns
-            global $wgParser;
-
-            $text = $wgParser->replaceSection( $oldtext, $section, $text );
-        }
-
-        $newContent = new WikitextContent( $text );
-
-        wfProfileOut( __METHOD__ );
-        return $newContent;
-    }
-
-    /**
-     * Returns a new WikitextContent object with the given section heading prepended.
-     *
-     * @param $header String
-     * @return Content
-     */
-    public function addSectionHeader( $header ) {
-        $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle ) . "\n\n" . $this->getNativeData();
-
-        return new WikitextContent( $text );
-    }
-
-    /**
-     * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
-     *
-     * @param Title $title
-     * @param User $user
-     * @param null|ParserOptions $popts
-     * @return Content
-     */
-    public function preSaveTransform( Title $title, User $user, ParserOptions $popts = null ) {
-        global $wgParser;
-
-        if ( $popts == null ) $popts = $this->getDefaultParserOptions();
-
-        $text = $this->getNativeData();
-        $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
-
-        return new WikitextContent( $pst );
-    }
-
-    /**
-     * Returns a Content object with preload transformations applied (or this object if no transformations apply).
-     *
-     * @param Title $title
-     * @param null|ParserOptions $popts
-     * @return Content
-     */
-    public function preloadTransform( Title $title, ParserOptions $popts = null ) {
-        global $wgParser;
-
-        if ( $popts == null ) $popts = $this->getDefaultParserOptions();
-
-        $text = $this->getNativeData();
-        $plt = $wgParser->getPreloadText( $text, $title, $popts );
-
-        return new WikitextContent( $plt );
-    }
-
-    public function getRedirectChain() {
-        $text = $this->getNativeData();
-        return Title::newFromRedirectArray( $text );
-    }
-
-    public function getRedirectTarget() {
-        $text = $this->getNativeData();
-        return Title::newFromRedirect( $text );
-    }
-
-    public function getUltimateRedirectTarget() {
-        $text = $this->getNativeData();
-        return Title::newFromRedirectRecurse( $text );
-    }
-
-    /**
-     * Returns true if this content is not a redirect, and this content's text is countable according to
-     * the criteria defiend by $wgArticleCountMethod.
-     *
-     * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
-     *                        to avoid redundant parsing to find out.
-     */
-    public function isCountable( $hasLinks = null ) {
-        global $wgArticleCountMethod;
-
-        if ( $this->isRedirect( ) ) {
-            return false;
-        }
-
-        $text = $this->getNativeData();
-
-        switch ( $wgArticleCountMethod ) {
-            case 'any':
-                return true;
-            case 'comma':
-                if ( $text === false ) {
-                    $text = $this->getRawText();
-                }
-                return strpos( $text,  ',' ) !== false;
-            case 'link':
-                if ( $hasLinks === null ) { # not know, find out
-                    $po = $this->getParserOutput();
-                    $links = $po->getLinks();
-                    $hasLinks = !empty( $links );
-                }
-
-                return $hasLinks;
-        }
-    }
-
-    public function getTextForSummary( $maxlength = 250 ) {
-        $truncatedtext = parent::getTextForSummary( $maxlength );
-
-        #clean up unfinished links
-        #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
-        $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
-
-        return $truncatedtext;
-    }
+               $newContent = new WikitextContent( $text );
+
+               wfProfileOut( __METHOD__ );
+               return $newContent;
+       }
+
+       /**
+        * Returns a new WikitextContent object with the given section heading
+        * prepended.
+        *
+        * @param $header string
+        * @return Content
+        */
+       public function addSectionHeader( $header ) {
+               $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . 
+                       $this->getNativeData();
+
+               return new WikitextContent( $text );
+       }
+
+       /**
+        * Returns a Content object with pre-save transformations applied using
+        * Parser::preSaveTransform().
+        *
+        * @param $title Title
+        * @param $user User
+        * @param $popts ParserOptions
+        * @return Content
+        */
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
+               global $wgParser;
+
+               $text = $this->getNativeData();
+               $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
+
+               return new WikitextContent( $pst );
+       }
+
+       /**
+        * Returns a Content object with preload transformations applied (or this
+        * object if no transformations apply).
+        *
+        * @param $title Title
+        * @param $popts ParserOptions
+        * @return Content
+        */
+       public function preloadTransform( Title $title, ParserOptions $popts ) {
+               global $wgParser;
+
+               $text = $this->getNativeData();
+               $plt = $wgParser->getPreloadText( $text, $title, $popts );
+
+               return new WikitextContent( $plt );
+       }
+
+       /**
+        * Implement redirect extraction for wikitext.
+        *
+        * @return null|Title
+        *
+        * @note: migrated here from Title::newFromRedirectInternal()
+        *
+        * @see Content::getRedirectTarget
+        * @see AbstractContent::getRedirectTarget
+        */
+       public function getRedirectTarget() {
+               global $wgMaxRedirects;
+               if ( $wgMaxRedirects < 1 ) {
+                       // redirects are disabled, so quit early
+                       return null;
+               }
+               $redir = MagicWord::get( 'redirect' );
+               $text = trim( $this->getNativeData() );
+               if ( $redir->matchStartAndRemove( $text ) ) {
+                       // Extract the first link and see if it's usable
+                       // Ensure that it really does come directly after #REDIRECT
+                       // Some older redirects included a colon, so don't freak about that!
+                       $m = array();
+                       if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
+                               // Strip preceding colon used to "escape" categories, etc.
+                               // and URL-decode links
+                               if ( strpos( $m[1], '%' ) !== false ) {
+                                       // Match behavior of inline link parsing here;
+                                       $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
+                               }
+                               $title = Title::newFromText( $m[1] );
+                               // If the title is a redirect to bad special pages or is invalid, return null
+                               if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
+                                       return null;
+                               }
+                               return $title;
+                       }
+               }
+               return null;
+       }
+
+       /**
+        * Returns true if this content is not a redirect, and this content's text
+        * is countable according to the criteria defined by $wgArticleCountMethod.
+        *
+        * @param $hasLinks Bool  if it is known whether this content contains
+        *    links, provide this information here, to avoid redundant parsing to
+        *    find out.
+        * @param $title null|\Title
+        *
+        * @internal param \IContextSource $context context for parsing if necessary
+        *
+        * @return bool True if the content is countable
+        */
+       public function isCountable( $hasLinks = null, Title $title = null ) {
+               global $wgArticleCountMethod;
+
+               if ( $this->isRedirect( ) ) {
+                       return false;
+               }
+
+               $text = $this->getNativeData();
+
+               switch ( $wgArticleCountMethod ) {
+                       case 'any':
+                               return true;
+                       case 'comma':
+                               return strpos( $text,  ',' ) !== false;
+                       case 'link':
+                               if ( $hasLinks === null ) { # not known, find out
+                                       if ( !$title ) {
+                                               $context = RequestContext::getMain();
+                                               $title = $context->getTitle();
+                                       }
+
+                                       $po = $this->getParserOutput( $title, null, null, false );
+                                       $links = $po->getLinks();
+                                       $hasLinks = !empty( $links );
+                               }
+
+                               return $hasLinks;
+               }
+
+               return false;
+       }
+
+       public function getTextForSummary( $maxlength = 250 ) {
+               $truncatedtext = parent::getTextForSummary( $maxlength );
+
+               # clean up unfinished links
+               # XXX: make this optional? wasn't there in autosummary, but required for
+               # deletion summary.
+               $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
+
+               return $truncatedtext;
+       }
 
 }
 
+/**
+ * @since WD.1
+ */
 class MessageContent extends TextContent {
-    public function __construct( $msg_key, $params = null, $options = null ) {
-        parent::__construct(null, CONTENT_MODEL_WIKITEXT); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
+       public function __construct( $msg_key, $params = null, $options = null ) {
+               # XXX: messages may be wikitext, html or plain text! and maybe even
+               # something else entirely.
+               parent::__construct( null, CONTENT_MODEL_WIKITEXT );
 
-        $this->mMessageKey = $msg_key;
+               $this->mMessageKey = $msg_key;
 
-        $this->mParameters = $params;
+               $this->mParameters = $params;
 
-        if ( is_null( $options ) ) {
+               if ( is_null( $options ) ) {
                        $options = array();
                }
                elseif ( is_string( $options ) ) {
                        $options = array( $options );
                }
 
-        $this->mOptions = $options;
-
-        $this->mHtmlOptions = null;
-    }
+               $this->mOptions = $options;
+       }
 
-    /**
-     * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
-     */
-    protected function getHtml(  ) {
-        $opt = array_merge( $this->mOptions, array('parse') );
+       /**
+        * Returns the message as rendered HTML, using the options supplied to the
+        * constructor plus "parse".
+        * @param   the message text, parsed
+        */
+       public function getHtml(  ) {
+               $opt = array_merge( $this->mOptions, array( 'parse' ) );
 
-        return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
-    }
+               return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
+       }
 
 
-    /**
-     * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
-     */
-    public function getNativeData( ) {
-        $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
+       /**
+        * Returns the message as raw text, using the options supplied to the
+        * constructor minus "parse" and "parseinline".
+        *
+        * @param   the message text, unparsed.
+        */
+       public function getNativeData( ) {
+               $opt = array_diff( $this->mOptions, array( 'parse', 'parseinline' ) );
 
-        return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
-    }
+               return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
+       }
 
 }
 
-
+/**
+ * @since WD.1
+ */
 class JavaScriptContent extends TextContent {
-    public function __construct( $text ) {
-        parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
-    }
+       public function __construct( $text ) {
+               parent::__construct( $text, CONTENT_MODEL_JAVASCRIPT );
+       }
+
+       /**
+        * Returns a Content object with pre-save transformations applied using
+        * Parser::preSaveTransform().
+        *
+        * @param Title $title
+        * @param User $user
+        * @param ParserOptions $popts
+        * @return Content
+        */
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
+               global $wgParser;
+               // @todo: make pre-save transformation optional for script pages
+               // See bug #32858
 
-    protected function getHtml( ) {
-        $html = "";
-        $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
-        $html .= htmlspecialchars( $this->getNativeData() );
-        $html .= "\n</pre>\n";
+               $text = $this->getNativeData();
+               $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
 
-        return $html;
-    }
+               return new JavaScriptContent( $pst );
+       }
 
 }
 
+/**
+ * @since WD.1
+ */
 class CssContent extends TextContent {
-    public function __construct( $text ) {
-        parent::__construct($text, CONTENT_MODEL_CSS);
-    }
-
-    protected function getHtml( ) {
-        $html = "";
-        $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
-        $html .= htmlspecialchars( $this->getNativeData() );
-        $html .= "\n</pre>\n";
-
-        return $html;
-    }
-}
+       public function __construct( $text ) {
+               parent::__construct( $text, CONTENT_MODEL_CSS );
+       }
+
+       /**
+        * Returns a Content object with pre-save transformations applied using
+        * Parser::preSaveTransform().
+        *
+        * @param $title Title
+        * @param $user User
+        * @param $popts ParserOptions
+        * @return Content
+        */
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
+               global $wgParser;
+               // @todo: make pre-save transformation optional for script pages
+
+               $text = $this->getNativeData();
+               $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
 
-#FUTURE: special type for redirects?!
-#FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
-#FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
-#EXAMPLE: CoordinatesContent
-#EXAMPLE: WikidataContent
+               return new CssContent( $pst );
+       }
+
+}