OpenSearch for Yii API

I wrote Yii API Open Search plug-in for Firefox and (as I knew later) for IE.

https://addons.mozilla.org/ru/firefox/addon/254552/

Here are screenshots:

in IE: http://img571.imageshack.us/img571/7951/ieyii.png

in FF: http://img42.imageshack.us/img42/5781/yiiscreen2.png

But Yii site not provides results for OpenSearch in proper format.

That`s why I wrote interfase for Yii suggester.


<?php

ob_start("ob_gzhandler");

header("Content-Type: text/html; charset=utf-8");

function make_response($content)

{

    $title = $desc = $url = array();

    $qa = explode("\n",$content);

    foreach($qa as $value)

    {

        $exploded = array();

        $title[] = $value;

        $exploded = explode('.',$value);

        $desc[] = $exploded[0];

        $str = "";

        $str = 'http://www.yiiframework.com/doc/api/1.1/'.$exploded[0];

        if(count($exploded) == 2)

        {

            $str .= '#'.trim($exploded[1],'()').'-detail';

        }

        $url[] = $str;

    }

    return '["'.$_GET['q'].'",["'.implode('","',$title).'"],["'.implode('","',$desc).'"],["'.implode('","',$url).'"]]';

}


if(!empty($_GET['q']))

{

    $link = "http://www.yiiframework.com/api/suggest/?q=".urlencode($_GET['q']).'&limit=100&timestamp='.time();

	$ch = curl_init ();

	curl_setopt ($ch , CURLOPT_URL , $link);

	curl_setopt ($ch , CURLOPT_RETURNTRANSFER , 1 );

	$content = curl_exec($ch);

    echo make_response($content);

}

ob_end_flush();

?>

Temporary hosted on another site and you can see output here: http://www.z-zag.com/suggest.php?q=Form

Can you help me with host this suggester, or make special suggester page in yii official site to make it more stable.

And the second wish is to redirect from search page if it is the only result for query.

Example: http://www.yiiframework.com/search/?q=CActiveDataProvider.getCriteria()&type=api-suggest

Because of in IE plugin works correctly and goes to defined page straightly. But in FF searcer can`t do it, and redirects to search page.

OPENSEARCH suggestions specification: http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1

P.S.> Sorry for long post and my english.

This is great. Could you describe what format of the response you are expecting? It would be better if you could illustrate it with an example. Thanks!

You can see in example. BUt shortly JSON arrays:




["sea", //what we search

  ["sears", //list of suggestions

   "search engines",

   "search engine",

   "search",

   "sears.com",

   "seattle times"],

  ["7,390,000 results", //list of descriptions

   "17,900,000 results",

   "25,700,000 results",

   "1,220,000,000 results",

   "1 result",

   "17,600,000 results"],

  ["http://example.com?q=sears", //list of url

   "http://example.com?q=search+engines",

   "http://example.com?q=search+engine",

   "http://example.com?q=search",

   "http://example.com?q=sears.com",

   "http://example.com?q=seattle+times"]]



Positions in array are fixed. I don`t know how to say it in english:


["search",["suggest1","suggest2",...],["descr1","descr2",...],["url1","url2",...]]

Mozilla not supports format with urls, that`s why I ask for redirect if only 1 result found.

Is description required?

Regarding redirect, do you mean if there is only one result, instead of returning json results, we should just send a redirect header?

Can you make the plugin in English so that more users can use it? After it is in good shape, I can add a link to the plugin on our website.

It`s multilang:

https://addons.mozilla.org/en-US/firefox/addon/254552/

I gave you you russian link - sorry.

About redirect - yes.

If I click on suggestion in IE - browser goes to defined in urls array page. But if i click on suggestion in FF - browser goes in search page with suggested query (FF not supports opensearch links) and finds here only 1 entry. After click on it - we go to correct page. That`s why straight redirect if 1 search result will be good for search form.

I simply use classname for description.

But you can provide blank array without descriptions


["search",["suggest1","suggest2",...],[],["url1","url2",...]]

I just added support for this. http://www.yiiframework.com/api/suggest/?q=clist&json=1 (you need to put json=1 in the query part)

Please let me know if this works well. Thanks!

It works!. I`ve updated this plugin.

But there are some troubles:

  1. You forget to add "-detail" if query is about method or property.

not


http://www.yiiframework.com/doc/api/1.1/CAccessControlFilter#rules

but


http://www.yiiframework.com/doc/api/1.1/CAccessControlFilter#rules-detail

  1. Because of big quantity of suggestions:

in FF it works in slow letter by letter input(((

in IE it works if there is not many suggestions (<=20 in tests)

I just enabled the limit GET parameter. You can set it to a smaller number to make it work fast.

Regarding "-detail", this is because a method could be defined in a base class whose detail description is only available in the base class API page.

OK. Now it works more stable.

you can save yiisearch.xml:


<?xml version="1.0" encoding="UTF-8"?>

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"

                       xmlns:moz="http://www.mozilla.org/2006/browser/search/">

<ShortName>Yiisearch</ShortName>

<Description>Yii Class Referrence Searcher</Description>

<InputEncoding>UTF-8</InputEncoding>

<OutputEncoding>UTF-8</OutputEncoding>

<Image width="16" height="16" type="image/x-icon">http://yiiframework.com/favicon.ico</Image>

<Url type="text/html" method="GET" xmlns:referrer="http://a9.com/-/opensearch/extensions/referrer/" template="http://www.yiiframework.com/search/?q={searchTerms}&amp;type=api-suggest"/>

<Url type="application/x-suggestions+json" template="http://www.yiiframework.com/api/suggest/?q={searchTerms}&amp;json=1&amp;limit=20" />

<moz:SearchForm>http://www.yiiframework.com/doc/api/</moz:SearchForm>

</OpenSearchDescription>

in the root directory of your site.

and add in head section:


<link rel="search" type="application/opensearchdescription+xml" title="Yiisearch"

        href="/yiisearch.xml" />

After that IE and FF will notify user, that there is opensearch plugin. By highlighting choose arrow in fastsearch panel. And make Google Chrome able to find opensearch and suggester on this site.

And will it possible about redirect if the only variant found? It makes Firefox to go directly to suggested API page.

Added the xml file and changed the head section. It is working nicely! Thank you very much!

Regarding the redirection, it is a bit complicated. I will need to ask Wei for help.

Is your FF search plug-in still needed?

Now I need to write something to advertise this. Thanks!

Could you please explain how to make it work for Chrome?

Also, it seems IE8 doesn’t give autocompletion. Is this expected?

Or even better, if you could write a wiki article explaining how to make use of this wonderful thing.

I will add a link to the article then.

This plugin duplicated xml-flie.

But firefox add-ons make plugin searching easier for users. And the can make own plugin collections and provide it to other users. Let it be in firefox too.

Right Click in the address bar and select Edit Search Engines and on the next window select Yiisearch and Edit.

here you can change alias. searching will be able as {alias}<TAB or SPACE>{query}. Autocomplete working nicely.

You make changes to search form URL - but it was wery handy to search only in API Docs. May be it would be good to make Yii Site Search, and Yii Api Search in 2 xml files. and 2 header links.

sitesearch.xml:


 http://www.yiiframework.com/search/?q={searchTerms}

apisearch.xml


 http://www.yiiframework.com/search/?type=api-suggest&amp;q={searchTerms}

It`s very handy to experienced programmers. In russian jabber yii conference - they were dissapointed searching in all site.

I have IE8 - completion works more narrow then in FF.

Check if completion is on. Click on search engines chooser (arrow)

check something like Search plugins Management (I have russian version ant can`t say more exactly). And check if completion is on for Yii Search. if it is on. try to enter letters more slowly.

My eng is not great to do it))). But I can try.

P.S.> for Opera

Tools>Preferences>Search>Add


Name: Yii Search

Keyword: yii

Address: http://www.yiiframework.com/search/?type=api-suggest&q=%s

Then search by "yii<space>searchstr" but Opera not works with suggestions.

Now Ill going sleepin for 8h.)))

Nice. I’ve got it working in Opera. It requires some config editing.

How to do it:

  1. Go to http://www.yiiframework.com/doc/api/.

  2. Put cursor on the left column search field and in context menu select "create search".

  3. Enter "yii" to keyword field and save.

  4. Close Opera.

  5. Go to Opera’s profile directory.

  6. Open search.ini.

  7. Find your search section by searching "yii". It should look like:




[Search Engine 24]

UNIQUEID=76AA614D8305D94BB62572AB5A59C224

…



  1. Add the following to the end of the section:



Suggest Protocol=JSON

Suggest URL=http://www.yiiframework.com/api/suggest/?q={SearchTerm}&json=1



  1. Save.

  2. Start Opera and try typing "yii ca" in your address bar.

samdark - Молодец…))) А я не думал, что саджест к опере прикрутить мона. Можно вдвоем статейку накидать)))

It seems there is a problem with cache headers. Yii website should send nocache headers to prevent wrong suggestions.

GOsha

Good suggestion about two separate opensearches. Separate API search is a must have.

Thank you very much guys for your work… Works nicely