* Add exception hooks to output pretty messages
authorVictor Vasiliev <vasilievvv@users.mediawiki.org>
Tue, 8 Jan 2008 18:34:06 +0000 (18:34 +0000)
committerVictor Vasiliev <vasilievvv@users.mediawiki.org>
Tue, 8 Jan 2008 18:34:06 +0000 (18:34 +0000)
* Also, there's an example of such extension

RELEASE-NOTES
includes/DefaultSettings.php
includes/Exception.php

index ec58ad7..3495ffc 100644 (file)
@@ -121,6 +121,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * Allow subpage syntax for Special:Userrights, i.e., Special:Userrights/Name.
 * When submitting changes on Special:Userrights, show the full form again, not
   just the search box.
+* Added exception hooks
 
 
 === Bug fixes in 1.12 ===
index 74e8d79..048eba3 100644 (file)
@@ -2854,3 +2854,13 @@ $wgSlaveLagCritical = 30;
 $wgParserConf = array( 
        'class' => 'Parser',
 );
+
+/**
+ * Hooks that are used for outputting exceptions
+ * Format is:
+ *     $wgExceptionHooks[] = $funcname
+ * or:
+ *     $wgExceptionHooks[] = array( $class, $funcname )
+ * Hooks should return strings or false
+ */
+$wgExceptionHooks = array();
\ No newline at end of file
index 3756fb7..2fd5435 100644 (file)
@@ -16,6 +16,26 @@ class MWException extends Exception
                return is_object( $wgLang );
        }
 
+       function runHooks( $name, $args = array() ) {
+               global $wgExceptionHooks;
+               if( !isset( $wgExceptionHooks ) || !is_array( $wgExceptionHooks ) ) 
+                       return; // Just silently ignore
+               if( !array_key_exists( $name, $wgExceptionHooks ) || !is_array( $wgExceptionHooks[ $name ] ) )
+                       return;
+               $hooks = $wgExceptionHooks[ $name ];
+               $callargs = array_merge( array( $this ), $args );
+
+               foreach( $hooks as $hook ) {
+                       if( is_string( $hook ) || ( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) {     //'function' or array( 'class', hook' )
+                               $result = call_user_func_array( $hook, $callargs );
+                       } else {
+                               $result = null;
+                       }
+                       if( is_string( $result ) )
+                               return $result;
+               }
+       }
+
        /** Get a message from i18n */
        function msg( $key, $fallback /*[, params...] */ ) {
                $args = array_slice( func_get_args(), 2 );
@@ -83,9 +103,16 @@ class MWException extends Exception
                        $wgOut->enableClientCache( false );
                        $wgOut->redirect( '' );
                        $wgOut->clearHTML();
-                       $wgOut->addHTML( $this->getHTML() );
+                       if( $hookResult = $this->runHooks( get_class( $this ) ) ) {
+                               $wgOut->addHTML( $hookResult );
+                       } else {
+                               $wgOut->addHTML( $this->getHTML() );
+                       }
                        $wgOut->output();
                } else {
+                       if( $hookResult = $this->runHooks( get_class( $this ) . "Raw" ) ) {
+                               die( $hookResult );
+                       }
                        echo $this->htmlHeader();
                        echo $this->getHTML();
                        echo $this->htmlFooter();
@@ -131,7 +158,6 @@ class MWException extends Exception
        function htmlFooter() {
                echo "</body></html>";
        }
-
 }
 
 /**