コード例 #1
0
 /**
  * @desc Validates fields and executes the provided method
  *
  * @param $method - Search method name to be called
  * @param array $fields = array of required request field names
  * @param bool $pagination - get pagination values from request
  * @throws NotFoundApiException
  * @throws InvalidParameterApiException
  */
 private function executeSearch($method, array $fields = [], $pagination = false)
 {
     $searchParams = new LyricsApiSearchParams();
     // Validate fields
     foreach ($fields as $fieldName) {
         $fieldValue = $this->wg->Request->getVal($fieldName);
         if (empty($fieldValue)) {
             throw new InvalidParameterApiException($fieldName);
         }
         $searchParams->addField($fieldName, $fieldValue);
     }
     // Get pagination
     if ($pagination) {
         list($limit, $offset) = $this->getPaginationParamsFromRequest();
         $searchParams->setLimit($limit);
         $searchParams->setOffset($offset);
     }
     $this->response->setFormat(WikiaResponse::FORMAT_JSON);
     $results = $this->lyricsApiHandler->{$method}($searchParams);
     // Validate result
     if (is_null($results)) {
         // New songs that we don't have yet are likely to get hit repeatedly by a
         // ton of users until we get the song, so this is a special case where it
         // is helpful to cache 404s for a little while.
         $this->response->setCacheValidity(WikiaResponse::CACHE_VERY_SHORT);
         throw new NotFoundApiException($this->getNotFoundDetails($method));
     }
     $this->response->setVal('result', $results);
     $this->response->setCacheValidity(WikiaResponse::CACHE_STANDARD);
 }
コード例 #2
0
 /**
  * @desc Searches for a song lyrics in Solr index
  *
  * @param LyricsApiSearchParams $searchParams
  *
  * @return array|null
  */
 public function searchLyrics(LyricsApiSearchParams $searchParams)
 {
     $query = $this->newQueryFromSearch(['type: %1%' => LyricsUtils::TYPE_SONG, 'lyrics: %P2%' => $searchParams->getField(LyricsApiController::PARAM_QUERY)]);
     $query->setStart($searchParams->getOffset());
     $query->setRows($searchParams->getLimit());
     $hl = $query->getHighlighting();
     $hl->setFields(self::INDEX_FIELD_NAME_LYRICS);
     $hl->setSimplePrefix(self::HIGHLIGHT_PREFIX);
     $hl->setSimplePostfix(self::HIGHLIGHT_POSTFIX);
     $solrSongs = $this->client->select($query);
     if ($solrSongs->getNumFound() <= 0) {
         return null;
     }
     $songs = [];
     $highlighting = $solrSongs->getHighlighting();
     /** @var Solarium_Document_ReadOnly $solrSong */
     foreach ($solrSongs as $solrSong) {
         $fields = $solrSong->getFields();
         $songs[] = $this->getOutputSong($solrSong, $highlighting->getResult($fields['id']), true);
     }
     return $songs;
 }