/**
  * Be careful due to a svn bug, this method will be call twice by svn server <= 1.7
  * The first time with the new commit_message as expected
  * The second time with the OLD commit message (oh yeah baby!)
  * http://subversion.tigris.org/issues/show_bug.cgi?id=3085
  *
  * @param String $repository_path
  * @param String $revision
  * @param String $user_name
  * @param String $old_commit_message
  */
 public function update($repository_path, $revision, $user_name, $old_commit_message)
 {
     $project = $this->svn_hooks->getProjectFromRepositoryPath($repository_path);
     $user = $this->svn_hooks->getUserByName($user_name);
     $message = $this->svn_hooks->getMessageFromRevision($repository_path, $revision);
     $this->dao->updateCommitMessage($project->getID(), $revision, $message);
     $this->removePreviousCrossReferences($project, $revision, $old_commit_message);
     // Marvelous, extractCrossRef depends on globals group_id to find the group
     // when it's not explicit... yeah!
     $GLOBALS['group_id'] = $project->getID();
     $this->reference_manager->extractCrossRef($message, $revision, ReferenceManager::REFERENCE_NATURE_SVNREVISION, $project->getID(), $user->getId());
 }
 function process($owner_type, $owner_id)
 {
     $dao = new SvnCommitsDao(CodendiDataAccess::instance());
     //The default duration is 3 months back
     $nb_weeks = 4 * 3;
     $duration = 7 * $nb_weeks;
     $day = 24 * 3600;
     $week = 7 * $day;
     //compute the stats
     $stats = array();
     $nb_of_commits = array();
     foreach ($dao->statsByGroupId($owner_id, $duration) as $row) {
         $stats[$row['whoid']]['by_day'][$row['day'] * $day] = $row['nb_commits'];
         $stats[$row['whoid']]['by_week'][$row['week']] = $row['nb_commits'];
         $this->tmp_nb_of_commit[$row['whoid']] = (isset($this->tmp_nb_of_commit[$row['whoid']]) ? $this->tmp_nb_of_commit[$row['whoid']] : 0) + $row['nb_commits'];
     }
     if (count($stats)) {
         //sort the results
         uksort($stats, array($this, 'sortByTop'));
         $today = $_SERVER['REQUEST_TIME'];
         $start_of_period = strtotime("-{$nb_weeks} weeks");
         //fill-in the holes
         $tmp_stats = array();
         foreach ($stats as $whoid => $stat) {
             $tmp_stats = array();
             for ($i = $start_of_period; $i <= $today; $i += $week) {
                 $w = (int) date('W', $i);
                 $tmp_stats[$w] = isset($stat['by_week'][$w]) ? $stat['by_week'][$w] : '0';
             }
             $stats[$whoid]['by_week'] = $tmp_stats;
         }
         //fill-in the labels
         $dates = array();
         for ($i = $start_of_period; $i <= $today; $i += $week) {
             $dates[] = date('M d', $i);
         }
         $nb_commiters = count($stats);
         $widgetFormatter = new Widget_ProjectSvnStats_Layout($nb_commiters);
         $legendRatio = $widgetFormatter->legend_ratio;
         $chartWidth = $widgetFormatter->getChartWidth();
         $chartHeigh = $widgetFormatter->getChartHeigh();
         $legend_x_position = $widgetFormatter->getLegendXPosition();
         $legend_y_position = $widgetFormatter->getLegendYPosition();
         $imgBottomMargin = $widgetFormatter->getImageBottomMargin();
         $legendAlign = $widgetFormatter->getLegendAlign();
         // @TODO: Centralize stuff at Chart class level to properly render a Jpgraph chart with a large number of legend items
         //Build the chart
         $c = new Chart($chartWidth, $chartHeigh);
         $c->SetScale('textlin');
         $c->img->SetMargin(40, 20, 20, $imgBottomMargin);
         $c->xaxis->SetTickLabels($dates);
         $c->legend->Pos($legend_x_position, $legend_y_position, 'left', $legendAlign);
         if ($legendRatio >= 1) {
             $c->legend->setColumns(2);
         }
         $colors = array_reverse(array_slice($GLOBALS['HTML']->getChartColors(), 0, $nb_commiters));
         $nb_colors = count($colors);
         $bars = array();
         $i = 0;
         foreach ($stats as $whoid => $stat) {
             $l = new BarPlot(array_values($stat['by_week']));
             $color = $colors[$i++ % $nb_colors];
             $l->SetColor($color . ':0.7');
             $l->setFillColor($color);
             if ($user = UserManager::instance()->getUserById($whoid)) {
                 $l->SetLegend(UserHelper::instance()->getDisplayNameFromUser($user));
             } else {
                 $l->SetLegend('Unknown user (' . (int) $whoid . ')');
             }
             $bars[] = $l;
         }
         $gbplot = new AccBarPlot($bars);
         $c->Add($gbplot);
     } else {
         $error = "No commits in the last {$duration} days";
         $c = new ErrorChart("No logged commits", $error, 400, 300);
     }
     echo $c->stroke();
 }