Index: /trunk/admin.php
===================================================================
--- /trunk/admin.php	(revision 8761)
+++ /trunk/admin.php	(revision 8762)
@@ -92,4 +92,5 @@
 $page['errors'] = array();
 $page['infos']  = array();
+$page['warnings']  = array();
 
 if (isset($_SESSION['page_infos']))
@@ -177,4 +178,9 @@
 }
 
+if (count($page['warnings']) != 0)
+{
+  $template->assign('warnings', $page['warnings']);
+}
+
 // Add the Piwigo Official menu
   $template->assign( 'pwgmenu', pwg_URL() );
Index: /trunk/admin/include/functions.php
===================================================================
--- /trunk/admin/include/functions.php	(revision 8761)
+++ /trunk/admin/include/functions.php	(revision 8762)
@@ -362,4 +362,52 @@
 
   trigger_action('delete_user', $user_id);
+}
+
+/**
+ * Deletes all tags linked to no photo
+ */
+function delete_orphan_tags()
+{
+  $orphan_tags = get_orphan_tags();
+  
+  if (count($orphan_tags) > 0)
+  {
+    $orphan_tag_ids = array();
+    foreach ($orphan_tags as $tag)
+    {
+      array_push($orphan_tag_ids, $tag['id']);
+    }
+
+    $query = '
+DELETE
+  FROM '.TAGS_TABLE.'
+  WHERE id IN ('.implode(',', $orphan_tag_ids).')
+;';
+    pwg_query($query);
+  }
+}
+
+/**
+ * Get all tags (id + name) linked to no photo
+ */
+function get_orphan_tags()
+{
+  $orphan_tags = array();
+  
+  $query = '
+SELECT
+    id,
+    name
+  FROM '.TAGS_TABLE.'
+    LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = tag_id
+  WHERE tag_id IS NULL
+;';
+  $result = pwg_query($query);
+  while ($row = pwg_db_fetch_assoc($result))
+  {
+    array_push($orphan_tags, $row);
+  }
+
+  return $orphan_tags;
 }
 
Index: /trunk/admin/maintenance.php
===================================================================
--- /trunk/admin/maintenance.php	(revision 8761)
+++ /trunk/admin/maintenance.php	(revision 8762)
@@ -54,4 +54,9 @@
     update_path();
     update_average_rate();
+    break;
+  }
+  case 'delete_orphan_tags' :
+  {
+    delete_orphan_tags();
     break;
   }
@@ -134,4 +139,5 @@
     'U_MAINT_CATEGORIES' => $start_url.'categories',
     'U_MAINT_IMAGES' => $start_url.'images',
+    'U_MAINT_ORPHAN_TAGS' => $start_url.'delete_orphan_tags',
     'U_MAINT_HISTORY_DETAIL' => $start_url.'history_detail',
     'U_MAINT_HISTORY_SUMMARY' => $start_url.'history_summary',
@@ -140,5 +146,5 @@
     'U_MAINT_DATABASE' => $start_url.'database',
     'U_MAINT_C13Y' => $start_url.'c13y',
-	'U_MAINT_SEARCH' => $start_url.'search',
+    'U_MAINT_SEARCH' => $start_url.'search',
     'U_MAINT_COMPILED_TEMPLATES' => $start_url.'compiled-templates',
     'U_HELP' => get_root_url().'admin/popuphelp.php?page=maintenance',
Index: /trunk/admin/tags.php
===================================================================
--- /trunk/admin/tags.php	(revision 8761)
+++ /trunk/admin/tags.php	(revision 8762)
@@ -139,4 +139,17 @@
 
 // +-----------------------------------------------------------------------+
+// |                           delete orphan tags                          |
+// +-----------------------------------------------------------------------+
+
+if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
+{
+  check_pwg_token();
+  
+  delete_orphan_tags();
+  $_SESSION['page_infos'] = array(l10n('Orphan tags deleted'));
+  redirect(get_root_url().'admin.php?page=tags');
+}
+
+// +-----------------------------------------------------------------------+
 // |                               add a tag                               |
 // +-----------------------------------------------------------------------+
@@ -201,4 +214,29 @@
 
 // +-----------------------------------------------------------------------+
+// |                              orphan tags                              |
+// +-----------------------------------------------------------------------+
+
+$orphan_tags = get_orphan_tags();
+
+$orphan_tag_names = array();
+foreach ($orphan_tags as $tag)
+{
+  array_push($orphan_tag_names, $tag['name']);
+}
+
+if (count($orphan_tag_names) > 0)
+{
+  array_push(
+    $page['warnings'],
+    sprintf(
+      l10n('You have %d orphan tags: %s.').' <a href="%s">'.l10n('Delete orphan tags').'</a>',
+      count($orphan_tag_names),
+      implode(', ', $orphan_tag_names),
+      get_root_url().'admin.php?page=tags&amp;action=delete_orphans&amp;pwg_token='.get_pwg_token()
+      )
+    );
+}
+
+// +-----------------------------------------------------------------------+
 // |                             form creation                             |
 // +-----------------------------------------------------------------------+
Index: /trunk/admin/themes/default/template/admin.tpl
===================================================================
--- /trunk/admin/themes/default/template/admin.tpl	(revision 8761)
+++ /trunk/admin/themes/default/template/admin.tpl	(revision 8762)
@@ -127,4 +127,14 @@
   {/if}
 
+  {if isset($warnings)}
+  <div class="warnings">
+    <ul>
+      {foreach from=$warnings item=warning}
+      <li>{$warning}</li>
+      {/foreach}
+    </ul>
+  </div>
+  {/if}
+
   {$ADMIN_CONTENT}
 </div>
Index: /trunk/admin/themes/default/template/maintenance.tpl
===================================================================
--- /trunk/admin/themes/default/template/maintenance.tpl	(revision 8761)
+++ /trunk/admin/themes/default/template/maintenance.tpl	(revision 8762)
@@ -12,8 +12,6 @@
   <li><a href="{$U_MAINT_CATEGORIES}">{'Update albums informations'|@translate}</a></li>
   <li><a href="{$U_MAINT_IMAGES}">{'Update photos information'|@translate}</a></li>
+  <li><a href="{$U_MAINT_ORPHAN_TAGS}">{'Delete orphan tags'|@translate}</a></li>
   <li><a href="{$U_MAINT_DATABASE}">{'Repair and optimize database'|@translate}</a></li>
-</ul>
-
-<ul>
   <li><a href="{$U_MAINT_HISTORY_DETAIL}" onclick="return confirm('{'Purge history detail'|@translate}');">{'Purge history detail'|@translate}</a></li>
   <li><a href="{$U_MAINT_HISTORY_SUMMARY}" onclick="return confirm('{'Purge history summary'|@translate}');">{'Purge history summary'|@translate}</a></li>
@@ -22,7 +20,4 @@
   <li><a href="{$U_MAINT_SEARCH}"onclick="return confirm('{'Purge search history'|@translate}');">{'Purge search history'|@translate}</a></li>
   <li><a href="{$U_MAINT_COMPILED_TEMPLATES}">{'Purge compiled templates'|@translate}</a></li>
-</ul>
-
-<ul>
   <li><a href="{$U_MAINT_C13Y}">{'Reinitialize check integrity'|@translate}</a></li>
 </ul>
Index: /trunk/language/en_UK/admin.lang.php
===================================================================
--- /trunk/language/en_UK/admin.lang.php	(revision 8761)
+++ /trunk/language/en_UK/admin.lang.php	(revision 8762)
@@ -215,4 +215,5 @@
 $lang['default'] = "default";
 $lang['delete album'] = "delete album";
+$lang['Delete orphan tags'] = 'Delete orphan tags';
 $lang['Delete Representant'] = "Delete Representant";
 $lang['Delete selected photos'] = "Delete selected photos";
@@ -471,4 +472,5 @@
 $lang['Order of menubar items has been updated successfully.'] = 'Order of menubar items has been updated successfully.';
 $lang['Original templates'] = "Original templates";
+$lang['Orphan tags deleted'] = 'Orphan tags deleted';
 $lang['Other plugins'] = "Other plugins available";
 $lang['Other private albums'] = "Other private albums";
@@ -762,4 +764,5 @@
 $lang['You cannot delete your account'] = "You cannot delete your account";
 $lang['You cannot move an album in its own sub album'] = "You cannot move an album in its own sub album";
+$lang['You have %d orphan tags: %s.'] = 'You have %d orphan tags: %s.';
 $lang['You have subscribed to receiving notifications by mail.'] = "You have subscribed to receive notifications by mail.";
 $lang['You have unsubscribed from receiving notifications by mail.'] = "You have unsubscribed from being notified by mail.";
Index: /trunk/language/fr_FR/admin.lang.php
===================================================================
--- /trunk/language/fr_FR/admin.lang.php	(revision 8761)
+++ /trunk/language/fr_FR/admin.lang.php	(revision 8762)
@@ -560,4 +560,5 @@
 $lang['Replacers (customized templates)'] = "Remplaçants (templates modifiés)";
 $lang['Original templates'] = "Templates d'origine";
+$lang['Orphan tags deleted'] = 'Tags orphelins supprimés';
 $lang['Optional URL keyword'] = "Paramètre facultatif de l'URL";
 $lang['Templates configuration has been recorded.'] = "La configuration des templates a été enregistrée.";
@@ -782,3 +783,5 @@
 $lang['with no tag'] = 'sans tag';
 $lang['Week starts on'] = 'La semaine commence le';
+$lang['You have %d orphan tags: %s.'] = 'Vous avez %d tags orphelins: %s.';
+$lang['Delete orphan tags'] = 'Supprimer les tags orphelins';
 ?>
