示例#1
0
 private function getOpenGrokSearchResults(string $branch, array $params) : \Generator
 {
     $branch = $branch === self::DEFAULT_BRANCH ? $branch : ($branch = 'PHP-' . $branch);
     $url = self::BASE_URL . '?project=' . $branch . '&n=10000&' . http_build_query($params);
     try {
         $request = (new HttpRequest())->setMethod('GET')->setUri($url);
         $this->cookieJar->store(new Cookie('OpenGrokProject', $branch, null, null, self::BASE_URL));
         /** @var HttpResponse $response */
         $response = (yield $this->httpClient->request($request));
         /** @var \DOMDocument $doc */
         $doc = domdocument_load_html($response->getBody());
     } catch (\Throwable $e) {
         throw new OpenGrokSearchFailureException("Totally failed to get a valid [results page]({$url})", 1);
     }
     if (!($resultsDiv = $doc->getElementById('results'))) {
         throw new OpenGrokSearchFailureException("The [results page]({$url}) is not in the format I expected it to be", 1);
     }
     try {
         $resultsTable = xpath_get_element($resultsDiv, './table');
     } catch (ElementNotFoundException $e) {
         throw new OpenGrokSearchFailureException("There were no [results]({$url}) for that search", 0);
     }
     $dir = null;
     $tests = false;
     $trim = strlen('/xref/' . $branch);
     $results = ['url' => $url, 'count' => 0, 'code' => [], 'tests' => []];
     $baseUrl = new Uri($url);
     foreach ($resultsTable->getElementsByTagName('tr') as $row) {
         /** @var \DOMElement $row */
         if (preg_match('#\\bdir\\b#', $row->getAttribute('class'))) {
             $tests = (bool) preg_match('#/tests/#i', xpath_get_element($row, './td/a')->textContent);
             continue;
         }
         foreach (xpath_get_elements($row, "./td/tt/a[@class='s']") as $resultAnchor) {
             $hrefAttr = $resultAnchor->getAttribute('href');
             $path = substr($hrefAttr, $trim);
             $href = (string) $baseUrl->resolve($hrefAttr);
             $el = xpath_get_element($resultAnchor, "./span[@class='l']");
             $line = $el->textContent;
             $code = '';
             while ($el = $el->nextSibling) {
                 $code .= $el->textContent;
             }
             $results[$tests ? 'tests' : 'code'][] = ['href' => $href, 'path' => $path, 'line' => $line, 'code' => trim(preg_replace('#\\s+#', ' ', $code))];
             $results['count']++;
         }
     }
     return $results;
 }
示例#2
0
 /**
  * @param ChatRoom|ChatRoomIdentifier $room
  * @return Promise
  */
 public function getPinnedMessages($room) : Promise
 {
     $url = $this->urlResolver->getEndpointURL($room, ChatRoomEndpoint::CHATROOM_STARS_LIST);
     return resolve(function () use($url) {
         /** @var HttpResponse $response */
         $this->logger->log(Level::DEBUG, 'Getting pinned messages');
         $response = (yield $this->httpClient->request($url));
         $doc = domdocument_load_html($response->getBody());
         try {
             $pinnedEls = xpath_get_elements($doc, ".//li[./span[contains(concat(' ', normalize-space(@class), ' '), ' owner-star ')]]");
         } catch (ElementNotFoundException $e) {
             return [];
         }
         $result = [];
         foreach ($pinnedEls as $el) {
             $result[] = (int) explode('_', $el->getAttribute('id'))[1];
         }
         $this->logger->log(Level::DEBUG, 'Got pinned messages: ' . implode(',', $result));
         return $result;
     });
 }