opensearch_desc.php 4.7 KB

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