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