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 
37 if ( $wgRequest->getVal( 'ctype' ) == 'application/xml' ) {
38  // Makes testing tweaks about a billion times easier
39  $ctype = 'application/xml';
40 } else {
41  $ctype = 'application/opensearchdescription+xml';
42 }
43 
44 $response = $wgRequest->response();
45 $response->header( "Content-type: $ctype" );
46 
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 
53 print '<?xml version="1.0"?>';
54 print Xml::openElement( 'OpenSearchDescription',
55  [
56  'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/',
57  'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ] );
58 
59 // The spec says the ShortName must be no longer than 16 characters,
60 // but 16 is *realllly* short. In practice, browsers don't appear to care
61 // when we give them a longer string, so we're no longer attempting to trim.
62 //
63 // Note: ShortName and the <link title=""> need to match; they are used as
64 // a key for identifying if the search engine has been added already, *and*
65 // as the display name presented to the end-user.
66 //
67 // Behavior seems about the same between Firefox and IE 7/8 here.
68 // 'Description' doesn't appear to be used by either.
69 $fullName = wfMessage( 'opensearch-desc' )->inContentLanguage()->text();
70 print Xml::element( 'ShortName', null, $fullName );
71 print Xml::element( 'Description', null, $fullName );
72 
73 // By default we'll use the site favicon.
74 // Double-check if IE supports this properly?
75 print Xml::element( 'Image',
76  [
77  'height' => 16,
78  'width' => 16,
79  'type' => 'image/x-icon' ],
81 
82 $urls = [];
83 
84 // General search template. Given an input term, this should bring up
85 // search results or a specific found page.
86 // At least Firefox and IE 7 support this.
88 $urls[] = [
89  'type' => 'text/html',
90  'method' => 'get',
91  'template' => $searchPage->getCanonicalURL( 'search={searchTerms}' ) ];
92 
93 foreach ( $wgOpenSearchTemplates as $type => $template ) {
94  if ( !$template ) {
96  }
97 
98  if ( $template ) {
99  $urls[] = [
100  'type' => $type,
101  'method' => 'get',
102  'template' => $template,
103  ];
104  }
105 }
106 
107 // Allow hooks to override the suggestion URL settings in a more
108 // general way than overriding the whole search engine...
109 Hooks::runner()->onOpenSearchUrls( $urls );
110 
111 foreach ( $urls as $attribs ) {
112  print Xml::element( 'Url', $attribs );
113 }
114 
115 // And for good measure, add a link to the straight search form.
116 // This is a custom format extension for Firefox, which otherwise
117 // sends you to the domain root if you hit "enter" with an empty
118 // search box.
119 print Xml::element( 'moz:SearchForm', null,
120  $searchPage->getCanonicalURL() );
121 
122 print '</OpenSearchDescription>';
$searchPage
$searchPage
Definition: opensearch_desc.php:87
$response
$response
Definition: opensearch_desc.php:44
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1198
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:90
$fullName
$fullName
Definition: opensearch_desc.php:69
$wgOpenSearchTemplates
$wgOpenSearchTemplates
Templates for OpenSearch suggestions, defaults to API action=opensearch.
Definition: DefaultSettings.php:6886
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:211
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
$urls
$urls
Definition: opensearch_desc.php:82
$expiryTime
$expiryTime
Definition: opensearch_desc.php:49
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:171
ApiOpenSearch\getOpenSearchTemplate
static getOpenSearchTemplate( $type)
Fetch the template for a type.
Definition: ApiOpenSearch.php:360
$wgFavicon
$wgFavicon
The URL path of the shortcut icon.
Definition: DefaultSettings.php:342
print
print
Definition: opensearch_desc.php:53
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:643
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:491
$type
$type
Definition: testCompression.php:52