<commit_before><?php
// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by the MIT license, a copy of which can
// be found in the LICENSE file.

class CommandLineFlags extends Service {
    // Registers the task which will be used for updating the command line flags
    // available in the Chromium repository.
    public static function RegisterTasks(ServiceManager $manager) {
        $manager->scheduleTask('3 hours', array(__CLASS__, 'updateFlags'));
        $manager->scheduleTask('24 hours', array(__CLASS__, 'sendDailyOverview'));
    }

    // Returns the current status of the command line flag tracking service.
    public static function QueryState(TaskEnvironment $env) {
        $result = $env->database()->query('
            SELECT
                switch_sourcepath AS filename,
                COUNT(switch_id) AS switch_count
            FROM
                chromium_switches
            WHERE
                removed IS NULL
            GROUP BY
                switch_sourcepath');

        $switches = 0;
        $files = 0;

        while ($result !== false && $row = $result->fetch_assoc()) {
            $switches += $row['switch_count'];
            $files++;
        }

        $last_update = 0;
        $result = $env->database()->query('
            SELECT
                UNIX_TIMESTAMP(GREATEST(added, updated, removed)) AS last_update
            FROM
                chromium_switches
            ORDER BY
                GREATEST(added, updated, removed) DESC
            LIMIT
                1');

        if ($result !== false)
            $last_update = $result->fetch_object()->last_update;

        return array(
            'name'          => 'Chromium command-line flag tracker',
            'description'   => 'Tracks and indexes the command line flags available to Chromium and Google Chrome.',
            'state'         => ServiceState::Good,
            'subtasks'      => array(
                array(
                    'name'  => 'Indexed command-line switches',
                    'date'  => $last_update,
                    'value' => '<a href="http://peter.sh/experiments/chromium-command-line-switches/">' . $switches . ' switches, ' . $files . ' files</a>',
                )
            )
        );
    }

    // ---------------------------------------------------------------------------------------------

    // Contains a list of switches which we parsed from the source files.
    private $m_switches;

    // Pre-processor directives which are currently active in the source file.
    private $m_preProcessorStack;

    // Sends a daily overview e-mail to a mailing list of added, updated and removed
    // switches in Chromium's source code.
    public function sendDailyOverview() {
        $since = time() - $this->interval;
        $sinceDate = date('Y-m-d H:i:s', $since);

        $result = $this->database()->query('
            SELECT
                chromium_switches.switch_id,
                chromium_switches.switch_name,
                chromium_switches.switch_description,
                UNIX_TIMESTAMP(chromium_switches.added) AS added,
                UNIX_TIMESTAMP(chromium_switches.updated) AS updated,
                UNIX_TIMESTAMP(chromium_switches.removed) AS removed
            FROM
                chromium_switches
            WHERE
                chromium_switches.added >= "' . $sinceDate . '" OR
                chromium_switches.updated >= "' . $sinceDate . '" OR
                (chromium_switches.removed IS NOT NULL AND
                 chromium_switches.removed >= "' . $sinceDate . '")
            ORDER BY
                chromium_switches.switch_name ASC');

        if ($result === false) {
            Error('CommandLineFlags: Unable to retrieve database list for daily overview e-mail.');
            return;
        }

        $switches = array('added' => array(), 'updated' => array(), 'removed' => array());
        $updates = 0;

        while ($switch = $result->fetch_assoc()) {
            $reason = 'added';
            if ($switch['updated'] >= $since)
                $reason = 'updated';
            else if ($switch['removed'] >= $since)
                $reason = 'removed';

            $switches[$reason][] = $switch;
            $updates++;
        }

        if ($updates == 0)
            return;

        $message = $this->loadMessage($switches, $updates);
        $subject = 'Chromium Command Line Updates for ' . date('Y-m-d');
        $headers = array(
            'From: <NAME> <<EMAIL>>',
            'Reply-To: <NAME> <<EMAIL>>',
            'Return-Path: <NAME> <<EMAIL>>',
            'Content-Type: text/html'
        );

        mail(Configuration::$commandLineUpdateAddress, $subject, $message, implode("\r\n", $headers));
    }

    // Loads the message to send to the list from message.html, having only |$switches| and
    // |$updates| available in the local scope. The message will be evaluated as PHP.
    private function loadMessage($switches, $updates) {
        $url = 'http://peter.sh/experiments/chromium-command-line-switches/';

        ob_start();
        include __DIR__ . '/message.html';
        return ob_get_clean();
    }

    // Updates the command line flags available in Chromium. We retrieve a list of
    // all the switch files from the Git repository hosted on Nishino, then attempt to
    // parse every file to identify the command line flags contained within it.
    public function updateFlags() {
        $sourceFlags = $this->loadFlagsFromSource();
        $databaseFlags = $this->loadFlagsFromDatabase();

        $createStatement = $this->database()->prepare('
            INSERT INTO
                chromium_switches
                (switch_name, switch_internal, switch_description, switch_conditions, switch_sourcepath, added)
            VALUES
                (?, ?, ?, ?, ?, NOW())');

        $updateStatement = $this->database()->prepare('
            UPDATE
                chromium_switches
            SET
                switch_internal = ?,
                switch_description = ?,
                switch_conditions = ?,
                switch_sourcepath = ?,<commit_msg>Remove the ability to send daily command line flag updates<commit_after><?php
// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by the MIT license, a copy of which can
// be found in the LICENSE file.

class CommandLineFlags extends Service {
    // Registers the task which will be used for updating the command line flags
    // available in the Chromium repository.
    public static function RegisterTasks(ServiceManager $manager) {
        $manager->scheduleTask('24 hours', array(__CLASS__, 'updateFlags'));
    }

    // Returns the current status of the command line flag tracking service.
    public static function QueryState(TaskEnvironment $env) {
        $result = $env->database()->query('
            SELECT
                switch_sourcepath AS filename,
                COUNT(switch_id) AS switch_count
            FROM
                chromium_switches
            WHERE
                removed IS NULL
            GROUP BY
                switch_sourcepath');

        $switches = 0;
        $files = 0;

        while ($result !== false && $row = $result->fetch_assoc()) {
            $switches += $row['switch_count'];
            $files++;
        }

        $last_update = 0;
        $result = $env->database()->query('
            SELECT
                UNIX_TIMESTAMP(GREATEST(added, updated, removed)) AS last_update
            FROM
                chromium_switches
            ORDER BY
                GREATEST(added, updated, removed) DESC
            LIMIT
                1');

        if ($result !== false)
            $last_update = $result->fetch_object()->last_update;

        return array(
            'name'          => 'Chromium command-line flag tracker',
            'description'   => 'Tracks and indexes the command line flags available to Chromium and Google Chrome.',
            'state'         => ServiceState::Good,
            'subtasks'      => array(
                array(
                    'name'  => 'Indexed command-line switches',
                    'date'  => $last_update,
                    'value' => '<a href="http://peter.sh/experiments/chromium-command-line-switches/">' . $switches . ' switches, ' . $files . ' files</a>',
                )
            )
        );
    }

    // ---------------------------------------------------------------------------------------------

    // Contains a list of switches which we parsed from the source files.
    private $m_switches;

    // Pre-processor directives which are currently active in the source file.
    private $m_preProcessorStack;

    // Updates the command line flags available in Chromium. We retrieve a list of
    // all the switch files from the Git repository hosted on Nishino, then attempt to
    // parse every file to identify the command line flags contained within it.
    public function updateFlags() {
        $sourceFlags = $this->loadFlagsFromSource();
        $databaseFlags = $this->loadFlagsFromDatabase();

        $createStatement = $this->database()->prepare('
            INSERT INTO
                chromium_switches
                (switch_name, switch_internal, switch_description, switch_conditions, switch_sourcepath, added)
            VALUES
                (?, ?, ?, ?, ?, NOW())');

        $updateStatement = $this->database()->prepare('
            UPDATE
                chromium_switches
            SET
                switch_internal = ?,
                switch_description = ?,
                switch_conditions = ?,
                switch_sourcepath = ?,
{'fraction_non_alphanumeric': 0.12289872898728987, 'fraction_numerical': 0.0023575235752357525, 'mean_word_length': 1.745357343837929, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 74, 'https://': 0, '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': '2139470', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2382, 'n_words': 718}