<commit_before><?php declare(strict_types=1);
namespace Room11\Jeeves\Plugins;

use Amp\Artax\HttpClient;
use Amp\Artax\Response as HttpResponse;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Room\Room as ChatRoom;
use Room11\Jeeves\Storage\KeyValue as KeyValueStore;
use Room11\Jeeves\System\PluginCommandEndpoint;
use function Amp\repeat;
use function Room11\DOMUtils\domdocument_load_html;

class PHPBugs extends BasePlugin
{
    private const RECENT_BUGS = "https://bugs.php.net/search.php?search_for=&boolean=0&limit=30&order_by=id&direction=DESC&cmd=display&status=All&bug_type=All&project=PHP&php_os=&phpver=&cve_id=&assign=&author_email=&bug_age=0&bug_updated=0";

    private $chatClient;
    private $httpClient;
    private $pluginData;
    private $rooms;
    private $lastKnownBugId = null;

    public function __construct(ChatClient $chatClient, HttpClient $httpClient, KeyValueStore $pluginData)
    {
        $this->chatClient = $chatClient;
        $this->httpClient = $httpClient;
        $this->pluginData = $pluginData;
        $this->rooms = [];

        repeat(
            function () {
                return $this->checkBugs();
            }, 300000
        );
    }

    public function enableForRoom(ChatRoom $room, bool $persist = true)
    {
        $this->rooms[$room->getIdentifier()->getIdentString()] = $room;
    }

    private function checkBugs()
    {
        $bugs = yield from $this->getRecentBugs();

        if (!$bugs) {
            return null;
        }

        if ($this->lastKnownBugId === null) {
            $this->lastKnownBugId = $bugs[0]["id"];
            return null;
        }

        $lastId = $this->lastKnownBugId;
        $this->lastKnownBugId = $bugs[0]["id"];

        foreach ($bugs as $bug) {
            if ($bug["id"] <= $lastId) {
                return null;
            }

            foreach ($this->rooms as $room) {
                yield $this->chatClient->postMessage($room, \sprintf(
                    "[tag:bug] #%d: %s – %s",
                    $bug["id"],
                    $bug["title"],
                    $bug["url"]
                ));
            }
        }

        return null;
    }

    private function getRecentBugs()
    {
        /** @var HttpResponse $response */
        $response = yield $this->httpClient->request(self::RECENT_BUGS);

        if ($response->getStatus() !== 200) {
            return false;
        }

        /** @var \DOMElement $table */
        $table = domdocument_load_html($response->getBody())
            ->getElementById("top")
            ->nextSibling;

        /** @var \DOMElement $content */
        $content = $table->getElementsByTagName("td")->item(0);
        $rows = $content->getElementsByTagName("tr");

        $bugs = [];

        foreach ($rows as $row) {
            /** @var \DOMElement $row */
            if (!$row->hasAttribute("valign")) {
                continue;
            }

            $cells = $row->getElementsByTagName("td");
            $id = (int) $cells->item(0)->firstChild->textContent;

            $bugs[] = [
                "id" => $id,
                "name" => $cells->item(8)->textContent ?: "*none*",
                "url" => "https://bugs.php.net/{$id}",
            ];
        }

        return $bugs;
    }

    public function getDescription(): string
    {
        return 'Pushes new PHP.net bugs';
    }

    /**
     * @return PluginCommandEndpoint[]
     */
    public function getCommandEndpoints(): array
    {
        return [];
    }
}
<commit_msg>Change bug pusher to persistently store the last bug ID<commit_after><?php declare(strict_types=1);
namespace Room11\Jeeves\Plugins;

use Amp\Artax\HttpClient;
use Amp\Artax\Response as HttpResponse;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Room\Room as ChatRoom;
use Room11\Jeeves\Storage\KeyValue as KeyValueStore;
use Room11\Jeeves\System\PluginCommandEndpoint;
use function Amp\repeat;
use function Room11\DOMUtils\domdocument_load_html;

class PHPBugs extends BasePlugin
{
    private const RECENT_BUGS = "https://bugs.php.net/search.php?search_for=&boolean=0&limit=30&order_by=id&direction=DESC&cmd=display&status=All&bug_type=All&project=PHP&php_os=&phpver=&cve_id=&assign=&author_email=&bug_age=0&bug_updated=0";
    private const DATA_KEY = "plugin.phpbugs.lastId";

    private $chatClient;
    private $httpClient;
    private $pluginData;
    private $rooms;

    public function __construct(ChatClient $chatClient, HttpClient $httpClient, KeyValueStore $pluginData)
    {
        $this->chatClient = $chatClient;
        $this->httpClient = $httpClient;
        $this->pluginData = $pluginData;
        $this->rooms = [];

        repeat(
            function () {
                return $this->checkBugs();
            }, 300000
        );
    }

    public function enableForRoom(ChatRoom $room, bool $persist = true)
    {
        $this->rooms[$room->getIdentifier()->getIdentString()] = $room;
    }

    private function checkBugs()
    {
        $bugs = yield from $this->getRecentBugs();

        if (!$bugs) {
            return null;
        }

        $lastId = $this->pluginData->get(self::DATA_KEY);
        $this->pluginData->set(self::DATA_KEY, $bugs[0]["id"]);

        if ($lastId === null) {
            return null;
        }

        foreach ($bugs as $bug) {
            if ($bug["id"] <= $lastId) {
                return null;
            }

            foreach ($this->rooms as $room) {
                yield $this->chatClient->postMessage($room, \sprintf(
                    "[tag:bug] #%d: %s – %s",
                    $bug["id"],
                    $bug["title"],
                    $bug["url"]
                ));
            }
        }

        return null;
    }

    private function getRecentBugs()
    {
        /** @var HttpResponse $response */
        $response = yield $this->httpClient->request(self::RECENT_BUGS);

        if ($response->getStatus() !== 200) {
            return false;
        }

        return $this->parseBugs($response->getBody());
    }

    private function parseBugs(string $body)
    {
        /** @var \DOMElement $table */
        $table = domdocument_load_html($body)
            ->getElementById("top")
            ->nextSibling;

        /** @var \DOMElement $content */
        $content = $table->getElementsByTagName("td")->item(0);
        $rows = $content->getElementsByTagName("tr");

        $bugs = [];

        foreach ($rows as $row) {
            /** @var \DOMElement $row */
            if (!$row->hasAttribute("valign")) {
                continue;
            }

            $cells = $row->getElementsByTagName("td");
            $id = (int) $cells->item(0)->firstChild->textContent;

            $bugs[] = [
                "id" => $id,
                "name" => $cells->item(8)->textContent ?: "*none*",
                "url" => "https://bugs.php.net/{$id}",
            ];
        }

        return $bugs;
    }

    public function getDescription(): string
    {
        return 'Pushes new PHP.net bugs';
    }

    /**
     * @return PluginCommandEndpoint[]
     */
    public function getCommandEndpoints(): array
    {
        return [];
    }
}

{'fraction_non_alphanumeric': 0.14708731147087312, 'fraction_numerical': 0.008717310087173101, 'mean_word_length': 2.030607966457023, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 74, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3272328', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2116, 'n_words': 402}