Merge "Pass message arguments as array to ErrorPageError"
[lhc/web/wiklou.git] / opensearch_desc.php
1 <?php
2 /**
3 * Generate an OpenSearch description file.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
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 require_once __DIR__ . '/includes/WebStart.php';
28
29 if ( $wgRequest->getVal( 'ctype' ) == 'application/xml' ) {
30 // Makes testing tweaks about a billion times easier
31 $ctype = 'application/xml';
32 } else {
33 $ctype = 'application/opensearchdescription+xml';
34 }
35
36 $response = $wgRequest->response();
37 $response->header( "Content-type: $ctype" );
38
39 // Set an Expires header so that CDN can cache it for a short time
40 // Short enough so that the sysadmin barely notices when $wgSitename is changed
41 $expiryTime = 600; # 10 minutes
42 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiryTime ) . ' GMT' );
43 $response->header( 'Cache-control: max-age=600' );
44
45 print '<?xml version="1.0"?>';
46 print Xml::openElement( 'OpenSearchDescription',
47 [
48 'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/',
49 'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ] );
50
51 // The spec says the ShortName must be no longer than 16 characters,
52 // but 16 is *realllly* short. In practice, browsers don't appear to care
53 // when we give them a longer string, so we're no longer attempting to trim.
54 //
55 // Note: ShortName and the <link title=""> need to match; they are used as
56 // a key for identifying if the search engine has been added already, *and*
57 // as the display name presented to the end-user.
58 //
59 // Behavior seems about the same between Firefox and IE 7/8 here.
60 // 'Description' doesn't appear to be used by either.
61 $fullName = wfMessage( 'opensearch-desc' )->inContentLanguage()->text();
62 print Xml::element( 'ShortName', null, $fullName );
63 print Xml::element( 'Description', null, $fullName );
64
65 // By default we'll use the site favicon.
66 // Double-check if IE supports this properly?
67 print Xml::element( 'Image',
68 [
69 'height' => 16,
70 'width' => 16,
71 'type' => 'image/x-icon' ],
72 wfExpandUrl( $wgFavicon, PROTO_CURRENT ) );
73
74 $urls = [];
75
76 // General search template. Given an input term, this should bring up
77 // search results or a specific found page.
78 // At least Firefox and IE 7 support this.
79 $searchPage = SpecialPage::getTitleFor( 'Search' );
80 $urls[] = [
81 'type' => 'text/html',
82 'method' => 'get',
83 'template' => $searchPage->getCanonicalURL( 'search={searchTerms}' ) ];
84
85 foreach ( $wgOpenSearchTemplates as $type => $template ) {
86 if ( !$template ) {
87 $template = ApiOpenSearch::getOpenSearchTemplate( $type );
88 }
89
90 if ( $template ) {
91 $urls[] = [
92 'type' => $type,
93 'method' => 'get',
94 'template' => $template,
95 ];
96 }
97 }
98
99 // Allow hooks to override the suggestion URL settings in a more
100 // general way than overriding the whole search engine...
101 Hooks::run( 'OpenSearchUrls', [ &$urls ] );
102
103 foreach ( $urls as $attribs ) {
104 print Xml::element( 'Url', $attribs );
105 }
106
107 // And for good measure, add a link to the straight search form.
108 // This is a custom format extension for Firefox, which otherwise
109 // sends you to the domain root if you hit "enter" with an empty
110 // search box.
111 print Xml::element( 'moz:SearchForm', null,
112 $searchPage->getCanonicalURL() );
113
114 print '</OpenSearchDescription>';