<<

Bugzilla::Hook

NAME

Bugzilla::Hook - Extendable extension hooks for Bugzilla code

SYNOPSIS

 use Bugzilla::Hook;

 Bugzilla::Hook::process("hookname", { arg => $value, arg2 => $value2 });

DESCRIPTION

Bugzilla allows extension modules to drop in and add routines at arbitrary points in Bugzilla code. These points are referred to as hooks. When a piece of standard Bugzilla code wants to allow an extension to perform additional functions, it uses Bugzilla::Hook's "process" subroutine to invoke any extension code if installed.

There is a sample extension in extensions/example/ that demonstrates most of the things described in this document, as well as many of the hooks available.

How Hooks Work

When a hook named HOOK_NAME is run, Bugzilla will attempt to invoke any source files named extensions/*/code/HOOK_NAME.pl.

So, for example, if your extension is called "testopia", and you want to have code run during the "install-update_db" hook, you would have a file called extensions/testopia/code/install-update_db.pl that contained perl code to run during that hook.

Arguments Passed to Hooks

Some hooks have params that are passed to them.

These params are accessible through "hook_args" in Bugzilla. That returns a hashref. Very frequently, if you want your hook to do anything, you have to modify these variables.

You may also want to use "input_params" in Bugzilla to get parameters that were passed to the current CGI script or WebService method.

Versioning Extensions

Every extension must have a file in its root called info.pl. This file must return a hash when called with do. The hash must contain a 'version' key with the current version of the extension. Extension authors can also add any extra infomration to this hash if required, by adding a new key beginning with x_ which will not be used the core Bugzilla code.

SUBROUTINES

process
Description

Invoke any code hooks with a matching name from any installed extensions.

See customization.xml in the Bugzilla Guide for more information on Bugzilla's extension mechanism.

Params
$name - The name of the hook to invoke.
$args - A hashref. The named args to pass to the hook. They will be accessible to the hook via "hook_args" in Bugzilla.
Returns (nothing)

HOOKS

This describes what hooks exist in Bugzilla currently. They are mostly in alphabetical order, but some related hooks are near each other instead of being alphabetical.

attachment-process_data

This happens at the very beginning process of the attachment creation. You can edit the attachment content itself as well as all attributes of the attachment, before they are validated and inserted into the DB.

Params:

data - A reference pointing either to the content of the file being uploaded or pointing to the filehandle associated with the file.
attributes - A hashref whose keys are the same as "create" in Bugzilla::Attachment. The data it contains hasn't been checked yet.

auth-login_methods

This allows you to add new login types to Bugzilla. (See Bugzilla::Auth::Login.)

Params:

modules

This is a hash--a mapping from login-type "names" to the actual module on disk. The keys will be all the values that were passed to "login" in Bugzilla::Auth for the Login parameter. The values are the actual path to the module on disk. (For example, if the key is DB, the value is Bugzilla/Auth/Login/DB.pm.)

For your extension, the path will start with extensions/yourextension/lib/. (See the code in the example extension.)

If your login type is in the hash as a key, you should set that key to the right path to your module. That module's new method will be called, probably with empty parameters. If your login type is not in the hash, you should not set it.

You will be prevented from adding new keys to the hash, so make sure your key is in there before you modify it. (In other words, you can't add in login methods that weren't passed to "login" in Bugzilla::Auth.)

auth-verify_methods

This works just like "auth-login_methods" except it's for login verification methods (See Bugzilla::Auth::Verify.) It also takes a modules parameter, just like "auth-login_methods".

bug-columns

This allows you to add new fields that will show up in every Bugzilla::Bug object. Note that you will also need to use the "bug-fields" hook in conjunction with this hook to make this work.

Params:

columns - An arrayref containing an array of column names. Push your column name(s) onto the array.

bug-end_of_create

This happens at the end of "create" in Bugzilla::Bug, after all other changes are made to the database. This occurs inside a database transaction.

Params:

bug - The changed bug object, with all fields set to their updated values.
timestamp - The timestamp used for all updates in this transaction.

bug-end_of_create_validators

This happens during "create" in Bugzilla::Bug, after all parameters have been validated, but before anything has been inserted into the database.

Params:

params

A hashref. The validated parameters passed to create.

bug-end_of_update

This happens at the end of "update" in Bugzilla::Bug, after all other changes are made to the database. This generally occurs inside a database transaction.

Params:

bug - The changed bug object, with all fields set to their updated values.
timestamp - The timestamp used for all updates in this transaction.
changes - The hash of changed fields. $changes->{field} = [old, new]

bug-fields

Allows the addition of database fields from the bugs table to the standard list of allowable fields in a Bugzilla::Bug object, so that you can call the field as a method.

Note: You should add here the names of any fields you added in "bug-columns".

Params:

columns - A arrayref containing an array of column names. Push your column name(s) onto the array.

bug-format_comment

Allows you to do custom parsing on comments before they are displayed. You do this by returning two regular expressions: one that matches the section you want to replace, and then another that says what you want to replace that match with.

The matching and replacement will be run with the /g switch on the regex.

Params:

regexes

An arrayref of hashrefs.

You should push a hashref containing two keys (match and replace) in to this array. match is the regular expression that matches the text you want to replace, replace is what you want to replace that text with. (This gets passed into a regular expression like s/$match/$replace/.)

Instead of specifying a regular expression for replace you can also return a coderef (a reference to a subroutine). If you want to use backreferences (using $1, $2, etc. in your replace), you have to use this method--it won't work if you specify $1, $2 in a regular expression for replace. Your subroutine will get a hashref as its only argument. This hashref contains a single key, matches. matches is an arrayref that contains $1, $2, $3, etc. in order, up to $10. Your subroutine should return what you want to replace the full match with. (See the code example for this hook if you want to see how this actually all works in code. It's simpler than it sounds.)

You are responsible for HTML-escaping your returned data. Failing to do so could open a security hole in Bugzilla.

text

A reference to the exact text that you are parsing.

Generally you should not modify this yourself. Instead you should be returning regular expressions using the regexes array.

The text has already been word-wrapped, but has not been parsed in any way otherwise. (So, for example, it is not HTML-escaped. You get "&", not "&amp;".)

bug

The Bugzilla::Bug object that this comment is on. Sometimes this is undef, meaning that we are parsing text that is not on a bug.

comment

A hashref representing the comment you are about to parse, including all of the fields that comments contain when they are returned by by "longdescs" in Bugzilla::Bug.

Sometimes this is undef, meaning that we are parsing text that is not a bug comment (but could still be some other part of a bug, like the summary line).

buglist-columns

This happens in buglist.cgi after the standard columns have been defined and right before the display column determination. It gives you the opportunity to add additional display columns.

Params:

columns - A hashref, where the keys are unique string identifiers for the column being defined and the values are hashrefs with the following fields:
name - The name of the column in the database.
title - The title of the column as displayed to users.

The definition is structured as:

 $columns->{$id} = { name => $name, title => $title };

colchange-columns

This happens in colchange.cgi right after the list of possible display columns have been defined and gives you the opportunity to add additional display columns to the list of selectable columns.

Params:

columns - An arrayref containing an array of column IDs. Any IDs added by this hook must have been defined in the the buglist-columns hook. See "buglist-columns".

config-add_panels

If you want to add new panels to the Parameters administrative interface, this is where you do it.

Params:

panel_modules

A hashref, where the keys are the "name" of the module and the value is the Perl module containing that config module. For example, if the name is Auth, the value would be Bugzilla::Config::Auth.

For your extension, the Perl module name must start with extensions::yourextension::lib. (See the code in the example extension.)

config-modify_panels

This is how you modify already-existing panels in the Parameters administrative interface. For example, if you wanted to add a new Auth method (modifying Bugzilla::Config::Auth) this is how you'd do it.

Params:

panels

A hashref, where the keys are lower-case panel "names" (like auth, admin, etc.) and the values are hashrefs. The hashref contains a single key, params. params is an arrayref--the return value from get_param_list for that module. You can modify params and your changes will be reflected in the interface.

Adding new keys to panels will have no effect. You should use "config-add_panels" if you want to add new panels.

enter_bug-entrydefaultvars

This happens right before the template is loaded on enter_bug.cgi.

Params:

vars - A hashref. The variables that will be passed into the template.

flag-end_of_update

This happens at the end of "update_flags" in Bugzilla::Flag, after all other changes are made to the database and after emails are sent. It gives you a before/after snapshot of flags so you can react to specific flag changes. This generally occurs inside a database transaction.

Note that the interface to this hook is UNSTABLE and it may change in the future.

Params:

object - The changed bug or attachment object.
timestamp - The timestamp used for all updates in this transaction.
old_flags - The snapshot of flag summaries from before the change.
new_flags - The snapshot of flag summaries after the change. Call my ($removed, $added) = diff_arrays(old_flags, new_flags) to get the list of changed flags, and search for a specific condition like added eq 'review-'.

install-before_final_checks

Allows execution of custom code before the final checks are done in checksetup.pl.

Params:

silent

A flag that indicates whether or not checksetup is running in silent mode.

install-requirements

Because of the way Bugzilla installation works, there can't be a normal hook during the time that checksetup.pl checks what modules are installed. (Bugzilla::Hook needs to have those modules installed--it's a chicken-and-egg problem.)

So instead of the way hooks normally work, this hook just looks for two subroutines (or constants, since all constants are just subroutines) in your file, called OPTIONAL_MODULES and REQUIRED_MODULES, which should return arrayrefs in the same format as OPTIONAL_MODULES and REQUIRED_MODULES in Bugzilla::Install::Requirements.

These subroutines will be passed an arrayref that contains the current Bugzilla requirements of the same type, in case you want to modify Bugzilla's requirements somehow. (Probably the most common would be to alter a version number or the "feature" element of OPTIONAL_MODULES.)

checksetup.pl will add these requirements to its own.

Please remember--if you put something in REQUIRED_MODULES, then checksetup.pl cannot complete unless the user has that module installed! So use OPTIONAL_MODULES whenever you can.

install-update_db

This happens at the very end of all the tables being updated during an installation or upgrade. If you need to modify your custom schema, do it here. No params are passed.

db_schema-abstract_schema

This allows you to add tables to Bugzilla. Note that we recommend that you prefix the names of your tables with some word, so that they don't conflict with any future Bugzilla tables.

If you wish to add new columns to existing Bugzilla tables, do that in "install-update_db".

Params:

schema - A hashref, in the format of "ABSTRACT_SCHEMA" in Bugzilla::DB::Schema. Add new hash keys to make new table definitions. checksetup.pl will automatically add these tables to the database when run.

mailer-before_send

Called right before Bugzilla::Mailer sends a message to the MTA.

Params:

email - The Email::MIME object that's about to be sent.

object-before_create

This happens at the beginning of "create" in Bugzilla::Object.

Params:

class

The name of the class that create was called on. You can check this like if ($class->isa('Some::Class')) in your code, to perform specific tasks before create for only certain classes.

params

A hashref. The set of named parameters passed to create.

object-before_set

Called during "set" in Bugzilla::Object, before any actual work is done. You can use this to perform actions before a value is changed for specific fields on certain types of objects.

Params:

object

The object that set was called on. You will probably want to do something like if ($object->isa('Some::Class')) in your code to limit your changes to only certain subclasses of Bugzilla::Object.

field

The name of the field being updated in the object.

value

The value being set on the object.

object-end_of_create_validators

Called at the end of "run_create_validators" in Bugzilla::Object. You can use this to run additional validation when creating an object.

If a subclass has overridden run_create_validators, then this usually happens before the subclass does its custom validation.

Params:

class

The name of the class that create was called on. You can check this like if ($class->isa('Some::Class')) in your code, to perform specific tasks for only certain classes.

params

A hashref. The set of named parameters passed to create, modified and validated by the VALIDATORS specified for the object.

page-before_template

This is a simple way to add your own pages to Bugzilla. This hooks page.cgi, which loads templates from template/en/default/pages. For example, page.cgi?id=fields.html loads template/en/default/pages/fields.html.tmpl.

This hook is called right before the template is loaded, so that you can pass your own variables to your own pages.

Params:

page_id

This is the name of the page being loaded, like fields.html.

Note that if two extensions use the same name, it is uncertain which will override the others, so you should be careful with how you name your pages.

vars

This is a hashref--put variables into here if you want them passed to your template.

product-confirm_delete

Called before displaying the confirmation message when deleting a product.

Params:

vars - The template vars hashref.

sanitycheck-check

This hook allows for extra sanity checks to be added, for use by sanitycheck.cgi.

Params:

status - a CODEREF that allows status messages to be displayed to the user. (sanitycheck.cgi's Status)

sanitycheck-repair

This hook allows for extra sanity check repairs to be made, for use by sanitycheck.cgi.

Params:

status - a CODEREF that allows status messages to be displayed to the user. (sanitycheck.cgi's Status)

template-before_process

This hook allows you to define additional variables that will be available to the template being processed. You probably want to restrict your hook to operating only if a certain file is being loaded (which is why you get a file argument below). Otherwise, modifying the vars argument will affect every single template in Bugzilla.

Params:

vars

The template vars hashref--these are the values that get passed to the template. Adding new keys to this hashref will cause those new values to also get passed to the template.

file

The name of the template being processed. This is relative to the main template directory for the language (i.e. for template/en/default/bug/show.html.tmpl, this variable will contain bug/show.html.tmpl).

template

The Bugzilla::Template object that process was called on.

webservice

This hook allows you to add your own modules to the WebService. (See Bugzilla::WebService.)

Params:

dispatch

A hashref that you can specify the names of your modules and what Perl module handles the functions for that module. (This is actually sent to "dispatch_with" in SOAP::Lite. You can see how that's used in xmlrpc.cgi.)

The Perl module name must start with extensions::yourextension::lib:: (replace yourextension with the name of your extension). The package declaration inside that module must also start with extensions::yourextension::lib:: in that module's code.

Example:

  $dispatch->{Example} = "extensions::example::lib::Example";

And then you'd have a module extensions/example/lib/Example.pm

It's recommended that all the keys you put in dispatch start with the name of your extension, so that you don't conflict with the standard Bugzilla WebService functions (and so that you also don't conflict with other plugins).

webservice-error_codes

If your webservice extension throws custom errors, you can set numeric codes for those errors here.

Extensions should use error codes above 10000, unless they are re-using an already-existing error code.

Params:

error_map

A hash that maps the names of errors (like invalid_param) to numbers. See "WS_ERROR_CODE" in Bugzilla::WebService::Constants for an example.

<<