7264153d44c1f446cd541c3d5c885f1f366589da
[WebKit-https.git] / Websites / perf.webkit.org / public / include / repository-group-finder.php
1 <?php
2
3 class RepositoryGroupFinder
4 {
5
6     function __construct($db, $triggerable_id) {
7         $this->db = $db;
8         $this->triggerable_id = $triggerable_id;
9         $this->repositories_by_group = NULL;
10     }
11
12     function find_by_repositories($repositories)
13     {
14         if ($this->repositories_by_group === NULL)
15             $this->populate_map();
16         sort($repositories, SORT_NUMERIC);
17         foreach ($this->repositories_by_group as $group_id => $group_repositories) {
18             if (count($repositories) == count($group_repositories) && !array_diff($repositories, $group_repositories))
19                 return $group_id;
20         }
21         return NULL;
22     }
23
24     private function populate_map()
25     {
26         $repository_rows = $this->db->query_and_fetch_all('SELECT * FROM triggerable_repositories WHERE trigrepo_group IN
27             (SELECT repositorygroup_id FROM triggerable_repository_groups WHERE repositorygroup_triggerable = $1)
28             ORDER BY trigrepo_group, trigrepo_repository', array($this->triggerable_id));
29         if ($repository_rows === FALSE)
30             exit_with_error('FailedToFetchRepositoryGroups', array('triggerable' => $this->triggerable_id));
31
32         $repositories_by_group = array();
33         foreach ($repository_rows as &$row) {
34             $group_id = $row['trigrepo_group'];
35             array_ensure_item_has_array($repositories_by_group, $group_id);
36             array_push($repositories_by_group[$group_id], $row['trigrepo_repository']);
37         }
38
39         $this->repositories_by_group = &$repositories_by_group;
40     }
41 }
42
43 ?>