Difference between revisions of "Extension:DatabaseFetchHook.php"
m |
({{Voodoo}}) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<?php | <?php | ||
− | # Extension: | + | # Extension:DatabaseFetchHook{{php}}{{Category:Extensions|DatabaseFetchHook}}{{Voodoo}} |
− | # - See http://www.mediawiki.org/wiki/Extension: | + | # - See http://www.mediawiki.org/wiki/Extension:DatabaseFetchHook for installation and usage details |
# - Started: 2007-03-05 | # - Started: 2007-03-05 | ||
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | # - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | ||
Line 7: | Line 7: | ||
if (!defined('MEDIAWIKI')) die('Not an entry point.'); | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
− | define(' | + | define('DFH_VERSION','2.0.0, 2007-10-11'); |
$wgExtensionCredits['parserhook'][] = array( | $wgExtensionCredits['parserhook'][] = array( | ||
− | 'name' => " | + | 'name' => "DatabaseFetchHook", |
'author' => '[http://www.organicdesign.co.nz/nad User:Nad]', | 'author' => '[http://www.organicdesign.co.nz/nad User:Nad]', | ||
− | 'description' => 'Dynamically adds a new hook to intercept database | + | 'description' => 'Dynamically adds a new hook to intercept the database fetchObject and fetchRow methods', |
− | 'url' => 'http://www.mediawiki.org/wiki/Extension: | + | 'url' => 'http://www.mediawiki.org/wiki/Extension:DatabaseFetchHook', |
− | 'version' => | + | 'version' => DFH_VERSION |
); | ); | ||
− | + | $wgExtensionFunctions[] = 'wfSetupDatabaseFetchHook'; | |
− | $ | + | function wfSetupDatabaseFetchHook() { |
− | $ | + | global $wgLoadBalancer,$wgDBtype; |
− | |||
− | + | wfGetDB(); # This ensures that $wgLoadBalancer is not a stub object when we subclass it | |
− | |||
− | |||
− | + | # Create a replica of the Database class which calls the hook in its fetch methods | |
− | + | $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; | |
+ | } | ||
+ | }'); | ||
− | return $ | + | # Create a replica of the LoadBalancer class which uses the new Database class for its connection objects |
− | } | + | $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 $wgLoadBalancer object with an identical instance of the new LoadBalancer2 class | |
+ | $wgLoadBalancer->closeAll(); # Close any open connections as they will be of the original Database class | ||
+ | $oldLoadBalancer = $wgLoadBalancer; | ||
+ | $wgLoadBalancer = new $LoadBalancer2($oldLoadBalancer->mServers); | ||
+ | foreach(array_keys(get_class_vars($LoadBalancer)) as $k) $wgLoadBalancer->$k = $oldLoadBalancer->$k; | ||
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
?> | ?> |
Latest revision as of 11:05, 23 July 2008
<?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'; function wfSetupDatabaseFetchHook() { global $wgLoadBalancer,$wgDBtype;
wfGetDB(); # This ensures that $wgLoadBalancer is not a stub object when we subclass it
# Create a replica of the Database class which calls the hook in its fetch methods $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; } }');
# Create a replica of the LoadBalancer class which uses the new Database class for its connection objects $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 $wgLoadBalancer object with an identical instance of the new LoadBalancer2 class $wgLoadBalancer->closeAll(); # Close any open connections as they will be of the original Database class $oldLoadBalancer = $wgLoadBalancer; $wgLoadBalancer = new $LoadBalancer2($oldLoadBalancer->mServers); foreach(array_keys(get_class_vars($LoadBalancer)) as $k) $wgLoadBalancer->$k = $oldLoadBalancer->$k;
}
?>