continued :)
[phpgitweb.git] / htdocs / pages / shortlog.class.php
diff --git a/htdocs/pages/shortlog.class.php b/htdocs/pages/shortlog.class.php
new file mode 100644 (file)
index 0000000..a7293c8
--- /dev/null
@@ -0,0 +1,140 @@
+<?php
+/* shortlog.class.php - phpgitweb
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+require_once('lib/graph.class.php');
+
+class shortlog {
+       private $project;
+       private $graph_data;
+       
+       public function generate_shortlog($project, $head, $max, $skip, $file = null, $pages = true, $next_page = 0) {
+               $this->project = $project;
+               $content = new ContentProvider('shortlog', 'shortlog');
+               $commits = GitCommand::get_commits($project['path'], $head, $max+$skip+1, 0, $file);
+               
+               $this->graph_data = new graph_data_generator();
+               if($head == null) {
+                       //add all refs to the graph
+                       foreach($this->project['refs'] as $ref => $rhash) {
+                               if(preg_match('#^refs/heads/#i', $ref) && preg_match('/^[a-f0-9]*$/i', $rhash)) {
+                                       $this->graph_data->add_branch($rhash, $ref);
+                               }
+                       }
+                       foreach($this->project['refs'] as $ref => $rhash) {
+                               if(preg_match('#^refs/remotes/#i', $ref) && preg_match('/^[a-f0-9]*$/i', $rhash)) {
+                                       $this->graph_data->add_branch($rhash, $ref);
+                               }
+                       }
+               }
+               $this->graph_data->parse($commits);
+               
+               $commit_counter = 0;
+               foreach($commits as $commit) {
+                       $commit_counter++;
+                       if($commit_counter < $skip)
+                               continue;
+                       if($commit_counter > $max+$skip) {
+                               if($pages) {
+                                       $content->append('entries', new ContentProvider('shortlog', 'shortlog_page', array("page" => $next_page)));
+                               } else
+                                       $content->append('entries', new ContentProvider('shortlog', 'shortlog_more'));
+                       } else
+                               $content->append('entries', $this->shortlog_entry(($commit_counter % 2 ? 'dark' : 'light'), $commit));
+               }
+               
+               return $content;
+       }
+       
+       private function shortlog_entry($class, $commit) {
+               $entry = new ContentProvider('shortlog', 'shortlog_entry');
+               $entry->set('class', $class);
+               $entry->set('hash', $commit['id']);
+               $entry->set('author', htmlentities($commit['author']));
+               $entry->set('message', htmlentities(Tools::chop_text($commit['text'], 50, 5)));
+               $age = time() - $commit['committer_time'];
+               $date_str = date("Y-m-d", ($commit['committer_time'] ? $commit['committer_time'] : $commit['author_time']));
+               $age_calc = Tools::age_calculate($commit['committer_time']);
+               $age_str = $age_calc['age_str'];
+               if($age > 60*60*24*7*2) {
+                       $entry->set('date', $age_str);
+                       $entry->set('age', $date_str);
+               } else {
+                       $entry->set('date', $date_str);
+                       $entry->set('age', $age_str);
+               }
+               $entry->set('graph_data', $this->graph_data->get_graph($commit['id']));
+               
+               $entry->set('refs', $this->shortlog_commit_refs($commit['id']));
+               
+               return $entry;
+       }
+       
+       private function shortlog_commit_refs($hash) {
+               if(!is_array($this->project['refs']))
+                       return "";
+               $refs = new ContentProvider('shortlog', 'shortlog_refs');
+               $found = false;
+               foreach($this->project['refs'] as $ref => $rhash) {
+                       if(strtolower($rhash) == strtolower($hash)) {
+                               $refexp = explode('/', $ref, 3);
+                               $reftype = $refexp[1];
+                               if($reftype == 'heads')
+                                       $reftype = 'head';
+                               else if($reftype == 'remotes')
+                                       $reftype = 'remote';
+                               else if($reftype == 'tags')
+                                       $reftype = 'tag';
+                               $refs->append('refs', new ContentProvider('shortlog', 'shortlog_ref_'.$reftype, array("name" => $refexp[2], "ref_link" => $ref)));
+                               $found = true;
+                       }
+               }
+               return ($found ? $refs : "");
+       }
+       
+}
+
+
+class page_shortlog {
+    private $page, $phpgitweb;
+       
+    public function main($phpgitweb, $project) {
+               $this->phpgitweb = $phpgitweb;
+               $this->project = $project;
+               if(!$this->project)
+                       return;
+               $project['refs'] = $phpgitweb->get_project_loader()->getProjectRefs($project);
+               $this->page = new ContentProvider('shortlog', 'main');
+               
+               //pages
+               if(array_key_exists('pg', $_GET)) {
+                       $skip = $_GET['pg'] * 100;
+                       $next_page = $_GET['pg'] + 1;
+               } else {
+                       $skip = 0;
+                       $next_page = 1;
+               }
+               
+               $shortlog = new shortlog();
+               $this->page->set('shortlog', $shortlog->generate_shortlog($project, null, 100, $skip, null, true, $next_page));
+               
+               return $this->page;
+       }
+       
+}
+
+?>
\ No newline at end of file