Skip to content

Commit

Permalink
Implemented retrieving a specific version of a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
remko committed Jul 19, 2008
1 parent bbce669 commit 034aaf1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
93 changes: 71 additions & 22 deletions index.php
@@ -1,37 +1,50 @@
<?php
require_once('classTextile.php');

// Default configuration. FIXME: use isset() and move after source config.php
$GIT = "git";
$BASE_URL = "/wigit";
$SCRIPT_URL = "$BASE_URL/index.php?r=";
$TITLE = "WiGit";
$DATA_DIR = "data";
$DEFAULT_PAGE = "Home";
$DEFAULT_AUTHOR = 'Anonymous <anonymous@wigit>';
$AUTHORS = array();

// Load user config
// Configuration
if (file_exists('config.php')) {
require_once('config.php');
}
if (!isset($GIT)) { $GIT = "git"; }
if (!isset($BASE_URL)) { $BASE_URL = "/wigit"; }
if (!isset($SCRIPT_URL)) { $SCRIPT_URL = "$BASE_URL/index.php?r="; }
if (!isset($TITLE)) { $TITLE = "WiGit"; }
if (!isset($DATA_DIR)) { $DATA_DIR = "data"; }
if (!isset($DEFAULT_PAGE)) { $DEFAULT_PAGE = "Home"; }
if (!isset($DEFAULT_AUTHOR)) { $DEFAULT_AUTHOR = 'Anonymous <anonymous@wigit>'; }
if (!isset($AUTHORS)) { $AUTHORS = array(); }

function wikify($text) {
return $text;
}

function getHistory($file = "") {
$output = array();
git("log --pretty=format:'%an>%ae>%aD>%s' -- $file", $output);
git("log --pretty=format:'%H>%T>%an>%ae>%aD>%s' -- $file", $output);
$history = array();
foreach ($output as $line) {
$logEntry = split(">", $line, 4);
$logEntry = explode(">", $line, 6);

// Find out which file was edited
$treeOutput = array();
if (!git("ls-tree ". $logEntry[1], $treeOutput) || sizeof($treeOutput) == 0) {
continue;
}
$page = end(split("\x09", $treeOutput[0]));

// Populate history structure
$history[] = array(
"author" => $logEntry[0],
"email" => $logEntry[1],
"linked-author" => ($logEntry[1] == "" ? $logEntry[0] : "<a href=\"mailto:$logEntry[1]\">$logEntry[0]</a>"),
"date" => $logEntry[2],
"message" => $logEntry[3]);
"author" => $logEntry[2],
"email" => $logEntry[3],
"linked-author" => (
$logEntry[3] == "" ?
$logEntry[2]
: "<a href=\"mailto:$logEntry[3]\">$logEntry[2]</a>"),
"date" => $logEntry[4],
"message" => $logEntry[5],
"page" => $page,
"commit" => $logEntry[0]
);
}
return $history;
}
Expand Down Expand Up @@ -128,6 +141,23 @@ function parseResource($resource) {
return array("page" => $page, "type" => $type);
}

// --------------------------------------------------------------------------
// Utility functions (for use inside templates)
// --------------------------------------------------------------------------

function getViewURL($page, $version = null) {
global $SCRIPT_URL;

if ($version) {
return "$SCRIPT_URL/$page/$version";
}
else {
return "$SCRIPT_URL/$page";
}
}

// --------------------------------------------------------------------------

// Get the page
$resource = parseResource($_GET['r']);

Expand All @@ -139,8 +169,8 @@ function parseResource($resource) {
$wikiFile = $DATA_DIR . "/" . $resource["page"];
$wikiFile = $wikiFile;
$wikiPage = $resource["page"];
$wikiPageViewURL = "$SCRIPT_URL/$wikiPage";
$wikiPageEditURL = "$SCRIPT_URL/$wikiPage/edit";
$wikiPagePostURL = "$SCRIPT_URL/$wikiPage";
$wikiPageHistoryURL = "$SCRIPT_URL/$wikiPage/history";
$wikiHistoryURL = "$SCRIPT_URL/history";
$wikiCSS = $CSS;
Expand Down Expand Up @@ -173,7 +203,7 @@ function parseResource($resource) {
if (!git("add $wikiPage")) { return; }
if (!git("commit --message='$commitMessage' --author='$author'")) { return; }
if (!git("gc")) { return; }
header("Location: $wikiPageViewURL");
header("Location: " . getViewURL($wikiPage));
return;
}
}
Expand Down Expand Up @@ -217,15 +247,34 @@ function parseResource($resource) {

// Put in template
$wikiData = $data;
$wikiPagePostURL = "$SCRIPT_URL/$wikiPage";
include('templates/edit.php');
}
else if ($resource["type"] == "history") {
$wikiHistory = getHistory($wikiPage);
include('templates/history.php');
}
// Error
else {
// Try commit.
// FIXME: Try to put this in an else if
$output = array();
if (git("cat-file -p " . $resource["type"] . ":$wikiPage", $output)) {
$data = join("\n", $output);
// FIXME Factor this out
// Add wiki links and other transformations
$wikifiedData = wikify($data);

// Textilify
$textile = new Textile();
$formattedData = $textile->TextileThis($wikifiedData);

// Put in template
// FIXME: Remove edit links
$wikiContent = $formattedData;
include('templates/view.php');
return;
}

// Fallback
print "Unknown type: " . $resource["type"];
}
}
Expand Down
5 changes: 3 additions & 2 deletions templates/history.php
Expand Up @@ -16,14 +16,15 @@

<div id="header">
<h1 id="title"><?php print $historyTitle ?></h1>
<p>[ <a href="<?php print $wikiPageViewURL?>">view</a> ]</p>
<p>[ <a href="<?php print getViewURL($wikiPage); ?>">view</a> ]</p>
</div>

<div id="history">
<table>
<tr><th>Date</th><th>Author</th><th>Page</th><th>Message</th></tr>
<?php
foreach ($wikiHistory as $item) {
print "<tr><td>" . $item["date"] . "</td><td class='author'>" . $item["linked-author"] . "</td><td>" . $item["message"] . "</td></tr>\n";
print "<tr><td>" . $item["date"] . "</td><td class='author'>" . $item["linked-author"] . "</td><td>" . $item["page"] . "</td><td>" . $item["message"] . "</td><td>" . "<a href=\"" . getViewURL($item["page"], $item["commit"]) . "\">View</a></td><td>" . "</td></tr>\n";
}
?>
</table>
Expand Down

0 comments on commit 034aaf1

Please sign in to comment.