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