Extract the code to format commit logs into its own PHP file
[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         $this->commits = array();
8     }
9
10     function repository_id_from_name($name)
11     {
12         $repository_row = $this->db->select_first_row('repositories', 'repository', array('name' => $name));
13         if (!$repository_row)
14             return NULL;
15         return $repository_row['repository_id'];
16     }
17
18     function fetch_between($repository_id, $first, $second, $keyword = NULL) {
19         $statements = 'SELECT commit_id as "id",
20             commit_revision as "revision",
21             commit_parent as "parent",
22             commit_time as "time",
23             committer_name as "authorName",
24             committer_account as "authorEmail",
25             commit_message as "message"
26             FROM commits LEFT OUTER JOIN committers ON commit_committer = committer_id
27             WHERE commit_repository = $1 AND commit_reported = true';
28         $values = array($repository_id);
29
30         if ($first && $second) {
31             $first_commit = $this->commit_for_revision($repository_id, $first);
32             $second_commit = $this->commit_for_revision($repository_id, $second);
33             $first = $first_commit['commit_time'];
34             $second = $second_commit['commit_time'];
35             $column_name = 'commit_time';
36             if (!$first || !$second) {
37                 $first = $first_commit['commit_order'];
38                 $second = $second_commit['commit_order'];
39                 $column_name = 'commit_order';
40             }
41
42             $in_order = $first < $second;
43             array_push($values, $in_order ? $first : $second);
44             $statements .= ' AND ' . $column_name . ' >= $' . count($values);
45             array_push($values, $in_order ? $second : $first);
46             $statements .= ' AND ' . $column_name . ' <= $' . count($values);
47         }
48
49         if ($keyword) {
50             array_push($values, '%' . str_replace(array('\\', '_', '%'), array('\\\\', '\\_', '\\%'), $keyword) . '%');
51             $keyword_index = '$' . count($values);
52             array_push($values, ltrim($keyword, 'r'));
53             $revision_index = '$' . count($values);
54             $statements .= "
55                 AND ((committer_name LIKE $keyword_index OR committer_account LIKE $keyword_index) OR commit_revision = $revision_index)";
56         }
57
58         $commits = $this->db->query_and_fetch_all($statements . ' ORDER BY commit_time, commit_order', $values);
59         if (!is_array($commits))
60             return NULL;
61
62         foreach ($commits as &$commit)
63             $commit['time'] = Database::to_js_time($commit['time']);
64
65         return $commits;
66     }
67
68     function fetch_oldest($repository_id) {
69         return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
70     }
71
72     function fetch_latest($repository_id) {
73         return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
74     }
75
76     function fetch_last_reported($repository_id) {
77         return $this->format_single_commit($this->db->select_last_row('commits', 'commit', array('repository' => $repository_id, 'reported' => true), 'time'));
78     }
79
80     function fetch_revision($repository_id, $revision) {
81         return $this->format_single_commit($this->db->commit_for_revision($repository_id, $revision));
82     }
83
84     private function commit_for_revision($repository_id, $revision) {
85         $all_but_first = substr($revision, 1);
86         if ($revision[0] == 'r' && ctype_digit($all_but_first))
87             $revision = $all_but_first;
88         $commit_info = array('repository' => $repository_id, 'revision' => $revision);
89         $row = $this->db->select_last_row('commits', 'commit', $commit_info);
90         if (!$row)
91             exit_with_error('UnknownCommit', $commit_info);
92         return $row;
93     }
94
95     private function format_single_commit($commit_row) {
96         $committer = $this->db->select_first_row('committers', 'committer', array('id' => $commit_row['commit_committer']));
97         return $this->format_commit($commit_row, $committer);
98     }
99
100     private function format_commit($commit_row, $committer_row) {
101         return array(
102             'id' => $commit_row['commit_id'],
103             'revision' => $commit_row['commit_revision'],
104             'parent' => $commit_row['commit_parent'],
105             'time' => Database::to_js_time($commit_row['commit_time']),
106             'order' => $commit_row['commit_order'],
107             'authorName' => $committer_row ? $committer_row['committer_name'] : null,
108             'authorEmail' => $committer_row ? $committer_row['committer_account'] : null,
109             'message' => $commit_row['commit_message']
110         );
111     }
112 }
113
114 ?>