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