- Example
- Event Hooks
- action:[name]
- api:[name]
- check_readable
- check_writable
- cli:[name]
- context_loaded
- do:[name]
- edit:[file-extension]
- error_msg
- infobox
- layout:[file-extension]
- log-props
- missing:[file-extension]
- missing_page
- navtools
- post-render
- post-render:[file-extension]
- postSave
- postSave:[file-extension]
- pre-render
- pre-render:[file-extension]
- preRead
- preSave
- preSave:[file-extension]
- read:[file-extension]
- read_folder
- read_page
- render:[file-extension]
- run_init
- save:[file-extension]
- view:[file-extension]
- Customizing errors
- REST API
NacoWiki is supposed to be extensible. This is done through a Plugin architecture. Plugins can hook events that change the behaviour of NacoWiki or add new functionality.
Documentation for the PHP Programming interface is generated automatically.
To create a plugin you can refer to the already existing plugins in the standard distribution. At a high-level you need to create a file containing the class that will encapsulate your plugin.
Example
For example:
use NWiki\PluginCollection as Plugins;
use NWiki\Util as Util;
use NWiki\Core as Core;
use NWiki\Cli as Cli;
class SamplePlugin {
const VERSION ='0.0';
/** sample command
*
* @param \NanoWikiApp $wiki running wiki instance
* @param array &$argv Command line arguments
* @phpcod commands##sample
* @event cli:sample
*/
static function render(\NacoWikiApp $wiki, array &$argv) : ?bool {
print_r($argv);
}
static function load(array $cfg) : void {
Plugins::autoload(self::class);
}
}
This implements a simple plugin that registers a new CLI command. The event registration
is by calling Plugins::autoload
method. This method looks for the PHP documentation
string that contains the tag @event
followed by the event to hook: cli:sample
in this
example.
Event Hooks
The following events can be hooked:
action:[name]
This event is called in response of a $_POST[action]
in the
HTTP request. It is used to trigger a specific action in response
to a HTTP post request.
There are no special event parameters
- nacowiki.php,554
api:[name]
This event is called in response of a $_GET[api]
in the URL's
query string. It is used to trigger a specific REST API end point.
Event data:
status
(output): pre-loaded with Plugins::API_OK to optimistically assume success. Event handlers should change this to Plugins:API_ERROR in the event of an error.msg
(output): event handler must fill this if an error happens. The convenience function Plugins::apiError is available to help this.- additional items (output): The API end-point shoudl populate the event data with additional items. The whole event data array is then send to the client in JSON format.
- nacowiki.php,524
check_readable
This event can be used by plugins that implement access control. This event is used to check if the given file path can be read by the current user.
Event data:
page
(input): URL being checkedfpath
(input): File system path to the URL.access
(output): pre-loaded with the access. Event handler must populate with the right access. This can be one of:true
: access is allowedfalse
: access is deniedNULL
: access is denied due to not being authenticated.
- nacowiki.php,328
check_writable
This event can be used by plugins that implement access control. This event is used to check if the given file path can be written by the current user.
Event data:
page
(input): URL being checkedfpath
(input): File system path to the URL.access
(output): pre-loaded with the access. Event handler must populate with the right access. This can be one of:true
: access is allowedfalse
: access is deniedNULL
: access is denied due to not being authenticated.
- nacowiki.php,289
cli:[name]
This event is used to handle CLI the [name]
sub-command.
The event data contains the command line arguments for the sub-command.
- nacowiki.php,442
context_loaded
This event is called right after the context has been loaded.
There are no special event parameters
- nacowiki.php,483
do:[name]
This event is called in response of a $_GET[do]
in the URL's
query string. It is used to trigger a specific command.
There are no special event parameters
- nacowiki.php,512
edit:[file-extension]
This event is used by media handlers to output to the web browser a possibly custom page to edit media.
Typically this is used to pre-format the editable content separating metadata and body text. Also, to display a codemirror with the right modules loaded and initialized to the right mode.
Event data:
ext
(input) : file extension for the given media
- classes/Core.php,996
error_msg
This event is called to implement custom error handlers.
Event data:
msg
(input) : error messagetag
(input) : error tagopts
(input) : flagsEM_NONE
orEM_PHPERR
.
- nacowiki.php,237
infobox
This event can be used to add elements to the infobox table of the top nav bar.
Event data:
infotable
(input|output): Current contents of the infobox table
- nacowiki.php,743
layout:[file-extension]
This event is used by media handlers to output to the web browser a possibly custom layout for the given media type.
Event data:
ext
(input) : file extension for the given mediaextras
(input) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,739
log-props
This event is used to manipulate properties and meta headers before a file is saved.
Event data:
props
(input|output) : properties to be modified.meta
(input|output) : meta data to be modifiedextra
(input) : extra data send by the calling plugin (usually the article body text)
- classes/Core.php,498
missing:[file-extension]
This event is used by media handlers to output to the web browser a possibly custom page to create missing media.
Event data:
ext
(input) : file extension for the given media
- classes/Core.php,787
missing_page
This event is used to handle http 404 errors (missing resource)
There are no special event parameters
- nacowiki.php,594
navtools
This event can be used to add elements to the meta data drop down.
Event data:
mode
(input): What part of the nav bar is being modified.html
(output): HTML elements to add.
Possible modes:
edit-top
: Top of the edit pgtools dropdownedit-bot
: Bottom of the edit pgtools dropdownnavtools-left
: Left of the navigation tool barnavtools-right
: Right of the navigation tool barinfobox-top
: Top of the infobox dropdowninfobox-bot
: Bottom of the infobox dropdowninfotable-top
: Top of the infobox tableinfotable-bot
: Bottom of the infobox table
- nacowiki.php,710
post-render
This event is used by plugins to post-process data. This specific event triggers for any file regardless of the file extension.
Plugins using this hook can post-process the generated HTML.
Event data:
html
(input|output) : current page contents which will be eventually rendered.ext
(input) : file extension for the given mediaextras
(input|output) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,721
post-render:[file-extension]
This event is used by plugins to post-process data. This specific event only triggers for pages matching the given file extension.
Plugins using this hook can post-process the generated HTML.
Event data:
html
(input|output) : current page contents which will be eventually rendered.ext
(input) : file extension for the given mediaextras
(input|output) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,705
postSave
This event is used by plugins to examine data after it was saved to storage.
Usually used to update additional meta data files, like for example update a tag cloud index.
Event data:
text
(input) : textual data that was savedprev
(input) : previous file contents (or NULL)ext
(input) : file extension for the given mediaprops
(input) : saved properties.
- classes/Core.php,1147
postSave:[file-extension]
This event is used by media handlers to examine data after it was saved to storage.
Event data:
text
(input) : textual data that was savedprev
(input) : previous file contents (or NULL)ext
(input) : file extension for the given mediaprops
(input) : saved properties.
- classes/Core.php,1163
pre-render
This event is used by plugins to pre-process data. This specific event triggers for any file regardless of the file extension.
Since this is a pre-render event, the html
element
actually contains text before HTML is generated, but may
be modified by the pre-render hook.
Event data:
html
(input|output) : current page contents which will be eventually rendered.ext
(input) : file extension for the given mediaextras
(input|output) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,670
pre-render:[file-extension]
This event is used by plugins to pre-process data. This specific event only triggers for pages matching the given file extension.
Since this is a pre-render event, the html
element
actually contains text before HTML is generated, but may
be modified by the pre-render hook.
Event data:
html
(input|output) : current page contents which will be eventually rendered.ext
(input) : file extension for the given mediaextras
(input|output) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,652
preRead
This event is used to modify data read from disk. It is intended for versioning plugins to display different versions of a file.
Event data:
filepath
(input) : file system path to sourceurl
(input) : web url to sourcesource
(input|output) : text containing the page document verbatimfilemeta
(input|output) : pre-loaded file-system based meta datameta
(input|output) : to be filed by event handler with meta-data. it is pre-loaded with data derived from filemeta.props
(input|output) : to be filed by event handler with properties. it is pre-loaded with data read by Core.payload
(output) : to be used by later event handlers.ext
(input) : file extension for the given mediaextras
(output) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,546
preSave
This event is used by plugins to pre-parse text before saving to storage.
Hooks can examine the data to be saved in the text
element
of the event array and modify it if needed.
Event data:
text
(input|output) : textual data to saveprev
(input) : current file contents (or NULL)ext
(input) : file extension for the given mediaprops
(output) : to be filed by event handler with properties. it is pre-loaded with data read by Core.
- classes/Core.php,1073
preSave:[file-extension]
This event is used by plugins to pre-parse text before saving to storage.
Hooks can examine the data to be saved in the text
element
of the event array and modify it if needed.
Usually is used by media handlers to pre-parse data, separating header meta data from actual content. And sanitizing any problematic input.
It is recommended to move 'log' keys from the user entered
meta data block to the props
array using Core::logProps
which will add the log text to the change log.
Event data:
text
(input|output) : textual data to saveprev
(input) : current file contents (or NULL)ext
(input) : file extension for the given mediaprops
(output) : to be filed by event handler with properties. it is pre-loaded with data read by Core.
- classes/Core.php,1090
read:[file-extension]
This event is used for media handlers to parse source text and extract header meta data, and the actual payload containing the body of the page content.
Event data:
filepath
(input) : file system path to sourceurl
(input) : web url to sourcesource
(input) : text containing the page document verbatimfilemeta
(input) : pre-loaded file-system based meta datameta
(output) : to be filed by event handler with meta-data. it is pre-loaded with data derived from filemeta.props
(output) : to be filed by event handler with properties. it is pre-loaded with data read by Core.payload
(output) : to be filed by event handler with the body of the page.ext
(input) : file extension for the given mediaextras
(output) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,567
read_folder
This event is used to handle navigation to a folder. i.e. URL is a folder.
There are no special event parameters
- nacowiki.php,571
read_page
This event is used to handle navigation to a page. i.e. URL is an actual file.
Event data:
no_exit
(input) : if true, the event handler will not exit. Otherwise it will exit.view
(input) : use the specified PHP template to view the content.
- nacowiki.php,583
render:[file-extension]
This event is used by media handlers to convert the source to HTML.
The hook must take the html
element from the event
which contains possibily pre-processed input, and
convert it to HTML.
Event data:
html
(input|output) : current page contents which will be eventually rendered.ext
(input) : file extension for the given mediaextras
(input|output) : additional data to display.annotate
: HTML containing text to be display after page title.
- classes/Core.php,688
run_init
This event is called right when the run
method is called.
Plugins can use this to initialize things. Specifically
for using the declareContext
.
There are no special event parameters
- nacowiki.php,462
save:[file-extension]
This event is used by plugins to save to storage.
Usually is used by media handlers to save data using custom file formats.
Event data:
saved
(output) : flag to indicates that we saved or not the file. If this is set tofalse
, thenpostSave
events will be skipped. Pre-set totrue
by default.text
(input) : textual data to saveprev
(input) : current file contents (or NULL)ext
(input) : file extension for the given mediaprops
(input) : properties to save
- classes/Core.php,1116
view:[file-extension]
This event is used by plugins to process data. If a plugin handle this event, it by-passes any other Core related functionality. The plugin then is responsible for generating all the output related to this document.
Event data:
ext
(input) : file extension for the given media
- classes/Core.php,628
Customizing errors
When hooking the error_msg
event, the following errors will be generated:
tag | error | reference |
---|---|---|
config | WIKI configuration error | nacowiki.php,197 |
duplicate | already exists | classes/Core.php,918 |
internal_error | Internal error. | classes/Core.php,939 |
invalid internal state | plugins/Versions.php,224, plugins/Versions.php,254 | |
broken versions | plugins/Versions.php,250 | |
Unable to calculate version | plugins/Versions.php,382, plugins/Versions.php,726 | |
invalid_param | Specified DO does not exist | nacowiki.php,519 |
Specified action does not exist | nacowiki.php,562 | |
un-supported file type | plugins/Versions.php,372, plugins/Versions.php,540, plugins/Versions.php,628, plugins/Versions.php,715 | |
no versions found | plugins/Versions.php,376, plugins/Versions.php,643, plugins/Versions.php,720 | |
versions to compare missing | plugins/Versions.php,552 | |
invalid_target | Can not delete root folder | classes/Core.php,822 |
Cannot rename root directory | classes/Core.php,899 | |
Folder not editable | classes/Core.php,975 | |
folders not editable | classes/Core.php,1049 | |
can not attach to file | classes/Core.php,1216, classes/Core.php,1221 | |
missing_param | Missing input parameter | nacowiki.php,552 |
no-op | no changes made | classes/Core.php,905 |
os_error | rmdir error | classes/Core.php,44, classes/Core.php,859, classes/Core.php,862 |
mkdir error | classes/Core.php,60 | |
unlink error | classes/Core.php,830, classes/Core.php,842, classes/Core.php,875 | |
rename file error | classes/Core.php,947 | |
write error | classes/Core.php,1139 | |
error saving uploaded file | classes/Core.php,1247 | |
invalid internal state | plugins/Versions.php,206 | |
error saving version data | plugins/Versions.php,271 | |
param | unknown scope | classes/Core.php,173 |
no name specified | classes/Core.php,902 | |
invalid form response | classes/Core.php,1231 | |
zero file upload | classes/Core.php,1234 | |
upload form error | classes/Core.php,1236 | |
missing file upload | classes/Core.php,1238 | |
read_access | User does not have read-access. | nacowiki.php,568, nacowiki.php,580 |
write_access | User does not have write-access. | nacowiki.php,549 |
Delete access error | classes/Core.php,818 | |
rename access error | classes/Core.php,909 | |
write access error | classes/Core.php,978, classes/Core.php,1052 | |
no write access | classes/Core.php,1207 |
REST API
A basic REST API is available for use for use with JavaScript code running on the browser.
The following API calls are currently defined:
page-list
Returns a page list
This is used by the JavaScript to render a tree of available pages.
The event parameter is filled with a property output
containing
-
array with directories
-
array with files
-
current page
-
base URL from $cfg[base_url]
-
@todo Should check permissions when returning files
-
@todo Should filter out attachment folders
-
@see \NWiki\PluginCollection::dispatchEvent
-
@phpcod RESTAPI##page-list
-
@event api:page-list
-
@param \NacoWikiApp $wiki NacoWiki instance
-
@param array $ev Event data.
-
@return ?bool Returns \NWiki\PluginCollection::OK to indicate that it was handled.
- classes/Core.php,1254