Difference between revisions of "Extension:IsAllowedHook.php"
m |
|||
Line 1: | Line 1: | ||
<?php | <?php | ||
− | # Extension:IsAllowedHook{{php}}{{Category:Extensions|IsAllowedHook}} | + | # Extension:IsAllowedHook{{php}}{{Category:Extensions|IsAllowedHook}}{{Category:Code that uses voodoo|IsAllowedHook}} |
# - Started: 2007-10-09 | # - Started: 2007-10-09 | ||
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | # - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) |
Revision as of 11:01, 23 October 2007
<?php
- Extension:IsAllowedHookTemplate:Php
In computer programming, "Voodoo", or "Magic" refers to techniques that are secret or not widely known, and may be deliberately kept secret. The Jargon File makes a distinction between "deep magic", which refers to code based on esoteric theoretical knowledge; "black magic" (voodoo), which refers to code based on techniques that appear to work but which lack a theoretical explanation; and "heavy wizardry", which refers to code based on obscure or undocumented intricacies of particular hardware or software.
At Organic Design the most common of these is extending an instance's class at runtime after it has been instantiated, a technique that can be used to provide additional hooks into existing code without requiring modification of code-base files. Another is reading in a class file, declaring it under a different name, then sub-classing that with a new class of the original name - that way the environment uses the new extended class thinking it's the original one.
See also
- Adding a variable to a class instance at runtime - demonstrated in many different languages
- - Started: 2007-10-09
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('ISALLOWED_VERSION','0.0.0, 2007-10-09');
$wgExtensionCredits['parserhook'][] = array( 'name' => "IsAllowedHook", 'author' => 'User:Nad', 'description' => 'Adds a new hook called "IsAllowed" which is called from the User::isAllowed method.', 'url' => 'http://www.organicdesign.co.nz/Extension:IsAllowedHook.php', 'version' => ISALLOWED_VERSION );
- This is the earliest hook I can find after which $wgUser is an instance of the User class
$wgHooks['userCan'][] = 'wfSetupIsAllowed'; function wfSetupIsAllowed() {
# Return unless first call static $first = 0; if ($first++) return true; global $wgUser;
# Create a new User class ($User2) by extending the existing one with an overridden isAllowed method $User = get_class($wgUser); $User2 = $User.'2'; eval("class $User2 extends $User".' { function isAllowed($action = "") { $result = NULL; wfRunHooks("IsAllowed",array(&$this,$action,&$result)); return $result === NULL ? $result = parent::isAllowed($action) : $result; } }');
# Replace the $wgUser object with an identical $User2 instance $oldUser = $wgUser; $wgUser = new $User2(); foreach(array_keys(get_class_vars($User)) as $k) $wgUser->$k = $oldUser->$k;
return true; }
?>