r31471 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r31470‎ | r31471 | r31472 >
Date:21:45, 2 March 2008
Author:btongminh
Status:old
Tags:
Comment:
Add API module to query Lucene (bug 10908)
Modified paths:
  • /trunk/extensions/LuceneSearch/ApiQueryLuceneSearch.php (added) (history)
  • /trunk/extensions/LuceneSearch/LuceneSearch.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LuceneSearch/ApiQueryLuceneSearch.php
@@ -0,0 +1,134 @@
 2+<?php
 3+
 4+/*
 5+ * Created on July 30, 2007
 6+ *
 7+ * API for MediaWiki 1.8+
 8+ *
 9+ * Copyright (C) 2008 Bryan Tong Minh Bryan.TongMinh@gmail.com
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ */
 26+
 27+require_once('LuceneSearch_body.php');
 28+
 29+/**
 30+ * Query module to perform full text search within wiki titles and content
 31+ *
 32+ * @addtogroup API
 33+ */
 34+class ApiQueryLuceneSearch extends ApiQueryGeneratorBase {
 35+
 36+ public function __construct($query, $moduleName) {
 37+ // Keep the prefix compatible with list=search
 38+ parent :: __construct($query, $moduleName, 'sr');
 39+ }
 40+
 41+ public function execute() {
 42+ $this->run();
 43+ }
 44+
 45+ public function executeGenerator($resultPageSet) {
 46+ $this->run($resultPageSet);
 47+ }
 48+
 49+ private function run($resultPageSet = null) {
 50+
 51+ $params = $this->extractRequestParams();
 52+
 53+ $limit = $params['limit'];
 54+ $offset = $params['offset'];
 55+ $query = $params['search'];
 56+ $namespaces = $params['namespace'];
 57+
 58+ if (is_null($query) || empty($query))
 59+ $this->dieUsage("empty search string is not allowed", 'param-search');
 60+
 61+ $results = LuceneSearchSet::newFromQuery( 'search', $query, $namespaces, $limit, $offset, 'ignore' );
 62+
 63+ if ($results === false) {
 64+ $this->dieUsage( 'There was a problem with Lucene backend', 'lucene-backend' );
 65+ }
 66+
 67+ $data = $results->iterateResults( 'ApiQueryLuceneSearch::formatItem', is_null($resultPageSet) );
 68+
 69+ if ( $results->getTotalHits() >= ($params['offset'] + $params['limit']) )
 70+ $this->setContinueEnumParameter('offset', $params['offset'] + $params['limit']);
 71+
 72+ if (is_null($resultPageSet)) {
 73+ $result = $this->getResult();
 74+ $result->setIndexedTagName($data, 'p');
 75+ $result->addValue('query', $this->getModuleName(), $data);
 76+ } else {
 77+ $resultPageSet->populateFromTitles($data);
 78+ }
 79+ }
 80+
 81+ public static function formatItem( $result, $asArray ) {
 82+ $title = $result->getTitle();
 83+ if ( $asArray ) {
 84+ return array(
 85+ 'ns' => intval($title->getNamespace()),
 86+ 'title' => $title->getPrefixedText());
 87+ } else {
 88+ return $title;
 89+ }
 90+ }
 91+
 92+ public function getAllowedParams() {
 93+ return array (
 94+ 'search' => null,
 95+ 'namespace' => array (
 96+ ApiBase :: PARAM_DFLT => 0,
 97+ ApiBase :: PARAM_TYPE => 'namespace',
 98+ ApiBase :: PARAM_ISMULTI => true,
 99+ ),
 100+ 'offset' => 0,
 101+ 'limit' => array (
 102+ ApiBase :: PARAM_DFLT => 10,
 103+ ApiBase :: PARAM_TYPE => 'limit',
 104+ ApiBase :: PARAM_MIN => 1,
 105+ ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
 106+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 107+ )
 108+ );
 109+ }
 110+
 111+ public function getParamDescription() {
 112+ return array (
 113+ 'search' => 'Search for all page titles (or content) that has this value.',
 114+ 'namespace' => 'The namespace(s) to enumerate.',
 115+ 'offset' => 'Use this value to continue paging (return by query)',
 116+ 'limit' => 'How many total pages to return.'
 117+ );
 118+ }
 119+
 120+ public function getDescription() {
 121+ return 'Perform a full text search';
 122+ }
 123+
 124+ protected function getExamples() {
 125+ return array (
 126+ 'api.php?action=query&list=search&srsearch=meaning',
 127+ 'api.php?action=query&generator=search&prop=info',
 128+ );
 129+ }
 130+
 131+ public function getVersion() {
 132+ return __CLASS__ . ': $Id: $';
 133+ }
 134+}
 135+
Property changes on: trunk/extensions/LuceneSearch/ApiQueryLuceneSearch.php
___________________________________________________________________
Added: svn:eol-style
1136 + native
Index: trunk/extensions/LuceneSearch/LuceneSearch.php
@@ -85,3 +85,8 @@
8686
8787 $wgExtensionMessagesFiles['LuceneSearch'] = dirname(__FILE__) . '/LuceneSearch.i18n.php';
8888 $wgAutoloadClasses['LuceneResult'] = dirname(__FILE__) . '/LuceneSearch_body.php';
 89+
 90+$wgAutoloadClasses['ApiQueryLuceneSearch'] = dirname(__FILE__) . '/ApiQueryLuceneSearch.php';
 91+// Override the default search engine
 92+$wgApiQueryListModules['search'] = 'ApiQueryLuceneSearch';
 93+

Status & tagging log

  NODES
os 3
text 3