| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Version Control API - An interface to version control systems
|
| 6 |
* whose functionality is provided by pluggable back-end modules.
|
| 7 |
*
|
| 8 |
* This file contains module hooks for users of Version Control API,
|
| 9 |
* with API documentation and a bit of example code.
|
| 10 |
* Hooks that are intended for VCS backends are not to be found in this file
|
| 11 |
* as they are already documented in versioncontrol_fakevcs.module.
|
| 12 |
*
|
| 13 |
* Copyright 2007, 2008, 2009 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
|
| 14 |
*/
|
| 15 |
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Act on database changes when commit, tag or branch operations are inserted
|
| 19 |
* or deleted. Note that this hook is not necessarily called at the time
|
| 20 |
* when the operation actually happens - operations can also be inserted
|
| 21 |
* by a cron script when the actual commit/branch/tag has been accomplished
|
| 22 |
* for quite a while already.
|
| 23 |
*
|
| 24 |
* @param $op
|
| 25 |
* 'insert' when the operation has just been recorded and inserted into the
|
| 26 |
* database, or 'delete' if it will be deleted right after this hook
|
| 27 |
* has been called.
|
| 28 |
*
|
| 29 |
* @param $operation
|
| 30 |
* An operation array containing basic information about the commit, branch
|
| 31 |
* or tag operation. It consists of the following elements:
|
| 32 |
*
|
| 33 |
* - 'vc_op_id': The Drupal-specific operation identifier (a simple integer)
|
| 34 |
* which is unique among all operations (commits, branch ops, tag ops)
|
| 35 |
* in all repositories.
|
| 36 |
* - 'type': The type of the operation - one of the
|
| 37 |
* VERSIONCONTROL_OPERATION_{COMMIT,BRANCH,TAG} constants.
|
| 38 |
* Note that if you pass branch or tag constraints, this function might
|
| 39 |
* nevertheless return commit operations too - that happens for version
|
| 40 |
* control systems without native branches or tags (like Subversion)
|
| 41 |
* when a branch or tag is affected by the commit.
|
| 42 |
* - 'repository': The repository where this operation occurred.
|
| 43 |
* This is a structured "repository array", like is returned
|
| 44 |
* by versioncontrol_get_repository().
|
| 45 |
* - 'date': The time when the operation was performed, given as
|
| 46 |
* Unix timestamp. (For commits, this is the time when the revision
|
| 47 |
* was committed, whereas for branch/tag operations it is the time
|
| 48 |
* when the files were branched or tagged.)
|
| 49 |
* - 'uid': The Drupal user id of the operation author, or 0 if no
|
| 50 |
* Drupal user could be associated to the author.
|
| 51 |
* - 'username': The system specific VCS username of the author.
|
| 52 |
* - 'message': The log message for the commit, tag or branch operation.
|
| 53 |
* If a version control system doesn't support messages for any of them,
|
| 54 |
* this element contains an empty string.
|
| 55 |
* - 'revision': The VCS specific repository-wide revision identifier,
|
| 56 |
* like '' in CVS, '27491' in Subversion or some SHA-1 key in various
|
| 57 |
* distributed version control systems. If there is no such revision
|
| 58 |
* (which may be the case for version control systems that don't support
|
| 59 |
* atomic commits) then the 'revision' element is an empty string.
|
| 60 |
* For branch and tag operations, this element indicates the
|
| 61 |
* (repository-wide) revision of the files that were branched or tagged.
|
| 62 |
*
|
| 63 |
* - 'labels': An array of branches or tags that were affected by this
|
| 64 |
* operation. Branch and tag operations are known to only affect one
|
| 65 |
* branch or tag, so for these there will be only one element (with 0
|
| 66 |
* as key) in 'labels'. Commits might affect any number of branches,
|
| 67 |
* including none. Commits that emulate branches and/or tags (like
|
| 68 |
* in Subversion, where they're not a native concept) can also include
|
| 69 |
* add/delete/move operations for labels, as detailed below.
|
| 70 |
* Mind that the main development branch - e.g. 'HEAD', 'trunk'
|
| 71 |
* or 'master' - is also considered a branch. Each element in 'labels'
|
| 72 |
* is a structured array with the following keys:
|
| 73 |
*
|
| 74 |
* - 'id': The label identifier (a simple integer), used for unique
|
| 75 |
* identification of branches and tags in the database.
|
| 76 |
* - 'name': The branch or tag name (a string).
|
| 77 |
* - 'action': Specifies what happened to this label in this operation.
|
| 78 |
* For plain commits, this is always VERSIONCONTROL_ACTION_MODIFIED.
|
| 79 |
* For branch or tag operations (or commits that emulate those),
|
| 80 |
* it can be either VERSIONCONTROL_ACTION_ADDED or
|
| 81 |
* VERSIONCONTROL_ACTION_DELETED.
|
| 82 |
*
|
| 83 |
* @param $operation_items
|
| 84 |
* A structured array containing all items that were affected by the above
|
| 85 |
* operation. Array keys are the current/new paths, even if the item doesn't
|
| 86 |
* exist anymore (as is the case with delete actions in commits).
|
| 87 |
* The associated array elements are structured item arrays and consist of
|
| 88 |
* the following elements:
|
| 89 |
*
|
| 90 |
* - 'type': Specifies the item type, which is either
|
| 91 |
* VERSIONCONTROL_ITEM_FILE or VERSIONCONTROL_ITEM_DIRECTORY for items
|
| 92 |
* that still exist, or VERSIONCONTROL_ITEM_FILE_DELETED respectively
|
| 93 |
* VERSIONCONTROL_ITEM_DIRECTORY_DELETED for items that have been
|
| 94 |
* removed (by a commit's delete action).
|
| 95 |
* - 'path': The path of the item at the specific revision.
|
| 96 |
* - 'revision': The (file-level) revision when the item was changed.
|
| 97 |
* If there is no such revision (which may be the case for
|
| 98 |
* directory items) then the 'revision' element is an empty string.
|
| 99 |
* - 'item_revision_id': Identifier of this item revision in the database.
|
| 100 |
* Note that you can only rely on this element to exist for
|
| 101 |
* operation items - functions that interface directly with the VCS
|
| 102 |
* (such as VersioncontrolItem::getDirectoryContents() or
|
| 103 |
* VersioncontrolItem::getParallelItems()) might not include
|
| 104 |
* this identifier, for obvious reasons.
|
| 105 |
*
|
| 106 |
* For commit operations, additional information about the origin of
|
| 107 |
* the items is also available. The following elements will be set
|
| 108 |
* for each item in addition to the ones listed above:
|
| 109 |
*
|
| 110 |
* - 'action': Specifies how the item was changed.
|
| 111 |
* One of the predefined VERSIONCONTROL_ACTION_* values.
|
| 112 |
* - 'source_items': An array with the previous state(s) of the affected item.
|
| 113 |
* Empty if 'action' is VERSIONCONTROL_ACTION_ADDED.
|
| 114 |
* - 'replaced_item': The previous but technically unrelated item at the
|
| 115 |
* same location as the current item. Only exists if this previous item
|
| 116 |
* was deleted and replaced by a different one that was just moved
|
| 117 |
* or copied to this location.
|
| 118 |
*
|
| 119 |
* @ingroup Operations
|
| 120 |
* @ingroup Commit notification
|
| 121 |
* @ingroup Database change notification
|
| 122 |
*/
|
| 123 |
function hook_versioncontrol_operation($op, $operation, $operation_items) {
|
| 124 |
if ($op == 'insert' && module_exists('commitlog')) {
|
| 125 |
if (variable_get('commitlog_send_notification_mails', 0)) {
|
| 126 |
$mailto = variable_get('versioncontrol_email_address', 'versioncontrol@example.com');
|
| 127 |
commitlog_notification_mail($mailto, $operation, $operation_items);
|
| 128 |
}
|
| 129 |
}
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
| 133 |
* Act on database changes when operation labels change or a given operation.
|
| 134 |
*
|
| 135 |
* @param $op
|
| 136 |
* 'insert' when the operation along with its label associations has just
|
| 137 |
* been recorded and inserted into the database, 'update' when the set of
|
| 138 |
* operation labels changes, or 'delete' if the operation along with its
|
| 139 |
* label associations. Note that updating or deleting an operation label
|
| 140 |
* does not automatically trigger deletion of the label itself in the
|
| 141 |
* database, just the association of the operation to the label.
|
| 142 |
*
|
| 143 |
* @param $operation
|
| 144 |
* An operation array containing basic information about the commit, branch
|
| 145 |
* or tag operation. This is the same operation array format as is passed
|
| 146 |
* to hook_versioncontrol_operation() (and other functions), see the
|
| 147 |
* API documentation there for an exact description of its properties.
|
| 148 |
*
|
| 149 |
* The operation array holds the labels' state *before* the action is being
|
| 150 |
* performed. That means when @p $op is 'insert', $operation['labels'] is an
|
| 151 |
* empty array, whereas with @p $op being 'update' or 'delete',
|
| 152 |
* $operation['labels'] holds the previous set of operation labels (which may
|
| 153 |
* also be empty of course).
|
| 154 |
*
|
| 155 |
* @param $labels
|
| 156 |
* The new set of operation labels - an array of branches or tags that were
|
| 157 |
* affected by the given operation. When @p $op is 'delete', this is an empty
|
| 158 |
* array, whereas with @p $op being 'insert' or 'update', @p $labels holds
|
| 159 |
* the new set of operation labels (which may also be empty of course).
|
| 160 |
* Same format as $operation['labels'].
|
| 161 |
*
|
| 162 |
* @ingroup Operations
|
| 163 |
* @ingroup Database change notification
|
| 164 |
*/
|
| 165 |
function hook_versioncontrol_operation_labels($op, $operation, $labels) {
|
| 166 |
// This crude example tracks which labels are being added and removed, and
|
| 167 |
// adjusts a counter accordingly. Untested, don't assume this actually works.
|
| 168 |
$old_label_ids = $new_label_ids = array();
|
| 169 |
$adjustment_operators = array();
|
| 170 |
|
| 171 |
foreach ($operation['labels'] as $label) {
|
| 172 |
$old_label_ids[] = $label['label_id'];
|
| 173 |
}
|
| 174 |
foreach ($labels as $label) {
|
| 175 |
if (!in_array($label['label_id'], $old_label_ids)) {
|
| 176 |
$adjustment_operators[$label['label_id']] = '+';
|
| 177 |
}
|
| 178 |
$new_label_ids[] = $label['label_id'];
|
| 179 |
}
|
| 180 |
foreach ($old_label_ids as $old_label_id) {
|
| 181 |
if (!in_array($old_label_id, $new_label_ids)) {
|
| 182 |
$adjustment_operators[$old_label_id] = '-';
|
| 183 |
}
|
| 184 |
}
|
| 185 |
|
| 186 |
foreach ($adjustment_operators as $label_id => $operator) {
|
| 187 |
$result = db_query('SELECT label_id FROM {mymodule_label_activity}');
|
| 188 |
if (!db_result($result)) { // does not yet exist in the database
|
| 189 |
db_query("INSERT INTO {mymodule_label_activity} (label_id, activity)
|
| 190 |
VALUES (%d, 0)", $label_id);
|
| 191 |
}
|
| 192 |
db_query("UPDATE {mymodule_label_activity}
|
| 193 |
SET activity = activity $operator 1
|
| 194 |
WHERE label_id = %d", $label_id);
|
| 195 |
}
|
| 196 |
}
|
| 197 |
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Restrict, ignore or explicitly allow a commit, branch or tag operation
|
| 201 |
* for a repository that is connected to the Version Control API
|
| 202 |
* by VCS specific hook scripts.
|
| 203 |
*
|
| 204 |
* @param $operation
|
| 205 |
* A single operation array like the ones returned by
|
| 206 |
* versioncontrol_get_operations(), but leaving out on a few details that
|
| 207 |
* will instead be determined by this function. This array describes
|
| 208 |
* the operation that is about to happen. As it's not committed yet,
|
| 209 |
* it's also not in the database yet, which means that any information
|
| 210 |
* retrieval functions won't work on this operation array.
|
| 211 |
* It also means there's no 'vc_op_id', 'revision' and 'date' elements like
|
| 212 |
* in regular operation arrays. The 'message' element will not be set
|
| 213 |
* if the VCS doesn't support log messages for the current operation
|
| 214 |
* (e.g., most version control systems don't have branch messages).
|
| 215 |
*
|
| 216 |
* Summed up, here's what this array contains for sure:
|
| 217 |
*
|
| 218 |
* - 'type': The type of the operation - one of the
|
| 219 |
* VERSIONCONTROL_OPERATION_{COMMIT,BRANCH,TAG} constants.
|
| 220 |
* - 'repository': The repository where this operation occurs,
|
| 221 |
* given as a structured array, like the return value
|
| 222 |
* of versioncontrol_get_repository().
|
| 223 |
* - 'uid': The Drupal user id of the committer.
|
| 224 |
* - 'username': The system specific VCS username of the committer.
|
| 225 |
*
|
| 226 |
* - 'labels': An array of branches or tags that will be affected by this
|
| 227 |
* operation. Branch and tag operations are known to only affect one
|
| 228 |
* branch or tag, so for these there will be only one element (with 0
|
| 229 |
* as key) in 'labels'. Commits might affect any number of branches,
|
| 230 |
* including none. Commits that emulate branches and/or tags (like
|
| 231 |
* in Subversion, where they're not a native concept) can also include
|
| 232 |
* add/delete/move operations for labels, as detailed below.
|
| 233 |
* Mind that the main development branch - e.g. 'HEAD', 'trunk'
|
| 234 |
* or 'master' - is also considered a branch. Each element in 'labels'
|
| 235 |
* is a structured array with the following keys:
|
| 236 |
*
|
| 237 |
* - 'name': The branch or tag name (a string).
|
| 238 |
* - 'action': Specifies what happened to this label in this operation.
|
| 239 |
* For plain commits, this is always VERSIONCONTROL_ACTION_MODIFIED.
|
| 240 |
* For branch or tag operations (or commits that emulate those),
|
| 241 |
* it can be either VERSIONCONTROL_ACTION_ADDED or
|
| 242 |
* VERSIONCONTROL_ACTION_DELETED.
|
| 243 |
*
|
| 244 |
* @param $operation_items
|
| 245 |
* A structured array containing the exact details of what is about to happen
|
| 246 |
* to each item in this commit. The structure of this array is the same as
|
| 247 |
* the return value of VersioncontrolOperation::getItems() - that is,
|
| 248 |
* elements for 'type', 'path' and 'revision' - but doesn't include
|
| 249 |
* the 'item_revision_id' element as there's no relation to the database yet.
|
| 250 |
*
|
| 251 |
* The 'action', 'source_items', 'replaced_item' and 'revision' elements
|
| 252 |
* of each item are optional for the VCS backend and may be left unset.
|
| 253 |
*
|
| 254 |
* @return
|
| 255 |
* An array with error messages (without trailing newlines) if the operation
|
| 256 |
* should not be allowed, or an empty array if you're indifferent,
|
| 257 |
* or TRUE if the operation should be allowed no matter what other
|
| 258 |
* write access callbacks say.
|
| 259 |
*
|
| 260 |
* @ingroup Operations
|
| 261 |
* @ingroup Commit access
|
| 262 |
* @ingroup Target audience: Commit access modules
|
| 263 |
*/
|
| 264 |
function hook_versioncontrol_write_access($operation, $operation_items) {
|
| 265 |
// Only allow users with a registered Drupal user account to commit.
|
| 266 |
if ($operation['uid'] != 0) {
|
| 267 |
$user = user_load(array('uid' => $operation['uid']));
|
| 268 |
}
|
| 269 |
if (!$user) {
|
| 270 |
$error_message = t(
|
| 271 |
"** ERROR: no Drupal user matches !vcs user '!user'.
|
| 272 |
** Please contact a !vcs administrator for help.",
|
| 273 |
array('!vcs' => $operation->repository->backend->name, '!user' => $operation->committer)
|
| 274 |
);
|
| 275 |
return array($error_message); // disallow the commit with an explanation
|
| 276 |
}
|
| 277 |
|
| 278 |
// Mind that also commits normally have labels, except for stuff like
|
| 279 |
// Subversion when the user commits outside of the trunk/branches/tags
|
| 280 |
// directories. Let's say we want to prevent such commits.
|
| 281 |
if (empty($operation['labels'])) {
|
| 282 |
$error_message = t("** ERROR: It is not allowed to commit without a branch or tag!");
|
| 283 |
return array($error_message);
|
| 284 |
}
|
| 285 |
|
| 286 |
// If an empty array is returned then that means we're indifferent:
|
| 287 |
// allow the operation if nobody else has objections.
|
| 288 |
$error_messages = array();
|
| 289 |
|
| 290 |
// Restrict disallowed branches and tags.
|
| 291 |
$valid_labels = array(
|
| 292 |
VERSIONCONTROL_LABEL_BRANCH => array('@^HEAD$@', '@^DRUPAL-5(--[2-9])?$@', '@^DRUPAL-6--[1-9]$@'),
|
| 293 |
VERSIONCONTROL_LABEL_TAG => array('@^DRUPAL-[56]--(\d+)-(\d+)(-[A-Z0-9]+)?$@'),
|
| 294 |
);
|
| 295 |
|
| 296 |
foreach ($operation['labels'] as $label) {
|
| 297 |
if ($label['type'] == VERSIONCONTROL_LABEL_TAG
|
| 298 |
&& $label['action'] == VERSIONCONTROL_ACTION_DELETED) {
|
| 299 |
continue; // no restrictions, even invalid tags should be allowed to be deleted
|
| 300 |
}
|
| 301 |
|
| 302 |
// Make sure that the assigned branch or tag name is allowed.
|
| 303 |
$valid = FALSE;
|
| 304 |
|
| 305 |
foreach ($valid_labels[$label['type']] as $valid_label_regexp) {
|
| 306 |
if (preg_match($valid_label_regexp, $label['name'])) {
|
| 307 |
$valid = TRUE;
|
| 308 |
break;
|
| 309 |
}
|
| 310 |
}
|
| 311 |
if (!$valid) {
|
| 312 |
// No regexps match this label, so deny it.
|
| 313 |
$error_messages[] = t('** ERROR: the !name !labeltype is not allowed in this repository.', array(
|
| 314 |
'!name' => $label['name'],
|
| 315 |
'!labeltype' => ($label['type'] == VERSIONCONTROL_LABEL_BRANCH)
|
| 316 |
? t('branch')
|
| 317 |
: t('tag'),
|
| 318 |
));
|
| 319 |
}
|
| 320 |
}
|
| 321 |
|
| 322 |
return $error_messages;
|
| 323 |
}
|
| 324 |
|
| 325 |
|
| 326 |
/**
|
| 327 |
* Extract repository data from the repository edit/add form's submitted
|
| 328 |
* values, and add it to the @p $repository array. Later, that array will be
|
| 329 |
* passed to hook_versioncontrol_repository() as part of the repository
|
| 330 |
* insert/update procedure.
|
| 331 |
*
|
| 332 |
* Elements written to $repository['data'][$module] will be automatically
|
| 333 |
* serialized and stored with the repository, you can write to that array
|
| 334 |
* in order to store module-specific repository settings. If there are settings
|
| 335 |
* that require a lot of memory or need to be accessible for SQL queries, you
|
| 336 |
* might be better off storing these in your own module's table with
|
| 337 |
* hook_versioncontrol_repository().
|
| 338 |
*
|
| 339 |
* @param $repository
|
| 340 |
* The repository array which is being passed by reference so that it can be
|
| 341 |
* written to.
|
| 342 |
* @param $form
|
| 343 |
* The form array of the submitted repository edit/add form, with
|
| 344 |
* $form['#id'] == 'versioncontrol-repository-form' (amongst others).
|
| 345 |
* @param $form_state
|
| 346 |
* The form state of the submitted repository edit/add form.
|
| 347 |
* If you altered this form and added an additional form element then
|
| 348 |
* $form_state['values'] will also contain the value of this form element.
|
| 349 |
*
|
| 350 |
* @ingroup Repositories
|
| 351 |
* @ingroup Form handling
|
| 352 |
* @ingroup Target audience: All modules with repository specific settings
|
| 353 |
*/
|
| 354 |
function hook_versioncontrol_repository_submit(&$repository, $form, $form_state) {
|
| 355 |
// The user can specify multiple repository ponies, separated by whitespace.
|
| 356 |
// So, split the string up into an array of ponies.
|
| 357 |
$ponies = trim($form_state['values']['mymodule_ponies']);
|
| 358 |
$ponies = empty($ponies) ? array() : explode(' ', $ponies);
|
| 359 |
$repository['mymodule']['ponies'] = $ponies;
|
| 360 |
}
|
| 361 |
|
| 362 |
/**
|
| 363 |
* Act on database changes when VCS repositories are inserted,
|
| 364 |
* updated or deleted.
|
| 365 |
*
|
| 366 |
* @param $op
|
| 367 |
* Either 'insert' when the repository has just been created, or 'update'
|
| 368 |
* when repository name, root, URL backend or module specific data change,
|
| 369 |
* or 'delete' if it will be deleted after this function has been called.
|
| 370 |
*
|
| 371 |
* @param $repository
|
| 372 |
* The repository array containing the repository. It's a single
|
| 373 |
* repository array like the one returned by versioncontrol_get_repository(),
|
| 374 |
* so it consists of the following elements:
|
| 375 |
*
|
| 376 |
* - 'repo_id': The unique repository id.
|
| 377 |
* - 'name': The user-visible name of the repository.
|
| 378 |
* - 'vcs': The unique string identifier of the version control system
|
| 379 |
* that powers this repository.
|
| 380 |
* - 'root': The root directory of the repository. In most cases,
|
| 381 |
* this will be a local directory (e.g. '/var/repos/drupal'),
|
| 382 |
* but it may also be some specialized string for remote repository
|
| 383 |
* access. How this string may look like depends on the backend.
|
| 384 |
* - 'authorization_method': The string identifier of the repository's
|
| 385 |
* authorization method, that is, how users may register accounts
|
| 386 |
* in this repository. Modules can provide their own methods
|
| 387 |
* by implementing hook_versioncontrol_authorization_methods().
|
| 388 |
* - 'data': An array where modules can store additional information about
|
| 389 |
* the repository, for settings or other data.
|
| 390 |
* - '[xxx]_specific': An array of VCS specific additional repository
|
| 391 |
* information. How this array looks like is defined by the
|
| 392 |
* corresponding backend module (versioncontrol_[xxx]).
|
| 393 |
* (Deprecated, to be replaced by the more general 'data' property.)
|
| 394 |
* - '???': Any other additions that modules added by implementing
|
| 395 |
* hook_versioncontrol_repository_submit().
|
| 396 |
*
|
| 397 |
* @ingroup Repositories
|
| 398 |
* @ingroup Database change notification
|
| 399 |
* @ingroup Form handling
|
| 400 |
* @ingroup Target audience: All modules with repository specific settings
|
| 401 |
*/
|
| 402 |
function hook_versioncontrol_repository($op, $repository) {
|
| 403 |
$ponies = $repository['mymodule']['ponies'];
|
| 404 |
|
| 405 |
switch ($op) {
|
| 406 |
case 'update':
|
| 407 |
db_query("DELETE FROM {mymodule_ponies}
|
| 408 |
WHERE repo_id = %d", $repository['repo_id']);
|
| 409 |
// fall through
|
| 410 |
case 'insert':
|
| 411 |
foreach ($ponies as $pony) {
|
| 412 |
db_query("INSERT INTO {mymodule_ponies} (repo_id, pony)
|
| 413 |
VALUES (%d, %s)", $repository['repo_id'], $pony);
|
| 414 |
}
|
| 415 |
break;
|
| 416 |
|
| 417 |
case 'delete':
|
| 418 |
db_query("DELETE FROM {mymodule_ponies}
|
| 419 |
WHERE repo_id = %d", $repository['repo_id']);
|
| 420 |
break;
|
| 421 |
}
|
| 422 |
}
|
| 423 |
|
| 424 |
|
| 425 |
/**
|
| 426 |
* Register new authorization methods that can be selected for a repository.
|
| 427 |
* A module may restrict access and alter forms depending on the selected
|
| 428 |
* authorization method which is a property of every repository array
|
| 429 |
* ($repository['authorization_method']).
|
| 430 |
*
|
| 431 |
* A list of all authorization methods can be retrieved
|
| 432 |
* by calling versioncontrol_get_authorization_methods().
|
| 433 |
*
|
| 434 |
* @return
|
| 435 |
* A structured array containing information about authorization methods
|
| 436 |
* provided by this module, wrapped in a structured array. Array keys are
|
| 437 |
* the unique string identifiers of each authorization method, and
|
| 438 |
* array values are the user-visible method descriptions (wrapped in t()).
|
| 439 |
*
|
| 440 |
* @ingroup Accounts
|
| 441 |
* @ingroup Authorization
|
| 442 |
* @ingroup Target audience: Authorization control modules
|
| 443 |
*/
|
| 444 |
function hook_versioncontrol_authorization_methods() {
|
| 445 |
return array(
|
| 446 |
'mymodule_code_ninja' => t('Code ninja skills required'),
|
| 447 |
);
|
| 448 |
}
|
| 449 |
|
| 450 |
/**
|
| 451 |
* Alter the list of repositories that are available for user registration
|
| 452 |
* and editing.
|
| 453 |
*
|
| 454 |
* @param $repository_names
|
| 455 |
* The list of repository names as it is shown in the select box
|
| 456 |
* at 'versioncontrol/register'. Array keys are the repository ids,
|
| 457 |
* and array elements are the captions in the select box.
|
| 458 |
* There's two things that can be done with this array:
|
| 459 |
* - Change (amend) the caption, in order to provide more information
|
| 460 |
* for the user. (E.g. note that an application is necessary.)
|
| 461 |
* - Unset any number of array elements. If you do so, the user will not
|
| 462 |
* be able to register a new account for this repository.
|
| 463 |
* @param $repositories
|
| 464 |
* A list of repositories (with the repository ids as array keys) that
|
| 465 |
* includes at least all of the repositories that correspond to the
|
| 466 |
* repository ids of the @p $repository_names array.
|
| 467 |
*
|
| 468 |
* @ingroup Accounts
|
| 469 |
* @ingroup Authorization
|
| 470 |
* @ingroup Repositories
|
| 471 |
* @ingroup Form handling
|
| 472 |
* @ingroup Target audience: Authorization control modules
|
| 473 |
*/
|
| 474 |
function hook_versioncontrol_alter_repository_selection(&$repository_names, $repositories) {
|
| 475 |
global $user;
|
| 476 |
|
| 477 |
foreach ($repository_names as $repo_id => $caption) {
|
| 478 |
if ($repositories[$repo_id]['authorization_method'] == 'mymodule_code_ninja') {
|
| 479 |
if (!in_array('code ninja', $user->roles)) {
|
| 480 |
unset($repository_names[$repo_id]);
|
| 481 |
}
|
| 482 |
}
|
| 483 |
}
|
| 484 |
}
|
| 485 |
|
| 486 |
/**
|
| 487 |
* Let the Version Control API know whether the given VCS account
|
| 488 |
* is authorized or not.
|
| 489 |
*
|
| 490 |
* @param $repository
|
| 491 |
* The repository where the status should be checked. (Note that the user's
|
| 492 |
* authorization status may differ for each repository.)
|
| 493 |
* @param $uid
|
| 494 |
* The user id of the checked account.
|
| 495 |
*
|
| 496 |
* @return
|
| 497 |
* TRUE if the account is authorized, or FALSE if it's not.
|
| 498 |
*
|
| 499 |
* @ingroup Accounts
|
| 500 |
* @ingroup Authorization
|
| 501 |
* @ingroup Target audience: Authorization control modules
|
| 502 |
*/
|
| 503 |
function hook_versioncontrol_is_account_authorized($repository, $uid) {
|
| 504 |
if ($repository['authorization_method'] != 'mymodule_dojo_status') {
|
| 505 |
return TRUE;
|
| 506 |
}
|
| 507 |
$result = db_query("SELECT status
|
| 508 |
FROM {mymodule_dojo_status}
|
| 509 |
WHERE uid = %d AND repo_id = %d",
|
| 510 |
$uid, $repository['repo_id']);
|
| 511 |
|
| 512 |
while ($account = db_fetch_object($result)) {
|
| 513 |
return ($account->status == MYMODULE_SENSEI);
|
| 514 |
}
|
| 515 |
return FALSE;
|
| 516 |
}
|
| 517 |
|
| 518 |
|
| 519 |
/**
|
| 520 |
* Unset filtered accounts before they are even attempted to be displayed
|
| 521 |
* on the account list ("admin/project/versioncontrol-accounts").
|
| 522 |
* You'll most probably use this in conjunction with an additional filter
|
| 523 |
* form element that is added to the account filter form
|
| 524 |
* ($form['#id'] == 'versioncontrol-account-filter-form') with form_alter().
|
| 525 |
*
|
| 526 |
* @param $accounts
|
| 527 |
* The accounts that would normally be displayed, in the same format as the
|
| 528 |
* return value of VersioncontrolAccountCache::getInstance()->getAccounts(). Entries in this list
|
| 529 |
* may be unset by this filter function.
|
| 530 |
*
|
| 531 |
* @ingroup Accounts
|
| 532 |
* @ingroup Form handling
|
| 533 |
* @ingroup Target audience: Authorization control modules
|
| 534 |
*/
|
| 535 |
function hook_versioncontrol_filter_accounts(&$accounts) {
|
| 536 |
if (empty($accounts)) {
|
| 537 |
return;
|
| 538 |
}
|
| 539 |
// Use a default value if the session variable hasn't yet been set.
|
| 540 |
if (!isset($_SESSION['mymodule_filter_username'])) {
|
| 541 |
$_SESSION['mymodule_filter_username'] = 'chx';
|
| 542 |
}
|
| 543 |
$mymodule_filter_username = $_SESSION['mymodule_filter_username'];
|
| 544 |
|
| 545 |
if ($mymodule_filter_username == '') {
|
| 546 |
return; // Don't change the list if no filtering should happen.
|
| 547 |
}
|
| 548 |
|
| 549 |
foreach ($accounts as $uid => $usernames_by_repository) {
|
| 550 |
foreach ($usernames_by_repository as $repo_id => $username) {
|
| 551 |
if ($username != $mymodule_filter_username) {
|
| 552 |
unset($accounts[$uid][$repo_id]);
|
| 553 |
if (empty($accounts[$uid])) {
|
| 554 |
unset($accounts[$uid]);
|
| 555 |
}
|
| 556 |
}
|
| 557 |
}
|
| 558 |
}
|
| 559 |
}
|
| 560 |
|
| 561 |
|
| 562 |
/**
|
| 563 |
* Extract account data from the account form's submitted
|
| 564 |
* values, and add it to the @p $additional_data array. Later, that array
|
| 565 |
* will be passed to hook_versioncontrol_account() as part of the account
|
| 566 |
* insert/update procedure.
|
| 567 |
*
|
| 568 |
* @param $additional_data
|
| 569 |
* The additional account data array which is being passed by reference so
|
| 570 |
* that it can be written to.
|
| 571 |
* @param $form
|
| 572 |
* The form array of the submitted account edit/register form, with
|
| 573 |
* $form['#id'] == 'versioncontrol-account-form' (amongst others).
|
| 574 |
* @param $form_state
|
| 575 |
* The form state of the submitted account edit/register form.
|
| 576 |
* If you altered this form and added an additional form element then
|
| 577 |
* $form_state['values'] will also contain the value of this form element.
|
| 578 |
*
|
| 579 |
* @ingroup Accounts
|
| 580 |
* @ingroup Form handling
|
| 581 |
* @ingroup Target audience: Commit access modules
|
| 582 |
* @ingroup Target audience: Authorization control modules
|
| 583 |
* @ingroup Target audience: All modules with account specific settings
|
| 584 |
*/
|
| 585 |
function hook_versioncontrol_account_submit(&$additional_data, $form, $form_state) {
|
| 586 |
if (empty($form_state['values']['mymodule_karma'])) {
|
| 587 |
return;
|
| 588 |
}
|
| 589 |
$additional_data['mymodule']['karma'] = $form_state['values']['mymodule_karma'];
|
| 590 |
}
|
| 591 |
|
| 592 |
/**
|
| 593 |
* Act on database changes when VCS accounts are inserted, updated or deleted.
|
| 594 |
*
|
| 595 |
* @param $op
|
| 596 |
* Either 'insert' when the account has just been created, 'update'
|
| 597 |
* when it has been updated, or 'delete' if it will be deleted after
|
| 598 |
* this function has been called.
|
| 599 |
* @param $uid
|
| 600 |
* The Drupal user id corresponding to the VCS account.
|
| 601 |
* @param $username
|
| 602 |
* The VCS specific username (a string) of the account.
|
| 603 |
* @param $repository
|
| 604 |
* The repository where the user has its VCS account.
|
| 605 |
* @param $additional_data
|
| 606 |
* An array of additional author information. Modules can fill this array
|
| 607 |
* by implementing hook_versioncontrol_account_submit().
|
| 608 |
*
|
| 609 |
* @ingroup Accounts
|
| 610 |
* @ingroup Form handling
|
| 611 |
* @ingroup Target audience: Commit access modules
|
| 612 |
* @ingroup Target audience: Authorization control modules
|
| 613 |
* @ingroup Target audience: All modules with account specific settings
|
| 614 |
*/
|
| 615 |
function hook_versioncontrol_account($op, $uid, $username, $repository, $additional_data = array()) {
|
| 616 |
switch ($op) {
|
| 617 |
case 'insert':
|
| 618 |
case 'update':
|
| 619 |
// Recap: if form_alter() wasn't applied, our array element is not set.
|
| 620 |
$mymodule_data = $additional_data['mymodule'];
|
| 621 |
|
| 622 |
if (!isset($mymodule_data)) {
|
| 623 |
// In most modules, form_alter() will always be applied to the
|
| 624 |
// account editing/creating form. If $mymodule_data is empty
|
| 625 |
// nevertheless then it means that the account has been created
|
| 626 |
// programmatically rather than with a form submit.
|
| 627 |
// In that case, we better assign a default value:
|
| 628 |
if ($op == 'insert') {
|
| 629 |
$mymodule_data = array('karma' => 50);
|
| 630 |
}
|
| 631 |
// Don't change the status for programmatical updates, though.
|
| 632 |
if ($op == 'update') {
|
| 633 |
break;
|
| 634 |
}
|
| 635 |
}
|
| 636 |
|
| 637 |
db_query("DELETE FROM {mymodule_karma} WHERE uid = %d", $uid);
|
| 638 |
db_query("INSERT INTO {mymodule_karma} (uid, karma) VALUES (%d, %d)",
|
| 639 |
$uid, $mymodule_data['karma']);
|
| 640 |
break;
|
| 641 |
|
| 642 |
case 'delete':
|
| 643 |
db_query("DELETE FROM {mymodule_karma} WHERE uid = %d", $uid);
|
| 644 |
break;
|
| 645 |
}
|
| 646 |
}
|
| 647 |
|
| 648 |
/**
|
| 649 |
* Add additional columns into the list of VCS accounts.
|
| 650 |
* By changing the @p $header and @p $rows_by_uid arguments,
|
| 651 |
* the account list can be customized accordingly.
|
| 652 |
*
|
| 653 |
* @param $accounts
|
| 654 |
* The list of accounts that is being displayed in the account table. This is
|
| 655 |
* a structured array like the one returned by VersioncontrolAccountCache::getInstance()->getAccounts().
|
| 656 |
* @param $repositories
|
| 657 |
* An array of repositories where the given users have a VCS account.
|
| 658 |
* Array keys are the repository ids, and array values are the
|
| 659 |
* repository arrays like returned from versioncontrol_get_repository().
|
| 660 |
* @param $header
|
| 661 |
* A list of columns that will be passed to theme('table').
|
| 662 |
* @param $rows_by_uid
|
| 663 |
* An array of existing table rows, with Drupal user ids as array keys.
|
| 664 |
* Each row already includes the generic column values, and for each row
|
| 665 |
* there is an account with the same uid given in the @p $accounts parameter.
|
| 666 |
*
|
| 667 |
* @ingroup Accounts
|
| 668 |
* @ingroup Form handling
|
| 669 |
* @ingroup Target audience: Authorization control modules
|
| 670 |
* @ingroup Target audience: All modules with account specific settings
|
| 671 |
*/
|
| 672 |
function hook_versioncontrol_alter_account_list($accounts, $repositories, &$header, &$rows_by_uid) {
|
| 673 |
$header[] = t('Karma');
|
| 674 |
|
| 675 |
foreach ($rows_by_uid as $uid => $row) {
|
| 676 |
$rows_by_uid[$uid][] = theme('user_karma', $uid);
|
| 677 |
}
|
| 678 |
}
|