| 1 |
<?php
|
| 2 |
// $Id: repoview.module,v 1.11 2009/01/25 21:38:00 jpetso Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Repository Viewer - Browse repositories for Version Control API backends
|
| 6 |
* supporting this functionality.
|
| 7 |
*
|
| 8 |
* Copyright 2008, 2009 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
|
| 9 |
* Copyright 2006, 2007 by Gavin Mogan ("halkeye", http://drupal.org/user/56779)
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_menu().
|
| 14 |
*/
|
| 15 |
function repoview_menu() {
|
| 16 |
$items = array();
|
| 17 |
$browse_access = 'browse version control repositories';
|
| 18 |
|
| 19 |
$items['repoview'] = array(
|
| 20 |
'title' => 'Repositories',
|
| 21 |
'page callback' => 'drupal_get_form',
|
| 22 |
'page arguments' => array('repoview_selection_form'),
|
| 23 |
'access arguments' => array($browse_access),
|
| 24 |
'type' => MENU_SUGGESTED_ITEM,
|
| 25 |
);
|
| 26 |
$items['repoview/%repoview_location'] = array(
|
| 27 |
'title callback' => 'repoview_location_title',
|
| 28 |
'title arguments' => array(1),
|
| 29 |
'page callback' => 'drupal_get_form',
|
| 30 |
'page arguments' => array('repoview_browser_form', 1),
|
| 31 |
'access arguments' => array($browse_access),
|
| 32 |
'load arguments' => array('%map', '%index'),
|
| 33 |
'type' => MENU_CALLBACK,
|
| 34 |
);
|
| 35 |
return $items;
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Menu wildcard loader for a repository location ('%repoview_location') that
|
| 40 |
* can be browsed with Repoview. Returns FALSE if the repository is not
|
| 41 |
* browsable, but does not check if the item in the repository actually exists.
|
| 42 |
* If the repository is browsable, this function returns an associative array
|
| 43 |
* with keys 'repository' and 'path' filled in. Requires array('%map', '%index')
|
| 44 |
* as load arguments so that the item path can be loaded correctly.
|
| 45 |
*/
|
| 46 |
function repoview_location_load($repo_id, $map_array, $index) {
|
| 47 |
$repository = versioncontrol_repository_load($repo_id);
|
| 48 |
|
| 49 |
if (!$repository || !_repoview_repository_supports_browsing($repository)) {
|
| 50 |
return FALSE;
|
| 51 |
}
|
| 52 |
// Construct the path out of the trailing menu path arguments.
|
| 53 |
$path = (count($map_array) > $index + 1)
|
| 54 |
? '/'. join('/', array_slice($map_array, $index + 1))
|
| 55 |
: '/';
|
| 56 |
return array('repository' => $repository, 'path' => $path);
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Title callback for a repository location loaded by repoview_location_load().
|
| 61 |
*/
|
| 62 |
function repoview_location_title($location) {
|
| 63 |
if ($location['path'] != '/') {
|
| 64 |
return check_plain($location['repository']['name']);
|
| 65 |
}
|
| 66 |
return check_plain(basename($location['path']));
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Implementation of hook_perm().
|
| 71 |
*/
|
| 72 |
function repoview_perm() {
|
| 73 |
return array('browse version control repositories');
|
| 74 |
}
|
| 75 |
|
| 76 |
|
| 77 |
/**
|
| 78 |
* Function to determine if a VCS backend provides enough functionality
|
| 79 |
* to support browsing.
|
| 80 |
*/
|
| 81 |
function _repoview_repository_supports_browsing($repository) {
|
| 82 |
$vcs = $repository['vcs'];
|
| 83 |
return versioncontrol_backend_implements($vcs, 'get_item')
|
| 84 |
&& versioncontrol_backend_implements($vcs, 'get_directory_contents')
|
| 85 |
&& versioncontrol_backend_implements($vcs, 'export_file');
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Return the URL for viewing a given item with repoview.
|
| 90 |
*/
|
| 91 |
function repoview_item_url($repository, $item) {
|
| 92 |
return 'repoview/'. $repository['repo_id'] . drupal_urlencode($item['path']);
|
| 93 |
}
|
| 94 |
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Form callback for the 'repoview' menu path.
|
| 98 |
*/
|
| 99 |
function repoview_selection_form($form_state) {
|
| 100 |
$form = array();
|
| 101 |
$repositories = versioncontrol_get_repositories();
|
| 102 |
|
| 103 |
foreach ($repositories as $repo_id => $repository) {
|
| 104 |
if (!_repoview_repository_supports_browsing($repository)) {
|
| 105 |
unset($repositories[$repo_id]);
|
| 106 |
}
|
| 107 |
}
|
| 108 |
|
| 109 |
if (empty($repositories)) {
|
| 110 |
$form['empty'] = array(
|
| 111 |
'#value' => '<p>'. t('No repositories available to browse.') .'</p>',
|
| 112 |
);
|
| 113 |
return $form;
|
| 114 |
}
|
| 115 |
if (count($repositories) == 1) {
|
| 116 |
$only_repo = reset($repositories);
|
| 117 |
drupal_goto('repoview/'. $only_repo['repo_id']);
|
| 118 |
}
|
| 119 |
|
| 120 |
$header = array('');
|
| 121 |
$rows = array();
|
| 122 |
|
| 123 |
foreach ($repositories as $repo_id => $repository) {
|
| 124 |
$rows[] = array(l($repository['name'], 'repoview/'. $repo_id));
|
| 125 |
}
|
| 126 |
$form['table'] = array(
|
| 127 |
'#value' => theme('table', $header, $rows),
|
| 128 |
);
|
| 129 |
return $form;
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
| 133 |
* Form callback for the 'repoview/%repoview_location[/...]' menu path.
|
| 134 |
*/
|
| 135 |
function repoview_browser_form($form_state, $location) {
|
| 136 |
$repository = $location['repository'];
|
| 137 |
$path = $location['path'];
|
| 138 |
|
| 139 |
$item = versioncontrol_get_item($repository, $path);
|
| 140 |
_repoview_breadcrumb($repository, $path);
|
| 141 |
|
| 142 |
if (empty($item)) {
|
| 143 |
drupal_set_title(check_plain(basename($path)));
|
| 144 |
$form['empty'] = array(
|
| 145 |
'#value' => '<p>'. t('File or directory doesn\'t exist at this revision, or doesn\'t exist at all.') .'</p>',
|
| 146 |
);
|
| 147 |
return $form;
|
| 148 |
}
|
| 149 |
drupal_add_css(drupal_get_path('module', 'repoview') .'/repoview.css');
|
| 150 |
|
| 151 |
if (versioncontrol_is_directory_item($item)) {
|
| 152 |
return repoview_directory_contents_form($form_state, $repository, $item);
|
| 153 |
}
|
| 154 |
return repoview_file_contents_form($form_state, $repository, $item);
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Form callback for 'repoview/%repoview_location[/...]'
|
| 159 |
* if the path is a directory item.
|
| 160 |
*/
|
| 161 |
function repoview_directory_contents_form($form_state, $repository, $dir_item) {
|
| 162 |
$form = array();
|
| 163 |
$children = versioncontrol_get_directory_contents($repository, $dir_item);
|
| 164 |
|
| 165 |
if (!isset($children)) {
|
| 166 |
$form['empty'] = array(
|
| 167 |
'#value' => '<p>'. t('Unable to fetch directory contents.') .'</p>',
|
| 168 |
);
|
| 169 |
return $form;
|
| 170 |
}
|
| 171 |
unset($children[$dir_item['path']]);
|
| 172 |
|
| 173 |
$children = _repoview_item_listing_sort($children);
|
| 174 |
|
| 175 |
$header = array(t('File'), t('Rev.'), t('Author'), t('Age'), t('Message'));
|
| 176 |
$rows = array();
|
| 177 |
|
| 178 |
if ($dir_item['path'] != '/') {
|
| 179 |
$parent_item = versioncontrol_get_parent_item($repository, $dir_item);
|
| 180 |
$item_url = repoview_item_url($repository, $parent_item);
|
| 181 |
|
| 182 |
// First column: image.
|
| 183 |
$image = theme('image', drupal_get_path('module', 'repoview') .'/icons/folder-parent.png');
|
| 184 |
$image_link = l($image, $item_url, array('html' => TRUE));
|
| 185 |
$image = '<div class="repoview-item-icon">'. $image_link .'</div>';
|
| 186 |
|
| 187 |
// Second column: directory name.
|
| 188 |
$item_link = l('<strong>'. t('Parent directory') .'</strong>', $item_url, array('html' => TRUE));
|
| 189 |
|
| 190 |
// Rest of the columns (even easier).
|
| 191 |
$rows[] = array(
|
| 192 |
'<div class="container-inline">'. $image . $item_link .'</div>',
|
| 193 |
'', '', '', ''
|
| 194 |
);
|
| 195 |
}
|
| 196 |
|
| 197 |
if (empty($children)) {
|
| 198 |
$rows[] = array(t('Directory is empty.'), '', '', '', '');
|
| 199 |
}
|
| 200 |
else {
|
| 201 |
versioncontrol_fetch_item_commit_operations($repository, $children);
|
| 202 |
|
| 203 |
foreach ($children as $path => $item) {
|
| 204 |
// First & second column: icon & file/directory name.
|
| 205 |
$name = check_plain(basename($item['path']));
|
| 206 |
$item_url = repoview_item_url($repository, $item);
|
| 207 |
|
| 208 |
if (versioncontrol_is_directory_item($item)) {
|
| 209 |
$image = theme('image', drupal_get_path('module', 'repoview') .'/icons/folder.png');
|
| 210 |
$name = '<strong>'. $name .'/</strong>';
|
| 211 |
}
|
| 212 |
else {
|
| 213 |
$image = theme('image', drupal_get_path('module', 'repoview') .'/icons/application-octet-stream.png');
|
| 214 |
}
|
| 215 |
$image_link = l($image, $item_url, array('html' => TRUE));
|
| 216 |
$image = '<div class="repoview-item-icon">'. $image_link .'</div>';
|
| 217 |
$item_link = l($name, $item_url, array('html' => TRUE));
|
| 218 |
|
| 219 |
// Author and commit message columns.
|
| 220 |
if (empty($item['commit_operation'])) {
|
| 221 |
$author = '';
|
| 222 |
$age = '';
|
| 223 |
$message = '';
|
| 224 |
}
|
| 225 |
else {
|
| 226 |
$operation = $item['commit_operation'];
|
| 227 |
$author = theme('versioncontrol_account_username',
|
| 228 |
$operation['uid'], $operation['username'], $repository, FALSE
|
| 229 |
);
|
| 230 |
$age = t('!time ago', array(
|
| 231 |
'!time' => format_interval(time() - $operation['date'], 1),
|
| 232 |
));
|
| 233 |
|
| 234 |
if (drupal_strlen($operation['message']) <= 60) {
|
| 235 |
$message = check_plain($operation['message']);
|
| 236 |
}
|
| 237 |
else {
|
| 238 |
$tooltip = check_plain($operation['message']);
|
| 239 |
$message = check_plain(drupal_substr($operation['message'], 0, 57)) .'...';
|
| 240 |
$message = '<span title="'. $tooltip .'">'. $message .'</span>';
|
| 241 |
}
|
| 242 |
}
|
| 243 |
|
| 244 |
// Combine all of these texts into a single row.
|
| 245 |
$rows[] = array(
|
| 246 |
'<div class="container-inline">'. $image . $item_link .'</div>',
|
| 247 |
check_plain($item['revision']), $author, $age, $message,
|
| 248 |
);
|
| 249 |
}
|
| 250 |
}
|
| 251 |
|
| 252 |
$form['listing'] = array(
|
| 253 |
'#value' => theme('table', $header, $rows),
|
| 254 |
);
|
| 255 |
return $form;
|
| 256 |
}
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Sort items by type (directories before files), then name (alphabetically).
|
| 260 |
*/
|
| 261 |
function _repoview_item_listing_sort($items) {
|
| 262 |
$dirs = array();
|
| 263 |
$files = array();
|
| 264 |
|
| 265 |
foreach ($items as $path => $item) {
|
| 266 |
if (versioncontrol_is_directory_item($item)) {
|
| 267 |
$dirs[$path] = $item;
|
| 268 |
}
|
| 269 |
else {
|
| 270 |
$files[$path] = $item;
|
| 271 |
}
|
| 272 |
}
|
| 273 |
ksort($dirs);
|
| 274 |
ksort($files);
|
| 275 |
return array_merge($dirs, $files);
|
| 276 |
}
|
| 277 |
|
| 278 |
/**
|
| 279 |
* Set the breadcrumb according to the given repository and path.
|
| 280 |
*/
|
| 281 |
function _repoview_breadcrumb($repository, $path) {
|
| 282 |
if ($path == '/') {
|
| 283 |
return; // no need to change the breadcrumb, it's already correct
|
| 284 |
}
|
| 285 |
$breadcrumb = drupal_get_breadcrumb();
|
| 286 |
$parts = explode('/', $path);
|
| 287 |
array_pop($parts); // don't include the last name, we're currently viewing it
|
| 288 |
|
| 289 |
$path = '';
|
| 290 |
foreach ($parts as $part) {
|
| 291 |
$title = empty($part) ? $repository['name'] : $part;
|
| 292 |
$path .= $part .'/';
|
| 293 |
$breadcrumb[] = l($title, 'repoview/'. $repository['repo_id'] . rtrim($path, '/'));
|
| 294 |
}
|
| 295 |
drupal_set_breadcrumb($breadcrumb);
|
| 296 |
}
|
| 297 |
|
| 298 |
/**
|
| 299 |
* Form callback for 'repoview/%repoview_location[/...]'
|
| 300 |
* if the path is a file item.
|
| 301 |
*/
|
| 302 |
function repoview_file_contents_form($form_state, $repository, $file_item, $force_download = FALSE) {
|
| 303 |
$form = array();
|
| 304 |
$filepath = versioncontrol_export_file($repository, $file_item);
|
| 305 |
|
| 306 |
if (empty($filepath)) {
|
| 307 |
$form['error'] = array(
|
| 308 |
'#value' => '<p>'. t('Error accessing the file.') .'</p>',
|
| 309 |
);
|
| 310 |
return $form;
|
| 311 |
}
|
| 312 |
|
| 313 |
// Retrieve the mimetype of the file - if possible, because that function is
|
| 314 |
// deprecated and the fileinfo PECL extension seems not to be built into
|
| 315 |
// PHP 5 at least.
|
| 316 |
if (function_exists('mime_content_type')) {
|
| 317 |
$mimetype = mime_content_type($filepath);
|
| 318 |
}
|
| 319 |
$is_text = $is_image = FALSE;
|
| 320 |
|
| 321 |
if (isset($mimetype) && (strpos($mimetype, 'text/') !== FALSE)) {
|
| 322 |
$is_text = TRUE;
|
| 323 |
}
|
| 324 |
if (isset($mimetype) && in_array($mimetype, array('image/png', 'image/jpeg', 'image/gif'))) {
|
| 325 |
$is_image = TRUE;
|
| 326 |
}
|
| 327 |
|
| 328 |
// We don't want to show normal binary files, let's just transfer them as is.
|
| 329 |
if ($force_download || (!$is_text /* && !$is_image ...later */)) {
|
| 330 |
// Also, make sure we delete the file even if file_transfer() exits.
|
| 331 |
register_shutdown_function('_repoview_delete_file_copy', $filepath);
|
| 332 |
|
| 333 |
// Transfer the file!
|
| 334 |
$headers = array(
|
| 335 |
'Content-Type: '. mime_header_encode($mimetype),
|
| 336 |
'Content-Length: '. filesize($filepath),
|
| 337 |
// Force file download and set the default target filename.
|
| 338 |
'Content-Disposition: attachment; filename="'. basename($file_item['path']) .'";',
|
| 339 |
);
|
| 340 |
file_transfer($filepath, $headers);
|
| 341 |
// exit(); -- called by file_transfer()
|
| 342 |
}
|
| 343 |
|
| 344 |
if ($is_text) {
|
| 345 |
// Cool, it's a text file. Let's display it in a nice table with a row
|
| 346 |
// for each line. (repoview.css makes sure it looks nice.)
|
| 347 |
$lines = file($filepath);
|
| 348 |
_repoview_delete_file_copy($filepath);
|
| 349 |
|
| 350 |
$linecount = 1;
|
| 351 |
$line_numbers = '';
|
| 352 |
$content = '';
|
| 353 |
$header = array();
|
| 354 |
$rows = array();
|
| 355 |
|
| 356 |
foreach ($lines as $line) {
|
| 357 |
$rows[] = array(
|
| 358 |
'<pre>'. $linecount .'</pre>',
|
| 359 |
'<pre>'. trim(check_plain($line), "\n\r") .'</pre>'
|
| 360 |
);
|
| 361 |
++$linecount;
|
| 362 |
}
|
| 363 |
$contents = theme('table', $header, $rows, $attributes);
|
| 364 |
$contents = '<div class="repoview-file-contents-text">'. $contents .'</div>';
|
| 365 |
}
|
| 366 |
else if ($is_image) {
|
| 367 |
// add a link to this same function with $force_download == TRUE,
|
| 368 |
// i.e. same path with 'view=download' query attribute (right?).
|
| 369 |
}
|
| 370 |
|
| 371 |
$form['contents'] = array(
|
| 372 |
'#value' => '<div class="repoview-file-contents">'. $contents .'</div>',
|
| 373 |
);
|
| 374 |
return $form;
|
| 375 |
}
|
| 376 |
|
| 377 |
/**
|
| 378 |
* Registered shutdown function, called after the request has ended:
|
| 379 |
* Delete the file copy that versioncontrol_export_file() had created.
|
| 380 |
*/
|
| 381 |
function _repoview_delete_file_copy($file_path) {
|
| 382 |
@unlink($file_path);
|
| 383 |
}
|