opensearch_desc.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Generate an OpenSearch description file
  4. *
  5. * @file
  6. */
  7. require_once( dirname(__FILE__) . '/includes/WebStart.php' );
  8. if( $wgRequest->getVal( 'ctype' ) == 'application/xml' ) {
  9. // Makes testing tweaks about a billion times easier
  10. $ctype = 'application/xml';
  11. } else {
  12. $ctype = 'application/opensearchdescription+xml';
  13. }
  14. $response = $wgRequest->response();
  15. $response->header( "Content-type: $ctype" );
  16. // Set an Expires header so that squid can cache it for a short time
  17. // Short enough so that the sysadmin barely notices when $wgSitename is changed
  18. $expiryTime = 600; # 10 minutes
  19. $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiryTime ) . ' GMT' );
  20. $response->header( 'Cache-control: max-age=600' );
  21. print '<?xml version="1.0"?>';
  22. print Xml::openElement( 'OpenSearchDescription',
  23. array(
  24. 'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/',
  25. 'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ) );
  26. // The spec says the ShortName must be no longer than 16 characters,
  27. // but 16 is *realllly* short. In practice, browsers don't appear to care
  28. // when we give them a longer string, so we're no longer attempting to trim.
  29. //
  30. // Note: ShortName and the <link title=""> need to match; they are used as
  31. // a key for identifying if the search engine has been added already, *and*
  32. // as the display name presented to the end-user.
  33. //
  34. // Behavior seems about the same between Firefox and IE 7/8 here.
  35. // 'Description' doesn't appear to be used by either.
  36. $fullName = wfMsgForContent( 'opensearch-desc' );
  37. print Xml::element( 'ShortName', null, $fullName );
  38. print Xml::element( 'Description', null, $fullName );
  39. // By default we'll use the site favicon.
  40. // Double-check if IE supports this properly?
  41. print Xml::element( 'Image',
  42. array(
  43. 'height' => 16,
  44. 'width' => 16,
  45. 'type' => 'image/x-icon' ),
  46. wfExpandUrl( $wgFavicon ) );
  47. $urls = array();
  48. // General search template. Given an input term, this should bring up
  49. // search results or a specific found page.
  50. // At least Firefox and IE 7 support this.
  51. $searchPage = SpecialPage::getTitleFor( 'Search' );
  52. $urls[] = array(
  53. 'type' => 'text/html',
  54. 'method' => 'get',
  55. 'template' => $searchPage->getFullURL( 'search={searchTerms}' ) );
  56. if( $wgEnableAPI ) {
  57. // JSON interface for search suggestions.
  58. // Supported in Firefox 2 and later.
  59. $urls[] = array(
  60. 'type' => 'application/x-suggestions+json',
  61. 'method' => 'get',
  62. 'template' => SearchEngine::getOpenSearchTemplate() );
  63. }
  64. // Allow hooks to override the suggestion URL settings in a more
  65. // general way than overriding the whole search engine...
  66. wfRunHooks( 'OpenSearchUrls', array( &$urls ) );
  67. foreach( $urls as $attribs ) {
  68. print Xml::element( 'Url', $attribs );
  69. }
  70. // And for good measure, add a link to the straight search form.
  71. // This is a custom format extension for Firefox, which otherwise
  72. // sends you to the domain root if you hit "enter" with an empty
  73. // search box.
  74. print Xml::element( 'moz:SearchForm', null,
  75. $searchPage->getFullUrl() );
  76. print '</OpenSearchDescription>';