MediaWiki r29444 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r29443‎ | r29444 (on ViewVC)‎ | r29445 >
Date:18:34, 8 January 2008
Author:vasilievvv
Status:old
Tags:
Comment:
* Add exception hooks to output pretty messages
* Also, there's an example of such extension
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 29443)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 29444)
@@ -2854,3 +2854,13 @@
 $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: trunk/phase3/includes/Exception.php
===================================================================
--- trunk/phase3/includes/Exception.php	(revision 29443)
+++ trunk/phase3/includes/Exception.php	(revision 29444)
@@ -16,6 +16,26 @@
 		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 @@
 			$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 @@
 	function htmlFooter() {
 		echo "</body></html>";
 	}
-
 }
 
 /**
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 29443)
+++ trunk/phase3/RELEASE-NOTES	(revision 29444)
@@ -121,6 +121,7 @@
 * 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: trunk/extensions/examples/ErrorPage/dberr.html
===================================================================
--- trunk/extensions/examples/ErrorPage/dberr.html	(revision 0)
+++ trunk/extensions/examples/ErrorPage/dberr.html	(revision 29444)
@@ -0,0 +1,40 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
+	<head>
+		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+		<title>Database error</title>
+		<style type="text/css" media="screen, projection">/*<![CDATA[*/
+			@import "{{skins}}/common/shared.css?104";
+			@import "{{skins}}/monobook/main.css?104";
+		/*]]>*/</style>
+	</head>
+	<body class="mediawiki">
+	<div id="globalWrapper">
+		<div id="column-content">
+		<div id="content">
+			<h1>
+				Database error
+			</h1>
+			<div id="bodyContent">
+				<p><strong>Database error was happend</strong></P>
+				<p>Last database query: <code>{{sql}}</code></p>
+				<p>Function: <code>{{function}}</code></p>
+				<p>MySQL error: <code>{{errno}}: {{error}}</code></p>
+			</div>
+		</div>
+		</div>
+		<div id="column-one">
+			<div class="portlet" id="p-logo">
+				<img src="{{logo}}" alt="{{sitename}} logo" style="float:left;margin-right:1em" />
+			</div>
+		</div>
+		<div class="visualClear"></div>
+		<div id="footer">
+			<div id="f-poweredbyico">
+				<a href="http://www.mediawiki.org/"><img src="{{skins}}/common/images/poweredby_mediawiki_88x31.png" alt="Powered by MediaWiki" /></a>
+			</div>
+			<p>Generated on {{now}} &mdash; <a href="irc://irc.freenode.net/#wikimedia-tech" class="link-irc">Technical IRC channel</a></p>
+		</div>
+	</div>
+	</body>
+</html>
\ No newline at end of file
Index: trunk/extensions/examples/ErrorPage/dbconn.html
===================================================================
--- trunk/extensions/examples/ErrorPage/dbconn.html	(revision 0)
+++ trunk/extensions/examples/ErrorPage/dbconn.html	(revision 29444)
@@ -0,0 +1,40 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
+	<head>
+		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+		<title>Wikimedia has some problems with database servers</title>
+		<style type="text/css" media="screen, projection">/*<![CDATA[*/
+			@import "{{skins}}/common/shared.css?104";
+			@import "{{skins}}/monobook/main.css?104";
+		/*]]>*/</style>
+	</head>
+	<body class="mediawiki">
+	<div id="globalWrapper">
+		<div id="column-content">
+		<div id="content">
+			<h1>
+				Wikimedia has some problems with database servers
+			</h1>
+			<div id="bodyContent">
+				<p style="font-weight: bold; font-size: 15px">Sorry! This site is experiencing technical difficulties.</p>
+				<p>Try waiting a few minutes and reloading.</p>
+				<p>If you report us about this error, please provide following information:</p>
+				<p><code>Error: {{dberror}}</code></p>
+			</div>
+		</div>
+		</div>
+		<div id="column-one">
+			<div class="portlet" id="p-logo">
+				<img src="{{logo}}" alt="{{sitename}} logo" style="float:left;margin-right:1em" />
+			</div>
+		</div>
+		<div class="visualClear"></div>
+		<div id="footer">
+			<div id="f-poweredbyico">
+				<a href="http://www.mediawiki.org/"><img src="{{skins}}/common/images/poweredby_mediawiki_88x31.png" alt="Powered by MediaWiki" /></a>
+			</div>
+			<p>Generated on {{now}} &mdash; <a href="http://wikimediafoundation.org/wiki/Donate/en">Donate</a> &mdash; <a href="irc://irc.freenode.net/#wikimedia-tech" class="link-irc">Technical IRC channel</a></p>
+		</div>
+	</div>
+	</body>
+</html>
\ No newline at end of file
Index: trunk/extensions/examples/ErrorPage/NiceErrorPage.php
===================================================================
--- trunk/extensions/examples/ErrorPage/NiceErrorPage.php	(revision 0)
+++ trunk/extensions/examples/ErrorPage/NiceErrorPage.php	(revision 29444)
@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * Example of the page that uses extensions hooks for
+ * making nice error page.
+ *
+ * @author VasilievVV
+ * @license GNU GPL 2 or later
+ */
+
+//$wgExceptionHooks['DBConnectionErrorRaw'][] = array( 'NiceErrorPage', 'dbConnectionOP' );
+$wgExceptionHooks['DBConnectionErrorRaw'][] = array( 'NiceErrorPage', 'dbConnection' );
+$wgExceptionHooks['DBQueryErrorRaw'][] = array( 'NiceErrorPage', 'dbQueryError' );
+
+$wgNiceErrorPagePath = "{$wgServer}{$wgScriptPath}/extensions/examples/ErrorPage";
+
+class NiceErrorPage {
+	static function dbConnection( $error ) {
+		if ( trim( $error->error ) == '' ) {
+			$error->error = $error->db->getProperty('mServer');
+		}
+		return self::preprocessPage( 'dbconn', array( 'dberror' => $error->error ) );
+	}
+	
+	static function dbQueryError( $error ) {
+		$args = array(
+			'sql' => $error->getSQL(),
+			'function' => $error->fname,
+			'errno' => $error->errno,
+			'error' => $error->error,
+		);
+		return self::preprocessPage( 'dberr', $args );
+	}
+
+	static function preprocessPage( $name, $args = array() ) {
+		$page = file_get_contents( dirname( __FILE__ ) . "/{$name}.html" );
+		$args = array_merge( self::commonArgs(), $args );
+		foreach( $args as $aname => $aval ) {
+			$page = preg_replace( '/{{' . $aname . '}}/i', $aval, $page );
+		}
+		return $page;
+	}
+
+	static function commonArgs() {
+		global $wgNiceErrorPagePath, $wgLogo, $wgSitename, $wgServer, $wgScriptPath;
+		$a = array(
+			'basepath' => $wgNiceErrorPagePath,
+			'logo' => $wgLogo,
+			'now' => wfTimestamp( TS_RFC2822 ),
+			'sitename' => $wgSitename,
+			'skins' => "{$wgServer}{$wgScriptPath}/skins",
+		);
+		return $a;
+	}
+
+	static function __call( $method, $args ) {
+		$name = str_replace( 'OP', '', $method );
+		return $this->$name( $args[0] );
+	}
+}
\ No newline at end of file

Property changes on: trunk/extensions/examples/ErrorPage/NiceErrorPage.php
___________________________________________________________________
Name: svn:eol-style
   + native

Status & tagging log

  • 15:23, 12 September 2011 Meno25 (Talk | contribs) changed the status of r29444 [removed: ok added: old]
Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox