Rewrite 'pull-os-versions' script in Javascript to add support for reporting os revis...
[WebKit-https.git] / Websites / perf.webkit.org / public / include / commit-log-fetcher.php
index 2ae5e45bd4b08b92c09fca53bc882fe715aaa172..05d0a01ccaaf745097a189e9e1c6c68f89f1edf2 100644 (file)
@@ -4,21 +4,40 @@ class CommitLogFetcher {
 
     function __construct($db) {
         $this->db = $db;
-        $this->commits = array();
+    }
+
+    function fetch_for_tasks($task_id_list, $task_by_id)
+    {
+        $commit_rows = $this->db->query_and_fetch_all('SELECT task_commits.*, commits.*, committers.*
+            FROM task_commits, commits LEFT OUTER JOIN committers ON commit_committer = committer_id
+            WHERE taskcommit_commit = commit_id AND taskcommit_task = ANY ($1)', array('{' . implode(', ', $task_id_list) . '}'));
+        if (!is_array($commit_rows))
+            return NULL;
+
+        $commits = array();
+        foreach ($commit_rows as &$commit_row) {
+            $associated_task = &$task_by_id[$commit_row['taskcommit_task']];
+            $commit = $this->format_commit($commit_row, $commit_row);
+            $commit['repository'] = $commit_row['commit_repository'];
+            array_push($commits, $commit);
+            array_push($associated_task[Database::is_true($commit_row['taskcommit_is_fix']) ? 'fixes' : 'causes'], $commit_row['commit_id']);
+        }
+        return $commits;
     }
 
     function repository_id_from_name($name)
     {
-        $repository_row = $this->db->select_first_row('repositories', 'repository', array('name' => $name));
+        $repository_row = $this->db->select_first_row('repositories', 'repository', array('name' => $name, 'owner' => NULL));
         if (!$repository_row)
             return NULL;
         return $repository_row['repository_id'];
     }
 
-    function fetch_between($repository_id, $first, $second, $keyword = NULL) {
+    function fetch_between($repository_id, $first, $second, $keyword = NULL)
+    {
         $statements = 'SELECT commit_id as "id",
             commit_revision as "revision",
-            commit_parent as "parent",
+            commit_previous_commit as "previousCommit",
             commit_time as "time",
             committer_name as "authorName",
             committer_account as "authorEmail",
@@ -47,7 +66,7 @@ class CommitLogFetcher {
         }
 
         if ($keyword) {
-            array_push($values, '%' . str_replace(array('\\', '_', '%'), array('\\\\', '\\_', '\\%'), $keyword) . '%');
+            array_push($values, '%' . Database::escape_for_like($keyword) . '%');
             $keyword_index = '$' . count($values);
             array_push($values, ltrim($keyword, 'r'));
             $revision_index = '$' . count($values);
@@ -65,20 +84,39 @@ class CommitLogFetcher {
         return $commits;
     }
 
+    # FIXME: this is not DRY. Ideally, $db should provide the ability to search with criteria that specifies a range.
+    function fetch_last_reported_between_orders($repository_id, $from, $to)
+    {
+        $statements = 'SELECT * FROM commits LEFT OUTER JOIN committers ON commit_committer = committer_id
+            WHERE commit_repository = $1 AND commit_reported = true';
+        $from = intval($from);
+        $to = intval($to);
+        $statements .= ' AND commit_order >= $2 AND commit_order <= $3 ORDER BY commit_order DESC LIMIT 1';
+
+        $commits = $this->db->query_and_fetch_all($statements, array($repository_id, $from, $to));
+        if (!is_array($commits))
+            return NULL;
+
+        foreach ($commits as &$commit)
+            $commit = $this->format_single_commit($commit)[0];
+
+        return $commits;
+    }
+
     function fetch_oldest($repository_id) {
-        return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
+        return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), array('time', 'order')));
     }
 
     function fetch_latest($repository_id) {
-        return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
+        return $this->format_single_commit($this->db->select_last_row('commits', 'commit', array('repository' => $repository_id), array('time', 'order')));
     }
 
     function fetch_last_reported($repository_id) {
-        return $this->format_single_commit($this->db->select_last_row('commits', 'commit', array('repository' => $repository_id, 'reported' => true), 'time'));
+        return $this->format_single_commit($this->db->select_last_row('commits', 'commit', array('repository' => $repository_id, 'reported' => true), array('time', 'order')));
     }
 
     function fetch_revision($repository_id, $revision) {
-        return $this->format_single_commit($this->db->commit_for_revision($repository_id, $revision));
+        return $this->format_single_commit($this->commit_for_revision($repository_id, $revision));
     }
 
     private function commit_for_revision($repository_id, $revision) {
@@ -93,15 +131,17 @@ class CommitLogFetcher {
     }
 
     private function format_single_commit($commit_row) {
+        if (!$commit_row)
+            return array();
         $committer = $this->db->select_first_row('committers', 'committer', array('id' => $commit_row['commit_committer']));
-        return $this->format_commit($commit_row, $committer);
+        return array($this->format_commit($commit_row, $committer));
     }
 
     private function format_commit($commit_row, $committer_row) {
         return array(
             'id' => $commit_row['commit_id'],
             'revision' => $commit_row['commit_revision'],
-            'parent' => $commit_row['commit_parent'],
+            'previousCommit' => $commit_row['commit_previous_commit'],
             'time' => Database::to_js_time($commit_row['commit_time']),
             'order' => $commit_row['commit_order'],
             'authorName' => $committer_row ? $committer_row['committer_name'] : null,
@@ -111,4 +151,4 @@ class CommitLogFetcher {
     }
 }
 
-?>
\ No newline at end of file
+?>