예제 #1
0
 private function run($resultPageSet = null)
 {
     $params = $this->extractRequestParams();
     $limit = $params['limit'];
     $offset = $params['offset'];
     $query = $params['search'];
     $namespaces = $params['namespace'];
     if (is_null($query) || empty($query)) {
         $this->dieUsage("empty search string is not allowed", 'param-search');
     }
     $results = LuceneSearchSet::newFromQuery('search', $query, $namespaces, $limit, $offset, 'ignore');
     if ($results === false) {
         $this->dieUsage('There was a problem with Lucene backend', 'lucene-backend');
     }
     $data = array_values($results->iterateResults(array('ApiQueryLuceneSearch', 'formatItem'), is_null($resultPageSet)));
     if ($results->getTotalHits() >= $params['offset'] + $params['limit']) {
         $this->setContinueEnumParameter('offset', $params['offset'] + $params['limit']);
     }
     if (is_null($resultPageSet)) {
         $result = $this->getResult();
         $result->setIndexedTagName($data, 'p');
         $result->addValue('query', $this->getModuleName(), $data);
     } else {
         $resultPageSet->populateFromTitles($data);
     }
 }
 /**
  *  PrefixSearchBackend override for OpenSearch results
  */
 static function prefixSearch($ns, $search, $limit, &$results)
 {
     $it = LuceneSearchSet::newFromQuery('prefix', $search, $ns, $limit, 0);
     $results = array();
     if ($it) {
         // $it can be null
         while ($res = $it->next()) {
             $results[] = $res->getTitle()->getPrefixedText();
         }
     }
     return false;
 }
예제 #3
0
 function execute($par)
 {
     global $wgRequest, $wgOut, $wgTitle, $wgContLang, $wgUser, $wgScriptPath, $wgLuceneDisableTitleMatches, $wgLuceneDisableSuggestions, $wgUser;
     global $wgGoToEdit;
     wfLoadExtensionMessages('LuceneSearch');
     $fname = 'LuceneSearch::execute';
     wfProfileIn($fname);
     $this->setHeaders();
     $wgOut->addHTML('<!-- titlens = ' . $wgTitle->getNamespace() . '-->');
     foreach (SearchEngine::searchableNamespaces() as $ns => $name) {
         if ($wgRequest->getCheck('ns' . $ns)) {
             $this->namespaces[] = $ns;
         }
     }
     if (count($this->namespaces) == 0) {
         foreach (SearchEngine::searchableNamespaces() as $ns => $name) {
             if ($wgUser->getOption('searchNs' . $ns)) {
                 $this->namespaces[] = $ns;
             }
         }
         if (count($this->namespaces) == 0) {
             global $wgNamespacesToBeSearchedDefault;
             foreach ($wgNamespacesToBeSearchedDefault as $ns => $searchit) {
                 if ($searchit) {
                     $this->namespaces[] = $ns;
                 }
             }
         }
     }
     $bits = split('/', $wgRequest->getVal('title'), 2);
     if (!empty($bits[1])) {
         $q = str_replace('_', ' ', $bits[1]);
     } else {
         $q = $wgRequest->getText('search');
     }
     list($limit, $offset) = $wgRequest->getLimitOffset(LS_PER_PAGE, 'searchlimit');
     if ($wgRequest->getVal('gen') == 'titlematch') {
         $this->sendTitlePrefixes($q, $limit);
         wfProfileOut($fname);
         return;
     }
     $this->mSkin =& $wgUser->getSkin();
     if (!$wgLuceneDisableSuggestions) {
         $wgOut->addHTML($this->makeSuggestJS());
     }
     $wgOut->addLink(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'screen,projection', 'href' => $wgScriptPath . '/extensions/LuceneSearch/lucenesearch.css'));
     $wgOut->addWikiText(wfMsg('searchresulttext'));
     $wgOut->addHTML($this->showShortDialog($q));
     if ($q === false || strlen($q) == 0) {
         // No search active. Put input focus in the search box.
         $wgOut->addHTML($this->makeFocusJS());
     } else {
         if (!$wgRequest->getText('fulltext')) {
             $t = SearchEngine::getNearMatch($q);
             if (!is_null($t)) {
                 $wgOut->redirect($t->getFullURL());
                 wfProfileOut($fname);
                 return;
             }
         }
         # No match, generate an edit URL
         $t = Title::newFromText($q);
         if (!$wgRequest->getText('go') || is_null($t)) {
             $editurl = '';
             # hrm...
         } else {
             wfRunHooks('SpecialSearchNogomatch', array(&$t));
             # If the feature is enabled, go straight to the edit page
             if ($wgGoToEdit) {
                 $wgOut->redirect($t->getFullURL('action=edit'));
                 return;
             }
             if ($t->quickUserCan('create') && $t->quickUserCan('edit')) {
                 $wgOut->addWikiText(wfMsg('noexactmatch', $t->getPrefixedText()));
             } else {
                 $wgOut->addWikiText(wfMsg('noexactmatch-nocreate', $t->getPrefixedText()));
             }
         }
         $case = 'ignore';
         # Replace localized namespace prefixes (from lucene-search 2.0)
         global $wgLuceneSearchVersion;
         if ($wgLuceneSearchVersion >= 2) {
             $searchq = $this->replacePrefixes($q);
             if ($wgRequest->getText('fulltext') == wfMsg('searchexactcase')) {
                 $case = 'exact';
             }
         } else {
             $searchq = $q;
         }
         global $wgDisableTextSearch;
         if (!$wgDisableTextSearch) {
             $results = LuceneSearchSet::newFromQuery('search', $searchq, $this->namespaces, $limit, $offset, $case);
         }
         if ($wgDisableTextSearch || $results === false) {
             if ($wgDisableTextSearch) {
                 $wgOut->addHTML(wfMsg('searchdisabled'));
             } else {
                 $wgOut->addWikiText(wfMsg('lucenefallback'));
             }
             $wgOut->addHTML(wfMsg('googlesearch', htmlspecialchars($q), 'utf-8', htmlspecialchars(wfMsg('search'))));
             wfProfileOut($fname);
             return;
         }
         $subtitleMsg = is_object(Title::newFromText($q)) ? 'searchsubtitle' : 'searchsubtitleinvalid';
         $wgOut->setSubtitle($wgOut->parse(wfMsg($subtitleMsg, wfEscapeWikiText($q))));
         // If the search returned no results, an alternate fuzzy search
         // match may be displayed as a suggested search. Link it.
         if ($results->hasSuggestion()) {
             $suggestion = $results->getSuggestion();
             $o = ' ' . wfMsg('searchdidyoumean', $this->makeLink($suggestion, $offset, $limit, $case), htmlspecialchars($suggestion));
             $wgOut->addHTML('<div style="text-align: center;">' . $o . '</div>');
         }
         $nmtext = '';
         if ($offset == 0 && !$wgLuceneDisableTitleMatches) {
             $titles = LuceneSearchSet::newFromQuery('titlematch', $q, $this->namespaces, 5, $case);
             if ($titles && $titles->hasResults()) {
                 $nmtext = '<p>' . wfMsg('searchnearmatches') . '</p>';
                 $nmtext .= '<ul>';
                 $nmtext .= implode("\n", $titles->iterateResults(array(&$this, 'formatNearMatch')));
                 $nmtext .= '</ul>';
                 $nmtext .= '<hr />';
             }
         }
         $wgOut->addHTML($nmtext);
         if (!$results->hasResults()) {
             # Pass search terms back in a few different formats
             # $1: Plain search terms
             # $2: Search terms with s/ /_/
             # $3: URL-encoded search terms
             $tmsg = array(htmlspecialchars($q), htmlspecialchars(str_replace(' ', '_', $q)), wfUrlEncode($q));
             $wgOut->addHtml(wfMsgWikiHtml('searchnoresults', $tmsg[0], $tmsg[1], $tmsg[2]));
         } else {
             #$showresults = min($limit, count($results)-$numresults);
             $i = $offset;
             $resq = trim(preg_replace("/[ |\\[\\]()\"{}+]+/", " ", $q));
             $contextWords = implode("|", array_map(array(&$this, 'regexQuote'), $wgContLang->convertForSearchResult(split(" ", $resq))));
             $top = wfMsg('searchnumber', $offset + 1, min($results->getTotalHits(), $offset + $limit), $results->getTotalHits());
             $out = '<ul id="lucene-results">';
             $numchunks = ceil($results->getTotalHits() / $limit);
             $whichchunk = $offset / $limit;
             $prevnext = "";
             if ($whichchunk > 0) {
                 $prevnext .= '<a href="' . $this->makelink($q, $offset - $limit, $limit, $case) . '">' . wfMsg('searchprev') . '</a> ';
             }
             $first = max($whichchunk - 11, 0);
             $last = min($numchunks, $whichchunk + 11);
             //$wgOut->addWikiText("whichchunk=$whichchunk numchunks=$numchunks first=$first last=$last num=".count($chunks)." limit=$limit offset=$offset results=".count($results)."\n\n");
             if ($last - $first > 1) {
                 for ($i = $first; $i < $last; $i++) {
                     if ($i === $whichchunk) {
                         $prevnext .= '<strong>' . ($i + 1) . '</strong> ';
                     } else {
                         $prevnext .= '<a href="' . $this->makelink($q, $limit * $i, $limit, $case) . '">' . ($i + 1) . '</a> ';
                     }
                 }
             }
             if ($whichchunk < $last - 1) {
                 $prevnext .= '<a href="' . $this->makelink($q, $offset + $limit, $limit, $case) . '">' . wfMsg('searchnext') . '</a> ';
             }
             $prevnext = '<div style="text-align: center;">' . $prevnext . '</div>';
             $top .= $prevnext;
             $out .= implode("\n", $results->iterateResults(array(&$this, 'showHit'), $contextWords));
             $out .= '</ul>';
         }
         #$wgOut->addHTML('<hr />');
         if (isset($top)) {
             $wgOut->addHTML($top);
         }
         if (isset($out)) {
             $wgOut->addHTML($out);
         }
         #if( isset( $prevnext ) ) $wgOut->addHTML('<hr />' . $prevnext);
         if (isset($prevnext)) {
             $wgOut->addHTML($prevnext);
         }
         $wgOut->addHTML($this->showFullDialog($q));
     }
     $wgOut->setRobotpolicy('noindex,nofollow');
     $wgOut->setArticleRelated(false);
     wfProfileOut($fname);
 }