OpenSearch cleanup; Firefox now sends you to the search page for empty searches inste...
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 1 Jul 2008 23:31:24 +0000 (23:31 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 1 Jul 2008 23:31:24 +0000 (23:31 +0000)
This is done by adding the <moz:SearchForm> extension element with the Special:Search URL.

Removed the 24-character truncation on ShortName... neither Firefox nor IE seems to care about longer names, but both expect the ShortName and <link> title to match, and will think you have a duplicate search provider if they don't match because only one was truncated. (Besides, 24 was longer than the spec's limit of 16 -- why bother limiting at all if not to spec?)

The JSON suggestions are now no longer included in the OpenSearch description if the API is disabled, since the interface won't work.

Generally removed some hardcoded escape-then-stick-vars-in-string-output eww... Extension hooks now have a more general opportunity to alter or replace the OpenSearch URLs by tying into the OpenSearchUrls hook.

RELEASE-NOTES
docs/hooks.txt
opensearch_desc.php

index 67e490a..5e91aaf 100644 (file)
@@ -403,6 +403,9 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 14515) HTML nesting cleanup on edit form
 * (bug 14647) Removed unused 'townBox' CSS classes
 * (bug 14687) OutputPage::addStyle() now adds type="text/css" like it should.
+* OpenSearch cleanup; Firefox now sends you to the search page for empty
+  searches instead of the domain root (which may not even be a wiki).
+
 
 === API changes in 1.13 ===
 
index 4fab816..e0f235d 100644 (file)
@@ -846,6 +846,10 @@ $article: the article edited
 $rev: the new revision
 $baseID: the revision ID this was based off, if any
 
+'OpenSearchUrls': Called when constructing the OpenSearch description XML.
+Hooks can alter or append to the array of URLs for search & suggestion formats.
+&$urls: array of associative arrays with Url element attributes
+
 'OutputPageBeforeHTML': a page has been processed by the parser and
 the resulting HTML is about to be displayed.  
 $parserOutput: the parserOutput (object) that corresponds to the page 
index 82e74b2..3dbf872 100644 (file)
@@ -5,45 +5,85 @@
  */
 
 require_once( dirname(__FILE__) . '/includes/WebStart.php' );
-require_once( dirname(__FILE__) . '/languages/Names.php' );
-$fullName = wfMsgForContent( 'opensearch-desc' );
-$shortName = htmlspecialchars( mb_substr( $fullName, 0, 24 ) );
-$siteName = htmlspecialchars( $fullName );
-
-
-$favicon = htmlspecialchars( wfExpandUrl( $wgFavicon ) );
-
-$title = SpecialPage::getTitleFor( 'Search' );
-$template = $title->escapeFullURL( 'search={searchTerms}' );
-
-$suggest = htmlspecialchars(SearchEngine::getOpenSearchTemplate() );
 
-
-$response = $wgRequest->response();
 if( $wgRequest->getVal( 'ctype' ) == 'application/xml' ) {
        // Makes testing tweaks about a billion times easier
        $ctype = 'application/xml';
 } else {
        $ctype = 'application/opensearchdescription+xml';
 }
+
+$response = $wgRequest->response();
 $response->header( "Content-type: $ctype" );
 
-# Set an Expires header so that squid can cache it for a short time
-# Short enough so that the sysadmin barely notices when $wgSitename is changed
+// Set an Expires header so that squid can cache it for a short time
+// Short enough so that the sysadmin barely notices when $wgSitename is changed
 $expiryTime = 600; # 10 minutes
 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiryTime ) . ' GMT' );
 $response->header( 'Cache-control: max-age=600' );
 
-echo <<<EOT
-<?xml version="1.0"?>
-<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
-<ShortName>$shortName</ShortName>
-<Description>$siteName</Description>
-<Image height="16" width="16" type="image/x-icon">$favicon</Image>
-<Url type="text/html" method="get" template="$template"/>
-<Url type="application/x-suggestions+json" method="GET" template="$suggest"/>
-</OpenSearchDescription>
-EOT;
+print '<?xml version="1.0"?>';
+print Xml::openElement( 'OpenSearchDescription',
+       array(
+               'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/',
+               'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ) );
+
+// The spec says the ShortName must be no longer than 16 characters,
+// but 16 is *realllly* short. In practice, browsers don't appear to care
+// when we give them a longer string, so we're no longer attempting to trim.
+//
+// Note: ShortName and the <link title=""> need to match; they are used as
+// a key for identifying if the search engine has been added already, *and*
+// as the display name presented to the end-user.
+//
+// Behavior seems about the same between Firefox and IE 7/8 here.
+// 'Description' doesn't appear to be used by either.
+$fullName = wfMsgForContent( 'opensearch-desc' );
+print Xml::element( 'ShortName', null, $fullName );
+print Xml::element( 'Description', null, $fullName );
+
+// By default we'll use the site favicon.
+// Double-check if IE supports this properly?
+print Xml::element( 'Image',
+       array(
+               'height' => 16,
+               'width' => 16,
+               'type' => 'image/x-icon' ),
+       wfExpandUrl( $wgFavicon ) );
+
+$urls = array();
+
+// General search template. Given an input term, this should bring up
+// search results or a specific found page.
+// At least Firefox and IE 7 support this.
+$searchPage = SpecialPage::getTitleFor( 'Search' );
+$urls[] = array(
+       'type' => 'text/html',
+       'method' => 'get',
+       'template' => $searchPage->getFullURL( 'search={searchTerms}' ) );
+
+if( $wgEnableAPI ) {
+       // JSON interface for search suggestions.
+       // Supported in Firefox 2 and later.
+       $urls[] = array(
+               'type' => 'application/x-suggestions+json',
+               'method' => 'get',
+               'template' => SearchEngine::getOpenSearchTemplate() );
+}
+
+// Allow hooks to override the suggestion URL settings in a more
+// general way than overriding the whole search engine...
+wfRunHooks( 'OpenSearchUrls', array( &$urls ) );
+
+foreach( $urls as $attribs ) {
+       print Xml::element( 'Url', $attribs );
+}
 
+// And for good measure, add a link to the straight search form.
+// This is a custom format extension for Firefox, which otherwise
+// sends you to the domain root if you hit "enter" with an empty
+// search box.
+print Xml::element( 'moz:SearchForm', null,
+       $searchPage->getFullUrl() );
 
-?>
+print '</OpenSearchDescription>';