Build fix after r198234.
[WebKit-https.git] / Websites / perf.webkit.org / public / include / commit-log-fetcher.php
1 <?php
2
3 class CommitLogFetcher {
4
5     function __construct($db) {
6         $this->db = $db;
7     }
8
9     function fetch_for_tasks($task_id_list, $task_by_id)
10     {
11         $commit_rows = $this->db->query_and_fetch_all('SELECT task_commits.*, commits.*, committers.*
12             FROM task_commits, commits LEFT OUTER JOIN committers ON commit_committer = committer_id
13             WHERE taskcommit_commit = commit_id AND taskcommit_task = ANY ($1)', array('{' . implode(', ', $task_id_list) . '}'));
14         if (!is_array($commit_rows))
15             return NULL;
16
17         $commits = array();
18         foreach ($commit_rows as &$commit_row) {
19             $associated_task = &$task_by_id[$commit_row['taskcommit_task']];
20             $commit = $this->format_commit($commit_row, $commit_row);
21             $commit['repository'] = $commit_row['commit_repository'];
22             array_push($commits, $commit);
23             array_push($associated_task[Database::is_true($commit_row['taskcommit_is_fix']) ? 'fixes' : 'causes'], $commit_row['commit_id']);
24         }
25         return $commits;
26     }
27
28     function repository_id_from_name($name)
29     {
30         $repository_row = $this->db->select_first_row('repositories', 'repository', array('name' => $name));
31         if (!$repository_row)
32             return NULL;
33         return $repository_row['repository_id'];
34     }
35
36     function fetch_between($repository_id, $first, $second, $keyword = NULL) {
37         $statements = 'SELECT commit_id as "id",
38             commit_revision as "revision",
39             commit_parent as "parent",
40             commit_time as "time",
41             committer_name as "authorName",
42             committer_account as "authorEmail",
43             commit_message as "message"
44             FROM commits LEFT OUTER JOIN committers ON commit_committer = committer_id
45             WHERE commit_repository = $1 AND commit_reported = true';
46         $values = array($repository_id);
47
48         if ($first && $second) {
49             $first_commit = $this->commit_for_revision($repository_id, $first);
50             $second_commit = $this->commit_for_revision($repository_id, $second);
51             $first = $first_commit['commit_time'];
52             $second = $second_commit['commit_time'];
53             $column_name = 'commit_time';
54             if (!$first || !$second) {
55                 $first = $first_commit['commit_order'];
56                 $second = $second_commit['commit_order'];
57                 $column_name = 'commit_order';
58             }
59
60             $in_order = $first < $second;
61             array_push($values, $in_order ? $first : $second);
62             $statements .= ' AND ' . $column_name . ' >= $' . count($values);
63             array_push($values, $in_order ? $second : $first);
64             $statements .= ' AND ' . $column_name . ' <= $' . count($values);
65         }
66
67         if ($keyword) {
68             array_push($values, '%' . str_replace(array('\\', '_', '%'), array('\\\\', '\\_', '\\%'), $keyword) . '%');
69             $keyword_index = '$' . count($values);
70             array_push($values, ltrim($keyword, 'r'));
71             $revision_index = '$' . count($values);
72             $statements .= "
73                 AND ((committer_name LIKE $keyword_index OR committer_account LIKE $keyword_index) OR commit_revision = $revision_index)";
74         }
75
76         $commits = $this->db->query_and_fetch_all($statements . ' ORDER BY commit_time, commit_order', $values);
77         if (!is_array($commits))
78             return NULL;
79
80         foreach ($commits as &$commit)
81             $commit['time'] = Database::to_js_time($commit['time']);
82
83         return $commits;
84     }
85
86     function fetch_oldest($repository_id) {
87         return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
88     }
89
90     function fetch_latest($repository_id) {
91         return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
92     }
93
94     function fetch_last_reported($repository_id) {
95         return $this->format_single_commit($this->db->select_last_row('commits', 'commit', array('repository' => $repository_id, 'reported' => true), 'time'));
96     }
97
98     function fetch_revision($repository_id, $revision) {
99         return $this->format_single_commit($this->db->commit_for_revision($repository_id, $revision));
100     }
101
102     private function commit_for_revision($repository_id, $revision) {
103         $all_but_first = substr($revision, 1);
104         if ($revision[0] == 'r' && ctype_digit($all_but_first))
105             $revision = $all_but_first;
106         $commit_info = array('repository' => $repository_id, 'revision' => $revision);
107         $row = $this->db->select_last_row('commits', 'commit', $commit_info);
108         if (!$row)
109             exit_with_error('UnknownCommit', $commit_info);
110         return $row;
111     }
112
113     private function format_single_commit($commit_row) {
114         $committer = $this->db->select_first_row('committers', 'committer', array('id' => $commit_row['commit_committer']));
115         return array($this->format_commit($commit_row, $committer));
116     }
117
118     private function format_commit($commit_row, $committer_row) {
119         return array(
120             'id' => $commit_row['commit_id'],
121             'revision' => $commit_row['commit_revision'],
122             'parent' => $commit_row['commit_parent'],
123             'time' => Database::to_js_time($commit_row['commit_time']),
124             'order' => $commit_row['commit_order'],
125             'authorName' => $committer_row ? $committer_row['committer_name'] : null,
126             'authorEmail' => $committer_row ? $committer_row['committer_account'] : null,
127             'message' => $commit_row['commit_message']
128         );
129     }
130 }
131
132 ?>