Gossamer Forum
Quote Reply
Hooks....
Please can someone explain what exactly pre and post hooks do because this is driving me crazy now......

When you create a new hook what is actually happening and what does it do?

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
PRE hooks can usually modify values and such, before they are placed on a page. While POST hooks modify the actual HTML which is passed through.

Also, basically when you create a hook, it does this:

In programs such as add.cgi, there is a user_add_link hook at the beginning. When is sees the hook, it searches the plugin.cfg file to see if there is any plugins which use the the user_add_link hook, and if there is, it runs a subroutine which you specify.

They all vary depending on were they are. I know that this is very VAGUE, but hopefully someone else can elaborate.


Robert Blackstone
Webmaster of Scato Search
http://www.scato.com
Quote Reply
Re: Hooks.... In reply to
Thanks...that has helped a little.

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
Ok......take this for example........

my $results = GT::Plugins->dispatch ($CFG->{admin_root_path} . '/Plugins', 'user_add_link', \&add_link, {});


...this is a PRE hook...yes?

But when I choose to create a PRE hook using the plugin wizard what happens?....because it doesnt create any code in any of the cgi files.

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
Hi,

In Reply To:
my $results = GT::Plugins->dispatch ($CFG->{admin_root_path} . '/Plugins', 'user_add_link', \&add_link, {});
This is a hook. Wherever you see GT::Plugins->dispatch, then the subroutine mentioned can be overridden (in this case add_link) by your own code.

You supply the PRE hook or POST hook code that gets run either before or after the add_link function.

The goal of this is not to modify the distributed CGI files.

So when you create a PRE hook in a file called YourHooks.pm in the Plugins directory, what happens is the program sees that you have a function registered as a PRE hook for user_add_link. It then runs your function first, and then depending on what your function does, runs the main code.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Hooks.... In reply to
Ok....I am learning more and more....

Right...I am basically trying to create the search timer plugin.

I have created a PRE hook called Start and a POST hook called Stop....and I have added the subroutines to SearchTimer.pm for Start and Stop and filled in all of the other information but got stuck with what to do with this code.....

my $results = GT::Plugins->dispatch ($CFG->{admin_root_path} . '/Plugins', 'search_results', \&query, {});
$results->{query_time} = _time_display();

.....which needs to go at the top of search.cgi

How do I get search.cgi to look for my PRE hook and execute the Start subroutine?....that is what's confusing me.

I realise that code above isn't for my plugin but I was using it as an example.



Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
Hi,

You don't need to touch search.cgi. Use the wizard to generate your plugin shell and when asked about hooks enter:

Name: search_results
Type: PRE
Code: Plugins::SearchTimer::start_timer
Position: FIRST

and another:

Name: search_results
Type: POST
Code: Plugins::SearchTimer::stop_timer
Position: LAST

Then go through the rest. You then just need to modify your start_timer and stop_timer functions in SearchTimer.pm file to start and stop the timer. Once you install the plugin, the Install.pm has the code required to register the hooks, and then the dispatch function will automatically run your code before and after.

Cheers,

Alex


--
Gossamer Threads Inc.
Quote Reply
Re: Hooks.... In reply to
ohhhhhhhhh.....

Ive just been reading the plugins guide that comes with Links SQL and I am clearer now.

I can't just create hooks can I?....they are already existing in the code and I just have to execute code before of after them.

Is that right?

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
It's almost working but I get this error when I perform a search......

A fatal error has occured:
Can't use string ("0.00003") as a HASH ref while "strict refs" in use at /home/sites/site9/web/cgi-bin/search.cgi line 50.


How do I correct this?....Also How do I get the tag <%search_query%> to print the search time?...do I put the code in sub Stop or create a global?


ps. I love the debugging...it lets you trace the whole search process...excellent!


Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
Your pre hook should look like:

my $args = shift;
$args->{start_time} = time;
return $args;

Your post hook should look like:

my $args = shift;
$args->{end_time} = time;
$args->{search_time} = sprintf ("%.2f", $args->{start_time} - $args->{end_time});
return $args;

You must return the hash ref that you got passed in.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Hooks.... In reply to
Thanks Alex...lol...you have basically just written the plugin yourself but hey, I would have gotten no where without your help so thanks, and I will give you full credit when I release the plugin.

ps. my code wasn't quite like that, but almost 8)

I know for next time which is what it's about....

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
The first few plug-ins are going to require a lot of Alex help :)

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://LinkSQL.com/FAQ


Quote Reply
Re: Hooks.... In reply to
I agree.
That way someone knows what they're talking about.Smile

Robert Blackstone
Webmaster of Scato Search
http://www.scato.com
Quote Reply
Re: Hooks.... In reply to
Yes...they seemed to be so tricky to get to grips with at first but I am beginning to understand now so I know for next time.

Im not sure if this is right, but I think that Alex has already included hooks in the code and what we are doing is kind of "hooking" the already existing hooks. For example there is a search_results hook existing in the code and so for the search timer plugin I had to add a PRE hook before the search_results hook was executed to start the timer and a POST hook to stop the timer which was executed after the search_results hook. Does that make sense?

Therefore our hooks are just telling Links SQL to execute our code before or after some already existing Links SQL code which can either alter the way the existing code works, or can just let it run as normal whilst allowing time to execute the code that we want.

PHEW..............

I may be totally wrong but thats the way it seems to me.....

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
that is how i see it as well paul, makes a lot of sense.

out of interest, is adding new hooks easy? So for example, the review plugin, will that have hooks in it as well, like an after review hook so that instead of re-doing the whole of the review plugin, another extra plugin can be made that uses the post review hook? See what i mean?

http://www.ASciFi.com/ - The Science Fiction Portal
Quote Reply
Re: Hooks.... In reply to
You mean embedding more hooks within plugins so that plugins can be made for plugins.....????.......LOL

Yes..now that I understand whats going on, adding plugins is easy..you just need to make sure that you are choosing the right option..ie PRE, POST, FIRST, LAST.



Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
Yes, adding hooks is easy. To create a hook, you wrap your subroutine with GT::Plugins->dispatch. So if you have a subroutine add_review() that takes form input, adds a review, and prints out the success page, you could wrap it like:

Code:
main();

sub main {
GT::Plugins->dispatch ($CFG->{admin_root_path} . '/Plugins', 'add_review', \&add_review, {});
}

sub add_review {
# Your code here
}
The arguments to dispatch are:

1. The path to the Plugins directory. This will always be $CFG->{admin_root_path} . '/Plugins'.
2. The hook name. You should make sure this is unique to avoid conflicts with other parts of the code. Try and prefix it with the name of your plugin to avoid confusion.
3. The subroutine to run. You pass in a code reference here, so typically it will just be \&yoursubroutine.
4. Any arguments that your subroutine takes. These argument will be passed into the pre hooks and can be modified, and will then be passed into your code.

Let me know if that makes sense.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Hooks.... In reply to
So taking that code as an example....that means that someone could create a plugin for the review plugin that does something else..yes?

They can add a hook either before or after add_review.

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
Right, all they would have to do is register their code to work off the 'add_review' hook. The Install should probably check to make sure that the Review module is installed first though.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Hooks.... In reply to
Did you come up with this system yourself or did you learn from somewhere?....In fact how did you learn so much?

So effectively as long as the hooks existed you could create plugins for plugins for plugins....into infinity.....

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: Hooks.... In reply to
how does this affect performance Alex? is is bad having a plugin trail.. say for review having 3 or 4 plugins each doing a bit more, is this less efficient than just having one or is the difference not going to be noticeable? if we have say 40 plugins, does it slow it down for the progam having to check each plugin to ensure there are no calls to the hook currently being processed?

http://www.ASciFi.com/ - The Science Fiction Portal