Difference between revisions of "Extension:DatabaseFetchHook.php"

From Organic Design wiki
(add comments)
Line 18: Line 18:
  
 
$wgExtensionFunctions[] = 'wfSetupDatabaseFetchHook';
 
$wgExtensionFunctions[] = 'wfSetupDatabaseFetchHook';
 
# Create a LoadBalancer2 class by extending the existing LoadBalancer
 
 
function wfSetupDatabaseFetchHook() {
 
function wfSetupDatabaseFetchHook() {
 
global $wgLoadBalancer,$wgDBtype;
 
global $wgLoadBalancer,$wgDBtype;
  
 +
# Create a replica of the Database class which calls the hook in its fetch methods
 
$type = ucfirst($wgDBtype);
 
$type = ucfirst($wgDBtype);
 
eval("class Database{$type}2 extends Database{$type}".' {
 
eval("class Database{$type}2 extends Database{$type}".' {
Line 39: Line 38:
 
}');
 
}');
  
 +
# Create a replica of the LoadBalancer class which uses the new Database class for its connection objects
 
$LoadBalancer  = get_class($wgLoadBalancer);
 
$LoadBalancer  = get_class($wgLoadBalancer);
 
$LoadBalancer2 = $LoadBalancer."2";
 
$LoadBalancer2 = $LoadBalancer."2";
Line 49: Line 49:
 
}');
 
}');
  
# Replace the global LoadBalancer object with an identical instance of the new LoadBalancer2 class
+
# Replace the global $wgLoadBalancer object with an identical instance of the new LoadBalancer2 class
 
$wgLoadBalancer->closeAll();
 
$wgLoadBalancer->closeAll();
 
$oldLoadBalancer = $wgLoadBalancer;
 
$oldLoadBalancer = $wgLoadBalancer;

Revision as of 10:22, 11 October 2007

<?php

  1. Extension:DatabaseFetchHookTemplate:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
  1. - See http://www.mediawiki.org/wiki/Extension:DatabaseFetchHook for installation and usage details
  2. - Started: 2007-03-05
  3. - 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;

# 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(); $oldLoadBalancer = $wgLoadBalancer; $wgLoadBalancer = new $LoadBalancer2($oldLoadBalancer->mServers); foreach(array_keys(get_class_vars($LoadBalancer)) as $k) $wgLoadBalancer->$k = $oldLoadBalancer->$k;

}

?>