/** * Subroutine for optionally prepending article match to result array. * @return WikiaSearchResultSet provides fluent interface */ public function prependArticleMatchIfExists() { wfProfileIn(__METHOD__); if (!($this->searchConfig->hasArticleMatch() && $this->resultsStart == 0)) { return $this; } $articleMatch = $this->searchConfig->getArticleMatch(); $article = $articleMatch->getCanonicalArticle(); $title = $article->getTitle(); $articleId = $article->getID(); if (!in_array($title->getNamespace(), $this->searchConfig->getNamespaces())) { // we had an article match by name, but not in our desired namespaces return $this; } $articleMatchId = sprintf('%s_%s', $this->wg->CityId, $articleId); $articleService = F::build('ArticleService', array($articleId)); $firstRev = $title->getFirstRevision(); $created = $firstRev ? wfTimestamp(TS_ISO_8601, $firstRev->getTimestamp()) : ''; $lastRev = Revision::newFromId($title->getLatestRevID()); $touched = $lastRev ? wfTimeStamp(TS_ISO_8601, $lastRev->getTimestamp()) : ''; $fieldsArray = array('wid' => $this->wg->CityId, 'title' => $article->mTitle, 'url' => urldecode($title->getFullUrl()), 'score' => 'PTT', 'isArticleMatch' => true, 'ns' => $title->getNamespace(), 'pageId' => $article->getID(), 'created' => $created, 'touched' => $touched); //@TODO: we could put categories ^^ here but we aren't really using them yet $result = F::build('WikiaSearchResult', array($fieldsArray)); $snippet = $articleService->getTextSnippet(250); $result->setText($snippet); if ($articleMatch->hasRedirect()) { $result->setVar('redirectTitle', $articleMatch->getArticle()->getTitle()); } $result->setVar('id', $articleMatchId); $this->addResult($result); $this->resultsFound++; wfProfileOut(__METHOD__); return $this; }
/** * Builds the necessary query clauses based on values set in the searchconfig object * @see WikiaSearchTest::testGetQueryClausesString * @param WikiaSearchConfig $searchConfig * @return string */ private function getQueryClausesString(WikiaSearchConfig $searchConfig) { $queryClauses = array(); if ($searchConfig->isInterWiki()) { $widQueries = array(); foreach ($this->getInterWikiSearchExcludedWikis() as $excludedWikiId) { $widQueries[] = self::valueForField('wid', $excludedWikiId, array('negate' => true)); } $queryClauses[] = implode(' AND ', $widQueries); $queryClauses[] = self::valueForField('lang', $this->wg->ContLang->mCode); $queryClauses[] = self::valueForField('iscontent', 'true'); $hub = $searchConfig->getHub(); if (!empty($hub)) { $queryClauses[] = self::valueForField('hub', $hub); } } else { if ($searchConfig->getVideoSearch()) { $searchConfig->setNamespaces(array(NS_FILE)); $queryClauses[] = self::valueForField('is_video', 'true'); $queryWithVideo = sprintf('(%s OR %s)', self::valueForField('wid', $searchConfig->getCityId()), self::valueForField('wid', self::VIDEO_WIKI_ID)); array_unshift($queryClauses, $queryWithVideo); } else { array_unshift($queryClauses, self::valueForField('wid', $searchConfig->getCityId())); } $nsQuery = ''; foreach ($searchConfig->getNamespaces() as $namespace) { $nsQuery .= (!empty($nsQuery) ? ' OR ' : '') . self::valueForField('ns', $namespace); } $queryClauses[] = "({$nsQuery})"; } if (!$searchConfig->getIncludeRedirects()) { $queryClauses[] = self::valueForField('is_redirect', 'false'); } return sprintf('(%s)', implode(' AND ', $queryClauses)); }