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. Log warnings for violations of the no-session |
25
|
|
|
// constraint. |
26
|
|
|
define( 'MW_NO_SESSION', 'warn' ); |
27
|
|
|
|
28
|
|
|
require_once __DIR__ . '/includes/WebStart.php'; |
29
|
|
|
|
30
|
|
|
if ( $wgRequest->getVal( 'ctype' ) == 'application/xml' ) { |
31
|
|
|
// Makes testing tweaks about a billion times easier |
32
|
|
|
$ctype = 'application/xml'; |
33
|
|
|
} else { |
34
|
|
|
$ctype = 'application/opensearchdescription+xml'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$response = $wgRequest->response(); |
38
|
|
|
$response->header( "Content-type: $ctype" ); |
39
|
|
|
|
40
|
|
|
// Set an Expires header so that squid can cache it for a short time |
41
|
|
|
// Short enough so that the sysadmin barely notices when $wgSitename is changed |
42
|
|
|
$expiryTime = 600; # 10 minutes |
43
|
|
|
$response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiryTime ) . ' GMT' ); |
44
|
|
|
$response->header( 'Cache-control: max-age=600' ); |
45
|
|
|
|
46
|
|
|
print '<?xml version="1.0"?>'; |
47
|
|
|
print Xml::openElement( 'OpenSearchDescription', |
48
|
|
|
[ |
49
|
|
|
'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/', |
50
|
|
|
'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ] ); |
51
|
|
|
|
52
|
|
|
/* The spec says the ShortName must be no longer than 16 characters, |
53
|
|
|
* but 16 is *realllly* short. In practice, browsers don't appear to care |
54
|
|
|
* when we give them a longer string, so we're no longer attempting to trim. |
55
|
|
|
* |
56
|
|
|
* Note: ShortName and the <link title=""> need to match; they are used as |
57
|
|
|
* a key for identifying if the search engine has been added already, *and* |
58
|
|
|
* as the display name presented to the end-user. |
59
|
|
|
* |
60
|
|
|
* Behavior seems about the same between Firefox and IE 7/8 here. |
61
|
|
|
* 'Description' doesn't appear to be used by either. |
62
|
|
|
*/ |
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' ], |
74
|
|
|
wfExpandUrl( $wgFavicon, PROTO_CURRENT ) ); |
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 && $wgEnableAPI ) { |
89
|
|
|
$template = ApiOpenSearch::getOpenSearchTemplate( $type ); |
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>'; |
117
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.