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; }
private function parseRoomAccessSection(\DOMElement $section) : array { try { $userEls = xpath_get_elements($section, ".//div[contains(concat(' ', normalize-space(@class), ' '), ' usercard ')]"); } catch (ElementNotFoundException $e) { return []; } $users = []; foreach ($userEls as $userEl) { $profileAnchor = xpath_get_element($userEl, ".//a[contains(concat(' ', normalize-space(@class), ' '), ' username ')]"); if (!preg_match('#^/users/([0-9]+)/#', $profileAnchor->getAttribute('href'), $match)) { continue; } $users[(int) $match[1]] = trim($profileAnchor->textContent); } return $users; }