34cfa8042f35aa6a8108e6c900a979fda062501d
[phpgitweb.git] / htdocs / pages / shortlog.class.php
1 <?php
2 /* shortlog.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 require_once('lib/graph.class.php');
20
21 class shortlog {
22         private $project;
23         private $graph_data;
24         private $first_commit;
25         
26         public function generate_shortlog($project, $head, $max, $skip, $file = null, $pages = true, $next_page = 0) {
27                 $this->project = $project;
28                 $content = new ContentProvider('shortlog', 'shortlog');
29                 
30                 if(GitConfig::GITGRAPH_ENABLE) {
31                         if($max+$skip >= 2000) { //only load the last 2k commits
32                                 $real_skip = ($max+$skip) - 2000;
33                                 $skip -= $real_skip;
34                         } else
35                                 $real_skip = 0;
36                 } else {
37                         $real_skip = $skip;
38                         $skip = 0;
39                 }
40                 
41                 $commits = GitCommand::get_commits($project['path'], $head, $max+$skip+1, $real_skip, $file);
42                 
43                 if(GitConfig::GITGRAPH_ENABLE) {
44                         $this->graph_data = new graph_data_generator();
45                         if($head == null) {
46                                 //add all refs to the graph
47                                 foreach($this->project['refs'] as $ref => $rhash) {
48                                         if(preg_match('#^refs/heads/#i', $ref) && preg_match('/^[a-f0-9]*$/i', $rhash)) {
49                                                 $this->graph_data->add_branch($rhash, $ref);
50                                         }
51                                 }
52                                 foreach($this->project['refs'] as $ref => $rhash) {
53                                         if(preg_match('#^refs/remotes/#i', $ref) && preg_match('/^[a-f0-9]*$/i', $rhash)) {
54                                                 $this->graph_data->add_branch($rhash, $ref);
55                                         }
56                                 }
57                         }
58                         $this->graph_data->parse($commits);
59                         $content->set('graph_data', $this->graph_data->get_header_graph());
60                 }
61                 
62                 $commit_counter = 0;
63                 $this->first_commit = $commits[0];
64                 foreach($commits as $commit) {
65                         $commit_counter++;
66                         if($commit_counter < $skip)
67                                 continue;
68                         if($commit_counter > $max+$skip) {
69                                 if($pages) {
70                                         $content->append('entries', new ContentProvider('shortlog', 'shortlog_page', array("page" => $next_page)));
71                                 } else
72                                         $content->append('entries', new ContentProvider('shortlog', 'shortlog_more'));
73                         } else
74                                 $content->append('entries', $this->shortlog_entry(($commit_counter % 2 ? 'dark' : 'light'), $commit));
75                 }
76                 
77                 return $content;
78         }
79         
80         public function get_first_commit() {
81                 return $this->first_commit;
82         }
83         
84         private function shortlog_entry($class, $commit) {
85                 $entry = new ContentProvider('shortlog', 'shortlog_entry');
86                 $entry->set('class', $class);
87                 $entry->set('hash', $commit['id']);
88                 $entry->set('author', htmlentities($commit['author']));
89                 $entry->set('message', htmlentities(Tools::chop_text($commit['text'], 50, 5)));
90                 $age = time() - $commit['committer_time'];
91                 $date_str = date("Y-m-d", ($commit['committer_time'] ? $commit['committer_time'] : $commit['author_time']));
92                 $age_calc = Tools::age_calculate($commit['committer_time']);
93                 $age_str = $age_calc['age_str'];
94                 if($age > 60*60*24*7*2) {
95                         $entry->set('date', $age_str);
96                         $entry->set('age', $date_str);
97                 } else {
98                         $entry->set('date', $date_str);
99                         $entry->set('age', $age_str);
100                 }
101                 if(GitConfig::GITGRAPH_ENABLE)
102                         $entry->set('graph_data', $this->graph_data->get_graph($commit['id']));
103                 
104                 $entry->set('refs', $this->shortlog_commit_refs($commit['id']));
105                 
106                 return $entry;
107         }
108         
109         private function shortlog_commit_refs($hash) {
110                 if(!is_array($this->project['refs']))
111                         return "";
112                 $refs = new ContentProvider('shortlog', 'shortlog_refs');
113                 $found = false;
114                 foreach($this->project['refs'] as $ref => $rhash) {
115                         if(strtolower($rhash) == strtolower($hash)) {
116                                 $refexp = explode('/', $ref, 3);
117                                 $reftype = $refexp[1];
118                                 if($reftype == 'heads')
119                                         $reftype = 'head';
120                                 else if($reftype == 'remotes')
121                                         $reftype = 'remote';
122                                 else if($reftype == 'tags')
123                                         $reftype = 'tag';
124                                 $refs->append('refs', new ContentProvider('shortlog', 'shortlog_ref_'.$reftype, array("name" => $refexp[2], "ref_link" => $ref)));
125                                 $found = true;
126                         }
127                 }
128                 return ($found ? $refs : "");
129         }
130         
131 }
132
133
134 class page_shortlog {
135     private $page, $phpgitweb;
136         
137     public function main($phpgitweb, $project) {
138                 $this->phpgitweb = $phpgitweb;
139                 $this->project = $project;
140                 if(!$this->project)
141                         return;
142                 $project['refs'] = $phpgitweb->get_project_loader()->getProjectRefs($project);
143                 $phpgitweb->append_header_nav("shortlog", null, true);
144                 
145                 $this->page = new ContentProvider('shortlog', 'main');
146                 
147                 //pages
148                 if(array_key_exists('pg', $_GET)) {
149                         $skip = $_GET['pg'] * 100;
150                         $next_page = $_GET['pg'] + 1;
151                 } else {
152                         $skip = 0;
153                         $next_page = 1;
154                 }
155                 
156                 $shortlog = new shortlog();
157                 $this->page->set('shortlog', $shortlog->generate_shortlog($project, null, 100, $skip, null, true, $next_page));
158                 
159                 return $this->page;
160         }
161         
162 }
163
164 ?>