ExifTool Forum

ExifTool => Developers => Topic started by: BigIsland on February 27, 2023, 04:43:06 PM

Title: Write metadata from Piwigo database problemWhen HTML code in IPTC decription tag
Post by: BigIsland on February 27, 2023, 04:43:06 PM
NO IPTC TAGS at all is written to the file.
<?php
/*
Plugin Name: Write Metadata
Description: Write Piwigo photo properties (title, description, author, tags) into IPTC fields
Author: plg
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=769
Version: 12.a
*/

// +-----------------------------------------------------------------------+
// | Define plugin constants                                               |
// +-----------------------------------------------------------------------+

defined('WRITE_METADATA_ID') or define('WRITE_METADATA_ID', basename(dirname(__FILE__)));
define('WRITE_METADATA_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');

// +-----------------------------------------------------------------------+
// | Edit Photo                                                            |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_admin_page', 'wm_add_link', 60);
function
wm_add_link()
{
 global
$template, $page;

 
$template->set_prefilter('picture_modify', 'wm_add_link_prefilter');

 if (isset(
$page['page']) and 'photo' == $page['page'])
 {
   
$template->assign(
     
'U_WRITEMETADATA',
     
get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties&amp;write_metadata=1'
     
);
 }
}

function
wm_add_link_prefilter($content)
{
 
$search = '{if !url_is_remote($PATH)}';
 
 
$replacement = '{if !url_is_remote($PATH)}
 <a class="icon-docs" href="{$U_WRITEMETADATA}" title="{\'Write metadata\'|translate}"></a>'
;

 return
str_replace($search, $replacement, $content);
}

add_event_handler('loc_begin_admin_page', 'wm_picture_write_metadata');
function
wm_picture_write_metadata()
{
 global
$page, $conf;

 
load_language('plugin.lang', dirname(__FILE__).'/');
 
 if (isset(
$page['page']) and 'photo' == $page['page'] and isset($_GET['write_metadata']))
 {
   
check_input_parameter('image_id', $_GET, false, PATTERN_ID);
   list(
$rc, $output) = wm_write_metadata($_GET['image_id']);

   if (
count($output) == 0)
   {
     
$_SESSION['page_infos'][] = l10n('Metadata written into file');
     
redirect(get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties');
   }
   else
   {
     
$page['errors'] = array_merge($page['errors'], $output);
   }
 }
}

// +-----------------------------------------------------------------------+
// | Batch Manager                                                         |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_element_set_global', 'wm_element_set_global_add_action');
function
wm_element_set_global_add_action()
{
 global
$template, $page;
 
 
$template->set_filename('writeMetadata', realpath(WRITE_METADATA_PATH.'element_set_global_action.tpl'));

 if (isset(
$_POST['submit']) and $_POST['selectAction'] == 'writeMetadata')
 {
   
$page['infos'][] = l10n('Metadata written into file');
 }

 
$template->assign(
   array(
     
'WM_PWG_TOKEN' => get_pwg_token(),
     )
   );

 
$template->append(
   
'element_set_global_plugins_actions',
   array(
     
'ID' => 'writeMetadata',
     
'NAME' => l10n('Write metadata'),
     
'CONTENT' => $template->parse('writeMetadata', true),
     )
   );
}

add_event_handler('ws_add_methods', 'wm_add_methods');
function
wm_add_methods($arr)
{
 include_once(
WRITE_METADATA_PATH.'ws_functions.inc.php');
}

// +-----------------------------------------------------------------------+
// | Common functions                                                      |
// +-----------------------------------------------------------------------+

/**
* inspired by convert_row_to_file_exiftool method in ExportImageMetadata
* class from plugin Tags2File. In plugin write_medata we just skip the
* batch command file, and execute directly on server (much more user
* friendly...).
*/
function wm_write_metadata($image_id)
{
 global
$conf, $logger;
 
 
$query = '
SELECT
   img.name,
   img.comment,
   img.author,
   img.date_creation,
   img.date_available,
   GROUP_CONCAT(tags.name) AS tags,
   img.path,
   img.representative_ext
 FROM '
.IMAGES_TABLE.' AS img
   LEFT OUTER JOIN '
.IMAGE_TAG_TABLE.' AS img_tag ON img_tag.image_id = img.id
   LEFT OUTER JOIN '
.TAGS_TABLE.' AS tags ON tags.id = img_tag.tag_id
 WHERE img.id = '
.$image_id.'
 GROUP BY img.id, img.name, img.comment, img.author, img.path, img.representative_ext
;'
;
 
$result = pwg_query($query);
 
$row = pwg_db_fetch_assoc($result);

 
$name = wm_prepare_string($row['name'], 500);
 
$description = wm_prepare_string($row['comment'], 10000);
 
$author = wm_prepare_string($row['author'], 500);
 
$date_creation = wm_prepare_string($row['date_creation'], 50);
 
$date_available = wm_prepare_string($row['date_available'], 50);


 
$command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool -overwrite_original';
 
$command.= ' -q';

 if (
strlen($name) > 0)
 {
   
# 2#105 in iptcparse($imginfo['APP13'])
   
$command.= ' -m -codedcharacterset=utf8 -IPTC:Headline="'.$name.'"';

   
# 2#005 in iptcparse($imginfo['APP13'])
   
$command.= ' -m -codedcharacterset=utf8 -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
 }

 if (
strlen($description) > 0)
 {
   
# 2#120 in iptcparse($imginfo['APP13'])
   
$command.= ' -m -codedcharacterset=utf8 -IPTC:Caption-Abstract="'.$description.'"';
 }

 if (
strlen($date_creation) > 0)
 {
   
# 2#055 in iptcparse($imginfo['APP13'])
   
$command.= ' -IPTC:DateCreated="'.$date_creation.'"';
 }

 if (
strlen($date_available) > 0)
 {
   
# 2#030 in iptcparse($imginfo['APP13'])
   
$command.= ' -IPTC:ReleaseDate="'.$date_available.'"';
 }

 if (
strlen($author) > 0)
 {
   
# 2#080 in iptcparse($imginfo['APP13'])
   
$iptc_field = 'By-line';

   if (
     
$conf['use_iptc']
     and isset(
$conf['use_iptc_mapping']['author'])
     and
'2#122' == $conf['use_iptc_mapping']['author']
     )
   {
     
# 2#122 in iptcparse($imginfo['APP13'])
     
$iptc_field = 'Writer-Editor';
   }

   
$command.= ' -m -codedcharacterset=utf8 -IPTC:'.$iptc_field.'="'.$author.'"';
 }
 
 if (
strlen($row['tags']) > 0)
 {
   
$tags = explode(',', $row['tags']);
   foreach (
$tags as $tag)
   {
     
$tag = wm_prepare_string($tag, 64);

     
# 2#025 in iptcparse($imginfo['APP13'])
     
$command.= ' -m -codedcharacterset=utf8 -IPTC:Keywords="'.$tag.'"';
   }
 }

 
$command.= ' "'.$row['path'].'"';
 
$command.= ' 2>&1';
 
// echo $command;
 
$logger->info(__FUNCTION__.' command = '.$command);

 
$exec_return = exec($command, $output, $rc);
 
// echo '$exec_return = '.$exec_return.'<br>';
 // echo '<pre>'; print_r($output); echo '</pre>';

 // as derivatives may contain metadata, they must be reset
 
delete_element_derivatives($row);

 return array(
$rc, $output);
}

function
wm_prepare_string($string, $maxLen)
{
 return
wm_cutString(
   
wm_explode_description(
     
wm_decode_html_string_to_unicode($string)
     ),
   
$maxLen
   
);
}

function
wm_cutString($description, $maxLen)
{
 if (
strlen($description) > $maxLen)
 {
   
$description = substr($description, 0, $maxLen);
 }
 return
$description;
}

function
wm_explode_description($description)
{
 return
str_replace(
   array(
'<br>', '<br />', "\n", "\r"),
   array(
'', '', '', ''),
   
$description
   
);
}

function
wm_decode_html_string_to_unicode($string)
{
 if (isset(
$string) and strlen($string) > 0)
 {
   
$string = html_entity_decode(trim($string), ENT_QUOTES, 'UTF8');
   
$string = stripslashes($string);
 }
 else
 {
   
$string = '';
 }
 
 return(
$string);
}
?>


Title: Re: Write metadata from Piwigo database problemWhen HTML code in IPTC decription tag
Post by: StarGeek on February 27, 2023, 07:46:42 PM
I'm not sure what you expect us to do? Install Piwigo and debug your 260+ lines of code?  Without knowing what your error messages are?
Title: Re: Write metadata from Piwigo database problemWhen HTML code in IPTC decription tag
Post by: BigIsland on February 28, 2023, 05:26:42 AM
OK! I Guess it's too complicated for you guys!  8)
Title: Re: Write metadata from Piwigo database problemWhen HTML code in IPTC decription tag
Post by: BigIsland on February 28, 2023, 11:52:42 AM
Cant you tell me where and/or how to check the log files for exiftool installed on CentoOS 9 Stream ?
Title: Re: Write metadata from Piwigo database problemWhen HTML code in IPTC decription tag
Post by: Phil Harvey on February 28, 2023, 12:59:49 PM
ExifTool doesn't create log files.

- Phil
Title: Re: Write metadata from Piwigo database problemWhen HTML code in IPTC decription tag
Post by: BigIsland on March 01, 2023, 04:42:08 AM
Ok!? So how am I going to find out the error then? ::)
Title: Re: Write metadata from Piwigo database problemWhen HTML code in IPTC decription tag
Post by: Phil Harvey on March 01, 2023, 07:28:02 AM
Run the exiftool command from a command shell.

- Phil