opensearch_desc.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // This endpoint is supposed to be independent of request cookies and other
  23. // details of the session. Enforce this constraint with respect to session use.
  24. define( 'MW_NO_SESSION', 1 );
  25. define( 'MW_ENTRY_POINT', 'opensearch_desc' );
  26. require_once __DIR__ . '/includes/WebStart.php';
  27. if ( $wgRequest->getVal( 'ctype' ) == 'application/xml' ) {
  28. // Makes testing tweaks about a billion times easier
  29. $ctype = 'application/xml';
  30. } else {
  31. $ctype = 'application/opensearchdescription+xml';
  32. }
  33. $response = $wgRequest->response();
  34. $response->header( "Content-type: $ctype" );
  35. // Set an Expires header so that CDN can cache it for a short time
  36. // Short enough so that the sysadmin barely notices when $wgSitename is changed
  37. $expiryTime = 600; # 10 minutes
  38. $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiryTime ) . ' GMT' );
  39. $response->header( 'Cache-control: max-age=600' );
  40. print '<?xml version="1.0"?>';
  41. print Xml::openElement( 'OpenSearchDescription',
  42. [
  43. 'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/',
  44. 'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ] );
  45. // The spec says the ShortName must be no longer than 16 characters,
  46. // but 16 is *realllly* short. In practice, browsers don't appear to care
  47. // when we give them a longer string, so we're no longer attempting to trim.
  48. //
  49. // Note: ShortName and the <link title=""> need to match; they are used as
  50. // a key for identifying if the search engine has been added already, *and*
  51. // as the display name presented to the end-user.
  52. //
  53. // Behavior seems about the same between Firefox and IE 7/8 here.
  54. // 'Description' doesn't appear to be used by either.
  55. $fullName = wfMessage( 'opensearch-desc' )->inContentLanguage()->text();
  56. print Xml::element( 'ShortName', null, $fullName );
  57. print Xml::element( 'Description', null, $fullName );
  58. // By default we'll use the site favicon.
  59. // Double-check if IE supports this properly?
  60. print Xml::element( 'Image',
  61. [
  62. 'height' => 16,
  63. 'width' => 16,
  64. 'type' => 'image/x-icon' ],
  65. wfExpandUrl( $wgFavicon, PROTO_CURRENT ) );
  66. $urls = [];
  67. // General search template. Given an input term, this should bring up
  68. // search results or a specific found page.
  69. // At least Firefox and IE 7 support this.
  70. $searchPage = SpecialPage::getTitleFor( 'Search' );
  71. $urls[] = [
  72. 'type' => 'text/html',
  73. 'method' => 'get',
  74. 'template' => $searchPage->getCanonicalURL( 'search={searchTerms}' ) ];
  75. foreach ( $wgOpenSearchTemplates as $type => $template ) {
  76. if ( !$template ) {
  77. $template = ApiOpenSearch::getOpenSearchTemplate( $type );
  78. }
  79. if ( $template ) {
  80. $urls[] = [
  81. 'type' => $type,
  82. 'method' => 'get',
  83. 'template' => $template,
  84. ];
  85. }
  86. }
  87. // Allow hooks to override the suggestion URL settings in a more
  88. // general way than overriding the whole search engine...
  89. Hooks::run( 'OpenSearchUrls', [ &$urls ] );
  90. foreach ( $urls as $attribs ) {
  91. print Xml::element( 'Url', $attribs );
  92. }
  93. // And for good measure, add a link to the straight search form.
  94. // This is a custom format extension for Firefox, which otherwise
  95. // sends you to the domain root if you hit "enter" with an empty
  96. // search box.
  97. print Xml::element( 'moz:SearchForm', null,
  98. $searchPage->getCanonicalURL() );
  99. print '</OpenSearchDescription>';