2006-10-14 07:18:08 +00:00
|
|
|
|
<?php
|
2010-02-23 18:05:46 +00:00
|
|
|
|
/**
|
2010-08-07 19:59:42 +00:00
|
|
|
|
* Created on Oct 13, 2006
|
|
|
|
|
|
*
|
2012-07-15 20:13:02 +00:00
|
|
|
|
* Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
|
2014-11-05 22:16:43 +00:00
|
|
|
|
* Copyright © 2008 Brion Vibber <brion@wikimedia.org>
|
|
|
|
|
|
* Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
|
2006-10-14 07:18:08 +00:00
|
|
|
|
*
|
|
|
|
|
|
* 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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-10-14 07:18:08 +00:00
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @file
|
2006-10-14 07:18:08 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
|
* @ingroup API
|
2007-04-20 08:55:14 +00:00
|
|
|
|
*/
|
2006-10-15 07:43:52 +00:00
|
|
|
|
class ApiOpenSearch extends ApiBase {
|
2006-10-14 07:18:08 +00:00
|
|
|
|
|
2014-11-05 22:16:43 +00:00
|
|
|
|
private $format = null;
|
|
|
|
|
|
private $fm = null;
|
|
|
|
|
|
|
2013-03-27 21:31:05 +00:00
|
|
|
|
/**
|
2014-11-05 22:16:43 +00:00
|
|
|
|
* Get the output format
|
2013-03-27 21:31:05 +00:00
|
|
|
|
*
|
2014-11-05 22:16:43 +00:00
|
|
|
|
* @return string
|
2013-03-27 21:31:05 +00:00
|
|
|
|
*/
|
2014-11-05 22:16:43 +00:00
|
|
|
|
protected function getFormat() {
|
|
|
|
|
|
if ( $this->format === null ) {
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
$format = $params['format'];
|
|
|
|
|
|
|
|
|
|
|
|
$allowedParams = $this->getAllowedParams();
|
|
|
|
|
|
if ( !in_array( $format, $allowedParams['format'][ApiBase::PARAM_TYPE] ) ) {
|
|
|
|
|
|
$format = $allowedParams['format'][ApiBase::PARAM_DFLT];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( substr( $format, -2 ) === 'fm' ) {
|
|
|
|
|
|
$this->format = substr( $format, 0, -2 );
|
|
|
|
|
|
$this->fm = 'fm';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->format = $format;
|
|
|
|
|
|
$this->fm = '';
|
|
|
|
|
|
}
|
2013-03-27 21:31:05 +00:00
|
|
|
|
}
|
2014-11-05 22:16:43 +00:00
|
|
|
|
return $this->format;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getCustomPrinter() {
|
2014-11-27 17:03:07 +00:00
|
|
|
|
switch ( $this->getFormat() ) {
|
2014-11-05 22:16:43 +00:00
|
|
|
|
case 'json':
|
2015-04-30 19:39:16 +00:00
|
|
|
|
return new ApiOpenSearchFormatJson(
|
|
|
|
|
|
$this->getMain(), $this->fm, $this->getParameter( 'warningsaserror' )
|
|
|
|
|
|
);
|
2013-11-14 12:47:20 +00:00
|
|
|
|
|
2014-11-05 22:16:43 +00:00
|
|
|
|
case 'xml':
|
|
|
|
|
|
$printer = $this->getMain()->createPrinterByName( 'xml' . $this->fm );
|
|
|
|
|
|
$printer->setRootElement( 'SearchSuggestion' );
|
|
|
|
|
|
return $printer;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
|
|
|
|
|
|
}
|
2006-10-14 07:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2007-07-15 00:52:35 +00:00
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
$search = $params['search'];
|
2008-01-30 01:07:49 +00:00
|
|
|
|
$limit = $params['limit'];
|
2008-04-15 23:06:28 +00:00
|
|
|
|
$namespaces = $params['namespace'];
|
2009-02-11 15:56:53 +00:00
|
|
|
|
$suggest = $params['suggest'];
|
2008-04-14 07:45:50 +00:00
|
|
|
|
|
2014-11-05 22:16:43 +00:00
|
|
|
|
if ( $params['redirects'] === null ) {
|
|
|
|
|
|
// Backwards compatibility, don't resolve for JSON.
|
|
|
|
|
|
$resolveRedir = $this->getFormat() !== 'json';
|
2010-02-23 18:05:46 +00:00
|
|
|
|
} else {
|
2014-11-05 22:16:43 +00:00
|
|
|
|
$resolveRedir = $params['redirects'] === 'resolve';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$results = [];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
if ( !$suggest || $this->getConfig()->get( 'EnableOpenSearchSuggest' ) ) {
|
2011-01-06 00:31:23 +00:00
|
|
|
|
// Open search results may be stored for a very long time
|
2014-01-24 02:51:11 +00:00
|
|
|
|
$this->getMain()->setCacheMaxAge( $this->getConfig()->get( 'SearchSuggestCacheExpiry' ) );
|
2010-07-23 07:17:56 +00:00
|
|
|
|
$this->getMain()->setCacheMode( 'public' );
|
2014-11-05 22:16:43 +00:00
|
|
|
|
$this->search( $search, $limit, $namespaces, $resolveRedir, $results );
|
|
|
|
|
|
|
|
|
|
|
|
// Allow hooks to populate extracts and images
|
2016-02-17 09:09:32 +00:00
|
|
|
|
Hooks::run( 'ApiOpenSearchSuggest', [ &$results ] );
|
2006-10-18 23:49:09 +00:00
|
|
|
|
|
2014-11-05 22:16:43 +00:00
|
|
|
|
// Trim extracts, if necessary
|
|
|
|
|
|
$length = $this->getConfig()->get( 'OpenSearchDescriptionLength' );
|
|
|
|
|
|
foreach ( $results as &$r ) {
|
|
|
|
|
|
if ( is_string( $r['extract'] ) && !$r['extract trimmed'] ) {
|
|
|
|
|
|
$r['extract'] = self::trimExtract( $r['extract'], $length );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2009-09-10 13:07:36 +00:00
|
|
|
|
}
|
2014-11-05 22:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
// Populate result object
|
|
|
|
|
|
$this->populateResult( $search, $results );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Perform the search
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $search Text to search
|
|
|
|
|
|
* @param int $limit Maximum items to return
|
|
|
|
|
|
* @param array $namespaces Namespaces to search
|
|
|
|
|
|
* @param bool $resolveRedir Whether to resolve redirects
|
2014-12-12 22:24:01 +00:00
|
|
|
|
* @param array &$results Put results here. Keys have to be integers.
|
2014-11-05 22:16:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
protected function search( $search, $limit, $namespaces, $resolveRedir, &$results ) {
|
2016-01-26 21:18:27 +00:00
|
|
|
|
|
|
|
|
|
|
$searchEngine = SearchEngine::create();
|
|
|
|
|
|
$searchEngine->setLimitOffset( $limit );
|
|
|
|
|
|
$searchEngine->setNamespaces( $namespaces );
|
|
|
|
|
|
$titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
|
|
|
|
|
|
|
2014-12-09 22:11:38 +00:00
|
|
|
|
if ( !$titles ) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2014-11-05 22:16:43 +00:00
|
|
|
|
|
2014-12-12 22:24:01 +00:00
|
|
|
|
// 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;
|
|
|
|
|
|
|
2014-11-05 22:16:43 +00:00
|
|
|
|
if ( $resolveRedir ) {
|
|
|
|
|
|
// Query for redirects
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$redirects = [];
|
2014-12-15 16:04:57 +00:00
|
|
|
|
$lb = new LinkBatch( $titles );
|
|
|
|
|
|
if ( !$lb->isEmpty() ) {
|
2015-11-07 21:10:23 +00:00
|
|
|
|
$db = $this->getDB();
|
2014-12-15 16:04:57 +00:00
|
|
|
|
$res = $db->select(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 'page', 'redirect' ],
|
|
|
|
|
|
[ 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ],
|
|
|
|
|
|
[
|
2014-12-15 16:04:57 +00:00
|
|
|
|
'rd_from = page_id',
|
|
|
|
|
|
'rd_interwiki IS NULL OR rd_interwiki = ' . $db->addQuotes( '' ),
|
|
|
|
|
|
$lb->constructSet( 'page', $db ),
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2014-12-15 16:04:57 +00:00
|
|
|
|
__METHOD__
|
|
|
|
|
|
);
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
|
$redirects[$row->page_namespace][$row->page_title] =
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ $row->rd_namespace, $row->rd_title ];
|
2014-12-15 16:04:57 +00:00
|
|
|
|
}
|
2014-11-05 22:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Bypass any redirects
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$seen = [];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
foreach ( $titles as $title ) {
|
|
|
|
|
|
$ns = $title->getNamespace();
|
|
|
|
|
|
$dbkey = $title->getDBkey();
|
|
|
|
|
|
$from = null;
|
|
|
|
|
|
if ( isset( $redirects[$ns][$dbkey] ) ) {
|
|
|
|
|
|
list( $ns, $dbkey ) = $redirects[$ns][$dbkey];
|
|
|
|
|
|
$from = $title;
|
|
|
|
|
|
$title = Title::makeTitle( $ns, $dbkey );
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( !isset( $seen[$ns][$dbkey] ) ) {
|
|
|
|
|
|
$seen[$ns][$dbkey] = true;
|
2015-11-07 21:10:23 +00:00
|
|
|
|
$resultId = $title->getArticleID();
|
2014-12-12 22:24:01 +00:00
|
|
|
|
if ( $resultId === 0 ) {
|
|
|
|
|
|
$resultId = $nextSpecialPageId;
|
|
|
|
|
|
$nextSpecialPageId -= 1;
|
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$results[$resultId] = [
|
2014-11-05 22:16:43 +00:00
|
|
|
|
'title' => $title,
|
|
|
|
|
|
'redirect from' => $from,
|
|
|
|
|
|
'extract' => false,
|
|
|
|
|
|
'extract trimmed' => false,
|
|
|
|
|
|
'image' => false,
|
2015-11-07 21:10:23 +00:00
|
|
|
|
'url' => wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ),
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
foreach ( $titles as $title ) {
|
2016-02-20 20:20:37 +00:00
|
|
|
|
$resultId = $title->getArticleID();
|
2014-12-12 22:24:01 +00:00
|
|
|
|
if ( $resultId === 0 ) {
|
|
|
|
|
|
$resultId = $nextSpecialPageId;
|
|
|
|
|
|
$nextSpecialPageId -= 1;
|
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$results[$resultId] = [
|
2014-11-05 22:16:43 +00:00
|
|
|
|
'title' => $title,
|
|
|
|
|
|
'redirect from' => null,
|
|
|
|
|
|
'extract' => false,
|
|
|
|
|
|
'extract trimmed' => false,
|
|
|
|
|
|
'image' => false,
|
2016-02-20 20:20:37 +00:00
|
|
|
|
'url' => wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ),
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param string $search
|
|
|
|
|
|
* @param array &$results
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function populateResult( $search, &$results ) {
|
2006-10-15 07:43:52 +00:00
|
|
|
|
$result = $this->getResult();
|
2014-11-05 22:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
switch ( $this->getFormat() ) {
|
|
|
|
|
|
case 'json':
|
|
|
|
|
|
// http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
|
2015-04-30 19:39:16 +00:00
|
|
|
|
$result->addArrayType( null, 'array' );
|
2014-11-05 22:16:43 +00:00
|
|
|
|
$result->addValue( null, 0, strval( $search ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$terms = [];
|
|
|
|
|
|
$descriptions = [];
|
|
|
|
|
|
$urls = [];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
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':
|
|
|
|
|
|
// http://msdn.microsoft.com/en-us/library/cc891508%28v=vs.85%29.aspx
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$imageKeys = [
|
2014-11-05 22:16:43 +00:00
|
|
|
|
'source' => true,
|
|
|
|
|
|
'alt' => true,
|
|
|
|
|
|
'width' => true,
|
|
|
|
|
|
'height' => true,
|
|
|
|
|
|
'align' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
|
|
|
|
|
$items = [];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
foreach ( $results as $r ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$item = [
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
|
'Text' => $r['title']->getPrefixedText(),
|
|
|
|
|
|
'Url' => $r['url'],
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
|
$item['Description'] = $r['extract'];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
|
|
|
|
|
|
$item['Image'] = array_intersect_key( $r['image'], $imageKeys );
|
|
|
|
|
|
}
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
|
ApiResult::setSubelementsList( $item, array_keys( $item ) );
|
2014-11-05 22:16:43 +00:00
|
|
|
|
$items[] = $item;
|
|
|
|
|
|
}
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
|
ApiResult::setIndexedTagName( $items, 'Item' );
|
2014-11-05 22:16:43 +00:00
|
|
|
|
$result->addValue( null, 'version', '2.0' );
|
|
|
|
|
|
$result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' );
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
|
$result->addValue( null, 'Query', strval( $search ) );
|
|
|
|
|
|
$result->addSubelementsList( null, 'Query' );
|
2014-11-05 22:16:43 +00:00
|
|
|
|
$result->addValue( null, 'Section', $items );
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
|
|
|
|
|
|
}
|
2006-10-14 07:18:08 +00:00
|
|
|
|
}
|
2006-10-18 23:49:09 +00:00
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
return [
|
2007-12-29 00:14:22 +00:00
|
|
|
|
'search' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
|
'limit' => [
|
2014-01-24 02:51:11 +00:00
|
|
|
|
ApiBase::PARAM_DFLT => $this->getConfig()->get( 'OpenSearchDefaultLimit' ),
|
2010-02-23 18:05:46 +00:00
|
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
|
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
|
|
ApiBase::PARAM_MAX => 100,
|
|
|
|
|
|
ApiBase::PARAM_MAX2 => 100
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
|
|
|
|
|
'namespace' => [
|
2010-02-23 18:05:46 +00:00
|
|
|
|
ApiBase::PARAM_DFLT => NS_MAIN,
|
|
|
|
|
|
ApiBase::PARAM_TYPE => 'namespace',
|
|
|
|
|
|
ApiBase::PARAM_ISMULTI => true
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2009-02-12 17:27:05 +00:00
|
|
|
|
'suggest' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
|
'redirects' => [
|
|
|
|
|
|
ApiBase::PARAM_TYPE => [ 'return', 'resolve' ],
|
|
|
|
|
|
],
|
|
|
|
|
|
'format' => [
|
2013-03-27 21:31:05 +00:00
|
|
|
|
ApiBase::PARAM_DFLT => 'json',
|
2016-02-17 09:09:32 +00:00
|
|
|
|
ApiBase::PARAM_TYPE => [ 'json', 'jsonfm', 'xml', 'xmlfm' ],
|
|
|
|
|
|
],
|
2015-04-30 19:39:16 +00:00
|
|
|
|
'warningsaserror' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2006-10-14 07:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
|
'action=opensearch&search=Te'
|
|
|
|
|
|
=> 'apihelp-opensearch-example-te',
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2006-10-14 07:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
|
public function getHelpUrls() {
|
2011-11-28 15:43:11 +00:00
|
|
|
|
return 'https://www.mediawiki.org/wiki/API:Opensearch';
|
2011-07-17 16:38:24 +00:00
|
|
|
|
}
|
2014-11-05 22:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Trim an extract to a sensible length.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Adapted from Extension:OpenSearchXml, which adapted it from
|
|
|
|
|
|
* Extension:ActiveAbstract.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $text
|
2015-11-07 21:25:04 +00:00
|
|
|
|
* @param int $length Target length; actual result will continue to the end of a sentence.
|
2014-11-05 22:16:43 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static function trimExtract( $text, $length ) {
|
|
|
|
|
|
static $regex = null;
|
|
|
|
|
|
|
|
|
|
|
|
if ( $regex === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$endchars = [
|
2014-11-05 22:16:43 +00:00
|
|
|
|
'([^\d])\.\s', '\!\s', '\?\s', // regular ASCII
|
|
|
|
|
|
'。', // full-width ideographic full-stop
|
|
|
|
|
|
'.', '!', '?', // double-width roman forms
|
|
|
|
|
|
'。', // half-width ideographic full stop
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
$endgroup = implode( '|', $endchars );
|
|
|
|
|
|
$end = "(?:$endgroup)";
|
|
|
|
|
|
$sentence = ".{{$length},}?$end+";
|
|
|
|
|
|
$regex = "/^($sentence)/u";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$matches = [];
|
2014-11-05 22:16:43 +00:00
|
|
|
|
if ( preg_match( $regex, $text, $matches ) ) {
|
|
|
|
|
|
return trim( $matches[1] );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Just return the first line
|
2016-02-17 19:54:59 +00:00
|
|
|
|
return trim( explode( "\n", $text )[0] );
|
2014-11-05 22:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Fetch the template for a type.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $type MIME type
|
|
|
|
|
|
* @return string
|
2014-12-24 13:49:20 +00:00
|
|
|
|
* @throws MWException
|
2014-11-05 22:16:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
public static function getOpenSearchTemplate( $type ) {
|
|
|
|
|
|
global $wgOpenSearchTemplate, $wgCanonicalServer;
|
|
|
|
|
|
|
|
|
|
|
|
if ( $wgOpenSearchTemplate && $type === 'application/x-suggestions+json' ) {
|
|
|
|
|
|
return $wgOpenSearchTemplate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$ns = implode( '|', SearchEngine::defaultNamespaces() );
|
|
|
|
|
|
if ( !$ns ) {
|
|
|
|
|
|
$ns = "0";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch ( $type ) {
|
|
|
|
|
|
case 'application/x-suggestions+json':
|
|
|
|
|
|
return $wgCanonicalServer . wfScript( 'api' )
|
|
|
|
|
|
. '?action=opensearch&search={searchTerms}&namespace=' . $ns;
|
|
|
|
|
|
|
|
|
|
|
|
case 'application/x-suggestions+xml':
|
|
|
|
|
|
return $wgCanonicalServer . wfScript( 'api' )
|
|
|
|
|
|
. '?action=opensearch&format=xml&search={searchTerms}&namespace=' . $ns;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new MWException( __METHOD__ . ": Unknown type '$type'" );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2006-10-14 07:18:08 +00:00
|
|
|
|
}
|
2015-04-30 19:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
class ApiOpenSearchFormatJson extends ApiFormatJson {
|
|
|
|
|
|
private $warningsAsError = false;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct( ApiMain $main, $fm, $warningsAsError ) {
|
|
|
|
|
|
parent::__construct( $main, "json$fm" );
|
|
|
|
|
|
$this->warningsAsError = $warningsAsError;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
|
if ( !$this->getResult()->getResultData( 'error' ) ) {
|
2015-05-28 14:38:19 +00:00
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
|
|
|
|
|
|
|
// Ignore warnings or treat as errors, as requested
|
|
|
|
|
|
$warnings = $result->removeValue( 'warnings', null );
|
2015-04-30 19:39:16 +00:00
|
|
|
|
if ( $this->warningsAsError && $warnings ) {
|
|
|
|
|
|
$this->dieUsage(
|
|
|
|
|
|
'Warnings cannot be represented in OpenSearch JSON format', 'warnings', 0,
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 'warnings' => $warnings ]
|
2015-04-30 19:39:16 +00:00
|
|
|
|
);
|
|
|
|
|
|
}
|
2015-05-28 14:38:19 +00:00
|
|
|
|
|
|
|
|
|
|
// Ignore any other unexpected keys (e.g. from $wgDebugToolbar)
|
|
|
|
|
|
$remove = array_keys( array_diff_key(
|
|
|
|
|
|
$result->getResultData(),
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 0 => 'search', 1 => 'terms', 2 => 'descriptions', 3 => 'urls' ]
|
2015-05-28 14:38:19 +00:00
|
|
|
|
) );
|
|
|
|
|
|
foreach ( $remove as $key ) {
|
|
|
|
|
|
$result->removeValue( $key, null );
|
|
|
|
|
|
}
|
2015-04-30 19:39:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parent::execute();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|