2006-10-14 07:18:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on Oct 13, 2006
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2007-05-20 23:31:44 +00:00
|
|
|
* Copyright (C) 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.,
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (!defined('MEDIAWIKI')) {
|
|
|
|
|
// Eclipse helper - will be ignored in production
|
2006-10-16 00:08:03 +00:00
|
|
|
require_once ("ApiBase.php");
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
|
|
|
|
* @addtogroup API
|
|
|
|
|
*/
|
2006-10-15 07:43:52 +00:00
|
|
|
class ApiOpenSearch extends ApiBase {
|
2006-10-14 07:18:08 +00:00
|
|
|
|
|
|
|
|
public function __construct($main, $action) {
|
2006-10-14 16:02:42 +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() {
|
2006-10-15 07:43:52 +00:00
|
|
|
return $this->getMain()->createPrinterByName('json');
|
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'];
|
2007-12-29 00:14:22 +00:00
|
|
|
$limit = intval( $params['limit'] );
|
|
|
|
|
if( $limit < 1 || $limit > 100 ) {
|
|
|
|
|
$limit = 10;
|
|
|
|
|
}
|
2006-10-18 23:49:09 +00:00
|
|
|
|
2006-10-22 23:45:20 +00:00
|
|
|
// Open search results may be stored for a very long time
|
|
|
|
|
$this->getMain()->setCacheMaxAge(1200);
|
|
|
|
|
|
2006-10-18 23:49:09 +00:00
|
|
|
$title = Title :: newFromText($search);
|
|
|
|
|
if(!$title)
|
|
|
|
|
return; // Return empty result
|
|
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
// Prepare nested request
|
2007-07-15 00:52:35 +00:00
|
|
|
$req = new FauxRequest(array (
|
2006-10-14 07:18:08 +00:00
|
|
|
'action' => 'query',
|
|
|
|
|
'list' => 'allpages',
|
2006-10-18 23:49:09 +00:00
|
|
|
'apnamespace' => $title->getNamespace(),
|
2007-12-29 00:14:22 +00:00
|
|
|
'aplimit' => $limit,
|
2006-10-18 23:49:09 +00:00
|
|
|
'apprefix' => $title->getDBkey()
|
2006-10-14 07:18:08 +00:00
|
|
|
));
|
2006-10-18 23:49:09 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
// Execute
|
2007-07-15 00:52:35 +00:00
|
|
|
$module = new ApiMain($req);
|
2006-10-14 07:18:08 +00:00
|
|
|
$module->execute();
|
2006-10-18 23:49:09 +00:00
|
|
|
|
2006-10-20 07:10:18 +00:00
|
|
|
// Get resulting data
|
2006-10-25 03:54:56 +00:00
|
|
|
$data = $module->getResultData();
|
2006-10-18 23:49:09 +00:00
|
|
|
|
2006-10-15 07:43:52 +00:00
|
|
|
// Reformat useful data for future printing by JSON engine
|
2006-10-18 23:49:09 +00:00
|
|
|
$srchres = array ();
|
2006-11-23 08:25:56 +00:00
|
|
|
foreach ($data['query']['allpages'] as & $pageinfo) {
|
2006-10-16 00:08:03 +00:00
|
|
|
// Note: this data will no be printable by the xml engine
|
|
|
|
|
// because it does not support lists of unnamed items
|
2006-10-15 07:43:52 +00:00
|
|
|
$srchres[] = $pageinfo['title'];
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
2006-10-18 23:49:09 +00:00
|
|
|
|
2006-10-15 07:43:52 +00:00
|
|
|
// Set top level elements
|
|
|
|
|
$result = $this->getResult();
|
2006-10-16 00:08:03 +00:00
|
|
|
$result->addValue(null, 0, $search);
|
2006-10-15 07:43:52 +00:00
|
|
|
$result->addValue(null, 1, $srchres);
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
2006-10-18 23:49:09 +00:00
|
|
|
|
|
|
|
|
protected function getAllowedParams() {
|
2006-10-14 07:18:08 +00:00
|
|
|
return array (
|
2007-12-29 00:14:22 +00:00
|
|
|
'search' => null,
|
|
|
|
|
'limit' => 10
|
2006-10-14 07:18:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-18 23:49:09 +00:00
|
|
|
protected function getParamDescription() {
|
2006-10-14 07:18:08 +00:00
|
|
|
return array (
|
2007-12-29 00:14:22 +00:00
|
|
|
'search' => 'Search string',
|
|
|
|
|
'limit' => 'Optional limit (default 10)'
|
2006-10-14 07:18:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-18 23:49:09 +00:00
|
|
|
protected function getDescription() {
|
2006-10-14 07:18:08 +00:00
|
|
|
return 'This module implements OpenSearch protocol';
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-18 23:49:09 +00:00
|
|
|
protected function getExamples() {
|
2006-10-14 07:18:08 +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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-06-29 01:19:14 +00:00
|
|
|
|