Difference between revisions of "Extension:DatabaseFetchHook.php"
(tested ok on 1.11) |
|||
Line 16: | Line 16: | ||
'version' => DFH_VERSION | 'version' => DFH_VERSION | ||
); | ); | ||
+ | |||
+ | $wgExtensionFunctions[] = 'wfSetupDatabaseFetchHook'; | ||
# Create a LoadBalancer2 class by extending the existing LoadBalancer | # Create a LoadBalancer2 class by extending the existing LoadBalancer | ||
− | + | function wfSetupDatabaseFetchHook() { | |
− | $ | + | global $wgLoadBalancer,$wgDBtype; |
− | eval("class $ | + | |
+ | $type = ucfirst($wgDBtype); | ||
+ | eval("class Database{$type}2 extends Database{$type}".' { | ||
− | + | function fetchObject($res) { | |
− | + | $row = parent::fetchObject($res); | |
− | + | wfRunHooks("DatabaseFetchHook", array(&$this,&$row)); | |
+ | return $row; | ||
+ | } | ||
− | + | function fetchRow($res) { | |
− | + | $row = parent::fetchRow($res); | |
− | + | wfRunHooks("DatabaseFetchHook", array(&$this,&$row)); | |
− | + | return $row; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | }'); | ||
− | + | $LoadBalancer = get_class($wgLoadBalancer); | |
− | + | $LoadBalancer2 = $LoadBalancer."2"; | |
− | + | eval("class $LoadBalancer2 extends $LoadBalancer".' { | |
− | + | function reallyOpenConnection(&$server) { | |
+ | $server["type"] .= "2"; | ||
+ | $db =& parent::reallyOpenConnection($server); | ||
+ | return $db; | ||
+ | } | ||
+ | }'); | ||
− | + | # Replace the global LoadBalancer object with an identical instance of the new LoadBalancer2 class | |
− | + | $wgLoadBalancer->closeAll(); | |
+ | $oldLoadBalancer = $wgLoadBalancer; | ||
+ | $wgLoadBalancer = new $LoadBalancer2($oldLoadBalancer->mServers); | ||
+ | foreach(array_keys(get_class_vars($LoadBalancer)) as $k) $wgLoadBalancer->$k = $oldLoadBalancer->$k; | ||
− | + | $GLOBALS['wgSiteNotice'] = '$wgLoadBalancer is a '.get_class($wgLoadBalancer) | |
+ | .'<br>'.'wfGetDB has returned a '.get_class(wfGetDB()); | ||
+ | |||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
?> | ?> |
Revision as of 09:51, 11 October 2007
<?php
- Extension:DatabaseFetchHookTemplate:Php
- - See http://www.mediawiki.org/wiki/Extension:DatabaseFetchHook for installation and usage details
- - Started: 2007-03-05
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('DFH_VERSION','2.0.0, 2007-10-11');
$wgExtensionCredits['parserhook'][] = array( 'name' => "DatabaseFetchHook", 'author' => 'User:Nad', 'description' => 'Dynamically adds a new hook to intercept the database fetchObject and fetchRow methods', 'url' => 'http://www.mediawiki.org/wiki/Extension:DatabaseFetchHook', 'version' => DFH_VERSION );
$wgExtensionFunctions[] = 'wfSetupDatabaseFetchHook';
- Create a LoadBalancer2 class by extending the existing LoadBalancer
function wfSetupDatabaseFetchHook() { global $wgLoadBalancer,$wgDBtype;
$type = ucfirst($wgDBtype); eval("class Database{$type}2 extends Database{$type}".' {
function fetchObject($res) { $row = parent::fetchObject($res); wfRunHooks("DatabaseFetchHook", array(&$this,&$row)); return $row; }
function fetchRow($res) { $row = parent::fetchRow($res); wfRunHooks("DatabaseFetchHook", array(&$this,&$row)); return $row; } }');
$LoadBalancer = get_class($wgLoadBalancer); $LoadBalancer2 = $LoadBalancer."2"; eval("class $LoadBalancer2 extends $LoadBalancer".' { function reallyOpenConnection(&$server) { $server["type"] .= "2"; $db =& parent::reallyOpenConnection($server); return $db; } }');
# Replace the global LoadBalancer object with an identical instance of the new LoadBalancer2 class $wgLoadBalancer->closeAll(); $oldLoadBalancer = $wgLoadBalancer; $wgLoadBalancer = new $LoadBalancer2($oldLoadBalancer->mServers); foreach(array_keys(get_class_vars($LoadBalancer)) as $k) $wgLoadBalancer->$k = $oldLoadBalancer->$k;
$GLOBALS['wgSiteNotice'] = '$wgLoadBalancer is a '.get_class($wgLoadBalancer)
.'
'.'wfGetDB has returned a '.get_class(wfGetDB());
}
?>