MediaWiki  master
opensearch_desc.php
Go to the documentation of this file.
1 <?php
29 // This endpoint is supposed to be independent of request cookies and other
30 // details of the session. Enforce this constraint with respect to session use.
31 define( 'MW_NO_SESSION', 1 );
32 
33 define( 'MW_ENTRY_POINT', 'opensearch_desc' );
34 
35 require_once __DIR__ . '/includes/WebStart.php';
36 
38 
41 
42  if ( $wgRequest->getVal( 'ctype' ) == 'application/xml' ) {
43  // Makes testing tweaks about a billion times easier
44  $ctype = 'application/xml';
45  } else {
46  $ctype = 'application/opensearchdescription+xml';
47  }
48 
49  $response = $wgRequest->response();
50  $response->header( "Content-type: $ctype" );
51 
52  // Set an Expires header so that CDN can cache it for a short time
53  // Short enough so that the sysadmin barely notices when $wgSitename is changed
54  $expiryTime = 600; # 10 minutes
55  $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiryTime ) . ' GMT' );
56  $response->header( 'Cache-control: max-age=600' );
57 
58  print '<?xml version="1.0"?>';
59  print Xml::openElement( 'OpenSearchDescription',
60  [
61  'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/',
62  'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ] );
63 
64  // The spec says the ShortName must be no longer than 16 characters,
65  // but 16 is *realllly* short. In practice, browsers don't appear to care
66  // when we give them a longer string, so we're no longer attempting to trim.
67  //
68  // Note: ShortName and the <link title=""> need to match; they are used as
69  // a key for identifying if the search engine has been added already, *and*
70  // as the display name presented to the end-user.
71  //
72  // Behavior seems about the same between Firefox and IE 7/8 here.
73  // 'Description' doesn't appear to be used by either.
74  $fullName = wfMessage( 'opensearch-desc' )->inContentLanguage()->text();
75  print Xml::element( 'ShortName', null, $fullName );
76  print Xml::element( 'Description', null, $fullName );
77 
78  // By default we'll use the site favicon.
79  // Double-check if IE supports this properly?
80  print Xml::element( 'Image',
81  [
82  'height' => 16,
83  'width' => 16,
84  'type' => 'image/x-icon' ],
86 
87  $urls = [];
88 
89  // General search template. Given an input term, this should bring up
90  // search results or a specific found page.
91  // At least Firefox and IE 7 support this.
92  $searchPage = SpecialPage::getTitleFor( 'Search' );
93  $urls[] = [
94  'type' => 'text/html',
95  'method' => 'get',
96  'template' => $searchPage->getCanonicalURL( 'search={searchTerms}' ) ];
97 
98  foreach ( $wgOpenSearchTemplates as $type => $template ) {
99  if ( !$template ) {
101  }
102 
103  if ( $template ) {
104  $urls[] = [
105  'type' => $type,
106  'method' => 'get',
107  'template' => $template,
108  ];
109  }
110  }
111 
112  // Allow hooks to override the suggestion URL settings in a more
113  // general way than overriding the whole search engine...
114  Hooks::runner()->onOpenSearchUrls( $urls );
115 
116  foreach ( $urls as $attribs ) {
117  print Xml::element( 'Url', $attribs );
118  }
119 
120  // And for good measure, add a link to the straight search form.
121  // This is a custom format extension for Firefox, which otherwise
122  // sends you to the domain root if you hit "enter" with an empty
123  // search box.
124  print Xml::element( 'moz:SearchForm', null,
125  $searchPage->getCanonicalURL() );
126 
127  print '</OpenSearchDescription>';
128 }
const PROTO_CURRENT
Definition: Defines.php:198
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
global $wgRequest
Definition: Setup.php:407
static getOpenSearchTemplate( $type)
Fetch the template for a type.
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:113
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:44
$wgOpenSearchTemplates
Config variable stub for the OpenSearchTemplates setting, for use by phpdoc and IDEs.
$wgFavicon
Config variable stub for the Favicon setting, for use by phpdoc and IDEs.
wfOpenSearchDescMain()