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