Not logged in. · Lost password · Register
Forum: General Help and Support Plugins RSS
IO_WIKIPAGE_WRITE sent twice??
Avatar
Falkor #1
Member since Apr 2007 · 32 posts
Group memberships: Members
Show profile · Link to this post
Subject: IO_WIKIPAGE_WRITE sent twice??
Hi,

I'm really trying to get my head around this - but can't see the problem.
I have a function _customedit() that prints a simple checkbox in the editform.
_customsubmit simply catches the IO_WIKIPAGE_WRITE event and sends a mail to say that the page has been sent.
For some reason (which is driving me crazy), I get two indentical mails. 

Anybody have an idea of what's wrong?


Here's the register code:

function register(&$controller) {
      $controller->register_hook('HTML_EDITFORM_INJECTION','AFTER', $this, '_customedit');
      $controller->register_hook('IO_WIKIPAGE_WRITE','AFTER', $this, '_customsubmit');
}


//Falkor
This post was edited on 2007-08-26, 22:09 by Falkor.
Avatar
andi (Administrator) #2
User title: splitbrain
Member since May 2006 · 3471 posts · Location: Berlin Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Try putting a dbg(dbg_backtrace()) in your handler function. It will print a list of called functions leading to your handler. It might give you an idea why it is called twice. Be sure to share your insights.

BTW: I have the feeling I've read about this problem at the mailinglist recently but I can't find any post on it.
Read this if you don't get any useful answers.
Lies dies wenn du keine hilfreichen Antworten bekommst.
Avatar
Falkor #3
Member since Apr 2007 · 32 posts
Group memberships: Members
Show profile · Link to this post
Thanks for the good advise. Using dbglog(dbg_backtrace()) I found something interesting:

The bug seems to be that it triggers the IO_WIKIPAGE_WRITE when the revision is saved as well (which I assume is not the intention). The emails I received looked identical because the filename is not in the event data.

It seems to work as follows:

On page delete:
Triggers once as it saves the page to a revision file in attic, but not the second time as it doesn't write the page to a file in the wiki data structure - it is simply deleted. I haven't investigated whether this means that another signal is sent.

On page edit/creation:
Triggers twice (see diff under), once for the page write, and once more for the revision saving. However,
when the event triggers for the revision saving, $event->data[3] is set with the revision number.

What was driving me mad was that I had a similar plugin that monitored certain namespaces for new pages and sent mails to a certain user group. However, this was only when the page was first created - not when the page was updated later. There is a crucial difference here since I both check if the file already exists via $INFO['exists'], and if $event->data[3] is unset in this plugin. When it triggers the second time, the revision is already set so $event->data[3] isn't empty anymore. Hence I will only get one mail.

$ diff wiki_dbg1.text wiki_dbg2.text
4c4,5
< io_writeWikiPage("##SNIP###/data/pages/falkortest.txt", "trigger twice test!", "falkortest") called at ##SNIP##/inc/common.php:772
---
> saveOldRevision("falkortest") called at ##SNIP##/inc/common.php:774
> io_writeWikiPage("##SNIP##/data/attic/falkortest.1188245754.txt.gz", "trigger twice test!", "falkortest", "1188245754") called at ##SNIP##/inc/common.php:810

In summary:
On page creation you can distinguish the triggerers by the fact that when the event is the actual pagewrite you have $event->data[3] unset and $INFO['exists'] == false. When it triggers for the revision saving, $event->data[3] is no longer unset, but contains the revision number.

On page editing, $INFO['exists'] == 1, and $event->data[3] is unset on the first save. When the revision is saving the latter is no longer unset, but contains the revision number.

On page delete $INFO['exists'] == 1, and $event->data[3] is set, but only triggered once for the revision save - not for the page save.


Hope this helps. Sorry about the messy writeup - it's kind of late.

Edit:
What just hit me was that maybe it would be more useful if the information that we have to look up in the $INFO variable was included in the IO_WIKIPAGE_WRITE event object instead? To me it seems more logical.

But is it so that the current page doesn't have a revision (since it's current?), and only gets a revision when it is put in the attic?

//Falkor
This post was edited 2 times, last on 2007-08-28, 14:32 by Falkor.
Avatar
Netsurfer #4
Member since Feb 2007 · 24 posts
Group memberships: Members
Show profile · Link to this post
Subject: An alternativ solution (maybe)
Hi Falkor.

While writing a menu-plugin for my wiki, I had the same trouble/ problems with that standard DW event 'IO_WIKIPAGE_WRITE'. I'm convinced that this event should only be used if you want to make any changes to the data that should be saved.

The more often needed case, I believe, is that authors wanted to be informed when the saving is done to call their functions then.

I now use my own trigger. I called it 'IO_WIKIPAGE_SAVED'.
Here are the four lines of code I have inserted into /inc/common.php file into the function saveWikiText() (round about at line 715):

The following lines:
 
if($text == rawWiki($id,'')){
    return;
}

I've changed to:

if($text == rawWiki($id,'')){
    $data = array("id"=>$id, "old"=>$old, "removed"=>$wasRemoved, "created"=>$wasCreated, "reverted"=>$wasReverted, "new_rev"=>$newRev, "old_rev"=>$oldRev);
    return trigger_event('IO_WIKIPAGE_SAVED', $data, '', false);
  }

and at the end of the function after the line:

io_saveFile($conf['cachedir'].'/purgefile',time());

I've added:

  $data = array("id"=>$id, "old"=>$old, "removed"=>$wasRemoved, "created"=>$wasCreated, "reverted"=>$wasReverted, "new_rev"=>$newRev, "old_rev"=>$oldRev);
  trigger_event('IO_WIKIPAGE_SAVED', $data, '', false);

And my action-plugin lokks like:

function register(&$controller) {
        $controller->register_hook('IO_WIKIPAGE_SAVED', 'AFTER', $this, 'AfterWikiPageSaved');
  }
 
  function AfterWikiPageSaved(&$event, $param) {
 
  }

Due to the data-array you could now easily detect what kind of operation took place. And as far as I mentioned is this event only once triggered.

Maybe there are still some side effects I did not discovered yet  ;-) , but for me it seems to work well.


Cheers
Gunther
Avatar
Falkor #5
Member since Apr 2007 · 32 posts
Group memberships: Members
Show profile · Link to this post
Hi Netsurfer,

I like the idea - but I'm sceptic to branch off from the official dokuwiki distro, if you know what I mean.

In my opinion, we should really have a better way to deal with this stuff when programming plugins - but
I'm guessing that andi and the other developers have far too many more important things on theuir hands to deal
with this now. Maybe I should add it to the wish-list, if there is one :-)

//Falkor
Avatar
Netsurfer #6
Member since Feb 2007 · 24 posts
Group memberships: Members
Show profile · Link to this post
Hi Falkor!

Quote by Falkor:
Hi Netsurfer,

I like the idea - but I'm sceptic to branch off from the official dokuwiki distro, if you know what I mean.

Yes, I know and I agree. But sometimes it's much easier to add/ change/ delete a few lines in the DW core to make things working as desired (only in your very own DW installation).

Quote by Falkor:
In my opinion, we should really have a better way to deal with this stuff when programming plugins - but
I'm guessing that andi and the other developers have far too many more important things on theuir hands to deal
with this now. Maybe I should add it to the wish-list, if there is one :-)

Well I think that in this case you are not getting into trouble, because if the trigger is implemented in one of the next versions -> OK! If not, you just have to add it manually again -> no big deal at all.

BTW: You can add a feature request at http://bugs.splitbrain.org/index.php?project=1


Cheers
Gunther
This post was edited on 2007-09-04, 23:58 by Netsurfer.
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Imprint
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Current time: 2019-08-21, 18:00:13 (UTC +02:00)