opensearch_desc.php 3.8 KB

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