예제 #1
0
 /**
  * @param string $uri
  * @return string
  */
 public function executeQuery($uri)
 {
     $bodyResource = Remote::getUriBody($uri);
     $contents = $bodyResource->getContents();
     $results = json_decode($contents, true);
     if (!$results || !array_key_exists('results', $results)) {
         return false;
     }
     $finalresults = [];
     foreach ($results['results'] as $resultset) {
         $repo = $resultset['repo'];
         $arch = $resultset['arch'];
         $pkgname = $resultset['pkgname'];
         $pkgver = $resultset['pkgver'] . '-' . $resultset['pkgrel'];
         $pkgdesc = $resultset['pkgdesc'] . ' -- version ' . $pkgver;
         $uri = $this->buildPackageUri($repo, $arch, $pkgname);
         $title = $pkgname;
         $searchResult = new SearchResult();
         $searchResult->setTitle($title);
         $searchResult->setDescription($pkgdesc);
         $searchResult->setUri($uri);
         $finalresults[] = $searchResult;
     }
     return $finalresults;
 }
예제 #2
0
 /**
  * @param string $uri
  * @return string
  */
 public static function getContentTypeFromUri($uri)
 {
     $headerResource = Remote::getUriHeaders($uri);
     if (!$headerResource->hasHeader('Content-Type')) {
         throw new ContentTypeNotFoundException();
     }
     $content_type = strtolower(explode(';', $headerResource->getHeaderLine('Content-Type'))[0]);
     if (empty($content_type)) {
         throw new ContentTypeNotFoundException();
     }
     return $content_type;
 }
예제 #3
0
파일: ShortenUri.php 프로젝트: WildPHP/api
 /**
  * @param string $uri
  * @return string
  */
 public static function createShortLink($uri)
 {
     if (!Validation::isValidLink($uri)) {
         throw new InvalidUriException($uri . ' is not a valid link');
     }
     // Pieces...
     $shortenerBaseUrl = 'http://is.gd/create.php?format=json&url=%s';
     $uri = urlencode($uri);
     // Add them together...
     $shortenerUrl = sprintf($shortenerBaseUrl, $uri);
     // And fire a request.
     $body = Remote::getUriBody($shortenerUrl);
     $contents = $body->getContents();
     if (!($decoded = json_decode($contents)) || empty($decoded->shorturl)) {
         throw new ShortUriCreationFailedException('Received an invalid result set');
     }
     return $decoded->shorturl;
 }
예제 #4
0
 protected function getSearchResults($searchUri)
 {
     $bodyResource = Remote::getUriBody($searchUri);
     $contents = $bodyResource->getContents();
     $result = json_decode($contents);
     $results = [];
     // Because MediaWiki returns results in this awkward way,
     // we first process them into 'sane' results.
     foreach ($result[1] as $key => $title) {
         $searchResult = new searchResult();
         $searchResult->setTitle($title);
         $results[$key] = $searchResult;
     }
     foreach ($result[2] as $key => $description) {
         $results[$key]->setDescription($description);
     }
     foreach ($result[3] as $key => $uri) {
         $results[$key]->setUri($uri);
     }
     return $results;
 }
예제 #5
0
 /**
  * The wiki command itself.
  * @param IrcDataObject $data The data received.
  */
 public function wikiCommand($command, $params, IrcDataObject $data)
 {
     $params = trim($params);
     $user = $data->getMessage()['nick'];
     if (preg_match('/ @ ([\\S]+)$/', $params, $out) && !empty($out[1])) {
         $user = $out[1];
         $params = preg_replace('/ @ ([\\S]+)$/', '', $params);
     }
     // Merge multiple spaces into one, trim the result, urlencode it.
     $query = urlencode(trim(preg_replace('/\\s\\s+/', ' ', $params)));
     $url = $this->wikiURL . '/api.php?' . 'action=opensearch' . '&limit=1&namespace=0' . '&format=json&redirects=resolve&search=';
     $bodyResource = Remote::getUriBody($url . $query);
     $contents = $bodyResource->getContents();
     $result = json_decode($contents);
     $connection = $this->getModule('Connection');
     if ($result === false || empty($result[1])) {
         $connection->write($connection->getGenerator()->ircPrivmsg($data->getTargets()[0], $user . ': Sorry, I could not find a page matching your query. Please try again.'));
         return;
     }
     // OpenSearch API
     $title = $result[1][0];
     $link = $result[3][0];
     $connection->write($connection->getGenerator()->ircPrivmsg($data->getTargets()[0], $user . ': ' . $title . ' - ' . $link));
 }