pluginsdokuwiki

Why is Dokuwiki IO_WIKIPAGE_SAVE action plugin called multiple times on a Save operation?


My first attempt at writing a Dokuwiki plugin is for an Action event, when an edited page is being saved to disk. I've written an action plugin class that extends DokuWiki_Action_Plugin; it contains a register method that registers two hooks with the controller:

$controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'handle_pagewrite_before', array());
$controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_pagewrite_after', array());

(The "before" method is called just before the contents of the page is written, and the "after" method is called just after it's written.)

I've written stubs for the two methods handle_pagewrite_before and handle_pagewrite_after. I put dbglog calls in all three stubs to log entry and exit.

From the wiki, I visit a page, and edit it. At this point I see three debug messages from the register method appear in data/cache/debug.log. On my first keystroke into the editor, a fourth debug message from register appears. When I click Save to save the page, a fifth register message appears, followed by two sets of messages from the hooks where I'd have expected only one, followed by more register messages. In all,

21:32:34 10.0.1.24: Enter gloss:action:register
21:32:34 10.0.1.24: Enter gloss:action:register
21:32:34 10.0.1.24: Enter gloss:action:register
21:33:56 10.0.1.24: Enter gloss:action:register
21:35:46 10.0.1.24: Enter gloss:action:register
21:35:46 10.0.1.24: Enter gloss:action:handle_pagewrite_before
21:35:46 10.0.1.24: Exit gloss:action:handle_pagewrite_before
21:35:46 10.0.1.24: Enter gloss:action:handle_pagewrite_after
21:35:46 10.0.1.24: Exit gloss:action:handle_pagewrite_after
21:35:46 10.0.1.24: Enter gloss:action:handle_pagewrite_before
21:35:46 10.0.1.24: Exit gloss:action:handle_pagewrite_before
21:35:46 10.0.1.24: Enter gloss:action:handle_pagewrite_after
21:35:46 10.0.1.24: Exit gloss:action:handle_pagewrite_after
21:35:46 10.0.1.24: Enter gloss:action:register
21:35:46 10.0.1.24: Enter gloss:action:register
21:35:47 10.0.1.24: Enter gloss:action:register

Why are there so many seemingly redundant calls? Is this normal?


Solution

  • Your plugin's register method will be called everytime the plugin system is initialized. Since PHP is request based that happens on every request. Loading a page creates multiple requests (the page, the JS dispatcher, the CSS dispatcher, the indexing webbug, ajax calls, etc).

    Your second question is answered here:

    On update to an existing page this event is called twice, once for the transfer of the old version to the attic (rev will have a value) and once to write the new version of the page into the wiki (rev is false)

    https://www.dokuwiki.org/devel:event:io_wikipage_write