ApiQueryPrefixSearch.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @since 1.23
  20. */
  21. /**
  22. * @ingroup API
  23. */
  24. class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
  25. use SearchApi;
  26. /** @var array list of api allowed params */
  27. private $allowedParams;
  28. public function __construct( $query, $moduleName ) {
  29. parent::__construct( $query, $moduleName, 'ps' );
  30. }
  31. public function execute() {
  32. $this->run();
  33. }
  34. public function executeGenerator( $resultPageSet ) {
  35. $this->run( $resultPageSet );
  36. }
  37. /**
  38. * @param ApiPageSet $resultPageSet
  39. */
  40. private function run( $resultPageSet = null ) {
  41. $params = $this->extractRequestParams();
  42. $search = $params['search'];
  43. $limit = $params['limit'];
  44. $offset = $params['offset'];
  45. $searchEngine = $this->buildSearchEngine( $params );
  46. $suggestions = $searchEngine->completionSearchWithVariants( $search );
  47. $titles = $searchEngine->extractTitles( $suggestions );
  48. if ( $suggestions->hasMoreResults() ) {
  49. $this->setContinueEnumParameter( 'offset', $offset + $limit );
  50. }
  51. if ( $resultPageSet ) {
  52. $resultPageSet->setRedirectMergePolicy( function ( array $current, array $new ) {
  53. if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
  54. $current['index'] = $new['index'];
  55. }
  56. return $current;
  57. } );
  58. $resultPageSet->populateFromTitles( $titles );
  59. foreach ( $titles as $index => $title ) {
  60. $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
  61. }
  62. } else {
  63. $result = $this->getResult();
  64. $count = 0;
  65. foreach ( $titles as $title ) {
  66. $vals = [
  67. 'ns' => (int)$title->getNamespace(),
  68. 'title' => $title->getPrefixedText(),
  69. ];
  70. if ( $title->isSpecialPage() ) {
  71. $vals['special'] = true;
  72. } else {
  73. $vals['pageid'] = (int)$title->getArticleID();
  74. }
  75. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  76. ++$count;
  77. if ( !$fit ) {
  78. $this->setContinueEnumParameter( 'offset', $offset + $count );
  79. break;
  80. }
  81. }
  82. $result->addIndexedTagName(
  83. [ 'query', $this->getModuleName() ], $this->getModulePrefix()
  84. );
  85. }
  86. }
  87. public function getCacheMode( $params ) {
  88. return 'public';
  89. }
  90. public function getAllowedParams() {
  91. if ( $this->allowedParams !== null ) {
  92. return $this->allowedParams;
  93. }
  94. $this->allowedParams = $this->buildCommonApiParams();
  95. return $this->allowedParams;
  96. }
  97. public function getSearchProfileParams() {
  98. return [
  99. 'profile' => [
  100. 'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
  101. 'help-message' => 'apihelp-query+prefixsearch-param-profile',
  102. ],
  103. ];
  104. }
  105. protected function getExamplesMessages() {
  106. return [
  107. 'action=query&list=prefixsearch&pssearch=meaning'
  108. => 'apihelp-query+prefixsearch-example-simple',
  109. ];
  110. }
  111. public function getHelpUrls() {
  112. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';
  113. }
  114. }