2006-10-14 07:18:08 +00:00
|
|
|
<?php
|
2010-02-23 18:05:46 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2006-10-14 07:18:08 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Oct 13, 2006
|
|
|
|
|
*
|
2010-02-23 18:05:46 +00:00
|
|
|
* Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
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
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
public function __construct( $main, $action ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
parent::__construct( $main, $action );
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-16 00:08:03 +00:00
|
|
|
public function getCustomPrinter() {
|
2010-01-11 15:55:52 +00:00
|
|
|
return $this->getMain()->createPrinterByName( 'json' );
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2010-02-15 20:42:43 +00:00
|
|
|
global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
|
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
|
|
|
|
2010-02-15 20:42:43 +00:00
|
|
|
// MWSuggest or similar hit
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $suggest && !$wgEnableOpenSearchSuggest ) {
|
2011-01-06 00:31:23 +00:00
|
|
|
$searches = array();
|
2010-02-23 18:05:46 +00:00
|
|
|
} else {
|
2011-01-06 00:31:23 +00:00
|
|
|
// Open search results may be stored for a very long time
|
2010-02-14 23:52:45 +00:00
|
|
|
$this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
|
2010-07-23 07:17:56 +00:00
|
|
|
$this->getMain()->setCacheMode( 'public' );
|
2006-10-18 23:49:09 +00:00
|
|
|
|
2011-01-06 00:31:23 +00:00
|
|
|
$searches = PrefixSearch::titleSearch( $search, $limit,
|
2009-09-10 13:07:36 +00:00
|
|
|
$namespaces );
|
2011-07-17 16:38:24 +00:00
|
|
|
|
2011-01-05 16:21:01 +00:00
|
|
|
// if the content language has variants, try to retrieve fallback results
|
2011-01-06 00:31:23 +00:00
|
|
|
$fallbackLimit = $limit - count( $searches );
|
|
|
|
|
if ( $fallbackLimit > 0 ) {
|
2011-01-05 16:21:01 +00:00
|
|
|
global $wgContLang;
|
2011-01-06 00:31:23 +00:00
|
|
|
|
|
|
|
|
$fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
|
|
|
|
|
$fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
|
|
|
|
|
|
|
|
|
|
foreach ( $fallbackSearches as $fbs ) {
|
|
|
|
|
$fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
|
2011-01-05 16:21:01 +00:00
|
|
|
$namespaces );
|
2011-01-06 00:31:23 +00:00
|
|
|
$searches = array_merge( $searches, $fallbackSearchResult );
|
|
|
|
|
$fallbackLimit -= count( $fallbackSearchResult );
|
|
|
|
|
|
|
|
|
|
if ( $fallbackLimit == 0 ) {
|
2011-01-05 16:21:01 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-10 13:07:36 +00:00
|
|
|
}
|
2006-10-15 07:43:52 +00:00
|
|
|
// Set top level elements
|
|
|
|
|
$result = $this->getResult();
|
2010-01-11 15:55:52 +00:00
|
|
|
$result->addValue( null, 0, $search );
|
2011-01-06 00:31:23 +00:00
|
|
|
$result->addValue( null, 1, $searches );
|
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() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2007-12-29 00:14:22 +00:00
|
|
|
'search' => null,
|
2009-02-11 15:56:53 +00:00
|
|
|
'limit' => array(
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_DFLT => 10,
|
|
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
|
ApiBase::PARAM_MAX => 100,
|
|
|
|
|
ApiBase::PARAM_MAX2 => 100
|
2008-04-15 23:06:28 +00:00
|
|
|
),
|
|
|
|
|
'namespace' => array(
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_DFLT => NS_MAIN,
|
|
|
|
|
ApiBase::PARAM_TYPE => 'namespace',
|
|
|
|
|
ApiBase::PARAM_ISMULTI => true
|
2008-04-15 23:06:28 +00:00
|
|
|
),
|
2009-02-12 17:27:05 +00:00
|
|
|
'suggest' => false,
|
2006-10-14 07:18:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2007-12-29 00:14:22 +00:00
|
|
|
'search' => 'Search string',
|
2008-04-15 23:06:28 +00:00
|
|
|
'limit' => 'Maximum amount of results to return',
|
|
|
|
|
'namespace' => 'Namespaces to search',
|
2009-10-21 18:44:58 +00:00
|
|
|
'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
|
2006-10-14 07:18:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2011-08-14 09:19:54 +00:00
|
|
|
return 'Search the wiki using the OpenSearch protocol';
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2006-10-16 00:08:03 +00:00
|
|
|
'api.php?action=opensearch&search=Te'
|
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
|
|
|
}
|
|
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
public function getVersion() {
|
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
|
}
|
|
|
|
|
}
|