summaryrefslogtreecommitdiffstats
path: root/commitlog/commitlog.module
blob: 6d191423270203a6ecfe2fd09b4617bd4e5e31db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
 * @file
 * Commit Log - Display a history of commits, branches and tags,
 * optionally filtered by a set of constraint arguments.
 *
 * Copyright 2006, 2007 Derek Wright ("dww" , http://drupal.org/user/46549)
 * Copyright 2007, 2008, 2009 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
 */

/**
 * Implements hook_menu().
 */
function commitlog_menu() {
  $items = array();

  $view_access = 'access commit messages';

  $items['commitlog'] = array(
    'title' => 'Commit messages',
    'page callback' => 'commitlog_operations_page',
    'access arguments' => array($view_access),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['commitlog/feed'] = array(
    'title' => 'Commit messages',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('global', 'feed'),
    'access arguments' => array($view_access),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['commitlog/repository/%versioncontrol_repository'] = array(
    'title' => 'Commit messages',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('repository', 'page', 2),
    'access arguments' => array($view_access),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['commitlog/repository/%versioncontrol_repository/feed'] = array(
    'title' => 'Commit messages',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('repository', 'feed', 2),
    'access arguments' => array($view_access),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['commitlog/author/%user'] = array(
    'title' => 'Commit messages',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('author', 'page', 2),
    'access callback' => 'commitlog_user_view_access',
    'access arguments' => array(2),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['commitlog/author/%user/feed'] = array(
    'title' => 'Commit messages',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('author', 'feed', 2),
    'access callback' => 'commitlog_user_view_access',
    'access arguments' => array(2),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['commitlog/commit/%versioncontrol_repository/%'] = array(
    'title' => 'Commit',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('commit', 'page', 2, 3),
    'access arguments' => array($view_access),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['user/%user/track/code'] = array(
    'title' => 'Commits',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('user', 'page', 1),
    'access callback' => 'commitlog_user_view_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
  );
  $items['user/%user/track/code/feed'] = array(
    'title' => 'Commits',
    'page callback' => 'commitlog_operations_page',
    'page arguments' => array('user', 'feed', 1),
    'access callback' => 'commitlog_user_view_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function commitlog_permission() {
  return array(
    'access commit messages' => array(
      'title' => t('Access commit messages'),
      'description' => t('See commits information about the version control repostories.'),
    ),
  );
}

/**
 * Menu access callback for menu items using user object.
 *
 * Pass through user_view_access() to follow standard drupal access.
 */
function commitlog_user_view_access($account) {
  return user_view_access($account) && user_access('access commit messages');
}

/**
 * Implements hook_views_api().
 *
 * @return array
 */
function commitlog_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'commitlog'). '/includes/views',
  );
}

/**
 * Implements ctools hook_ctools_plugin_directory().
 */
function commitlog_ctools_plugin_directory($module, $plugin) {
  if ($module == 'versioncontrol') {
    return "includes/plugins/$plugin";
  }
}

/**
 * Implements hook_ctools_plugin_type().
 */
function commitlog_ctools_plugin_type() {
  return array(
    'views_sets' => array(
      'cache' => TRUE,
      'use hooks' => TRUE,
      'classes' => array('handler'),
    ),
  );
}

/**
 * Page callback for the 'commitlog' family of menu paths.
 *
 * @param $view_type
 *   What type of commit log listing we're creating.  Supported types are 'author', 'commit', 'global', 'repository' and 'user'.
 * @param $display_type
 *   What type of display output we are creating. Supported types are 'page' and 'feed'.
 * @param $argument
 *   The argument with which to filter the view.  This is used as the first argument in the view in question.
 * @param $argument2
 *   Another argument to filter the view.  This is used as the second argument in the view in question.
 */
function commitlog_operations_page($view_type = NULL, $display_type='page', $argument = NULL, $argument2 = NULL) {
  drupal_add_css(drupal_get_path('module', 'commitlog') . '/commitlog.css', 'module');
  $view_name = '';
  if (($view_type == 'author' || $view_type == 'user') && !is_null($argument)) {
    $account = $argument;
    $set = versioncontrol_get_views_set('user_commit_view');
    $view_name = $set->getViewName();
    $feed_url = ($view_type == 'author') ? "commitlog/author/{$account->uid}/feed" : "user/{$account->uid}/track/code/feed";

    if ($display_type == 'feed') {
      return versioncontrol_render_commitlog_view($view_name, 'feed', array($account->uid), $feed_url);
    }
    return versioncontrol_render_commitlog_view($view_name, 'default', array($account->uid), '', $feed_url);
  }
  if ($view_type == 'repository' && !is_null($argument)) {
    $repository = $argument;
    $set = versioncontrol_get_views_set('repository_commit_view');
    $view_name = $set->getViewNameByEntity($repository);
    $feed_url = "commitlog/repository/{$repository->repo_id}/feed";
    if ($display_type == 'feed') {
      return versioncontrol_render_commitlog_view($view_name, 'feed', array($repository->repo_id), $feed_url);
    }
    return versioncontrol_render_commitlog_view($view_name, 'default', array($repository->repo_id), '', $feed_url);
  }
  if ($view_type == 'commit' && !is_null($argument) && !is_null($argument2)) {
    $repository = $argument;
    $revision = $argument2;
    $set = versioncontrol_get_views_set('individual_commit_view');
    $view_name = $set->getViewName();
    return versioncontrol_render_commitlog_view($view_name, 'default', array($repository->repo_id, $revision));
  }
  // Global by default if no view type provided.
  if (is_null($view_type) || $view_type == 'global') {
    $set = versioncontrol_get_views_set('global_commit_view');
    $view_name = $set->getViewName();
    $feed_url = 'commitlog/feed';

    if ($display_type == 'feed') {
      return versioncontrol_render_commitlog_view($view_name, 'feed', array(), $feed_url);
    }
    return versioncontrol_render_commitlog_view($view_name, 'default', array(), '', $feed_url);
  }

  // Nothing catched the current arguments, so react acordingly.
  return drupal_not_found();
}

/**
 * A convinience function to render a commitlog view.
 * Basic logic here borrowed from views_embed_view().
 *
 * @param $view_name
 *   Name of the view to render.
 * @param $display_type
 *   What type of display output we are creating. Supported types are 'default' and 'feed'.
 * @param $arguments
 *   The arguments needed for render the view.
 * @param $path
 *   The path to overwrite the view with.
 * @param $feed_url
 *   The url of the feed this display is linked. Only valid for $display_type = 'default'.
 * @return
 *   On $display_type == 'default', the content to show.
 *   On $display_type == 'feed', NULL, but handle RSS printning by hand.
 */
function versioncontrol_render_commitlog_view($view_name, $display_type, $arguments = array(), $path = '', $feed_url = '') {
  $view = views_get_view($view_name);
  if (!$view || !$view->access($display_type)) {
    return drupal_not_found();
  }

  if (!empty($path)) {
    $view->override_path = $path;
    if ($display_type == 'feed') {
      // Ugh, this line is needed, as override_path seems to not be enough
      $view->display['feed']->display_options['path'] = $path;
    }
  }
  $output = $view->preview($display_type, $arguments);
  if ($display_type == 'feed') {
    drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
    print $output;
    return;
  }
  else { // default display type
    if (!empty($feed_url)) {
      $feed_url = url($feed_url, array('absolute' => TRUE));
      drupal_add_feed($feed_url);
    }
  }
  // default
  $title = $view->get_title();
  if (!empty($title)) {
    drupal_set_title($title);
  }

  return $output;
}