Here you can find a script to import
xmp.dc:subject tags from the AMD database into the piwigo's tags system.
1/ first, install the plugin
[extension by Piwigo Team] LocalFiles Editor
2/ copy the code given below, and put it between the
<?php ?> in the personnal plugin tabs of the LocalFileEditor plugin
3/ save the personnal plugin, and activate it in the plugin manager
4/ check if everything is OK : just go to the
http://your-adress-here/admin.php?importAmdTags=y url ; this url will not modify the database, it just display some information about the import
5/ if everything is OK, you go to the
http://your-adress-here/admin.php?importAmdTags=y&checkOnly=n url ; this url will update the database
When everything is done, delete the personnal plugin (not a good idea to keep this one).
function importTag()
{
if(!defined('IN_ADMIN')) return(false);
if(isset($_REQUEST['importAmdTags']) and $_REQUEST['importAmdTags']=='y')
{
if(isset($_REQUEST['checkOnly']) and $_REQUEST['checkOnly']=='n')
{
xmpSubject2Tag(false);
}
else
{
xmpSubject2Tag(true);
}
}
}
function dumpNfo($nfo, $mode="")
{
global $template;
switch($mode)
{
case 'warning':
$colors='font-weight:bold;color:#ffffff;background:#cc0000;';
break;
case 'info':
$colors='font-weight:bold;color:#ffffff;background:#0000cc;';
break;
case 'ignore':
$colors='font-weight:bold;color:#ffffff;background:#ff00ff;';
break;
default:
$colors="";
}
echo "<span style='text-align:left;font-family:mono;$colors'>$nfo</span>";
}
function xmpSubject2Tag($checkOnly=true)
{
$piwigoTagsExist=array();
$existingTags=array();
$piwigoTags=array();
$tagList=array();
$counter=array(
'old' => 0,
'new' => 0,
'img' => 0,
'ignore' => 0,
);
dumpNfo("---Start to build tag from XMP metadata---<br>");
if($checkOnly)
{
dumpNfo("(!) build is in simulate mode, database will not be modified</span><br>", "warning");
}
// build a piwigo tags list
$sql="SELECT id, name, url_name
FROM ".TAGS_TABLE;
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$piwigoTags[$row['url_name']]=array(
'id' => $row['id'],
'name' => $row['name']
);
}
}
// build a tags list from xmp metadata
$sql="SELECT imageId, value
FROM piwigo_amd_images_tags pait
LEFT JOIN piwigo_amd_used_tags paut ON pait.numId = paut.numId
WHERE paut.tagId='xmp.dc:subject'";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$tmp=unserialize($row['value']);
foreach($tmp['values'] as $tag)
{
$urlName=str2url($tag);
if(array_key_exists($urlName, $tagList))
{
$tagList[$urlName]['images'][]=$row['imageId'];
}
else
{
$tagList[$urlName]=array(
'id' => 0,
'name' => $tag,
'images' => array($row['imageId'])
);
}
}
$counter['img']++;
}
}
// for each xmp tag found, check if already exist in the piwigo tags list
// if not, insert the new tag and get the tag Id
foreach($tagList as $key => $tag)
{
if(!array_key_exists($key, $piwigoTags))
{
$counter['new']++;
dumpNfo("(i) Tag <b>".$tag['name']."</b> doesn't exist in Piwigo, create it ; Tag Id = ");
if($checkOnly)
{
dumpNfo("--<br>");
}
else
{
$sql="INSERT INTO ".TAGS_TABLE." VALUES ('', '".addslashes($tag['name'])."', '".$key."')";
$result=pwg_query($sql);
if($result)
{
$tagList[$key]['id']=pwg_db_insert_id(TAGS_TABLE);
dumpNfo($tagList[$key]['id']."<br>");
}
else
{
dumpNfo("failed to insert new tag id !<br>", "warning");
}
}
}
else
{
$counter['old']++;
dumpNfo("(i) Tag <b>".$tag['name']."</b> already exist in Piwigo, don't create it<br>", "ignore");
$tagList[$key]['id']=$piwigoTags[$key]['id'];
$existingTags[]=$piwigoTags[$key]['id'];
}
}
dumpNfo("(i) Tags found from XMP metadata :<br>", "info");
dumpNfo("(i) - New created = ".$counter['new']."<br>", "info");
dumpNfo("(i) - Already exists = ".$counter['old']."<br>", "info");
dumpNfo("(i) - From pictures = ".$counter['img']."<br>", "info");
// build a piwigo picture-tags list with existing tags
if(count($existingTags)>0)
{
$sql="SELECT image_id, tag_id
FROM ".IMAGE_TAG_TABLE."
WHERE tag_id in (".implode(',', $existingTags).")";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$piwigoTagsExist[$row['image_id'].'-'.$row['tag_id']]='';
}
}
}
dumpNfo("Associating tags with images<br>");
$inserts=array();
// associate tags id to pictures
foreach($tagList as $key => $tag)
{
foreach($tag['images'] as $imageId)
{
if(!array_key_exists($imageId.'-'.$tag['id'], $piwigoTagsExist) )
{
$inserts[]=array(
'image_id' => $imageId,
'tag_id' => $tag['id']
);
dumpNfo("(i) Tag <b>".$tag['id']."</b> (".$tag['name'].") is associated with picture <b>$imageId</b><br>");
}
else
{
dumpNfo("(i) Tag <b>".$tag['id']."</b> (".$tag['name'].") is already associated with picture <b>$imageId</b><br>", "ignore");
$counter['ignore']++;
}
}
}
dumpNfo("(i) Associate tag with picture<br>", "info");
dumpNfo("(i) - New = ".count($insert)."<br>", "info");
dumpNfo("(i) - Ignored = ".$counter['ignore']."<br>", "info");
if(!$checkOnly and count($inserts))
{
mass_inserts(
IMAGE_TAG_TABLE,
array('image_id', 'tag_id'),
$inserts
);
}
dumpNfo("---Done---<br>");
}
importTag();