Adjust Search Plugin

Creating and modifying plugins.
Post Reply
danst0
Regular
Posts: 197
Joined: Tue Jul 13, 2004 10:50 am

Adjust Search Plugin

Post by danst0 »

Hi,

where do I find the code for the quicksearch plugin. I would love to replace the current OR-jointed search with an AND-jointed one.

Daniel
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Adjust Search Plugin

Post by garvinhicking »

Hi!

This is core functionality, you cannot easily change that.

You can pass "+word1 +word2" to the search, then it will AND join both searches. To force that, you could write an even tplugin that listens on 'frontend_configure' and rewrites the $serendipity['GET']['searchTerm'] variable so that each word has a "+" in front of it.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
danst0
Regular
Posts: 197
Joined: Tue Jul 13, 2004 10:50 am

Re: Adjust Search Plugin

Post by danst0 »

When I do a: find . -name \*.php -exec grep -i -l -d skip "serendipity_plugin_api::hook_event('frontend_configure'" {} \;

On my installation I only get serendipity_config.inc.php as result, do you by accident know a plugin that uses the hook?

Daniel
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Adjust Search Plugin

Post by garvinhicking »

Hi!

You better search for "case 'frontend_configure'" to find these.

A simple plugin that listens there is serendipity_event_templatechooser (or maybe serendipity_event_templatedropdown I don't remember).

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
danst0
Regular
Posts: 197
Joined: Tue Jul 13, 2004 10:50 am

Re: Adjust Search Plugin

Post by danst0 »

Ok, and I call it ANDSearch

Code: Select all

class serendipity_event_andsearch extends serendipity_event
{
    var $title = PLUGIN_EVENT_ANDSEARCH_NAME;

    function introspect(&$propbag)
    {
        global $serendipity;

        $propbag->add('name',        PLUGIN_EVENT_ANDSEARCH_NAME);
        $propbag->add('description', PLUGIN_EVENT_ANDSEARCH_DESC);
        $propbag->add('stackable',   false);
        $propbag->add('author',      'Daniel Stengel');
        $propbag->add('version',     '0.9');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'smarty'      => '2.6.7',
            'php'         => '4.1.0'
        ));
        $propbag->add('groups', array('FRONTEND_FEATURES'));
        $propbag->add('event_hooks', array('frontend_configure' => true));
    }

    function generate_content(&$title) {
        $title = $this->title;
    }

    function event_hook($event, &$bag, &$eventData) {
        global $serendipity;

        $hooks = &$bag->get('event_hooks');

        if (isset($hooks[$event])) {
            switch($event) {
              case 'frontend_configure':
                
                if (isset($serendipity['GET']['searchTerm'])) {
                    $serendipity['GET']['searchTerm'] = str_replace(" ", " +", " " . $serendipity['GET']['searchTerm']);
                    //echo $serendipity['GET']['searchTerm'];
                }
                return true;
                break;

              default:
                return false;
            }

        } else {
            return false;
        }
    }
}

/* vim: set sts=4 ts=4 expandtab : */
?>
Works as designed.
Is it somehow possible to get rid of the pluses at the result output? (here: "Your search for +test returned 6 results")

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

Re: Adjust Search Plugin

Post by Timbalu »

HI

Yes with smarty inside the output tpl:

Code: Select all

{$result|replace:"+":""}
Ian
danst0
Regular
Posts: 197
Joined: Tue Jul 13, 2004 10:50 am

Re: Adjust Search Plugin

Post by danst0 »

While playing with this, i discovered the following

Code: Select all

$pos = strpos("+test","+");
if ($pos == false) {
echo "No Plus found";
}
$pos is going to be 0, but the if-clause will always be executed...
I have this code basically directly from the php-manual, http://php.net/manual/de/function.strpos.php

Isn't it possible to detect a plus sign at the first string position? If yes, tell me how?

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

Re: Adjust Search Plugin

Post by Timbalu »

Well you asked for replacing the + in the output. This wont work in your plugin, while it puts together the search string only andf gives it to core. The output is inside a template file. (which one I dont know out of the bin)

If you are just playing around try

Code: Select all

if ($pos === false) echo "No Plus found";
else echo 'Found + in string';
else have a look at
http://www.php.net/manual/de/function.stripos.php

Ian
danst0
Regular
Posts: 197
Joined: Tue Jul 13, 2004 10:50 am

Re: Adjust Search Plugin

Post by danst0 »

Ok the code looks like this now and works perfectly:

Code: Select all

switch($event) {
              case 'frontend_configure':
                           
                if (isset($serendipity['GET']['searchTerm'])) {
                    $position = strpos($serendipity['GET']['searchTerm'],"+");
                    if ($position === false) {
                        $serendipity['GET']['searchTerm'] = str_replace(" ", " +", "+" . $serendipity['GET']['searchTerm']);
                    }
                   
                }
                return true;
                break;

              default:
                return false;
            }

I realized my problem was that there were three = signs. So at least now I know the evolution, from Basic (=) to C (==) to PHP (===)?

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

Re: Adjust Search Plugin

Post by Timbalu »

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "".
So the === is a evolution, true!
I just like it that PHP is picky in these things.

Ian
Carl @ ATS
Regular
Posts: 55
Joined: Fri Jun 07, 2013 11:41 am
Location: Fürstenfeldbruck, Germany
Contact:

Re: Adjust Search Plugin

Post by Carl @ ATS »

Hi,

Although I'm new to Serendipity and am not a programmer, I've been able to install the software and create a blog of my own on my web host's server. The quicksearch plug-in is part of the basic Serendipity bundle, but where exactly can I find the plug-in (it's not where I'd expect it to be, namely together with the other plug-ins I've installed using Spartacus) and how do I go about changing the title of the feature as it appears on my blog? I don't want it to be called "quicksearch", but "quick search" with a space in the middle (standard English); since I'm a translator and editor, I want it to be spelt the standard way. So once I've managed to pinpoint the file, what do I need to change, please?

Thanks for any help.

Carl
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Adjust Search Plugin

Post by onli »

The Quicksearch plugin as a core plugin is in the file include/plugin_internal.inc.php. In there you would find that it uses the constant QUICKSEARCH to generate the name and the title. The easiest way to spell the name differently is to change that constant: Add

Code: Select all

@define('QUICKSEARCH', 'Quick search');
to your serendipity_config_local.inc.php
Carl @ ATS
Regular
Posts: 55
Joined: Fri Jun 07, 2013 11:41 am
Location: Fürstenfeldbruck, Germany
Contact:

Re: Adjust Search Plugin

Post by Carl @ ATS »

Thanks onli,

That worked nicely. :D

Regards

Carl

P.S. I'm looking forward to version 2.0 of Serendipity! (I see you're working on it.) Did you know that some of the optional plug-ins for the current version don't work any more? The sites they refer to have changed. So I think it's time for the developers to update certain plug-ins now -- and also add a few new ones offering extra features. Is there a relatively easy way of creating a blogroll, for example? The current way using a plug-in with Cron jobs is very complicated and time-consuming IMHO. The easier it is to set up a blog, the better.)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Adjust Search Plugin

Post by onli »

Reagarding your PS: Thanks. We didn't touch plugins yet, apart from a bit of preparation. It is quite possible that a cleanup is necessary, but I don't think we will be able to check them all and make them work again if they are broken. Maybe a few very important ones.
Not familiar with the or another blogroll-plugin, sorry.
Post Reply