Extension:VIKI/Plugin Structure

Overview
By design, VIKI is architected to support either an individual wiki or a series of collectively managed wikis (wiki farm), and this support comes in the form of a hook structure for plugins. Similarly to MediaWiki, VIKI defines a set of points of time during the execution when it will call any hook functions which are registered with it.

Plugins are MediaWiki extensions themselves which register with VIKI hooks. Plugins may execute code on both the PHP side and the JavaScript side. If the plugin includes JavaScript, a resource module containing the JS files must be declared and registered with VIKI to load. As your plugin is a MediaWiki extension itself, you may choose to (for example) define MediaWiki API extensions in your plugin.

Currently, VIKI defines one PHP hook and 11 JavaScript hooks:

PHP Hooks

JavaScript Hooks

Declaring A Plugin
Declaring a JavaScript plugin requires two steps:   In your plugin's PHP file, register the name of the plugin function hook with $wgwgVIKI_Function_Hooks.   Also in your plugin's PHP file, have VIKI add the resource module for the plugin at ParserFirstCallInit. Note that for historical reasons, the core VIKI class is called 'VikiJS'.  

Declaring a PHP plugin requires one step:   In your plugin's PHP file, register the name of the plugin function hook with VIKI. Any parameters passed in should be passed as an array, and the function should be declared to take one parameter, which is the array of parameters.  

If the plugin contains both a PHP and JavaScript side, all three steps must be taken.

JavaScript Structure
VIKI takes advantage of the JavaScript Loose Augmentation Module Pattern. Core VIKI is declared as a submodule within the VIKI module called VikiJS, and any VIKI plugins with a JavaScript component are expected to be declared as submodules to VIKI as well.

If your JavaScript plugin is an object literal, it may take the following form:

If your JavaScript plugin is an object with a prototype, it may take the following form:

JavaScript Hook Functions
JavaScript hook functions all take the same parameters:
 * vikiObject – a reference to the VIKI object which this hook was called from
 * parameters – any parameters passed to the hook function (reference chart above)
 * hookName – the name of the hook this function was called from

All JavaScript hook functions must call  at the conclusion of their execution. is a JSON object of certain parameters that VIKI can handle. Currently, VIKI can handle three possible parameters:
 * - this will cause VIKI to redraw the entire graph at the completion of your hook.
 * - this will cause VIKI to redraw just the node you specify (below) at the completion of your hook.
 * - this passes a node back to VIKI to be used to redraw that node.

If the hook function involves one or more asynchronous operations, e.g. an AJAX call, care should be taken to ensure that hookCompletion is only called at the end of the last possible success/error handler, for example.

Examples
We have developed several VIKI plugins which will be open-sourced over time. For now, one example is available:
 * Extension:VikiSemanticTitle