From 724f278b8a49069e7c79e1ed965bbf4e4e9335b6 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Fri, 14 Jan 2011 16:50:58 -0500
Subject: [PATCH] feature #1027356: Provide a way to set default plugins for repositories.

- Add global variables for each default repository plugin.
- Stop hardcoding default repository plugins.
- Remove "Use Author mapping plugin", to avoid handling a special form
  submit case at the repository getCommitterMapper().
- Add a 'none' repository mapper plugin that do not map.
---
 includes/VersioncontrolRepository.php              |   28 ++++---------------
 .../VersioncontrolUserMapperNone.class.php         |   15 ++++++++++
 includes/plugins/user_mapping_methods/none.inc     |    9 ++++++
 tests/VersioncontrolTestCase.test                  |    4 +-
 versioncontrol.admin.inc                           |    8 +++---
 versioncontrol.install                             |   12 ++++++++
 6 files changed, 48 insertions(+), 28 deletions(-)
 create mode 100644 includes/plugins/user_mapping_methods/VersioncontrolUserMapperNone.class.php
 create mode 100644 includes/plugins/user_mapping_methods/none.inc

diff --git includes/VersioncontrolRepository.php includes/VersioncontrolRepository.php
index cb2ce06..fe9bd8f 100644
--- includes/VersioncontrolRepository.php
+++ includes/VersioncontrolRepository.php
@@ -387,10 +387,11 @@ abstract class VersioncontrolRepository implements VersioncontrolEntityInterface
     ctools_include('plugins');
 
     if (empty($this->plugins[$plugin_slot])) {
-      throw new Exception("Attempted to get plugin in slot '$plugin_slot', but no plugin has been assigned to that slot on this repository.", E_STRICT);
-      return FALSE;
+      $plugin_name = variable_get('versioncontrol_repository_plugin_default_' . $plugin_slot, '');
+    }
+    else {
+      $plugin_name = $this->plugins[$plugin_slot];
     }
-    $plugin_name = $this->plugins[$plugin_slot];
 
     $plugin = ctools_get_plugins('versioncontrol', $plugin_type, $plugin_name);
     if (!is_array($plugin)) {
@@ -423,11 +424,6 @@ abstract class VersioncontrolRepository implements VersioncontrolEntityInterface
 
   public function getAuthHandler() {
     if (!isset($this->pluginInstances['auth_handler'])) {
-      // If no plugin is set, use the free-for-all plugin
-      if (empty($this->plugins['auth_handler'])) {
-        // FIXME temporarily writing to a db-recorded field like this is very hacky
-        $this->plugins['auth_handler'] = 'ffa';
-      }
       $this->pluginInstances['auth_handler'] = $this->getPluginClass('auth_handler', 'vcs_auth', 'handler');
       $this->pluginInstances['auth_handler']->setRepository($this);
     }
@@ -436,26 +432,14 @@ abstract class VersioncontrolRepository implements VersioncontrolEntityInterface
 
   public function getAuthorMapper() {
     if (!isset($this->pluginInstances['author_mapper'])) {
-      // if no plugin is set, just directly register FALSE for the instance
-      if (empty($this->plugins['author_mapper'])) {
-        $this->pluginInstances['author_mapper'] = FALSE;
-      }
-      else {
-        $this->pluginInstances['author_mapper'] = $this->getPluginClass('author_mapper', 'user_mapping_methods', 'mapper');
-      }
+      $this->pluginInstances['author_mapper'] = $this->getPluginClass('author_mapper', 'user_mapping_methods', 'mapper');
     }
     return $this->pluginInstances['author_mapper'];
   }
 
   public function getCommitterMapper() {
     if (!isset($this->pluginInstances['committer_mapper'])) {
-      // If nothing is set for the committer mapper plugin, reuse the author one
-      if (empty($this->plugins['committer_mapper'])) {
-        $this->pluginInstances['committer_mapper'] = $this->getAuthorMapper();
-      }
-      else {
-        $this->pluginInstances['committer_mapper'] = $this->getPluginClass('committer_mapper', 'user_mapping_methods', 'mapper');
-      }
+      $this->pluginInstances['committer_mapper'] = $this->getPluginClass('committer_mapper', 'user_mapping_methods', 'mapper');
     }
 
     return $this->pluginInstances['committer_mapper'];
diff --git includes/plugins/user_mapping_methods/VersioncontrolUserMapperNone.class.php includes/plugins/user_mapping_methods/VersioncontrolUserMapperNone.class.php
new file mode 100644
index 0000000..bb7d3c9
--- /dev/null
+++ includes/plugins/user_mapping_methods/VersioncontrolUserMapperNone.class.php
@@ -0,0 +1,15 @@
+<?php
+//$Id
+
+/**
+ * Plugin that do not map.
+ */
+class VersioncontrolUserMapperSimpleMail implements VersioncontrolUserMapperInterface {
+  public function mapAuthor(VersioncontrolOperation $commit) {
+    return FALSE;
+  }
+
+  public function mapCommitter(VersioncontrolOperation $commit) {
+    return FALSE;
+  }
+}
diff --git includes/plugins/user_mapping_methods/none.inc includes/plugins/user_mapping_methods/none.inc
new file mode 100644
index 0000000..0070d55
--- /dev/null
+++ includes/plugins/user_mapping_methods/none.inc
@@ -0,0 +1,9 @@
+<?php
+// $Id$
+
+$plugin = array(
+  'title' => t('None (no mapping)'),
+  'mapper' => array(
+    'class' => 'VersioncontrolUserMapperNone',
+  ),
+);
diff --git tests/VersioncontrolTestCase.test tests/VersioncontrolTestCase.test
index fa258f2..11d1f34 100644
--- tests/VersioncontrolTestCase.test
+++ tests/VersioncontrolTestCase.test
@@ -147,8 +147,8 @@ abstract class VersioncontrolTestCase extends DrupalWebTestCase {
     );
     $default_plugins = array(
       'auth_handler' => 'ffa',
-      'author_mapper' => 'simple_mail',
-      'committer_mapper' => 'simple_mail',
+      'author_mapper' => 'none',
+      'committer_mapper' => 'none',
       'webviewer_url_handler' => 'none',
     );
 
diff --git versioncontrol.admin.inc versioncontrol.admin.inc
index 97ae7a8..1972d20 100644
--- versioncontrol.admin.inc
+++ versioncontrol.admin.inc
@@ -342,16 +342,16 @@ function versioncontrol_admin_repository_edit(&$form_state, $repository, $vcs =
     '#type' => 'radios',
     '#title' => t('Author mapping'),
     '#description' => t('The mapping logic to be used for "author" data. All VCSes have some sort of author field.'),
-    '#default_value' => $repository_exists ? $repository->plugins['author_mapper'] : 0,
-    '#options' => array(0 => '<em>' . t('None (no mapping)') . '</em>')  + versioncontrol_user_mapping_methods_get_names(),
+    '#default_value' => $repository_exists ? $repository->plugins['author_mapper'] : variable_get('versioncontrol_repository_plugin_default_author_mapper', 'none'),
+    '#options' => versioncontrol_user_mapping_methods_get_names(),
   );
 
   $form['user_mapping']['committer_mapper'] = array(
     '#type' => 'radios',
     '#title' => t('Committer mapping'),
     '#description' => t('The mapping logic to be used for "committer" data. Only some VCSes distinguish between author and committer. For most use cases, it is recommended best to set this to use the same plugin as for author mapping, especially if using "None (No mapping)" for author mapping.'),
-    '#default_value' => $repository_exists ? $repository->plugins['committer_mapper'] : 0,
-    '#options' => array(0 => '<em>' . t('Use Author mapping plugin') . '</em>') + versioncontrol_user_mapping_methods_get_names(),
+    '#default_value' => $repository_exists ? $repository->plugins['committer_mapper'] : variable_get('versioncontrol_repository_plugin_default_committer_mapper', 'none'),
+    '#options' => versioncontrol_user_mapping_methods_get_names(),
   );
 
   $form['user_authentication'] = array(
diff --git versioncontrol.install versioncontrol.install
index c537b0e..4c46d68 100644
--- versioncontrol.install
+++ versioncontrol.install
@@ -1164,3 +1164,15 @@ function versioncontrol_update_6313() {
   db_drop_unique_key(&$ret, 'versioncontrol_repositories', 'name');
   return $ret;
 }
+
+/**
+ * Add default plugins for repositories.
+ */
+function versioncontrol_update_6314() {
+  $ret = array();
+  variable_set('versioncontrol_repository_plugin_default_webviewer_url_handler', 'none');
+  variable_set('versioncontrol_repository_plugin_default_auth_handler', 'ffa');
+  variable_set('versioncontrol_repository_plugin_default_author_mapper', 'none');
+  variable_set('versioncontrol_repository_plugin_default_committer_mapper', 'none');
+  return $ret;
+}
-- 
1.7.2.3

