1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
<?php
/**
* Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
* Copyright © 2008 Brooke Vibber <bvibber@wikimedia.org>
* Copyright © 2014 Wikimedia Foundation and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Api;
use InvalidArgumentException;
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
use MediaWiki\Utils\UrlUtils;
use SearchEngine;
use SearchEngineConfig;
use SearchEngineFactory;
use Wikimedia\ParamValidator\ParamValidator;
/**
* @ingroup API
*/
class ApiOpenSearch extends ApiBase {
use \MediaWiki\Api\SearchApi;
/** @var string|null */
private $format = null;
/** @var string|null */
private $fm = null;
private LinkBatchFactory $linkBatchFactory;
private UrlUtils $urlUtils;
public function __construct(
ApiMain $mainModule,
string $moduleName,
LinkBatchFactory $linkBatchFactory,
SearchEngineConfig $searchEngineConfig,
SearchEngineFactory $searchEngineFactory,
UrlUtils $urlUtils
) {
parent::__construct( $mainModule, $moduleName );
$this->linkBatchFactory = $linkBatchFactory;
// Services needed in SearchApi trait
$this->searchEngineConfig = $searchEngineConfig;
$this->searchEngineFactory = $searchEngineFactory;
$this->urlUtils = $urlUtils;
}
/**
* Get the output format
*
* @return string
*/
protected function getFormat() {
if ( $this->format === null ) {
$format = $this->getParameter( 'format' );
if ( str_ends_with( $format, 'fm' ) ) {
$this->format = substr( $format, 0, -2 );
$this->fm = 'fm';
} else {
$this->format = $format;
$this->fm = '';
}
}
return $this->format;
}
public function getCustomPrinter() {
switch ( $this->getFormat() ) {
case 'json':
return new ApiOpenSearchFormatJson(
$this->getMain(), $this->fm, $this->getParameter( 'warningsaserror' )
);
case 'xml':
$printer = $this->getMain()->createPrinterByName( 'xml' . $this->fm );
'@phan-var ApiFormatXml $printer';
/** @var ApiFormatXml $printer */
$printer->setRootElement( 'SearchSuggestion' );
return $printer;
default:
ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
}
}
public function execute() {
$params = $this->extractRequestParams();
$search = $params['search'];
// Open search results may be stored for a very long time
$this->getMain()->setCacheMaxAge(
$this->getConfig()->get( MainConfigNames::SearchSuggestCacheExpiry ) );
$this->getMain()->setCacheMode( 'public' );
$results = $this->search( $search, $params );
// Allow hooks to populate extracts and images
$this->getHookRunner()->onApiOpenSearchSuggest( $results );
// Trim extracts, if necessary
$length = $this->getConfig()->get( MainConfigNames::OpenSearchDescriptionLength );
foreach ( $results as &$r ) {
if ( is_string( $r['extract'] ) && !$r['extract trimmed'] ) {
$r['extract'] = self::trimExtract( $r['extract'], $length );
}
}
// Populate result object
$this->populateResult( $search, $results );
}
/**
* Perform the search
* @param string $search the search query
* @param array $params api request params
* @return array search results. Keys are integers.
* @phan-return array<array{title:Title,redirect_from:?Title,extract:false,extract_trimmed:false,image:false,url:string}>
* Note that phan annotations don't support keys containing a space.
*/
private function search( $search, array $params ) {
$searchEngine = $this->buildSearchEngine( $params );
$titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
$results = [];
if ( !$titles ) {
return $results;
}
// Special pages need unique integer ids in the return list, so we just
// assign them negative numbers because those won't clash with the
// always positive articleIds that non-special pages get.
$nextSpecialPageId = -1;
if ( $params['redirects'] === null ) {
// Backwards compatibility, don't resolve for JSON.
$resolveRedir = $this->getFormat() !== 'json';
} else {
$resolveRedir = $params['redirects'] === 'resolve';
}
if ( $resolveRedir ) {
// Query for redirects
$redirects = [];
$lb = $this->linkBatchFactory->newLinkBatch( $titles );
if ( !$lb->isEmpty() ) {
$db = $this->getDB();
$res = $db->newSelectQueryBuilder()
->select( [ 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ] )
->from( 'page' )
->join( 'redirect', null, [ 'rd_from = page_id' ] )
->where( [
'rd_interwiki' => '',
$lb->constructSet( 'page', $db )
] )
->caller( __METHOD__ )
->fetchResultSet();
foreach ( $res as $row ) {
$redirects[$row->page_namespace][$row->page_title] =
[ $row->rd_namespace, $row->rd_title ];
}
}
// Bypass any redirects
$seen = [];
foreach ( $titles as $title ) {
$ns = $title->getNamespace();
$dbkey = $title->getDBkey();
$from = null;
if ( isset( $redirects[$ns][$dbkey] ) ) {
[ $ns, $dbkey ] = $redirects[$ns][$dbkey];
$from = $title;
$title = Title::makeTitle( $ns, $dbkey );
}
if ( !isset( $seen[$ns][$dbkey] ) ) {
$seen[$ns][$dbkey] = true;
$resultId = $title->getArticleID();
if ( $resultId === 0 ) {
$resultId = $nextSpecialPageId;
$nextSpecialPageId--;
}
$results[$resultId] = [
'title' => $title,
'redirect from' => $from,
'extract' => false,
'extract trimmed' => false,
'image' => false,
'url' => (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT ),
];
}
}
} else {
foreach ( $titles as $title ) {
$resultId = $title->getArticleID();
if ( $resultId === 0 ) {
$resultId = $nextSpecialPageId;
$nextSpecialPageId--;
}
$results[$resultId] = [
'title' => $title,
'redirect from' => null,
'extract' => false,
'extract trimmed' => false,
'image' => false,
'url' => (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT ),
];
}
}
return $results;
}
/**
* @param string $search
* @param array[] &$results
*/
protected function populateResult( $search, &$results ) {
$result = $this->getResult();
switch ( $this->getFormat() ) {
case 'json':
// http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
$result->addArrayType( null, 'array' );
$result->addValue( null, 0, strval( $search ) );
$terms = [];
$descriptions = [];
$urls = [];
foreach ( $results as $r ) {
$terms[] = $r['title']->getPrefixedText();
$descriptions[] = strval( $r['extract'] );
$urls[] = $r['url'];
}
$result->addValue( null, 1, $terms );
$result->addValue( null, 2, $descriptions );
$result->addValue( null, 3, $urls );
break;
case 'xml':
// https://msdn.microsoft.com/en-us/library/cc891508(v=vs.85).aspx
$imageKeys = [
'source' => true,
'alt' => true,
'width' => true,
'height' => true,
'align' => true,
];
$items = [];
foreach ( $results as $r ) {
$item = [
'Text' => $r['title']->getPrefixedText(),
'Url' => $r['url'],
];
if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
$item['Description'] = $r['extract'];
}
if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
$item['Image'] = array_intersect_key( $r['image'], $imageKeys );
}
ApiResult::setSubelementsList( $item, array_keys( $item ) );
$items[] = $item;
}
ApiResult::setIndexedTagName( $items, 'Item' );
$result->addValue( null, 'version', '2.0' );
$result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' );
$result->addValue( null, 'Query', strval( $search ) );
$result->addSubelementsList( null, 'Query' );
$result->addValue( null, 'Section', $items );
break;
default:
ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
}
}
public function getAllowedParams() {
$allowedParams = $this->buildCommonApiParams( false ) + [
'suggest' => [
ParamValidator::PARAM_DEFAULT => false,
// Deprecated since 1.35
ParamValidator::PARAM_DEPRECATED => true,
],
'redirects' => [
ParamValidator::PARAM_TYPE => [ 'return', 'resolve' ],
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
ApiBase::PARAM_HELP_MSG_APPEND => [ 'apihelp-opensearch-param-redirects-append' ],
],
'format' => [
ParamValidator::PARAM_DEFAULT => 'json',
ParamValidator::PARAM_TYPE => [ 'json', 'jsonfm', 'xml', 'xmlfm' ],
],
'warningsaserror' => false,
];
// Use open search specific default limit
$allowedParams['limit'][ParamValidator::PARAM_DEFAULT] = $this->getConfig()->get(
MainConfigNames::OpenSearchDefaultLimit
);
return $allowedParams;
}
public function getSearchProfileParams() {
return [
'profile' => [
'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
'help-message' => 'apihelp-query+prefixsearch-param-profile'
],
];
}
protected function getExamplesMessages() {
return [
'action=opensearch&search=Te'
=> 'apihelp-opensearch-example-te',
];
}
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Opensearch';
}
/**
* Trim an extract to a sensible length.
*
* Adapted from Extension:OpenSearchXml, which adapted it from
* Extension:ActiveAbstract.
*
* @param string $text
* @param int $length Target length; actual result will continue to the end of a sentence.
* @return string
*/
public static function trimExtract( $text, $length ) {
static $regex = null;
if ( $regex === null ) {
$endchars = [
'([^\d])\.\s', '\!\s', '\?\s', // regular ASCII
'。', // full-width ideographic full-stop
'.', '!', '?', // double-width roman forms
'。', // half-width ideographic full stop
];
$endgroup = implode( '|', $endchars );
$end = "(?:$endgroup)";
$sentence = ".{{$length},}?$end+";
$regex = "/^($sentence)/u";
}
$matches = [];
if ( preg_match( $regex, $text, $matches ) ) {
return trim( $matches[1] );
} else {
// Just return the first line
return trim( explode( "\n", $text )[0] );
}
}
/**
* Fetch the template for a type.
*
* @param string $type MIME type
* @return string
*/
public static function getOpenSearchTemplate( $type ) {
$services = MediaWikiServices::getInstance();
$canonicalServer = $services->getMainConfig()->get( MainConfigNames::CanonicalServer );
$searchEngineConfig = $services->getSearchEngineConfig();
$ns = implode( '|', $searchEngineConfig->defaultNamespaces() );
if ( !$ns ) {
$ns = '0';
}
switch ( $type ) {
case 'application/x-suggestions+json':
return $canonicalServer .
wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
case 'application/x-suggestions+xml':
return $canonicalServer .
wfScript( 'api' ) .
'?action=opensearch&format=xml&search={searchTerms}&namespace=' . $ns;
default:
throw new InvalidArgumentException( __METHOD__ . ": Unknown type '$type'" );
}
}
}
/** @deprecated class alias since 1.43 */
class_alias( ApiOpenSearch::class, 'ApiOpenSearch' );
|