opensearch_desc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. [
  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. */
  52. $fullName = wfMessage( 'opensearch-desc' )->inContentLanguage()->text();
  53. print Xml::element( 'ShortName', null, $fullName );
  54. print Xml::element( 'Description', null, $fullName );
  55. // By default we'll use the site favicon.
  56. // Double-check if IE supports this properly?
  57. print Xml::element( 'Image',
  58. [
  59. 'height' => 16,
  60. 'width' => 16,
  61. 'type' => 'image/x-icon' ],
  62. wfExpandUrl( $wgFavicon, PROTO_CURRENT ) );
  63. $urls = [];
  64. // General search template. Given an input term, this should bring up
  65. // search results or a specific found page.
  66. // At least Firefox and IE 7 support this.
  67. $searchPage = SpecialPage::getTitleFor( 'Search' );
  68. $urls[] = [
  69. 'type' => 'text/html',
  70. 'method' => 'get',
  71. 'template' => $searchPage->getCanonicalURL( 'search={searchTerms}' ) ];
  72. foreach ( $wgOpenSearchTemplates as $type => $template ) {
  73. if ( !$template && $wgEnableAPI ) {
  74. $template = ApiOpenSearch::getOpenSearchTemplate( $type );
  75. }
  76. if ( $template ) {
  77. $urls[] = [
  78. 'type' => $type,
  79. 'method' => 'get',
  80. 'template' => $template,
  81. ];
  82. }
  83. }
  84. // Allow hooks to override the suggestion URL settings in a more
  85. // general way than overriding the whole search engine...
  86. Hooks::run( 'OpenSearchUrls', [ &$urls ] );
  87. foreach ( $urls as $attribs ) {
  88. print Xml::element( 'Url', $attribs );
  89. }
  90. // And for good measure, add a link to the straight search form.
  91. // This is a custom format extension for Firefox, which otherwise
  92. // sends you to the domain root if you hit "enter" with an empty
  93. // search box.
  94. print Xml::element( 'moz:SearchForm', null,
  95. $searchPage->getCanonicalURL() );
  96. print '</OpenSearchDescription>';