How do I change comments before saving them?

Creating and modifying plugins.
User avatar
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

How do I change comments before saving them?

Post by blog.brockha.us » Sat Jan 21, 2012 8:58 pm

I try to change a comment before it is saved. For that I hook into frontend_saveComment and my method doing the patch is called.

But when I change values of the comment, the changes are not propagated to the save method. The reason is this:

Code: Select all

function serendipity_saveComment ..
serendipity_plugin_api::hook_event('frontend_saveComment', $ca, $commentInfo);
Why isn't it

Code: Select all

serendipity_plugin_api::hook_event('frontend_saveComment', &$ca, &$commentInfo);
What is even more irritating to me: The AntiSpam plugins are setting $ca['allow_comments'] and the save function checks this attribute after calling the hook. So these changes seem to be visible to the function (what should not be the case, as ca is given as $ca not &$ca, too).

What am I missing here? :shock:
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/

User avatar
onli
Regular
Posts: 2362
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: How do I change comments before saving them?

Post by onli » Sun Jan 22, 2012 2:42 pm

I'm not sure. The depth of php are not my area. But I remember having the same question and I answered it for me:

Code: Select all

function hook_event($event_name, &$eventData, $addData = null) 
in plugin_api.inc.php could very well override the non-pointer-declaration in the code itself.

Maybe we both miss something else - Garvin is probably able to explain this.

User avatar
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How do I change comments before saving them?

Post by Timbalu » Sun Jan 22, 2012 3:41 pm

Yes, $eventData is already referenced, as you can modify the streaming content. The $addData array is always null, if not set once by one of these plugin hooks, to drop-in once.

Using references like in your question will also throw: "Deprecated: Call-time pass-by-reference has been deprecated in ..." notices, as of PHP 5.3.0 or 8.
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian

User avatar
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us » Sun Jan 22, 2012 7:02 pm

Hmm.. Sorry. I think I don't understand your answers. Yes, I also noticed already my code on top is nonsense.
But do you have an answer, why I can't modify the eventData in my hook code?

I have

Code: Select all

function event_hook($event, &$bag, &$eventData, &$addData)
in my plugin, so it should be able to change the eventData. But it isn't..
(But the antispam is still able to change addData)
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/

User avatar
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How do I change comments before saving them?

Post by Timbalu » Sun Jan 22, 2012 7:14 pm

I am not sure if I really got where your problem is or what you are trying to do.... but you have to remove the & by $addData for sure.
The serendipity_plugin_api::hook_event('frontend_saveComment', $ca, $commentInfo);
means $ca carrying the same var names as they have in $eventData and $commentInfo to give some additional data to the called hook, which has to do something with it, AFAIK.
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian

User avatar
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us » Sun Jan 22, 2012 8:00 pm

My problem (debugged with extensive logging):

* Before actually saving a comment, s9y is calling frontend_saveComment.
* I added a hook to that event into my plugin
* I change the commentInfo['comment'] to another value
* after the hook inside the save function of s9y commentInfo['comment'] is empty (like it was before)

I want to have s9y save my changed comment. How do I do this?
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/

User avatar
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How do I change comments before saving them?

Post by Timbalu » Mon Jan 23, 2012 11:15 am

Even without the &?
Maybe with
serendipity_plugin_api::hook_event('frontend_saveComment_finish', $ca, $commentInfo);
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian

User avatar
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us » Tue Jan 24, 2012 12:33 am

Well.. Then the save is already finished and I don't need to change it anymore (or resave it, what I don't want to do in order to not abuse the db..)
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/

User avatar
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How do I change comments before saving them?

Post by Timbalu » Tue Jan 24, 2012 11:11 am

Oh yes, sorry.
Is your plugins 'frontend_saveComment' the last in row (of installed plugins with this hook)?
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian

User avatar
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us » Tue Jan 24, 2012 12:08 pm

No.. It is the first. But other plugins should see my changed comment code, not the original I guess?
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/

User avatar
onli
Regular
Posts: 2362
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: How do I change comments before saving them?

Post by onli » Tue Jan 24, 2012 3:05 pm

Which php-version are you using? have a look at http://www.php.net/manual/en/language.r ... s.pass.php. I didn't find a proper explanation. But my understanding is: don't use & when calling a function, use it only when defining a function.

When you access $commentInfo['comment'], you are accessing $addData. When you access somethin in $ca, you are accessing &$eventData. This explains why changes in $ca (= &$eventData) are propagated and changes in $commentInfo (= $addData) not.

If that's correct, passing a reference deprecated and $commentInfo=$addData contains the comment you want to change for all following plugins/code, then you can't do that without changing the definition of $addData in plugin_api.

User avatar
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us » Wed Jan 25, 2012 11:48 am

I have PHP5. But please ignore the patch in my first post, onli. I already understood, that this was nonsense.

As I stated some posts before my eventhook looks like that:

Code: Select all

function event_hook($event, &$bag, &$eventData, &$addData)
.. so I should be able to change eventData and addData. I'm trying to change addData['comment'] w/o success. It is changed inside my plugin but it stays unchanged after being called in serendipity_save_comment. And I still don't get, why .. :cry:
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/

User avatar
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us » Wed Jan 25, 2012 1:09 pm

Later: from the developer docs I read now:
The first parameter is the hook name, documented below. $eventData is a referenced array that can hold any input/output data for your plugin. $addData is a read-only array of additional information for this event.
As the comment is addData here, it would explain, why it is not changed. But: When looking at the code, I don't understand, why this is the case. There is nothing in serendipity_saveComment, that prevents from changing addData (nothing I see at least).

And I don't understand, why this hook should be defined for comment as readOnly. Mmhh..
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/

User avatar
onli
Regular
Posts: 2362
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: How do I change comments before saving them?

Post by onli » Wed Jan 25, 2012 3:25 pm

Beware: hook_event != event_hook.

User avatar
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How do I change comments before saving them?

Post by Timbalu » Wed Jan 25, 2012 6:25 pm

Good point, Malte!

Grischa, I would like to help, but therefor I would need some explaination that I could downgrade to a testscript to understand what is going on... or can we see where you did pull and change the send comment, pass it by, and where you can't see the changed comment afterwards?

Btw, I remembered havin seen some plugins doing this:

Code: Select all

function event_hook($event, &$bag, &$eventData) {
because they don't use addData and being lazy. When playing around with the new exception error function, I discoverd this being monitored as bad syntax in strict error settings:

Code: Select all

Fatal error:  Uncaught exception 'ErrorException' with message 'Declaration of serendipity_event_s9ymarkup::event_hook() should be compatible with that of serendipity_event::event_hook()' in /var/www/serendipity/serendipity-nightly/plugins/serendipity_event_s9ymarkup/serendipity_event_s9ymarkup.php:147
in example.

So a plugins function event_hook() should always look like the MOTHER in plugin_api.inc:

Code: Select all

function event_hook($event, &$bag, &$eventData, $addData = null) {
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian

Post Reply