ApiOpenSearch.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * Created on Oct 13, 2006
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiBase.php");
  27. }
  28. /**
  29. * @ingroup API
  30. */
  31. class ApiOpenSearch extends ApiBase {
  32. public function __construct($main, $action) {
  33. parent :: __construct($main, $action);
  34. }
  35. public function getCustomPrinter() {
  36. return $this->getMain()->createPrinterByName('json');
  37. }
  38. public function execute() {
  39. global $wgEnableMWSuggest;
  40. $params = $this->extractRequestParams();
  41. $search = $params['search'];
  42. $limit = $params['limit'];
  43. $namespaces = $params['namespace'];
  44. $suggest = $params['suggest'];
  45. # $wgEnableMWSuggest hit incoming when $wgEnableMWSuggest is disabled
  46. if( $suggest && !$wgEnableMWSuggest ) return;
  47. // Open search results may be stored for a very long time
  48. $this->getMain()->setCacheMaxAge(1200);
  49. $srchres = PrefixSearch::titleSearch( $search, $limit, $namespaces );
  50. // Set top level elements
  51. $result = $this->getResult();
  52. $result->addValue(null, 0, $search);
  53. $result->addValue(null, 1, $srchres);
  54. }
  55. public function getAllowedParams() {
  56. return array (
  57. 'search' => null,
  58. 'limit' => array(
  59. ApiBase :: PARAM_DFLT => 10,
  60. ApiBase :: PARAM_TYPE => 'limit',
  61. ApiBase :: PARAM_MIN => 1,
  62. ApiBase :: PARAM_MAX => 100,
  63. ApiBase :: PARAM_MAX2 => 100
  64. ),
  65. 'namespace' => array(
  66. ApiBase :: PARAM_DFLT => NS_MAIN,
  67. ApiBase :: PARAM_TYPE => 'namespace',
  68. ApiBase :: PARAM_ISMULTI => true
  69. ),
  70. 'suggest' => false,
  71. );
  72. }
  73. public function getParamDescription() {
  74. return array (
  75. 'search' => 'Search string',
  76. 'limit' => 'Maximum amount of results to return',
  77. 'namespace' => 'Namespaces to search',
  78. 'suggest' => 'Do nothing if $wgEnableMWSuggest is false',
  79. );
  80. }
  81. public function getDescription() {
  82. return 'This module implements OpenSearch protocol';
  83. }
  84. protected function getExamples() {
  85. return array (
  86. 'api.php?action=opensearch&search=Te'
  87. );
  88. }
  89. public function getVersion() {
  90. return __CLASS__ . ': $Id: ApiOpenSearch.php 47188 2009-02-12 17:27:05Z catrope $';
  91. }
  92. }