0bd823f15b837c66b0eccce332c61beee344ed75
[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         private $have_more = false;
26         
27         public function generate_shortlog($project, $head, $max, $skip, $file = null, $pages = true, $next_page = 0) {
28                 $this->project = $project;
29                 $content = new ContentProvider('shortlog', 'shortlog');
30                 
31                 if(GitConfig::GITGRAPH_ENABLE) {
32                         if($max+$skip >= 2000) { //only load the last 2k commits
33                                 $real_skip = ($max+$skip) - 2000;
34                                 $skip -= $real_skip;
35                         } else
36                                 $real_skip = 0;
37                 } else {
38                         $real_skip = $skip;
39                         $skip = 0;
40                 }
41                 
42                 $commits = GitCommand::get_commits($project['path'], $head, $max+$skip+1, $real_skip, $file);
43                 
44                 if(GitConfig::GITGRAPH_ENABLE) {
45                         $this->graph_data = new graph_data_generator();
46                         if($head == null) {
47                                 //add all refs to the graph
48                                 $rhash = GitCommand::get_hash($project['path'], "HEAD");
49                                 if($rhash)
50                                         $this->graph_data->add_branch($rhash, null);
51                                 
52                                 foreach($this->project['refs'] as $ref => $rhash) {
53                                         if(preg_match('#^refs/heads/#i', $ref) && preg_match('/^[a-f0-9]*$/i', $rhash)) {
54                                                 $this->graph_data->add_branch($rhash, $ref);
55                                         }
56                                 }
57                                 foreach($this->project['refs'] as $ref => $rhash) {
58                                         if(preg_match('#^refs/remotes/#i', $ref) && preg_match('/^[a-f0-9]*$/i', $rhash)) {
59                                                 $this->graph_data->add_branch($rhash, $ref);
60                                         }
61                                 }
62                         }
63                         $this->graph_data->parse($commits);
64                         $content->set('graph_data', $this->graph_data->get_header_graph());
65                 }
66                 
67                 $commit_counter = 0;
68                 $this->first_commit = $commits[0];
69                 foreach($commits as $commit) {
70                         $commit_counter++;
71                         if($commit_counter < $skip)
72                                 continue;
73                         if($commit_counter > $max+$skip) {
74                                 $this->have_more = true;
75                                 if($pages) {
76                                         $content->append('entries', new ContentProvider('shortlog', 'shortlog_page', array("page" => $next_page)));
77                                 } else
78                                         $content->append('entries', new ContentProvider('shortlog', 'shortlog_more'));
79                         } else
80                                 $content->append('entries', $this->shortlog_entry(($commit_counter % 2 ? 'dark' : 'light'), $commit));
81                 }
82                 
83                 return $content;
84         }
85         
86         public function get_first_commit() {
87                 return $this->first_commit;
88         }
89         
90         public function get_have_more() {
91                 return $this->have_more;
92         }
93         
94         private function shortlog_entry($class, $commit) {
95                 $entry = new ContentProvider('shortlog', 'shortlog_entry');
96                 $entry->set('class', $class);
97                 $entry->set('hash', $commit['id']);
98                 $entry->set('author', htmlentities($commit['author']));
99                 $entry->set('message', htmlentities(Tools::chop_text($commit['text'], 50, 5)));
100                 $age = time() - $commit['committer_time'];
101                 $date_str = date("Y-m-d", ($commit['committer_time'] ? $commit['committer_time'] : $commit['author_time']));
102                 $age_calc = Tools::age_calculate($commit['committer_time']);
103                 $age_str = $age_calc['age_str'];
104                 if($age > 60*60*24*7*2) {
105                         $entry->set('date', $age_str);
106                         $entry->set('age', $date_str);
107                 } else {
108                         $entry->set('date', $date_str);
109                         $entry->set('age', $age_str);
110                 }
111                 if(GitConfig::GITGRAPH_ENABLE)
112                         $entry->set('graph_data', $this->graph_data->get_graph($commit['id']));
113                 
114                 $entry->set('refs', $this->shortlog_commit_refs($this->project, $commit['id']));
115                 
116                 return $entry;
117         }
118         
119         public function shortlog_commit_refs($project, $hash) {
120                 if(!is_array($project['refs']))
121                         return "";
122                 $refs = new ContentProvider('shortlog', 'shortlog_refs');
123                 $found = false;
124                 foreach($project['refs'] as $ref => $rhash) {
125                         if(strtolower($rhash) == strtolower($hash)) {
126                                 $refexp = explode('/', $ref, 3);
127                                 $reftype = $refexp[1];
128                                 if($reftype == 'heads')
129                                         $reftype = 'head';
130                                 else if($reftype == 'remotes')
131                                         $reftype = 'remote';
132                                 else if($reftype == 'tags')
133                                         $reftype = 'tag';
134                                 $refs->append('refs', new ContentProvider('shortlog', 'shortlog_ref_'.$reftype, array("name" => $refexp[2], "ref_link" => $ref)));
135                                 $found = true;
136                         }
137                 }
138                 return ($found ? $refs : "");
139         }
140         
141 }
142
143
144 class page_shortlog {
145     private $page, $phpgitweb;
146         
147     public function main($phpgitweb, $project) {
148                 $this->phpgitweb = $phpgitweb;
149                 $this->project = $project;
150                 if(!$this->project)
151                         return new ContentProvider('main', 'err400');
152                 $project['refs'] = $phpgitweb->get_project_loader()->getProjectRefs($project);
153                 $phpgitweb->append_header_nav("shortlog", null, true);
154                 $phpgitweb->append_title("shortlog");
155                 
156                 $this->page = new ContentProvider('shortlog', 'main');
157                 
158                 //pages
159                 if(array_key_exists('pg', $_GET)) {
160                         $pg = $_GET['pg'];
161                         if($pg < 0)
162                                 $pg = 0;
163                         $skip = $_GET['pg'] * 100;
164                         $next_page = $_GET['pg'] + 1;
165                 } else {
166                         $pg = 0;
167                         $skip = 0;
168                         $next_page = 1;
169                 }
170                 
171                 $subnav = new ContentProvider('shortlog', 'shortlog_subnav');
172                 $phpgitweb->append_sub_nav($subnav);
173                 
174                 if($pg) {
175                         $subnav->set('first', new ContentProvider('shortlog', 'shortlog_subnav_first_link'));
176                         $subnav->set('prev', new ContentProvider('shortlog', 'shortlog_subnav_prev_link', array('page' => ($pg - 1))));
177                 } else {
178                         $subnav->set('first', new ContentProvider('shortlog', 'shortlog_subnav_first'));
179                         $subnav->set('prev', new ContentProvider('shortlog', 'shortlog_subnav_prev'));
180                 }
181                 
182                 $shortlog = new shortlog();
183                 $this->page->set('shortlog', $shortlog->generate_shortlog($project, null, 100, $skip, null, true, $next_page));
184                 
185                 if($shortlog->get_have_more())
186                         $subnav->set('next', new ContentProvider('shortlog', 'shortlog_subnav_next_link', array('page' => ($pg + 1))));
187                 else
188                         $subnav->set('next', new ContentProvider('shortlog', 'shortlog_subnav_next'));
189                 
190                 return $this->page;
191         }
192         
193 }
194
195 ?>