fix var name
[lhc/web/wiklou.git] / includes / Content.php
index 41b7ac3..9f6b4d6 100644 (file)
@@ -4,27 +4,42 @@
  * 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
  */
 abstract class Content {
 
-       // TODO: create actual fields and document them
+       /**
+        * Name of the content model this Content object represents.
+        * Use with CONTENT_MODEL_XXX constants
+        *
+        * @var String $model_name
+        */
+       protected $modelName;
 
        /**
+        * @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.
         */
        public abstract 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: use in parser, etc!
         */
-       #TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
-       public abstract function getWikitextForTransclusion( ); #FIXME: use in parser, etc!
+       public abstract function getWikitextForTransclusion( );
 
        /**
         * Returns a textual representation of the content suitable for use in edit summaries and log messages.
         *
+        * @since WD.1
+        *
         * @param int $maxlength maximum length of the summary text
         * @return String the summary text
         */
@@ -34,10 +49,14 @@ abstract class Content {
         * Returns native represenation 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 abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
+       public abstract function getNativeData( );
 
        /**
         * returns the content's nominal size in bogo-bytes.
@@ -47,32 +66,33 @@ abstract class Content {
        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
+        * @param $model_name
         */
        public function __construct( $modelName = null ) {
-               $this->mModelName = $modelName;
+               $this->modelName = $modelName;
        }
 
        /**
         * Returns the name of the content model used by this content objects.
         * Corresponds to the CONTENT_MODEL_XXX constants.
         *
+        * @since WD.1
+        *
         * @return String the model name
         */
        public function getModelName() {
-               return $this->mModelName;
+               return $this->modelName;
        }
 
        /**
-        * Throws an MWException if $modelName is not the name of the content model
+        * Throws an MWException if $model_name is not the name of the content model
         * supported by this Content object.
+        *
+        * @param String $modelName the model to check
         */
        protected function checkModelName( $modelName ) {
-               if ( $modelName !== $this->mModelName ) {
-                       throw new MWException( "Bad content model: expected " . $this->mModelName . " but got found " . $modelName );
+               if ( $modelName !== $this->modelName ) {
+                       throw new MWException( "Bad content model: expected " . $this->modelName . " but got found " . $modelName );
                }
        }
 
@@ -82,6 +102,8 @@ abstract class Content {
         *
         * Shorthand for ContentHandler::getForContent( $this )
         *
+        * @since WD.1
+        *
         * @return ContentHandler
         */
        public function getContentHandler() {
@@ -94,6 +116,8 @@ abstract class Content {
         *
         * Shorthand for $this->getContentHandler()->getDefaultFormat()
         *
+        * @since WD.1
+        *
         * @return ContentHandler
         */
        public function getDefaultFormat() {
@@ -106,6 +130,8 @@ abstract class Content {
         *
         * Shorthand for $this->getContentHandler()->getSupportedFormats()
         *
+        * @since WD.1
+        *
         * @return array of supported serialization formats
         */
        public function getSupportedFormats() {
@@ -121,6 +147,8 @@ abstract class Content {
         *
         * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
         *
+        * @since WD.1
+        *
         * @param String $format the format to check
         * @return bool whether the format is supported
         */
@@ -149,6 +177,8 @@ abstract class Content {
         *
         * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
         *
+        * @since WD.1
+        *
         * @param null|String $format the desired serialization format (or null for the default format).
         * @return String serialized form of this Content object
         */
@@ -159,6 +189,8 @@ abstract class Content {
        /**
         * Returns true if this Content object represents empty content.
         *
+        * @since WD.1
+        *
         * @return bool whether this Content object is empty
         */
        public function isEmpty() {
@@ -179,6 +211,8 @@ abstract class Content {
         * * $a->equals( $b ) <=> $b->equals( $b )
         * * $a->equals( $b ) &&  $b->equals( $c ) ==> $a->equals( $c )
         *
+        * @since WD.1
+        *
         * @param Content $that the Content object to compare to
         * @return bool true if this Content object is euqual to $that, false otherwise.
         */
@@ -210,6 +244,8 @@ abstract class Content {
         * return $this. That is,  $copy === $original may be true, but only for imutable content
         * objects.
         *
+        * @since WD.1
+        *
         * @return Content. A copy of this object
         */
        public abstract function copy( );
@@ -218,6 +254,8 @@ abstract class Content {
         * 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
@@ -232,6 +270,8 @@ abstract class Content {
         *        the result of calling getText() on the ParserOutput object returned by
         *        this method is undefined.
         *
+        * @since WD.1
+        *
         * @return ParserOutput
         */
        public abstract function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true );
@@ -242,6 +282,8 @@ abstract class Content {
         * 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() {
@@ -254,6 +296,8 @@ abstract class Content {
         * 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() {
@@ -266,14 +310,21 @@ abstract class Content {
         * 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.
         *
+        * @since WD.1
+        *
         * @return Title
         */
        public function getUltimateRedirectTarget() {
                return null;
        }
 
+       /**
+        * @since WD.1
+        *
+        * @return bool
+        */
        public function isRedirect() {
-               return $this->getRedirectTarget() != null;
+               return $this->getRedirectTarget() !== null;
        }
 
        /**
@@ -281,6 +332,8 @@ abstract class Content {
         *
         * The default implementation returns null.
         *
+        * @since WD.1
+        *
         * @param String $sectionId 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 (inluded) 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
@@ -292,6 +345,8 @@ abstract class Content {
        /**
         * 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'
@@ -304,12 +359,14 @@ abstract class Content {
        /**
         * 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 null|ParserOptions $popts
         * @return Content
         */
-       public function preSaveTransform( Title $title, User $user, ParserOptions $popts = null ) {
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
                return $this;
        }
 
@@ -317,6 +374,8 @@ abstract class Content {
         * 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
         */
@@ -327,20 +386,20 @@ abstract class Content {
        /**
         * Returns a Content object with preload transformations applied (or this object if no transformations apply).
         *
+        * @since WD.1
+        *
         * @param Title $title
         * @param null|ParserOptions $popts
         * @return Content
         */
-       public function preloadTransform( Title $title, ParserOptions $popts = null ) {
+       public function preloadTransform( Title $title, ParserOptions $popts ) {
                return $this;
        }
 
        # 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
+       # 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
 
@@ -349,20 +408,23 @@ abstract class Content {
        # 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?
+       # FUTURE: make EditForm plugin for EditPage
 }
+       # FUTURE: special type for redirects?!
+       # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
+       # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
 
 /**
  * Content object implementation for representing flat text.
  *
  * TextContent instances are imutable
+ *
+ * @since WD.1
  */
 abstract class TextContent extends Content {
 
-       public function __construct( $text, $modelName = null ) {
-               parent::__construct( $modelName );
+       public function __construct( $text, $model_name = null ) {
+               parent::__construct( $model_name );
 
                $this->mText = $text;
        }
@@ -384,7 +446,9 @@ abstract class TextContent extends Content {
        }
 
        /**
-        * returns the content's nominal size in bogo-bytes.
+        * returns the text's size in bytes.
+        *
+        * @return int the size
         */
        public function getSize( ) {
                $text = $this->getNativeData( );
@@ -396,6 +460,8 @@ abstract class TextContent extends Content {
         *
         * @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;
@@ -426,7 +492,7 @@ abstract class TextContent extends Content {
         *
         * @return String the raw text
         */
-       public function getTextForSearchIndex( ) { #FIXME: use!
+       public function getTextForSearchIndex( ) {
                return $this->getNativeData();
        }
 
@@ -435,7 +501,7 @@ abstract class TextContent extends Content {
         *
         * @return String the raw text
         */
-       public function getWikitextForTransclusion( ) { #FIXME: use!
+       public function getWikitextForTransclusion( ) {
                return $this->getNativeData();
        }
 
@@ -459,28 +525,19 @@ abstract class TextContent extends Content {
 
 }
 
+/**
+ * @since WD.1
+ */
 class WikitextContent extends TextContent {
 
        public function __construct( $text ) {
                parent::__construct($text, CONTENT_MODEL_WIKITEXT);
-
-               $this->mDefaultParserOptions = null; #TODO: use per-class static member?!
        }
 
        protected function getHtml( ) {
                throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
        }
 
-       public function getDefaultParserOptions() {
-               global $wgUser, $wgContLang;
-
-               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.
         *
@@ -497,7 +554,7 @@ class WikitextContent extends TextContent {
                global $wgParser;
 
                if ( !$options ) {
-                       $options = $this->getDefaultParserOptions();
+                       $options = ParserOptions::newFromUserAndLang( $context->getUser(), $context->getLanguage() );
                }
 
                $po = $wgParser->parse( $this->mText, $context->getTitle(), $options, true, true, $revId );
@@ -581,13 +638,11 @@ class WikitextContent extends TextContent {
         *
         * @param Title $title
         * @param User $user
-        * @param null|ParserOptions $popts
+        * @param ParserOptions $popts
         * @return Content
         */
-       public function preSaveTransform( Title $title, User $user, ParserOptions $popts = null ) {
-               global $wgParser;
-
-               if ( $popts == null ) $popts = $this->getDefaultParserOptions();
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
+               global $wgParser, $wgConteLang;
 
                $text = $this->getNativeData();
                $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
@@ -599,13 +654,11 @@ class WikitextContent extends TextContent {
         * Returns a Content object with preload transformations applied (or this object if no transformations apply).
         *
         * @param Title $title
-        * @param null|ParserOptions $popts
+        * @param ParserOptions $popts
         * @return Content
         */
-       public function preloadTransform( Title $title, ParserOptions $popts = null ) {
-               global $wgParser;
-
-               if ( $popts == null ) $popts = $this->getDefaultParserOptions();
+       public function preloadTransform( Title $title, ParserOptions $popts ) {
+               global $wgParser, $wgConteLang;
 
                $text = $this->getNativeData();
                $plt = $wgParser->getPreloadText( $text, $title, $popts );
@@ -635,6 +688,8 @@ class WikitextContent extends TextContent {
         * @param Bool $hasLinks if it is known whether this content contains links, provide this information here,
         *                        to avoid redundant parsing to find out.
         * @param IContextSource $context context for parsing if necessary
+        *
+        * @return bool true if the content is countable
         */
        public function isCountable( $hasLinks = null, IContextSource $context = null ) {
                global $wgArticleCountMethod, $wgRequest;
@@ -649,9 +704,6 @@ class WikitextContent extends TextContent {
                        case 'any':
                                return true;
                        case 'comma':
-                               if ( $text === false ) {
-                                       $text = $this->getRawText();
-                               }
                                return strpos( $text,  ',' ) !== false;
                        case 'link':
                                if ( $hasLinks === null ) { # not known, find out
@@ -681,6 +733,9 @@ class WikitextContent extends TextContent {
 
 }
 
+/**
+ * @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.
@@ -722,7 +777,9 @@ class MessageContent extends TextContent {
 
 }
 
-
+/**
+ * @since WD.1
+ */
 class JavaScriptContent extends TextContent {
        public function __construct( $text ) {
                parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
@@ -739,6 +796,9 @@ class JavaScriptContent extends TextContent {
 
 }
 
+/**
+ * @since WD.1
+ */
 class CssContent extends TextContent {
        public function __construct( $text ) {
                parent::__construct($text, CONTENT_MODEL_CSS);
@@ -753,9 +813,3 @@ class CssContent extends TextContent {
                return $html;
        }
 }
-
-#FUTURE: special type for redirects?!
-#FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
-#FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
-#EXAMPLE: CoordinatesContent
-#EXAMPLE: WikidataContent