/WebSVN/blame.php
1,8 → 1,6
<?php
# vim:et:ts=3:sts=3:sw=3:fdm=marker:
 
// WebSVN - Subversion repository viewing via the web using PHP
// Copyright © 2004-2006 Tim Armes, Matt Sicker
// Copyright (C) 2004-2006 Tim Armes
//
// 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
11,12 → 9,12
//
// 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
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// --
//
25,107 → 23,241
// Show the blame information of a file.
//
 
require_once 'include/setup.inc';
require_once 'include/svnlook.inc';
require_once 'include/utils.inc';
require_once 'include/template.inc';
require_once 'include/setup.php';
require_once 'include/svnlook.php';
require_once 'include/utils.php';
require_once 'include/template.php';
 
$vars['action'] = $lang['BLAME'];
 
// Make sure that we have a repository
if (!$rep)
{
renderTemplate404('blame','NOREP');
}
 
$svnrep = new SVNRepository($rep);
 
// If there's no revision info, go to the lastest revision for this path
$history = $svnrep->getLog($path, '', '', true);
$youngest = $history->entries[0]->rev;
$history = $svnrep->getLog($path, 'HEAD', 1, false, 2, ($path == '/') ? '' : $peg);
 
if (empty($rev))
$rev = $youngest;
if (!$history)
{
unset($vars['error']);
$history = $svnrep->getLog($path, '', '', false, 2, ($path == '/') ? '' : $peg);
 
if ($path{0} != '/')
$ppath = '/'.$path;
else
$ppath = $path;
if (!$history)
{
renderTemplate404('blame','NOPATH');
}
}
$youngest = ($history && isset($history->entries[0])) ? $history->entries[0]->rev : false;
 
if (empty($rev))
{
$rev = $youngest;
}
else
{
$history = $svnrep->getLog($path, $rev, '', false, 2, $peg);
if (!$history)
{
renderTemplate404('blame','NOPATH');
}
}
 
if ($path[0] != '/')
{
$ppath = '/'.$path;
}
else
{
$ppath = $path;
}
 
// Find the parent path (or the whole path if it's already a directory)
$pos = strrpos($ppath, '/');
$parent = substr($ppath, 0, $pos + 1);
 
$vars['repname'] = $rep->getDisplayName();
$vars['rev'] = $rev;
$vars['path'] = $ppath;
$vars['peg'] = $peg;
$vars['path'] = str_replace('%2F', '/', rawurlencode($ppath));
$vars['safepath'] = escape($ppath);
 
createDirLinks($rep, $ppath, $rev, $showchanged);
if (isset($history->entries[0]))
{
$vars['log'] = xml_entities($history->entries[0]->msg);
$vars['date'] = $history->entries[0]->date;
$vars['age'] = datetimeFormatDuration(time() - strtotime($history->entries[0]->date));
$vars['author'] = $history->entries[0]->author;
}
 
$listing = array();
createPathLinks($rep, $ppath, $passrev, $peg);
$passRevString = createRevAndPegString($rev, $peg);
 
// Get the contents of the file
$tfname = tempnam('temp', '');
$svnrep->getFileContents($path, $tfname, $rev, '', true);
if ($rev != $youngest)
{
$vars['goyoungesturl'] = $config->getURL($rep, $path, 'blame').createRevAndPegString('', $peg);
$vars['goyoungestlink'] = '<a href="'.$vars['goyoungesturl'].'"'.($youngest ? ' title="'.$lang['REV'].' '.$youngest.'"' : '').'>'.$lang['GOYOUNGEST'].'</a>';
}
 
$filecache = array();
$revurl = $config->getURL($rep, $path, 'blame');
 
if ($file = fopen($tfname, 'r'))
if ($rev < $youngest)
{
// Get the blame info
$tbname = tempnam('temp', '');
$svnrep->getBlameDetails($path, $tbname, $rev);
$ent = true;
$extension = strrchr(basename($path), '.');
if (($extension && isset($extEnscript[$extension]) && ('php' == $extEnscript[$extension])) || ($config->useEnscript))
$ent = false;
$history2 = $svnrep->getLog($path, $rev, $youngest, true, 2, $peg);
 
if ($blame = fopen($tbname, 'r'))
{
// Create an array of version/author/line
$index = 0;
while (!feof($blame) && !feof($file))
{
$blameline = fgets($blame);
if ($blameline != '')
{
list($revision, $author) = sscanf($blameline, '%d %s');
$listing[$index]['lineno'] = $index + 1;
$url = $config->getURL($rep, $parent, 'dir');
$listing[$index]['revision'] = "<a href=\"${url}rev=$revision&amp;sc=1\">$revision</a>";
if (isset($history2->entries[1]))
{
$nextRev = $history2->entries[1]->rev;
if ($nextRev != $youngest)
{
$vars['nextrev'] = $nextRev;
$vars['nextrevurl'] = $revurl.createRevAndPegString($nextRev, $peg);
}
}
 
$listing[$index]['author'] = $author;
if ($ent)
$line = replaceEntities(rtrim(fgets($file)), $rep);
else
$line = rtrim(fgets($file));
unset($vars['error']);
}
 
$listing[$index]['line'] = hardspace($line);
if (trim($listing[$index]['line']) == '')
$listing[$index]['line'] = '&nbsp;';
$index++;
}
}
fclose($blame);
}
fclose($file);
if (isset($history->entries[1]))
{
$prevRev = $history->entries[1]->rev;
$prevPath = $history->entries[1]->path;
$vars['prevrev'] = $prevRev;
$vars['prevrevurl'] = $revurl.createRevAndPegString($prevRev, $peg);
}
 
unlink($tfname);
unlink($tbname);
$vars['revurl'] = $config->getURL($rep, $path, 'revision').$passRevString;
$vars['revlink'] = '<a href="'.$vars['revurl'].'">'.$lang['LASTMOD'].'</a>';
 
$vars['version'] = $version;
$vars['logurl'] = $config->getURL($rep, $path, 'log').$passRevString;
$vars['loglink'] = '<a href="'.$vars['logurl'].'">'.$lang['VIEWLOG'].'</a>';
 
if (!$rep->hasReadAccess($path, false))
$vars['noaccess'] = true;
$vars['filedetailurl'] = $config->getURL($rep, $path, 'file').$passRevString;
$vars['filedetaillink'] = '<a href="'.$vars['filedetailurl'].'">'.$lang['FILEDETAIL'].'</a>';
 
parseTemplate($rep->getTemplatePath().'header.tmpl', $vars, $listing);
parseTemplate($rep->getTemplatePath().'blame.tmpl', $vars, $listing);
parseTemplate($rep->getTemplatePath().'footer.tmpl', $vars, $listing);
?>
if ($history == null || count($history->entries) > 1)
{
$vars['diffurl'] = $config->getURL($rep, $path, 'diff').$passRevString;
$vars['difflink'] = '<a href="'.$vars['diffurl'].'">'.$lang['DIFFPREV'].'</a>';
}
 
if ($rep->isRssEnabled())
{
$vars['rssurl'] = $config->getURL($rep, $path, 'rss').createRevAndPegString('', $peg);
$vars['rsslink'] = '<a href="'.$vars['rssurl'].'">'.$lang['RSSFEED'].'</a>';
}
 
// Check for binary file type before grabbing blame information.
$svnMimeType = $svnrep->getProperty($path, 'svn:mime-type', $rev, $peg);
 
if (!$rep->getIgnoreSvnMimeTypes() && preg_match('~application/*~', $svnMimeType))
{
$vars['warning'] = 'Cannot display blame info for binary file. (svn:mime-type = '.$svnMimeType.')';
$vars['javascript'] = '';
}
else
{
// Get the contents of the file
$tfname = tempnamWithCheck($config->getTempDir(), '');
$highlighted = $svnrep->getFileContents($path, $tfname, $rev, $peg, '', 'line');
 
if ($file = fopen($tfname, 'r'))
{
// Get the blame info
$tbname = tempnamWithCheck($config->getTempDir(), '');
 
$svnrep->getBlameDetails($path, $tbname, $rev, $peg);
 
if ($blame = fopen($tbname, 'r'))
{
// Create an array of version/author/line
 
$index = 0;
$seen_rev = array();
$last_rev = '';
$row_class = '';
 
while (!feof($blame) && !feof($file))
{
$blameline = rtrim(fgets($blame), "\n\r");
 
if ($blameline != '')
{
list($revision, $author, $remainder) = sscanf($blameline, '%d %s %s');
$empty = !$remainder;
 
$listvar = &$listing[$index];
$listvar['lineno'] = $index + 1;
 
if ($last_rev != $revision)
{
$listvar['revision'] = '<a id="l'.$index.'-rev" class="blame-revision" href="'.$config->getURL($rep, $path, 'blame').createRevAndPegString($revision, $peg ? $peg : $rev).'">'.$revision.'</a>';
$seen_rev[$revision] = 1;
$row_class = ($row_class == 'light') ? 'dark' : 'light';
$listvar['author'] = $author;
}
else
{
$listvar['revision'] = '';
$listvar['author'] = '';
}
 
$listvar['row_class'] = $row_class;
$last_rev = $revision;
 
$line = rtrim(fgets($file), "\n\r");
if (!$highlighted)
{
$line = escape(toOutputEncoding($line));
}
$listvar['line'] = ($empty) ? '&nbsp;' : wrapInCodeTagIfNecessary($line);
$index++;
}
}
fclose($blame);
}
 
fclose($file);
@unlink($tbname);
}
@unlink($tfname);
 
// Build the necessary JavaScript as an array of lines, then join them with \n
$javascript = array();
$javascript[] = '<script type="text/javascript" src="'.$locwebsvnhttp.'/javascript/blame-popup.js"></script>';
$javascript[] = '<script type="text/javascript">';
$javascript[] = '/* <![CDATA[ */';
$javascript[] = 'var rev = new Array();';
 
ksort($seen_rev); // Sort revisions in descending order by key
if (empty($peg))
{
$peg = $rev;
}
if (!isset($vars['warning']))
{
foreach ($seen_rev as $key => $val)
{
$history = $svnrep->getLog($path, $key, $key, false, 1, $peg);
 
if ($history && $history->curEntry)
{
$javascript[] = 'rev['.$key.'] = \'<div class="date">'.$history->curEntry->date.'</div><div class="msg">'.addslashes(preg_replace('/\n/', ' ', $history->curEntry->msg)).'</div>\';';
}
}
}
 
$javascript[] = '/* ]]> */';
$javascript[] = '</script>';
$vars['javascript'] = implode("\n", $javascript);
}
 
if (!$rep->hasReadAccess($path, false))
{
$vars['error'] = $lang['NOACCESS'];
sendHeaderForbidden();
}
 
renderTemplate('blame');