| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Rest\Handler; |
| 22 |
|
| 23 |
use MediaWiki\Api\ApiOpenSearch; |
| 24 |
use MediaWiki\Config\Config; |
| 25 |
use MediaWiki\HookContainer\HookRunner; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\MainConfigSchema; |
| 28 |
use MediaWiki\Rest\Handler; |
| 29 |
use MediaWiki\Rest\Response; |
| 30 |
use MediaWiki\Rest\StringStream; |
| 31 |
use MediaWiki\SpecialPage\SpecialPage; |
| 32 |
use MediaWiki\Utils\UrlUtils; |
| 33 |
use MediaWiki\Xml\Xml; |
| 34 |
use Wikimedia\Http\HttpAcceptParser; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class OpenSearchDescriptionHandler extends Handler { |
| 49 |
|
| 50 |
private UrlUtils $urlUtils; |
| 51 |
|
| 52 |
|
| 53 |
private string $favicon; |
| 54 |
|
| 55 |
|
| 56 |
private array $templates; |
| 57 |
|
| 58 |
public function __construct( Config $config, UrlUtils $urlUtils ) { |
| 59 |
$this->favicon = $config->get( MainConfigNames::Favicon ); |
| 60 |
$this->templates = $config->get( MainConfigNames::OpenSearchTemplates ); |
| 61 |
$this->urlUtils = $urlUtils; |
| 62 |
} |
| 63 |
|
| 64 |
public function execute(): Response { |
| 65 |
$ctype = $this->getContentType(); |
| 66 |
|
| 67 |
$response = $this->getResponseFactory()->create(); |
| 68 |
$response->setHeader( 'Content-type', $ctype ); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
$expiryTime = 600; |
| 73 |
$response->setHeader( 'Expires', gmdate( 'D, d M Y H:i:s', time() + $expiryTime ) . ' GMT' ); |
| 74 |
$response->setHeader( 'Cache-control', 'max-age=600' ); |
| 75 |
|
| 76 |
$body = new StringStream(); |
| 77 |
|
| 78 |
$body->write( '<?xml version="1.0"?>' ); |
| 79 |
$body->write( Xml::openElement( 'OpenSearchDescription', |
| 80 |
[ |
| 81 |
'xmlns' => 'http://a9.com/-/spec/opensearch/1.1/', |
| 82 |
'xmlns:moz' => 'http://www.mozilla.org/2006/browser/search/' ] ) ); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
$fullName = wfMessage( 'opensearch-desc' )->inContentLanguage()->text(); |
| 95 |
$body->write( Xml::element( 'ShortName', null, $fullName ) ); |
| 96 |
$body->write( Xml::element( 'Description', null, $fullName ) ); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
$body->write( Xml::element( 'Image', |
| 101 |
[ |
| 102 |
'height' => 16, |
| 103 |
'width' => 16, |
| 104 |
'type' => 'image/x-icon' |
| 105 |
], |
| 106 |
(string)$this->urlUtils->expand( $this->favicon, PROTO_CURRENT ) |
| 107 |
) ); |
| 108 |
|
| 109 |
$urls = []; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
$searchPage = SpecialPage::getTitleFor( 'Search' ); |
| 115 |
$urls[] = [ |
| 116 |
'type' => 'text/html', |
| 117 |
'method' => 'get', |
| 118 |
'template' => $searchPage->getCanonicalURL( 'search={searchTerms}' ) ]; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
foreach ( $this->templates as $type => $template ) { |
| 123 |
if ( !$template ) { |
| 124 |
$template = ApiOpenSearch::getOpenSearchTemplate( $type ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( $template ) { |
| 128 |
$urls[] = [ |
| 129 |
'type' => $type, |
| 130 |
'method' => 'get', |
| 131 |
'template' => $template, |
| 132 |
]; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
( new HookRunner( $this->getHookContainer() ) )->onOpenSearchUrls( $urls ); |
| 139 |
|
| 140 |
foreach ( $urls as $attribs ) { |
| 141 |
$body->write( Xml::element( 'Url', $attribs ) ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
$body->write( Xml::element( 'moz:SearchForm', null, |
| 149 |
$searchPage->getCanonicalURL() ) ); |
| 150 |
|
| 151 |
$body->write( Xml::closeElement( 'OpenSearchDescription' ) ); |
| 152 |
|
| 153 |
$response->setBody( $body ); |
| 154 |
return $response; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
private function getContentType(): string { |
| 165 |
$params = $this->getValidatedParams(); |
| 166 |
if ( $params['ctype'] == 'application/xml' ) { |
| 167 |
|
| 168 |
return 'application/xml'; |
| 169 |
} |
| 170 |
|
| 171 |
$acceptHeader = $this->getRequest()->getHeader( 'accept' ); |
| 172 |
|
| 173 |
if ( $acceptHeader ) { |
| 174 |
$parser = new HttpAcceptParser(); |
| 175 |
$acceptableTypes = $parser->parseAccept( $acceptHeader[0] ); |
| 176 |
|
| 177 |
foreach ( $acceptableTypes as $acc ) { |
| 178 |
if ( $acc['type'] === 'application/xml' ) { |
| 179 |
return 'application/xml'; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return 'application/opensearchdescription+xml'; |
| 185 |
} |
| 186 |
|
| 187 |
public function getParamSettings() { |
| 188 |
return [ |
| 189 |
'ctype' => [ |
| 190 |
self::PARAM_SOURCE => 'query', |
| 191 |
] |
| 192 |
]; |
| 193 |
} |
| 194 |
|
| 195 |
} |