<commit_before><?php
/**
 * GitAccess MediaWiki Extension---Access wiki content with Git.
 * Copyright (C) 2016  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class GitCommit
{
    public $commit_hash;
    public $parent_hashes;
    public $author_name;
    public $author_email;
    public $author_timestamp;
    public $author_tzOffset;
    public $committer_name;
    public $committer_email;
    public $committer_timestamp;
    public $committer_tzOffset;
    
    public $commit_message;
    
    public $linked_rev_ids;
    public $linked_log_ids;
    
    public $root_tree;
    public $root_tree_hash;
    
    protected $dbw;
    protected $repo;
    
    public function __construct(&$repo)
    {
        $this->repo = &$repo;
        $this->dbw = wfGetDB(DB_MASTER);
    }
    
    protected function populateIdsFromRevision()
    {
        $linked_rev_result = $this->dbw->select(
            'git_edit_hash',
            'affected_rev_id',
            array('commit_hash' => $this->commit_hash)
        );
        
        $this->linked_rev_ids = array();
        do
        {
            $row = $linked_rev_result->fetchRow();
            if ($row)
            {
                array_push($this->linked_rev_ids, $row['affected_rev_id']);
            }
        }
        while ($row);
    }
    
    protected function populateIdsFromLogging()
    {
        $linked_log_result = $this->dbw->select(
            'git_status_modify_hash',
            'log_id',
            array('commit_hash' => $this->commit_hash)
        );
        
        $this->linked_log_ids = array();
        do
        {
            $row = $linked_log_result->fetchRow();
            if ($row)
            {
                array_push($this->linked_log_ids, $row['log_id']);
            }
        }
        while ($row);
    }
    
    public function export()
    {
        $commit = 'tree '. $this->root_tree->getHash() . "\n";
        foreach($this->parent_hashes as $parent)
        {
            $commit .= 'parent ' . $parent . "\n";
        }
        
        $author_tstamp_unix = wfTimestamp(TS_UNIX, $this->author_timestamp);
        $committer_tstamp_unix = wfTimestamp(TS_UNIX, $this->committer_timestamp);
        
        $commit .=
            'author ' .
            $this->author_name .
            ' <' .
            $this->author_email .
            '> ' .
            $author_tstamp_unix .
            ' ' .
            self::tzOffsetToHrsMins($this->author_tzOffset) .
            "\n";
        $commit .=
            'committer ' .
            $this->committer_name .
            ' <' .
            $this->committer_email .
            '> ' .
            $committer_tstamp_unix .
            ' ' .
            self::tzOffsetToHrsMins($this->committer_tzOffset) .
            "\n";
        $commit .= "\n\n";
        $commit .= $this->commit_message;
        
        $length = strlen($commit);
        
        $commit = "commit $length" . "\0". $commit;
        
        return $commit;
    }
    
    public static function newFromData($commit, &$repo)
    {
        $commit_data = array();
        preg_match(
            "commit (.[0-9]*)\\0tree (.{40})\\n((?:parent .{40}\\n)+)author (.*) <(.*@.*\..*)> ([0-9]*) ([0-9+-]*)\\ncommitter (.*) <(.*@.*\..*)> ([0-9]*) ([0-9+-]*)\\n\\n(.*)",
            $commit,
            $commit_data
        );
        
        $parents = array();
        $parent_pieces = explode("\n", $commit_data[2]);
        foreach($parent_pieces as $parent_piece)
        {
            array_push($parents, sscanf($parent_piece, "parent %s")[0]);
        }
        
        $hash = hash('sha1', $commit);
        
        $instance = new self($repo);
        $instance->commit_hash = $hash;
        $instance->parent_hashes = $parents;
        $instance->author_name = $commit_data[3];
        $instance->author_email = $commit_data[4];
        $instance->author_timestamp = $commit_data[5];
        $instance->author_tzOffset = self::tzOffsetToUnix($commit_data[6]);
        $instance->committer_name = $commit_data[7];
        $instance->committer_email = $commit_data[8];
        $instance->committer_timestamp = $commit_data[9];<commit_msg>Tweak regex to allow zero parents (i.e. initial commit)<commit_after><?php
/**
 * GitAccess MediaWiki Extension---Access wiki content with Git.
 * Copyright (C) 2016  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class GitCommit
{
    public $commit_hash;
    public $parent_hashes;
    public $author_name;
    public $author_email;
    public $author_timestamp;
    public $author_tzOffset;
    public $committer_name;
    public $committer_email;
    public $committer_timestamp;
    public $committer_tzOffset;
    
    public $commit_message;
    
    public $linked_rev_ids;
    public $linked_log_ids;
    
    public $root_tree;
    public $root_tree_hash;
    
    protected $dbw;
    protected $repo;
    
    public function __construct(&$repo)
    {
        $this->repo = &$repo;
        $this->dbw = wfGetDB(DB_MASTER);
    }
    
    protected function populateIdsFromRevision()
    {
        $linked_rev_result = $this->dbw->select(
            'git_edit_hash',
            'affected_rev_id',
            array('commit_hash' => $this->commit_hash)
        );
        
        $this->linked_rev_ids = array();
        do
        {
            $row = $linked_rev_result->fetchRow();
            if ($row)
            {
                array_push($this->linked_rev_ids, $row['affected_rev_id']);
            }
        }
        while ($row);
    }
    
    protected function populateIdsFromLogging()
    {
        $linked_log_result = $this->dbw->select(
            'git_status_modify_hash',
            'log_id',
            array('commit_hash' => $this->commit_hash)
        );
        
        $this->linked_log_ids = array();
        do
        {
            $row = $linked_log_result->fetchRow();
            if ($row)
            {
                array_push($this->linked_log_ids, $row['log_id']);
            }
        }
        while ($row);
    }
    
    public function export()
    {
        $commit = 'tree '. $this->root_tree->getHash() . "\n";
        foreach($this->parent_hashes as $parent)
        {
            $commit .= 'parent ' . $parent . "\n";
        }
        
        $author_tstamp_unix = wfTimestamp(TS_UNIX, $this->author_timestamp);
        $committer_tstamp_unix = wfTimestamp(TS_UNIX, $this->committer_timestamp);
        
        $commit .=
            'author ' .
            $this->author_name .
            ' <' .
            $this->author_email .
            '> ' .
            $author_tstamp_unix .
            ' ' .
            self::tzOffsetToHrsMins($this->author_tzOffset) .
            "\n";
        $commit .=
            'committer ' .
            $this->committer_name .
            ' <' .
            $this->committer_email .
            '> ' .
            $committer_tstamp_unix .
            ' ' .
            self::tzOffsetToHrsMins($this->committer_tzOffset) .
            "\n";
        $commit .= "\n\n";
        $commit .= $this->commit_message;
        
        $length = strlen($commit);
        
        $commit = "commit $length" . "\0". $commit;
        
        return $commit;
    }
    
    public static function newFromData($commit, &$repo)
    {
        $commit_data = array();
        preg_match(
            "commit (.[0-9]*)\\0tree (.{40})\\n((?:parent .{40}\\n)*)author (.*) <(.*@.*\..*)> ([0-9]*) ([0-9+-]*)\\ncommitter (.*) <(.*@.*\..*)> ([0-9]*) ([0-9+-]*)\\n\\n(.*)",
            $commit,
            $commit_data
        );
        
        $parents = array();
        $parent_pieces = explode("\n", $commit_data[2]);
        foreach($parent_pieces as $parent_piece)
        {
            array_push($parents, sscanf($parent_piece, "parent %s")[0]);
        }
        
        $hash = hash('sha1', $commit);
        
        $instance = new self($repo);
        $instance->commit_hash = $hash;
        $instance->parent_hashes = $parents;
        $instance->author_name = $commit_data[3];
        $instance->author_email = $commit_data[4];
        $instance->author_timestamp = $commit_data[5];
        $instance->author_tzOffset = self::tzOffsetToUnix($commit_data[6]);
        $instance->committer_name = $commit_data[7];
        $instance->committer_email = $commit_data[8];
        $instance->committer_timestamp = $commit_data[9];
{'fraction_non_alphanumeric': 0.13713341594382486, 'fraction_numerical': 0.006402313093762908, 'mean_word_length': 1.8781575037147102, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5812649', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2913, 'n_words': 603}