OpenSearch

OpenSearch in Firefox
OpenSearch autodiscovery feature in Firefox


1. OpenSearch description format


OpenSearch specification
MDN: OpenSearch description format


<?xml version="1.0"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
    <ShortName>WackoWiki</ShortName>
    <Description>WackoWiki</Description>
    <Image height="16" width="16" type="image/x-icon">https://example.com/theme/default/icon/favicon.ico</Image>
    <Url type="text/html" method="get" template="https://example.com/Search?phrase={searchTerms}" />
    <moz:SearchForm>https://example.com/Search</moz:SearchForm>
</OpenSearchDescription>

2. Implementation

OpenSearch (type="text/html") + Autodiscovery

2.1. XML description file

class/feed.php

<?php

// OpenSearch XML description file
function open_search()
{
    $this->engine->canonical = true;

    $xml  = '<?xml version="1.0"?>' . "\n";
    $xml .= '<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">' . "\n";
    $xml .= '<ShortName>' . $this->engine->db->site_name . '</ShortName>' . "\n";
    $xml .= '<Description>' . $this->engine->db->site_name . '</Description>' . "\n";
    $xml .= '<InputEncoding>UTF-8</InputEncoding>' . "\n";
    $xml .= '<Image height="16" width="16" type="image/x-icon">' .
        ($this->engine->db->site_favicon
            ? $this->engine->db->base_url . Ut::join_path(IMAGE_DIR, $this->engine->db->site_favicon)
            : $this->engine->db->base_url . Ut::join_path(THEME_DIR, $this->engine->db->theme) . '/' . 'icon/favicon.ico')
        . '</Image>' . "\n";
    $xml .= '<Url type="text/html" method="get" template="' . $this->engine->href('', $this->engine->db->search_page) . '?phrase={searchTerms}" />' . "\n";
    $xml .= '</OpenSearchDescription>' . "\n";

    $file_name = Ut::join_path(XML_DIR, 'opensearch.xml');
    file_put_contents($file_name, $xml);
    @chmod('opensearch.xml', CHMOD_FILE);
    $this->engine->canonical = false;
}

xml/opensearch.xml

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>WackoWiki</ShortName>
    <Description>WackoWiki</Description>
    <InputEncoding>UTF-8</InputEncoding>
    <Image height="16" width="16" type="image/x-icon">https://example.com/theme/default/icon/favicon.ico</Image>
    <Url type="text/html" method="get" template="https://example.com/Search?phrase={searchTerms}"/>
</OpenSearchDescription>

2.2. Autodiscovery

theme/_common/_header.php

<?php

$tpl->os_href = $this->db->base_path . XML_DIR . '/';

theme/_common/_header.tpl

[= os _ =
	<link rel="search" type="application/opensearchdescription+xml" title="[ ' db: site_name | e ' ]" href="[ ' href ' ]opensearch.xml">
=]	

HTML head

<link rel="search" type="application/opensearchdescription+xml" href="/xml/opensearch.xml" title="WackoWiki">

2.3. Routing

config/router.conf

`^xml/opensearch\.xml$`		_ok!
`^xml/{}$`			_ok! age=0		// feeds	

Uses default cache: $age = 31 (days)

2.4. Resync XML description file

AP -> Maintenance -> Data Synchronization -> Feeds -> Synchronize

2.5. Notes

  • The xmlns attribute is important — without it you could get the error message "Firefox could not download the search plugin".
    • Some providers just use <OpenSearchDescription> without xmlns="http://a9.com/-/spec/opensearch/1.1/" and it may work, but it won't work with the current solution (tested)
  • Adds only type="text/html". It has been implemented along with the feed functions (feed class & xml folder), its static and is written only once.

commit:6e07dab4d91892f7c78de77475bf32b91bbb9951

3. Questions

  • Do we want a option to disable OpenSearch?
  • Do we need the AJAX auto-suggest feature as option (type="application/x-suggestions+json")
  • What is <moz:SearchForm>https://example.com/Search</moz:SearchForm> good for?

Show Files (1 file)