Page MenuHomePhabricator

Var-args format for wfRunHooks() doesn't allow modifying params
Closed, ResolvedPublic

Description

Author: evan

Description:
The problem is with Hooks.php, the event-handling mechanism for
optional functionality and third-party extensions. wfRunHooks(), the
interface for mainline code to raise events for hooks to handle,
uses PHP's variable-argument syntax. This is so you can give different
parameter sets to different events:

wfRunHooks('Event1', $param1, $param2, $param3); # three params
wfRunHooks('Event2', $param4, $param5); # just two params

The problem with this is that the var args syntax silently changes
pass-by-reference semantics to pass-by-value semantics. So events that
should allow modifying parameters do not. For example:

$wgEventHooks['Event3'][] = 'MyEvent3Handler';
# ...
function MyEvent3Handler(&$str) {
    $str .= " and so on.";
    return true;
    }
# ...
$somestring = "Eggs, bananas, bacon";
# NOTE: OLD CALLING FORMAT
if (wfRunHooks('Event3', &$somestring)) {
    echo $somestring; # prints "Eggs, bananas, bacon"
}

After some consulting with PHP gurus, the best solution I could come up
with was to pack the params in an array when passing them to
wfRunHooks(). Now the last few lines are:

if (wfRunHooks('Event3', array(&$somestring))) {
    echo $somestring; # prints "Eggs, bananas, bacon and so on."
}

Adding a reference to an array keeps its "reference-ness". Note that there's no
change to the (extension-located) hook function, just to the core code calling
conventions.

The above change has been implemented in HEAD; it needs to be ported to REL1_4.
(It was implemented and tested in REL1_4 originally, then moved to HEAD at the
request of vibber).


Version: 1.4.x
Severity: normal

Details

Reference
bz1694

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 8:13 PM
bzimport set Reference to bz1694.
bzimport added a subscriber: Unknown Object (MLST).

This got done a while ago iirc. Closing.