Commit 6e927d8b authored by Jonny Bradley's avatar Jonny Bradley

[NEW] Combined backport for convert tracker attachments to files script

[bp/r65918][NEW] Add script to convert tracker attachments to tracker files 
[bp/r65941][FIX] trackers: Mainly suppressing notices on the script committed in r65918 (thanks xorti) but a few other improvements: e.g. use the field's default gallery if there is one (or use the root gallery if not), wrapped most of the code in a function to ease debugging, added a "remove" option (warning: irreversible) and made the output a little less verbose [thanks marclaporte] 
[bp/r65955][FIX] trackers: Add description and comment to files when converting from attachments 
[FIX] trackers: Try to convert encoding of descriptions when converting from attachments [from revision 65958]
[bp/r65959][revert r65958] Didn't work and may cause more issues than it might have solved
[was][FIX] trackers: Try to convert encoding of descriptions when converting from attachments 
[bp/r65961][FIX] trackers: Try to carry on if a database error occurs when converting from attachments 
[bp/r65963][FIX] trackers: Add a summary after converting from attachments
parent bfb87111
......@@ -91,6 +91,7 @@ doc/devtools/clear_tracker_fast.sql -text
doc/devtools/composer_http_mode.php -text
doc/devtools/convert_templates_prefs-2.0.php -text
doc/devtools/convert_tpl_ajax.php -text
doc/devtools/convert_tracker_attachments.php -text
doc/devtools/csscheck.sh -text
doc/devtools/cssmap.pl -text
doc/devtools/dbdiff/composer.json -text
......
#!/usr/bin/php
<?php
// (c) Copyright 2002-2018 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
if ($argc < 3) {
$helpMsg = "\nUsage: php doc/devtools/convert_tracker_attachments.php trackerId fieldId [fileGalId] ['remove']\n";
$helpMsg .= "\nExamples: \n\t\tphp doc/devtools/convert_tracker_attachments.php 1 2";
$helpMsg .= "\n\t\tphp doc/devtools/convert_tracker_attachments.php 1 2 2\n\n";
exit($helpMsg);
}
require_once('tiki-setup.php');
$context = new Perms_Context("admin");
$trackerId = $argv[1];
$fieldId = $argv[2];
if (! isset($trackerId)) {
echo "Error: Missing trackerId\n";
exit(1);
}
if (! isset($fieldId)) {
echo "Error: Missing fieldId\n";
exit(1);
}
if (isset($argv[3])) {
$galleryId = $argv[3];
} else {
$galleryId = 0;
}
if (isset($argv[4]) && $argv[4] === 'remove') {
$remove = true;
} else {
$remove = false;
}
/**
* @param $trackerId
* @param $fieldId
* @param int $galleryId
* @throws Services_Exception
* @throws Exception
*/
function convertAttachments($trackerId, $fieldId, $galleryId = 0, $remove = false)
{
global $prefs;
$trklib = TikiLib::lib('trk');
$trackerUtilities = new Services_Tracker_Utilities;
$definition = Tracker_Definition::get($trackerId);
// Check if tracker and fieldId are valid
$fgField = $trackerUtilities->getFieldsFromIds($definition, [$fieldId]);
if (! $fgField || $fgField[0]['type'] !== 'FG') {
echo "Error: Invalid fieldId {$fieldId} for trackerId {$trackerId}\n";
exit(1);
}
$fgField = $fgField[0];
if (! $galleryId && isset($fgField['options_map']['galleryId'])) {
$galleryId = $fgField['options_map']['galleryId'];
}
if (! $galleryId) {
$galleryId = $prefs['fgal_root_id'];
}
// Check if its a valid file gallery
try {
$fileUtilities = new Services_File_Utilities;
$galInfo = $fileUtilities->checkTargetGallery($galleryId);
} catch (Services_Exception $e) {
echo "Error: {$e->getMessage()}\n";
exit(1);
}
$items = $trackerUtilities->getItems(['trackerId' => $trackerId]);
$failedAttIds = [];
$itemsFailed = 0;
$itemsProcessed = 0;
$attachmentsProcessed = 0;
foreach ($items as $item) {
$itemId = $item['itemId'];
$itemObject = Tracker_Item::fromId($itemId);
if (! $itemObject || $itemObject->getDefinition() !== $definition) {
continue;
}
$atts = $trklib->list_item_attachments($itemId, 0, -1, 'comment_asc', '');
$fileIdList = [];
$numAttachments = sizeof($atts['data']);
if ($numAttachments === 0) {
echo "Tracker Item {$itemId} skipped (no attachments)\n";
continue;
} else {
echo "Updating tracker item {$itemId}:\n";
$ess = $numAttachments > 1 ? 's' : '';
echo "- Found {$numAttachments} attachment$ess\n";
}
foreach ($atts['data'] as $attachment) {
$attachment = $trklib->get_item_attachment($attachment['attId']);
if (! $attachment) {
echo "- Warning: Unable to get item attachment with attId {$attachment['attId']}\n";
continue;
}
$name = $attachment['filename'];
$size = $attachment['filesize'];
$type = $attachment['filetype'];
$description = $attachment['longdesc'];
if ($attachment['comment']) {
$description .= "\nComment\n" . $attachment['comment'];
}
$data = $attachment['data'];
try {
$fileId = $fileUtilities->uploadFile($galInfo, $name, $size, $type, $data, null, null, null, $description);
} catch (Exception $e) {
$fileId = false;
echo "Error: File {$attachment['filename']} on item {$itemId} could not be saved\n";
echo "{$e->getMessage()}\n";
}
if ($fileId !== false) {
array_push($fileIdList, $fileId);
echo "- Attachment {$attachment['filename']} uploaded to file gallery\n";
} else {
echo "- Failed to upload attachment {$attachment['filename']} to file gallery\n";
$failedAttIds[] = $attachment['attId'];
}
}
if (empty($fileIdList)) {
echo "No files were uploaded to the file gallery\n";
echo "Tracker Item {$itemId} skipped\n";
continue;
}
$input = new JitFilter([
'trackerId' => $trackerId,
'itemId' => $itemId,
]);
$processedFields = $itemObject->prepareInput($input);
$fields = [];
foreach ($processedFields as $key => $field) {
$permName = $field['permName'];
$fields[$permName] = isset($field['value']) ? $field['value'] : '';
if ($field['fieldId'] == $fieldId && $field['type'] == 'FG') {
$fields[$permName] = empty($fields[$permName]) ? implode(',', $fileIdList) : $fields[$permName] . ',' . implode(',', $fileIdList);
}
}
$result = $trackerUtilities->updateItem(
$definition,
[
'itemId' => $itemId,
'status' => '',
'fields' => $fields,
]
);
if ($result !== false) {
$itemsProcessed++;
if ($remove) {
foreach ($atts['data'] as $attachment) {
if (! in_array($attachment['attId'], $failedAttIds)) {
$trklib->remove_item_attachment($attachment['attId'], $itemId);
$attachmentsProcessed++;
} else {
echo "(Attachment {$attachment['attId']} {$attachment['filename']} not removed)\n";
$numAttachments--;
}
}
echo "Tracker item {$itemId} updated successfully and $numAttachments attachment$ess removed\n";
} else {
$attachmentsProcessed += $numAttachments;
echo "Tracker item {$itemId} updated successfully\n";
}
} else {
echo "Tracker item {$itemId} update failed\n";
$itemsFailed++;
}
}
$failCount = count($failedAttIds);
$op = $remove ? "moved" : "copied";
echo "\nConvert completed:\n {$itemsProcessed} processed ({$itemsFailed} failed) and\n {$attachmentsProcessed} attachments {$op} ({$failCount} failed)\n\n";
}
convertAttachments($trackerId, $fieldId, $galleryId, $remove);
exit(0);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment