Extract the code to format commit logs into its own PHP file
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 15 Mar 2016 22:42:15 +0000 (22:42 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 15 Mar 2016 22:42:15 +0000 (22:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=155514

Rubber-stamped by Chris Dumez.

Extracted CommitLogFetcher out of /api/commits so that it could be used in analysis-tasks.php
in the future to support associating cause/fix for each analysis task.

* public/api/commits.php:
* public/include/commit-log-fetcher.php: Added.
(CommitLogFetcher)
(CommitLogFetcher::__construct): Added.
(CommitLogFetcher::repository_id_from_name): Added.
(CommitLogFetcher::fetch_between): Added.
(CommitLogFetcher::fetch_oldest): Added.
(CommitLogFetcher::fetch_latest): Added.
(CommitLogFetcher::fetch_last_reported): Added.
(CommitLogFetcher::fetch_revision): Added.
(CommitLogFetcher::commit_for_revision): Added.
(CommitLogFetcher::format_single_commit): Added.
(CommitLogFetcher::format_commit): Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@198234 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Websites/perf.webkit.org/ChangeLog
Websites/perf.webkit.org/public/api/commits.php
Websites/perf.webkit.org/public/include/commit-log-fetcher.php [new file with mode: 0644]

index 6429f67..927d970 100644 (file)
@@ -1,3 +1,27 @@
+2016-03-15  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Extract the code to format commit logs into its own PHP file
+        https://bugs.webkit.org/show_bug.cgi?id=155514
+
+        Rubber-stamped by Chris Dumez.
+
+        Extracted CommitLogFetcher out of /api/commits so that it could be used in analysis-tasks.php
+        in the future to support associating cause/fix for each analysis task.
+
+        * public/api/commits.php:
+        * public/include/commit-log-fetcher.php: Added.
+        (CommitLogFetcher)
+        (CommitLogFetcher::__construct): Added.
+        (CommitLogFetcher::repository_id_from_name): Added.
+        (CommitLogFetcher::fetch_between): Added.
+        (CommitLogFetcher::fetch_oldest): Added.
+        (CommitLogFetcher::fetch_latest): Added.
+        (CommitLogFetcher::fetch_last_reported): Added.
+        (CommitLogFetcher::fetch_revision): Added.
+        (CommitLogFetcher::commit_for_revision): Added.
+        (CommitLogFetcher::format_single_commit): Added.
+        (CommitLogFetcher::format_commit): Added.
+
 2016-03-09  Ryosuke Niwa  <rniwa@webkit.org>
 
         Build fix after r196870.
index bee8661..72e0492 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 require_once('../include/json-header.php');
+require_once('../include/commit-log-fetcher.php');
 
 function main($paths) {
     if (count($paths) < 1 || count($paths) > 2)
@@ -10,117 +11,41 @@ function main($paths) {
     if (!$db->connect())
         exit_with_error('DatabaseConnectionFailure');
 
+    $fetcher = new CommitLogFetcher($db);
+
     if (!is_numeric($paths[0])) {
-        $repository_name = $paths[0];
-        $repository_row = $db->select_first_row('repositories', 'repository', array('name' => $repository_name));
-        if (!$repository_row)
-            exit_with_error('RepositoryNotFound', array('repositoryName' => $repository_name));
-        $repository_id = $repository_row['repository_id'];
+        $repository_id = $fetcher->repository_id_from_name($paths[0]);
+        if (!$repository_id)
+            exit_with_error('RepositoryNotFound', array('repositoryName' => $paths[0]));
     } else
         $repository_id = intval($paths[0]);
 
     $filter = array_get($paths, 1);
-    $single_commit = NULL;
     $commits = array();
     if (!$filter) {
         $keyword = array_get($_GET, 'keyword');
         $from = array_get($_GET, 'from');
         $to = array_get($_GET, 'to');
-        $commits = fetch_commits_between($db, $repository_id, $from, $to, $keyword);
+        $commits = $fetcher->fetch_between($repository_id, $from, $to, $keyword);
     } else if ($filter == 'oldest') {
-        $single_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time');
+        $commits = $fetcher->fetch_oldest($repository_id);
     } else if ($filter == 'latest') {
-        $single_commit = $db->select_last_row('commits', 'commit', array('repository' => $repository_id), 'time');
+        $commits = $fetcher->fetch_latest($repository_id);
     } else if ($filter == 'last-reported') {
-        $single_commit = $db->select_last_row('commits', 'commit', array('repository' => $repository_id, 'reported' => true), 'time');
+        $commits = $fetcher->fetch_latest_reported($repository_id);
     } else if (ctype_alnum($filter)) {
-        $single_commit = commit_from_revision($db, $repository_id, $filter);
+        $commits = $fetcher->fetch_revision($repository_id, $filter);
     } else {
         $matches = array();
         if (!preg_match('/([A-Za-z0-9]+)[\:\-]([A-Za-z0-9]+)/', $filter, $matches))
             exit_with_error('UnknownFilter', array('repositoryName' => $repository_name, 'filter' => $filter));
-
-        $commits = fetch_commits_between($db, $repository_id, $matches[1], $matches[2]);
+        $commits = $fetcher->fetch_between($repository_id, $matches[1], $matches[2]);
     }
 
-    if ($single_commit) {
-        $committer = $db->select_first_row('committers', 'committer', array('id' => $single_commit['commit_committer']));
-        exit_with_success(array('commits' => array(format_commit($single_commit, $committer))));
-    }
-
-    exit_with_success(array('commits' => $commits));
-}
-
-function commit_from_revision($db, $repository_id, $revision) {
-    $all_but_first = substr($revision, 1);
-    if ($revision[0] == 'r' && ctype_digit($all_but_first))
-        $revision = $all_but_first;
-    $commit_info = array('repository' => $repository_id, 'revision' => $revision);
-    $row = $db->select_last_row('commits', 'commit', $commit_info);
-    if (!$row)
-        exit_with_error('UnknownCommit', $commit_info);
-    return $row;
-}
-
-function fetch_commits_between($db, $repository_id, $first, $second, $keyword = NULL) {
-    $statements = 'SELECT commit_id as "id",
-        commit_revision as "revision",
-        commit_parent as "parent",
-        commit_time as "time",
-        committer_name as "authorName",
-        committer_account as "authorEmail",
-        commit_message as "message"
-        FROM commits LEFT OUTER JOIN committers ON commit_committer = committer_id
-        WHERE commit_repository = $1 AND commit_reported = true';
-    $values = array($repository_id);
-
-    if ($first && $second) {
-        $first_commit = commit_from_revision($db, $repository_id, $first);
-        $second_commit = commit_from_revision($db, $repository_id, $second);
-        $first = $first_commit['commit_time'];
-        $second = $second_commit['commit_time'];
-        $column_name = 'commit_time';
-        if (!$first || !$second) {
-            $first = $first_commit['commit_order'];
-            $second = $second_commit['commit_order'];
-            $column_name = 'commit_order';
-        }
-
-        $in_order = $first < $second;
-        array_push($values, $in_order ? $first : $second);
-        $statements .= ' AND ' . $column_name . ' >= $' . count($values);
-        array_push($values, $in_order ? $second : $first);
-        $statements .= ' AND ' . $column_name . ' <= $' . count($values);
-    }
-
-    if ($keyword) {
-        array_push($values, '%' . str_replace(array('\\', '_', '%'), array('\\\\', '\\_', '\\%'), $keyword) . '%');
-        $keyword_index = '$' . count($values);
-        array_push($values, ltrim($keyword, 'r'));
-        $revision_index = '$' . count($values);
-        $statements .= "
-            AND ((committer_name LIKE $keyword_index OR committer_account LIKE $keyword_index) OR commit_revision = $revision_index)";
-    }
-
-    $commits = $db->query_and_fetch_all($statements . ' ORDER BY commit_time, commit_order', $values);
     if (!is_array($commits))
-        exit_with_error('FailedToFetchCommits', array('repository' => $repository_id, 'first' => $first, 'second' => $second));
-    foreach ($commits as &$commit)
-        $commit['time'] = Database::to_js_time($commit['time']);
-    return $commits;
-}
+        exit_with_error('FailedToFetchCommits', array('repository' => $repository_id, 'filter' => $filter));
 
-function format_commit($commit_row, $committer_row) {
-    return array(
-        'id' => $commit_row['commit_id'],
-        'revision' => $commit_row['commit_revision'],
-        'parent' => $commit_row['commit_parent'],
-        'time' => Database::to_js_time($commit_row['commit_time']),
-        'order' => $commit_row['commit_order'],
-        'authorName' => $committer_row ? $committer_row['committer_name'] : null,
-        'authorEmail' => $committer_row ? $committer_row['committer_account'] : null,
-        'message' => $commit_row['commit_message']
-    );
+    exit_with_success(array('commits' => $commits));
 }
 
 main(array_key_exists('PATH_INFO', $_SERVER) ? explode('/', trim($_SERVER['PATH_INFO'], '/')) : array());
diff --git a/Websites/perf.webkit.org/public/include/commit-log-fetcher.php b/Websites/perf.webkit.org/public/include/commit-log-fetcher.php
new file mode 100644 (file)
index 0000000..2ae5e45
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+
+class CommitLogFetcher {
+
+    function __construct($db) {
+        $this->db = $db;
+        $this->commits = array();
+    }
+
+    function repository_id_from_name($name)
+    {
+        $repository_row = $this->db->select_first_row('repositories', 'repository', array('name' => $name));
+        if (!$repository_row)
+            return NULL;
+        return $repository_row['repository_id'];
+    }
+
+    function fetch_between($repository_id, $first, $second, $keyword = NULL) {
+        $statements = 'SELECT commit_id as "id",
+            commit_revision as "revision",
+            commit_parent as "parent",
+            commit_time as "time",
+            committer_name as "authorName",
+            committer_account as "authorEmail",
+            commit_message as "message"
+            FROM commits LEFT OUTER JOIN committers ON commit_committer = committer_id
+            WHERE commit_repository = $1 AND commit_reported = true';
+        $values = array($repository_id);
+
+        if ($first && $second) {
+            $first_commit = $this->commit_for_revision($repository_id, $first);
+            $second_commit = $this->commit_for_revision($repository_id, $second);
+            $first = $first_commit['commit_time'];
+            $second = $second_commit['commit_time'];
+            $column_name = 'commit_time';
+            if (!$first || !$second) {
+                $first = $first_commit['commit_order'];
+                $second = $second_commit['commit_order'];
+                $column_name = 'commit_order';
+            }
+
+            $in_order = $first < $second;
+            array_push($values, $in_order ? $first : $second);
+            $statements .= ' AND ' . $column_name . ' >= $' . count($values);
+            array_push($values, $in_order ? $second : $first);
+            $statements .= ' AND ' . $column_name . ' <= $' . count($values);
+        }
+
+        if ($keyword) {
+            array_push($values, '%' . str_replace(array('\\', '_', '%'), array('\\\\', '\\_', '\\%'), $keyword) . '%');
+            $keyword_index = '$' . count($values);
+            array_push($values, ltrim($keyword, 'r'));
+            $revision_index = '$' . count($values);
+            $statements .= "
+                AND ((committer_name LIKE $keyword_index OR committer_account LIKE $keyword_index) OR commit_revision = $revision_index)";
+        }
+
+        $commits = $this->db->query_and_fetch_all($statements . ' ORDER BY commit_time, commit_order', $values);
+        if (!is_array($commits))
+            return NULL;
+
+        foreach ($commits as &$commit)
+            $commit['time'] = Database::to_js_time($commit['time']);
+
+        return $commits;
+    }
+
+    function fetch_oldest($repository_id) {
+        return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
+    }
+
+    function fetch_latest($repository_id) {
+        return $this->format_single_commit($this->db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time'));
+    }
+
+    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'));
+    }
+
+    function fetch_revision($repository_id, $revision) {
+        return $this->format_single_commit($this->db->commit_for_revision($repository_id, $revision));
+    }
+
+    private function commit_for_revision($repository_id, $revision) {
+        $all_but_first = substr($revision, 1);
+        if ($revision[0] == 'r' && ctype_digit($all_but_first))
+            $revision = $all_but_first;
+        $commit_info = array('repository' => $repository_id, 'revision' => $revision);
+        $row = $this->db->select_last_row('commits', 'commit', $commit_info);
+        if (!$row)
+            exit_with_error('UnknownCommit', $commit_info);
+        return $row;
+    }
+
+    private function format_single_commit($commit_row) {
+        $committer = $this->db->select_first_row('committers', 'committer', array('id' => $commit_row['commit_committer']));
+        return $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'],
+            'time' => Database::to_js_time($commit_row['commit_time']),
+            'order' => $commit_row['commit_order'],
+            'authorName' => $committer_row ? $committer_row['committer_name'] : null,
+            'authorEmail' => $committer_row ? $committer_row['committer_account'] : null,
+            'message' => $commit_row['commit_message']
+        );
+    }
+}
+
+?>
\ No newline at end of file