Skip to content

Commit

Permalink
[#27] added separate dedupe log option
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Oct 23, 2023
2 parents 321f8f0 + 36f80de commit 777dbf6
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CRM/Xdedupe/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CRM_Xdedupe_Configuration
'is_automatic' => 'Integer',
'is_scheduled' => 'Integer',
'weight' => 'Integer',
'merge_log' => 'String',
];

protected $configuration_id;
Expand Down Expand Up @@ -125,7 +126,7 @@ public static function getConfigurations($sql_query)
while ($configuration_search->fetch()) {
$data = [];
foreach (self::$main_attributes as $attribute_name => $attribute_type) {
$data[$attribute_name] = $configuration_search->$attribute_name;
$data[$attribute_name] = $configuration_search->$attribute_name ?? null;
}
if (isset($configuration_search->config)) {
$config = json_decode($configuration_search->config, true);
Expand Down
37 changes: 33 additions & 4 deletions CRM/Xdedupe/Form/ControlRoom.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ public function buildQuickForm()
$last_configuration = $configuration->getConfig();
$last_configuration['name'] = $configuration->getAttribute('name');
$last_configuration['description'] = $configuration->getAttribute('description');
$last_configuration['merge_log'] = $configuration->getAttribute('merge_log');
CRM_Utils_System::setTitle(
E::ts("Extendend Dedupe: '%1'", [1 => $configuration->getAttribute('name')])
E::ts("Extended Dedupe: '%1'", [1 => $configuration->getAttribute('name')])
);
} else {
// this is an
$last_configuration = self::getUserSettings()->get('xdedup_last_configuration');
$last_configuration = self::getUserSettings()->get('xdedupe_last_configuration');
unset($last_configuration['cid'], $last_configuration['name'], $last_configuration['description']);
}

Expand Down Expand Up @@ -389,7 +390,16 @@ public function buildQuickForm()
['class' => 'huge crm-select2', 'multiple' => 'multiple']
);

// build buttons
$this->add(
'text',
'merge_log',
E::ts("Merge Log"),
['class' => 'huge'],
false,
[]
);

// build buttons
$buttons = [
[
'type' => 'find',
Expand Down Expand Up @@ -471,6 +481,24 @@ public function validate()
}
}
}

// make sure that the merge_log is writable
if (!empty($values['merge_log'])) {
if (file_exists($values['merge_log'])) {
// file is there, let's see if we can append
if (!is_writable($values['merge_log'])) {
$this->_errors['merge_log'] =
E::ts("CiviCRM cannot write to file '%1'.", [1 => $values['merge_log']]);
}
} else {
// file does not exist, see if we can create it
if (!touch($values['merge_log'])) {
$this->_errors['merge_log'] =
E::ts("CiviCRM cannot create log file '%1'.", [1 => $values['merge_log']]);
}
}
}

return count($this->_errors) == 0;
}

Expand Down Expand Up @@ -560,7 +588,7 @@ public function postProcess()
$this->prepareSubmissionData($values);

// store settings by user
self::getUserSettings()->set('xdedup_last_configuration', $values);
self::getUserSettings()->set('xdedupe_last_configuration', $values);

if ($this->cr_command == 'clear') {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/xdedupe/controlroom', "reset=1"));
Expand Down Expand Up @@ -609,6 +637,7 @@ public function postProcess()
'force_merge' => empty($values['force_merge']) ? '0' : '1',
'resolvers' => $values['auto_resolve'],
'pickers' => $values['main_contact'],
'merge_log' => $values['merge_log'],
],
$return_url
);
Expand Down
17 changes: 11 additions & 6 deletions CRM/Xdedupe/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ public function __construct($params)
// initialise merge_log
if (!empty($params['merge_log'])) {
$this->merge_log = $params['merge_log'];
$this->merge_log_handle = fopen($this->merge_log, 'a');
} else {
$this->merge_log = tempnam('/tmp', 'xdedupe_merge');
$this->merge_log = null;
$this->merge_log_handle = null;
}
$this->merge_log_handle = fopen($this->merge_log, 'a');

$this->log("Initialised merger: " . json_encode($params));
}
Expand Down Expand Up @@ -134,16 +135,20 @@ public function setAborted($reason)
*/
public function log($message)
{
fputs($this->merge_log_handle, $message);
CRM_Core_Error::debug_log_message("XMERGE: {$message}");
if (empty($this->merge_log_handle)) {
CRM_Core_Error::debug_log_message("XMERGE: {$message}");
} else {
$message = date('[Y-m-d H:i:s] ') . $message . "\n";
fputs($this->merge_log_handle, $message);
}
}

/**
* Log an error message to the merge log, and the internal error counter
*
* @param $message string message
*/
public function logError($message)
public function logError(string $message)
{
$this->stats['errors'][] = $message;
$this->log("ERROR: " . $message);
Expand All @@ -155,7 +160,7 @@ public function logError($message)
*/
public function multiMerge($main_contact_id, $other_contact_ids)
{
$this->log("Merging [{$main_contact_id}] with [" . implode(',', $other_contact_ids) . ']');
$this->log("Merging into contact [{$main_contact_id}]: [" . implode(',', $other_contact_ids) . ']');

// first check for poor judgement:
if (in_array($main_contact_id, $other_contact_ids)) {
Expand Down
1 change: 1 addition & 0 deletions CRM/Xdedupe/Page/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected function processAutomergeCommand()
'force_merge' => empty($config['force_merge']) ? '0' : '1',
'resolvers' => $config['auto_resolve'],
'pickers' => $config['main_contact'],
'merge_log' => $config['merge_log'],
'config_id' => $configuration->getID(),
],
CRM_Utils_System::url('civicrm/xdedupe/manage', 'reset=1')
Expand Down
22 changes: 22 additions & 0 deletions CRM/Xdedupe/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,26 @@ public function upgrade_0502()
return true;
}

/**
* Make sure the new table is known to logging
*
* @return boolean
* TRUE on success
* @throws Exception
* if something goes wrong
*/
public function upgrade_1201()
{
$this->ctx->log->info('Adding merge log setting');

// add new column for new merge_log setting
if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_xdedupe_configuration', 'merge_log')) {
CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_xdedupe_configuration` ADD COLUMN `merge_log` TEXT");
}

// fix logging schema differences
$logging = new CRM_Logging_Schema();
$logging->fixSchemaDifferences();
return true;
}
}
7 changes: 7 additions & 0 deletions api/v3/Xdedupe/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function _civicrm_api3_xdedupe_merge_spec(&$spec)
'title' => 'Dedupe Run ID',
'description' => 'If given, the tuple will be removed from this dedupe run, if the merge was successful',
);
$spec['merge_log'] = array(
'name' => 'merge_log',
'api.default' => '',
'type' => CRM_Utils_Type::T_STRING,
'title' => 'Merge Log',
'description' => 'If given, the merge\'s log messages will be appended to this log file, rather than the CiviCRM log. The file needs to be writable for CiviCRM.',
);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions js/controlroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ cj("table.xdedupe-result").click(function (e) {
let main_contact_id = cj(e.target).parent().find("span.xdedupe-main-contact-id").text();
let other_contact_ids = cj(e.target).parent().find("span.xdedupe-other-contact-ids").text();
let force_merge = cj("#force_merge").prop('checked') ? "1" : "0";
let merge_log = cj("#merge_log").val();
let resolvers = cj("#auto_resolve").val();
if (resolvers == null) {
resolvers = [];
Expand All @@ -108,6 +109,7 @@ cj("table.xdedupe-result").click(function (e) {
"main_contact_id": main_contact_id,
"other_contact_ids": other_contact_ids,
"force_merge": force_merge,
"merge_log": merge_log,
"resolvers": resolvers.join(','),
"pickers": pickers.join(','),
"dedupe_run": CRM.vars['xdedupe_controlroom'].dedupe_run_id
Expand Down
1 change: 1 addition & 0 deletions sql/civicrm_xdedupe_configuration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS `civicrm_xdedupe_configuration`(
`is_scheduled` tinyint COMMENT 'is configuration enabled for scheduled unsupervised execution',
`config` text COMMENT 'configuration (JSON)',
`last_run` text COMMENT 'stats of the last run (JSON)',
`merge_log` text COMMENT 'log file to be used (absolute file path)',
`weight` int unsigned COMMENT 'defines listing order',
PRIMARY KEY ( `id` )
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
4 changes: 4 additions & 0 deletions templates/CRM/Xdedupe/Form/ControlRoom.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@
{htxt id='id-xdedupe-forcemerge'}
<p>{ts domain="de.systopia.xdedupe"}You can use this flag to merge contacts even if they still have conflicts.{/ts}</p>
<p>{ts domain="de.systopia.xdedupe"}<strong>This is highly discouraged! You should use an appropriate set of resolvers, to merge contacts automatically. Merging contacts by force can destroy them. If you must, be sure to test thoroughly before use.</strong>{/ts}</p>
{/htxt}

{htxt id='id-merge-log'}
<p>{ts domain="de.systopia.xdedupe"}You can provide a separate log file for merges. If you don't, the information will be logged to the CiviCRM log.{/ts}</p>
{/htxt}
7 changes: 7 additions & 0 deletions templates/CRM/Xdedupe/Form/ControlRoom.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@
<div class="content">{$form.auto_resolve.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.merge_log.label}&nbsp;<a
onclick='CRM.help("{ts domain="de.systopia.xdedupe"}Merge Log{/ts}", {literal}{"id":"id-merge-log","file":"CRM\/Xdedupe\/Form\/ControlRoom"}{/literal}); return false;'
href="#" title="{ts domain="de.systopia.xdedupe"}Help{/ts}" class="helpicon">&nbsp;</a></div>
<div class="content">{$form.merge_log.html}</div>
<div class="clear"></div>
</div>
</div>
<br/>
Expand Down
10 changes: 0 additions & 10 deletions xdedupe.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ function xdedupe_civicrm_alterLogTables(&$logTableSpec)
}
}

/**
* Implements hook_civicrm_xmlMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function xdedupe_civicrm_xmlMenu(&$files)
{
_xdedupe_civix_civicrm_xmlMenu($files);
}

/**
* Implements hook_civicrm_install().
*
Expand Down

0 comments on commit 777dbf6

Please sign in to comment.