Wedge

Public area => Features => The Pub => Features: Miscellaneous => Topic started by: Nao on May 7th, 2011, 12:55 PM

Title: Improvements to Hooks
Post by: Nao on May 7th, 2011, 12:55 PM
Feature: Improvements to Hooks
Developer: Nao (main), Arantor
Target: modders, admins
Status: 60% (source hooks completed; template hooks in progress; considering whether to add file param; once everything's done, need to look through the code and add hooks where useful.)
Comment:

It took SMF a long time to add "real" support for hooks -- a way to allow plug-ins to safely add code to the main codebase without actually editing the original files. Still, it's far from being a usable standard.

We simplified the calls to begin with. "add_hook('hook_point', 'my_function', 'my_source_file')" will execute 'function' once Wedge reaches the specified hook point (you may specify a source file to load before calling it.)

We provided context data to a few more hooks, and deleted a few useless hooks and outdated mechanisms (such as pre_include, catch_settings or the SMF_INTEGRATION_SETTINGS array).

Mostly, though, we added hooks to various places that SMF doesn't handle, like auto-suggest, behavior check, friend additions, post creation/modification (before and after), browser detection, query string analysis, dynamic rewrite (pre-pretty URL output buffer time), admin settings, credits...

The code internals are also better laid out. Hooks are unregistered if they're no longer available (e.g. uninstalled add-on.) Everything is put into a $modSettings['hooks'] array, instead of scattered through multiple annoying $modSettings['integrate_*'] variables.
We also try and make sure existing hooks can actually do something instead of just stare at the sky. They can now for instance intercept and modify outstanding personal messages, new topics and error messages.

This feature is under development and hasn't yet a state where it can be considered to be feature-frozen. The goal is to enable modders to add their code anywhere, without having to use SMF's code edit hacks... Thus making it possible to upgrade Wedge versions without uninstalling and reinstalling plugins in the process.
Title: Re: Improvements to Hooks
Post by: Nao on May 11th, 2011, 08:23 AM
:edit: Expanded added/deleted hook paragraphs.
Title: What's a hook?
Post by: Farjo on May 22nd, 2011, 02:16 PM
In many of the new features threads there is reference to programming hooks. As a non-coder I have only a vague idea what these are and why they are so important to demand so many threads.


My best guess is:
Code: [Select]
hook(a)
codeblock_1{code...

..
..
..
}
hook(b)
codeblock_2{code...
etc.
So someone can write a mod so that, say, hook(b) executes[1] their code after the codeblock_1. Thus the actual wedge code does not need to be changed as it is with SMF where a chunk of code would be inserted.


Is this anywhere near reality  :blush:
 1. sorry for my poor programming grammar if I use incorrect terms
Title: Re: What's a hook?
Post by: Arantor on May 22nd, 2011, 03:08 PM
That's basically it, yes.

A practical example of the hook usage would be the Display template as of 2.0 RC4 (or RC5, not sure off hand which)

Prior to that, if you wanted to add a button to the menu under a topic - like the Reply, Notify or whatever (here we have a Move To Tracker button!), you have to modify the code.

But with the later version, the array that holds that menu is provided to a function you can specify, which means you can modify it without editing any files.

Another example is the bbcode system; previously, if you wanted to create a new bbcode, you had to edit the file in some way.


Hooks just being able to run functions isn't necessarily that beneficial - but if you can take data out of the main program and manipulate it inside a hook, you're able to do a whole lot more without any code changes, theme or otherwise.
Title: Re: What's a hook?
Post by: DoctorMalboro on May 22nd, 2011, 04:11 PM
I don't get them in SMF, and I don't think I've get them here... :P

I need good examples of it.
Title: Re: What's a hook?
Post by: Nao on May 22nd, 2011, 04:26 PM
Hmm, makes me think we should add hooks at post preparation time...
If only to enable mods to add their own entries into the user menu, things like that.
Title: Re: Improvements to Hooks
Post by: Arantor on May 22nd, 2011, 04:53 PM
Quote from Nao/Gilles on May 22nd, 2011, 04:26 PM
Hmm, makes me think we should add hooks at post preparation time...
If only to enable mods to add their own entries into the user menu, things like that.
That's mostly what SMF uses them for. It's functional if not particularly imaginative.
Quote from DoctorMalboro on May 22nd, 2011, 04:11 PM
I don't get them in SMF, and I don't think I've get them here... :P

I need good examples of it.
OK, let me grab you some from SimpleDesk 2.0.

SimpleDesk is big and scary and does stuff like load its own permissions on every page, amongst plenty of other things it does.

One of them is to load Subs-SimpleDesk.php. Now, I could make a code edit somewhere to load that file, or I could avoid editing any SMF file and add it to the integrate_pre_load hook. The hook is just a list of files to load, so it loads that file. I set it up in the installer, and I'm done. (Look, ma, no edits.)

I then need to do some work with actions. At a minimum I need to define action=helpdesk, and if it's in the weird 'standalone' mode, I also need to remove other actions. Plus I need to juggle action=unread and action=unreadreplies for an SD specific wrapper around them for staff.

Now, I *could* modify index.php for all this, like SimpleDesk 1.0 does, but why should I make file edits when I can achieve the same thing with no hassle for forum owners? And there's a hook for this sort of thing, integrate_actions I think it's called.

Inside that monster Subs-SimpleDesk.php is a function called shd_init_actions. It receives the $actionArray as a variable, in a form it can modify, then I can do my own stuff. I simply tell SMF to call my function at the right time and my function does the rest. So now I have my own file loaded and my own actions added... and I haven't made a single file edit at this point.

I can do exactly the same for the main menu, the admin menu, and plenty more besides.

SlammedDime's SimpleSEF 2.0 (for 2.0 RC5) can actually do everything it does without a single file edit, because it just instructs SMF to load it with the designated hooks and call the relevant functions at the relevant times.

I'd say that the ability to make changes, functionally, to a forum without file edits is pretty damn important. That's our goal with Wedge: to drastically expand the functionality there so it is possible to write most add-ons in a way that doesn't require file edits all over the show.

Even as powerful as the hooks are now (they were stupid before), it's still not enough; SimpleDesk still makes over a dozen file edits, but it's now far fewer than the dozens of edits it used to have to make.
Title: Re: Improvements to Hooks
Post by: Nao on May 22nd, 2011, 05:30 PM
I suppose what he's looking for is some sample code as in a "real life" situation of a mod attempting to take over an area of the program ;)
Title: Re: Improvements to Hooks
Post by: Farjo on May 22nd, 2011, 05:33 PM
Thanks for the reply Arantor. I don't understand all of it but it sounds very clever  :P


I presume that you have to make an edit to the file that lists the files (/functions?) to be loaded? And if we don't fully understand now I guess it will become apparent when mods start to become available and do much more than on SMF?
Title: Re: Improvements to Hooks
Post by: Arantor on May 22nd, 2011, 05:35 PM
Quote
I presume that you have to make an edit to the file that lists the files (/functions?) to be loaded?
Nope.

You have an install script, it calls existing SMF functions to add to the list of files to load. No edits required.

(I think there's a mod by Miss All Sunday, a spoiler mod IIRC, that uses hooks to add a new bbcode without any edits, might be worth looking at that.)
Title: Re: Improvements to Hooks
Post by: DoctorMalboro on May 22nd, 2011, 05:39 PM
Once it's published, I'll take a look at SD 2.0 and it's hooks ;)
Title: Re: Improvements to Hooks
Post by: Arantor on May 22nd, 2011, 05:43 PM
Oh, that might not be the best idea... I did some rather creative interpretation of the hooks at times.
Title: Re: Improvements to Hooks
Post by: DoctorMalboro on May 22nd, 2011, 05:44 PM
Then I have to stay with the old school file edits? I need to watch some code to get it, something simple at least :P
Title: Re: Improvements to Hooks
Post by: Arantor on May 22nd, 2011, 05:49 PM
Quote from Arantor on May 22nd, 2011, 05:35 PM
(I think there's a mod by Miss All Sunday, a spoiler mod IIRC, that uses hooks to add a new bbcode without any edits, might be worth looking at that.)
Title: Re: Improvements to Hooks
Post by: Farjo on May 22nd, 2011, 06:23 PM
I hope this isn't copyright and that Miss All Sunday takes this copying as flattery  :)
Code: [Select]
$hooks = array(
'integrate_bbc_codes' => 'spoiler_bbc_add_code',
'integrate_bbc_buttons' => 'spoiler_bbc_add_button',
'integrate_pre_include' => '$boarddir/Sources/Subs-SimpleSpoiler.php',
);


foreach ($hooks as $hook => $function)
add_integration_function($hook, $function);
"You have an install script, it calls existing SMF functions to add to the list of files to load. No edits required."
Is that the "integrate_pre_include" bit?
Title: Re: Improvements to Hooks
Post by: Arantor on May 22nd, 2011, 06:25 PM
Yup. integrate_pre_include just contains a list of files to load on SMF startup, you set it in the installer and no file edit is required to have that file loaded later.
Title: Re: Improvements to Hooks
Post by: Farjo on May 22nd, 2011, 06:32 PM
Well that's quite clever then :) So the other bits:- integrate_bbc_codes and integrate_bbc_buttons... they're hooks are they? And they call / load (again sorry for my mis-usage of coding terms) the functions in her Subs-SimpleSpoiler file?
Title: Re: Improvements to Hooks
Post by: Arantor on May 22nd, 2011, 06:36 PM
Yes, basically.

Really, all a hook physically is, is just a setting in $modSettings that contains a 'to do' list. For the include hooks, it's a list of files to load, for the rest, it's a list of functions to call.

You'll see call_integration_hook() mentions throughout the main SMF source (we renamed the function and the hooks, more on this later), which say, "Get this list of functions, and run them" - and often passing information to the hooked functions, so you can manipulate them right there.
Title: Re: Improvements to Hooks
Post by: Farjo on May 22nd, 2011, 06:51 PM
Cor look at this old dog learning new coding tricks :P  I'm having afternoon tea to celebrate :eheh:

Thanks again Arantor for your time, patience and explanations.
Title: Re: Improvements to Hooks
Post by: Nao on May 22nd, 2011, 07:17 PM
Here's what Miss All Sunday's code would look like in Wedge:

Code: [Select]
add_hook('bbc_codes', 'spoiler_bbc_add_code', 'Subs-SimpleSpoiler');
add_hook('bbc_buttons', 'spoiler_bbc_add_button', 'Subs-SimpleSpoiler');

A bit simpler, eh ;)
Title: Re: Improvements to Hooks
Post by: Farjo on May 22nd, 2011, 08:15 PM
Yes, even I can see that's much neater. The original post now makes more sense and I can see why it's got an entire thread dedicated to it.


What about the additional menu items (buttons) that I have on my SMF installation - this is just a code hack. Would I be able to make my own hook function? This would be cool as at present I must re-edit the code with each upgrade. Perhaps:
Code: [Select]
add_hook('menu_items', 'farjo_add_menu_items', 'farjo_code_changes');

where menu_items would be substituted for the correct name of the hook that controls the buttons, farjo_code_changes.php contains the code that I currently paste into Subs.php but wrapped within the function farjo_add_menu_items.
And if so will there be a idiot-readable list of hooks so that someone like me would be able to make  this type of minor changes to their set-up? I know that this change is aimed at proper modders, but it would also encourage others with my limited coding skill to make changes (changing the actual source code is something I wouldn't have the confidence to do, but making a separate file.... well what's the harm?).
Title: Re: Improvements to Hooks
Post by: Arantor on May 22nd, 2011, 08:21 PM
Well, not *that* code - that's Wedge code.

In your case, you'll create your file, farjo_code_changes.php, and put it in Sources/

Then your package installer would contain:
Code: [Select]
$hooks = array(
'integrate_menu_buttons' => 'farjo_add_menu_items',
'integrate_pre_include' => '$boarddir/Sources/farjo_code_changes.php',
);

foreach ($hooks as $hook => $function)
add_integration_function($hook, $function);

But the principle's the same. Nao just streamlined what the add function did to avoid the complexity and hassle.
Title: Re: Improvements to Hooks
Post by: Farjo on May 22nd, 2011, 09:19 PM
Sorry, I meant if my club switches to wedge how it could be done. From experience, the SMF way that you show would take me a while as there's plenty for the inexperienced fool to get wrong - a missing single-quote, a misspelt word... But the wedge way would be simple - even I could only make a maximum 1 mistake ;)

Anyway thanks for your time. Another reason to look forward to its release - best you get on with it instead of answering my stupid questions :)
Title: Re: Improvements to Hooks
Post by: Nao on May 22nd, 2011, 10:43 PM
Quote from Farjo on May 22nd, 2011, 08:15 PM
Yes, even I can see that's much neater. The original post now makes more sense and I can see why it's got an entire thread dedicated to it.
There are more features that deserve their own topic, but I was too lazy in the end so they're just in the Minor Features topic ;)
Quote
What about the additional menu items (buttons) that I have on my SMF installation - this is just a code hack. Would I be able to make my own hook function?
Yes.
Quote
This would be cool as at present I must re-edit the code with each upgrade. Perhaps:
Code: [Select]
add_hook('menu_items', 'farjo_add_menu_items', 'farjo_code_changes');
No, the first param is the original hook name, the second parameter is the function to add to the hook (whatever you wrote), and the third parameter (which is optional) is the name of a source file to load where the function can be found.
i.e. if you load a hook that's in a file that also contains a hook function called right before that hook in the code path, then you don't need to bother loading the file.
Quote
where menu_items would be substituted for the correct name of the hook that controls the buttons,
That's 'menu_buttons', AFAIK.
Quote
And if so will there be a idiot-readable list of hooks so that someone like me would be able to make  this type of minor changes to their set-up?
I'll try to document the hooks in the future. Although a list can easily be built from simply searching for "add_hook(" in the Wedge codebase. Right now it's pretty similar to the SMF hook list -- a few were removed, and I probably added a dozen or so. Thing is, I'm not really excited with the idea of adding hooks that never get used, so I'd rather wait for modders to actually ask me for a hook at this or that position. Then I'll figure out the ideal parameter list and the ideal position for the hook, and add it.
Considering that if your mod only uses hooks, it doesn't need any reinstalling when you upgrade your forum, it'll definitely be worth upgrading said forum just to add support for these hooks.
Quote
I know that this change is aimed at proper modders, but it would also encourage others with my limited coding skill to make changes (changing the actual source code is something I wouldn't have the confidence to do, but making a separate file.... well what's the harm?).
Sure!
Title: Re: Improvements to Hooks
Post by: Farjo on May 23rd, 2011, 05:22 PM
Thanks for the reply.
The hook is as you say integrate_menu_buttons and the list of hooks is here: http://wiki.simplemachines.org/smf/index.php?title=Integration_hooks&oldid=5972 So there's another mini-project for me - to write a mod  :wow:
Quote
There are more features that deserve their own topic, but I was too lazy in the end so they're just in the Minor Features topic ;)
Yes that's a massive topic which I'm still only halfway through reading :)
Title: Re: Improvements to Hooks
Post by: Arantor on May 23rd, 2011, 05:34 PM
They really need to update that, all the hooks are described with the original limitations they had when they were in RC3... (back when I originally wrote it)
Title: Re: Improvements to Hooks
Post by: DoctorMalboro on May 29th, 2011, 11:54 PM
Now that mods would be "edit-free" wouldn't be better to be saved in a folder apart from Sources to have things more nice and clean?
Title: Re: Improvements to Hooks
Post by: Arantor on May 30th, 2011, 12:04 AM
Two things.

Firstly, both SMF and Wedge have limitations as far as the hooks go. Wedge does it better, much better, but there's still a limitation as to where files can be.

Secondly, some people do crazy shit like moving their Sources dir out of the web tree, so you kind of need to put it in there - but I guess we could do it as having a folder for add-ons where each add-on gets its own folder therein?
Title: Re: Improvements to Hooks
Post by: DoctorMalboro on May 30th, 2011, 03:23 AM
I think that could make the understanding of WHAT IS A MOD and WHAT IS CORE STUFF IMO :P
Title: Re: Improvements to Hooks
Post by: Nao on May 30th, 2011, 09:50 AM
I'm still pushing for Wedge to completely disable the ability to hack into source files... And I'm ready to add as many hook points as requested to ensure as many mods as possible can be done for Wedge.
It really doesn't kill performance at all, in my tests at least. It's just an extra function call and an empty() test on each new hook...
Title: Re: Improvements to Hooks
Post by: live627 on May 30th, 2011, 10:04 AM
And SM is worried over THAT? :lol:
Title: Re: Improvements to Hooks
Post by: Nao on May 30th, 2011, 11:01 AM
Go figure.