minor cleanup, added todos and attempted (but aborted) refactoring to make stuff...
[lhc/web/wiklou.git] / includes / ContentHandler.php
index 91ebeb4..1a55788 100644 (file)
@@ -14,27 +14,59 @@ class MWContentSerializationException extends MWException {
  * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
  * this also applies to wikitext (mediawiki's default content type), but wikitext
  * content may be represented by a DOM or AST structure in the future.
- *  
+ *
+ * TODO: add documentation
  */
 abstract class ContentHandler {
 
+       /**
+        * @abstract
+        * @param Content $content
+        * @param null $format
+        * @return String
+        */
+       public abstract function serialize( Content $content, $format = null );
+
+       /**
+        * TODO: calling unserialize on a ContentHandler returns a Content?!! Something looks wrong here...
+        *
+        * @abstract
+        * @param $blob String
+        * @param null $format
+        * @return Content
+        */
+       public abstract function unserialize( $blob, $format = null );
+
+       /**
+        * FIXME: bad method name: suggests it empties the content of an instance rather then creating a new empty one
+        */
+       public abstract function emptyContent();
+
     public static function getContentText( Content $content = null ) {
         global $wgContentHandlerTextFallback;
 
-        if ( !$content ) return '';
+        if ( is_null( $content ) ) {
+                       return '';
+               }
 
         if ( $content instanceof TextContent ) {
             return $content->getNativeData();
         }
 
-        if ( $wgContentHandlerTextFallback == 'fail' ) throw new MWException( "Attempt to get text from Content with model " . $content->getModelName() );
-        if ( $wgContentHandlerTextFallback == 'serialize' ) return $content->serialize();
+        if ( $wgContentHandlerTextFallback == 'fail' ) {
+                       throw new MWException( "Attempt to get text from Content with model " . $content->getModelName() );
+               }
+
+        if ( $wgContentHandlerTextFallback == 'serialize' ) {
+                       return $content->serialize();
+               }
 
         return null;
     }
 
     public static function makeContent( $text, Title $title, $modelName = null, $format = null ) {
-        if ( !$modelName ) {
+
+        if ( is_null( $modelName ) ) {
             $modelName = $title->getContentModelName();
         }
 
@@ -45,8 +77,8 @@ abstract class ContentHandler {
     public static function getDefaultModelFor( Title $title ) {
         global $wgNamespaceContentModels;
 
-        # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
-        #       because it is used to initialized the mContentModelName memebr.
+        // NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
+        //       because it is used to initialized the mContentModelName memebr.
 
         $ns = $title->getNamespace();
 
@@ -58,43 +90,48 @@ abstract class ContentHandler {
             $model = $wgNamespaceContentModels[ $ns ];
         }
 
-        # hook can determin default model
+        // hook can determin default model
         if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
-            if ( $model ) return $model;
+            if ( !is_null( $model ) ) {
+                               return $model;
+                       }
         }
 
-        # Could this page contain custom CSS or JavaScript, based on the title?
-        $isCssOrJsPage = ( NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m ) );
-        if ( $isCssOrJsPage ) $ext = $m[1];
+        // Could this page contain custom CSS or JavaScript, based on the title?
+        $isCssOrJsPage = NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
+        if ( $isCssOrJsPage ) {
+                       $ext = $m[1];
+               }
 
-        # hook can force js/css
+        // hook can force js/css
         wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
 
-        # Is this a .css subpage of a user page?
-        $isJsCssSubpage = ( NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
-        if ( $isJsCssSubpage ) $ext = $m[1];
+        // Is this a .css subpage of a user page?
+        $isJsCssSubpage = NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
+        if ( $isJsCssSubpage ) {
+                       $ext = $m[1];
+               }
 
-        # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
-        $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT || $model === null );
-        $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
+        // is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
+        $isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
+        $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
 
-        # hook can override $isWikitext
+        // hook can override $isWikitext
         wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
 
         if ( !$isWikitext ) {
-
-            if ( $ext == 'js' )
-                return CONTENT_MODEL_JAVASCRIPT;
-            else if ( $ext == 'css' )
-                return CONTENT_MODEL_CSS;
-
-            if ( $model )
-                return $model;
-            else
-                return CONTENT_MODEL_TEXT;
+                       switch ( $ext ) {
+                               case 'js':
+                                       return CONTENT_MODEL_JAVASCRIPT;
+                               case 'css':
+                                       return CONTENT_MODEL_CSS;
+                               default:
+                                       return is_null( $model ) ? CONTENT_MODEL_TEXT : $model;
+                       }
         }
 
-        # we established that is must be wikitext
+        // we established that is must be wikitext
+
         return CONTENT_MODEL_WIKITEXT;
     }
 
@@ -119,9 +156,11 @@ abstract class ContentHandler {
 
         if ( empty( $wgContentHandlers[$modelName] ) ) {
             $handler = null;
-            wfRunHooks( "ContentHandlerForModelName", array( $modelName, &$handler ) );  #FIXME: document new hook
 
-            if ( $handler ) { # NOTE: may be a string or an object, either is fine!
+                       // TODO: document new hook
+            wfRunHooks( 'ContentHandlerForModelName', array( $modelName, &$handler ) );
+
+            if ( $handler ) { // NOTE: may be a string or an object, either is fine!
                 $wgContentHandlers[$modelName] = $handler;
             } else {
                 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
@@ -136,15 +175,15 @@ abstract class ContentHandler {
         return $wgContentHandlers[$modelName];
     }
 
-    # ----------------------------------------------------------------------------------------------------------
+    // ----------------------------------------------------------------------------------------------------------
     public function __construct( $modelName, $formats ) {
         $this->mModelName = $modelName;
         $this->mSupportedFormats = $formats;
     }
 
     public function getModelName() {
-        # for wikitext: wikitext; in the future: wikiast, wikidom?
-        # for wikidata: wikidata
+        // for wikitext: wikitext; in the future: wikiast, wikidom?
+        // for wikidata: wikidata
         return $this->mModelName;
     }
 
@@ -155,8 +194,8 @@ abstract class ContentHandler {
     }
 
     public function getSupportedFormats() {
-        # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
-        # for wikidata: "application/json", "application/x-php", etc
+        // for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
+        // for wikidata: "application/json", "application/x-php", etc
         return $this->mSupportedFormats;
     }
 
@@ -165,7 +204,10 @@ abstract class ContentHandler {
     }
 
     public function isSupportedFormat( $format ) {
-        if ( !$format ) return true; # this means "use the default"
+
+        if ( !$format ) {
+                       return true; // this means "use the default"
+               }
 
         return in_array( $format, $this->mSupportedFormats );
     }
@@ -176,32 +218,14 @@ abstract class ContentHandler {
         }
     }
 
-    /**
-     * @abstract
-     * @param Content $content
-     * @param null $format
-     * @return String
-     */
-    public abstract function serialize( Content $content, $format = null );
-
-    /**
-     * @abstract
-     * @param $blob String
-     * @param null $format
-     * @return Content
-     */
-    public abstract function unserialize( $blob, $format = null );
-
-    public abstract function emptyContent();
-
     /**
      * Return an Article object suitable for viewing the given object
      *
      * NOTE: does *not* do special handling for Image and Category pages!
      *       Use Article::newFromTitle() for that!
      *
-     * @param type $title
-     * @return \Article
+     * @param Title $title
+     * @return Article
      * @todo Article is being refactored into an action class, keep track of that
      */
     public function createArticle( Title $title ) {
@@ -213,12 +237,12 @@ abstract class ContentHandler {
 
     /**
      * Return an EditPage object suitable for editing the given object
-     * 
-     * @param type $article
-     * @return \EditPage 
+     *
+     * @param Article $article
+     * @return EditPage
      */
     public function createEditPage( Article $article ) {
-        $this->checkModelName( $article->getContentObject()->getModelName() );
+        $this->checkModelName( $article->getContentModelName() );
 
         $editPage = new EditPage( $article );
         return $editPage;
@@ -227,8 +251,8 @@ abstract class ContentHandler {
     /**
      * Return an ExternalEdit object suitable for editing the given object
      *
-     * @param type $article
-     * @return \ExternalEdit
+     * @param IContextSource $context
+     * @return ExternalEdit
      */
     public function createExternalEdit( IContextSource $context ) {
         $this->checkModelName( $context->getTitle()->getModelName() );
@@ -245,17 +269,30 @@ abstract class ContentHandler {
      * @param $rcid Integer ??? FIXME (default 0)
      * @param $refreshCache boolean If set, refreshes the diff cache
      * @param $unhide boolean If set, allow viewing deleted revs
+        *
+        * @return DifferenceEngine
      */
     public function getDifferenceEngine( IContextSource $context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
                                          $refreshCache = false, $unhide = false ) {
 
         $this->checkModelName( $context->getTitle()->getModelName() );
 
-        $de = new DifferenceEngine( $context, $old, $new, $rcid, $refreshCache, $unhide );
+               $diffEngineClass = $this->getDiffEngineClass();
 
-        return $de;
+        return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
     }
 
+       /**
+        * Returns the name of the diff engine to use.
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getDiffEngineClass() {
+               return 'DifferenceEngine';
+       }
+
     /**
      * attempts to merge differences between three versions.
      * Returns a new Content object for a clean merge and false for failure or a conflict.
@@ -274,20 +311,21 @@ abstract class ContentHandler {
     /**
      * Return an applicable autosummary if one exists for the given edit.
      *
-     * @param $oldContent Content: the previous text of the page.
-     * @param $newContent Content: The submitted text of the page.
+     * @param $oldContent Content|null: the previous text of the page.
+     * @param $newContent Content|null: The submitted text of the page.
      * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
      *
      * @return string An appropriate autosummary, or an empty string.
      */
-    public function getAutosummary( Content $oldContent, Content $newContent, $flags ) {
+    public function getAutosummary( Content $oldContent = null, Content $newContent = null, $flags ) {
         global $wgContLang;
 
-        # Decide what kind of autosummary is needed.
+        // Decide what kind of autosummary is needed.
+
+        // Redirect autosummaries
 
-        # Redirect autosummaries
-        $ot = $oldContent->getRedirectTarget();
-        $rt = $newContent->getRedirectTarget();
+        $ot = !empty( $ot ) ? $oldContent->getRedirectTarget() : false;
+        $rt = !empty( $rt ) ? $newContent->getRedirectTarget() : false;
 
         if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
 
@@ -299,9 +337,9 @@ abstract class ContentHandler {
             return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
         }
 
-        # New page autosummaries
+        // New page autosummaries
         if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
-            # If they're making a new article, give its text, truncated, in the summary.
+            // If they're making a new article, give its text, truncated, in the summary.
 
             $truncatedtext = $newContent->getTextForSummary(
                 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
@@ -309,11 +347,11 @@ abstract class ContentHandler {
             return wfMsgForContent( 'autosumm-new', $truncatedtext );
         }
 
-        # Blanking autosummaries
+        // Blanking autosummaries
         if ( $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
             return wfMsgForContent( 'autosumm-blank' );
         } elseif ( $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
-            # Removing more than 90% of the article
+            // Removing more than 90% of the article
 
             $truncatedtext = $newContent->getTextForSummary(
                 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
@@ -321,8 +359,9 @@ abstract class ContentHandler {
             return wfMsgForContent( 'autosumm-replace', $truncatedtext );
         }
 
-        # If we reach this point, there's no applicable autosummary for our case, so our
-        # autosummary is empty.
+        // If we reach this point, there's no applicable autosummary for our case, so our
+        // autosummary is empty.
+
         return '';
     }
 
@@ -335,8 +374,6 @@ abstract class ContentHandler {
      *    if no revision occurred
      */
     public function getAutoDeleteReason( Title $title, &$hasHistory ) {
-        global $wgContLang;
-
         $dbw = wfGetDB( DB_MASTER );
 
         // Get the last revision
@@ -436,7 +473,7 @@ abstract class ContentHandler {
         $undoafter_content = $undoafter->getContent();
 
         if ( $cur_content->equals( $undo_content ) ) {
-            # No use doing a merge if it's just a straight revert.
+            // No use doing a merge if it's just a straight revert.
             return $undoafter_content;
         }
 
@@ -444,19 +481,6 @@ abstract class ContentHandler {
 
         return $undone_content;
     }
-
-    #TODO: how to handle extra message for JS/CSS previews??
-    #TODO: Article::showCssOrJsPage ---> specialized classes!
-
-    #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
-
-    #TODO: hook into dump generation to serialize and record model and format!
-    #TODO: cover action=raw
-    #TODO: make sure we cover lucene search / wikisearch.
-    #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?!)
 }
 
 
@@ -494,8 +518,13 @@ abstract class TextContentHandler extends ContentHandler {
 
         $ok = wfMerge( $old, $mine, $yours, $result );
 
-        if ( !$ok ) return false;
-        if ( !$result ) return $this->emptyContent();
+        if ( !$ok ) {
+                       return false;
+               }
+
+        if ( !$result ) {
+                       return $this->emptyContent();
+               }
 
         $mergedContent = $this->unserialize( $result, $format );
         return $mergedContent;
@@ -516,7 +545,7 @@ class WikitextContentHandler extends TextContentHandler {
     }
 
     public function emptyContent() {
-        return new WikitextContent( "" );
+        return new WikitextContent( '' );
     }
 
 
@@ -535,7 +564,7 @@ class JavaScriptContentHandler extends TextContentHandler {
     }
 
     public function emptyContent() {
-        return new JavaScriptContent( "" );
+        return new JavaScriptContent( '' );
     }
 }
 
@@ -550,7 +579,7 @@ class CssContentHandler extends TextContentHandler {
     }
 
     public function emptyContent() {
-        return new CssContent( "" );
+        return new CssContent( '' );
     }
 
 }