add a "already exist" when thumbnails are created on the command line. without it...
[phpfspot.git] / phpfspot.class.php
index 15697ad78fd73df8bcdf325ac0f2de191318c2a0..841da503f46454bb9ac38c458172b9c13d123e44 100644 (file)
@@ -46,7 +46,7 @@ class PHPFSPOT {
 
       /* set application name and version information */
       $this->cfg->product = "phpfspot";
-      $this->cfg->version = "1.1";
+      $this->cfg->version = "1.2";
 
       /* Check necessary requirements */
       if(!$this->checkRequirements()) {
@@ -54,6 +54,10 @@ class PHPFSPOT {
       }
 
       $this->db  = new PHPFSPOT_DB($this, $this->cfg->fspot_db);
+      if(!is_writeable($this->cfg->fspot_db)) {
+         print $this->cfg->fspot_db ." is not writeable for user ". $this->getuid() ."\n";
+         exit(1);
+      }
       
       if(!is_writeable(dirname($this->cfg->phpfspot_db))) {
          print dirname($this->cfg->phpfspot_db) .": directory is not writeable!";
@@ -76,7 +80,8 @@ class PHPFSPOT {
       require_once "phpfspot_tmpl.php";
       $this->tmpl = new PHPFSPOT_TMPL($this);
 
-      $this->get_tags();
+      /* check if all necessary indices exist */
+      $this->checkDbIndices();
 
       session_start();
 
@@ -196,11 +201,33 @@ class PHPFSPOT {
       $this->avail_tags = Array();
       $count = 0;
    
-      $result = $this->db->db_query("
-         SELECT id,name
-         FROM tags
-         ORDER BY sort_priority ASC
-      ");
+      if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
+         $query_str="
+            SELECT
+               DISTINCT t1.id as id, t1.name as name
+            FROM  
+               photo_tags pt1
+            INNER JOIN photo_tags
+               pt2 ON pt1.photo_id=pt2.photo_id
+            INNER JOIN tags t1
+               ON t1.id=pt1.tag_id
+            INNER JOIN tags t2
+               ON t2.id=pt2.tag_id
+            WHERE
+               t2.name IN  ('".implode("','",$this->cfg->show_tags)."')
+            ORDER BY
+               t1.sort_priority ASC";
+
+         $result = $this->db->db_query($query_str);
+      }
+      else
+      {
+         $result = $this->db->db_query("
+            SELECT id,name
+            FROM tags
+            ORDER BY sort_priority ASC
+         ");
+      }
       
       while($row = $this->db->db_fetch_object($result)) {
 
@@ -214,13 +241,14 @@ class PHPFSPOT {
          if(in_array($row['name'], $this->cfg->hide_tags))
             continue;
 
-         /* if the user has specified to only show certain tags which
-            are specified in phpfspot's configuration, ignore all others
-            so they will not be added to the tag list.
-         */
+         /* if you include the following if-clause and the user has specified
+            to only show certain tags which are specified in phpfspot's
+            configuration, ignore all others so they will not be added to the
+            tag list.
          if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags) &&
             !in_array($row['name'], $this->cfg->show_tags))
             continue;
+         */
 
          $this->tags[$tag_id] = $tag_name; 
          $this->avail_tags[$count] = $tag_id;
@@ -253,13 +281,7 @@ class PHPFSPOT {
             INNER JOIN tags t
                ON pt.tag_id=t.id
             WHERE p.id='". $idx ."'
-            AND t.name IN (
-         ";
-         foreach($this->cfg->show_tags as $tag) {
-            $query_str.= "'". $tag ."',";
-         }
-         $query_str = substr($query_str, 0, strlen($query_str)-1);
-         $query_str.= ")";
+            AND t.name IN ('".implode("','",$this->cfg->show_tags)."')";
       }
       else {
          $query_str.= "
@@ -455,6 +477,8 @@ class PHPFSPOT {
     */
    public function getAvailableTags()
    {
+      $this->get_tags();
+
       $output = "";
 
       $result = $this->db->db_query("
@@ -524,6 +548,8 @@ class PHPFSPOT {
     */
    public function getSelectedTags()
    {
+      $this->get_tags();
+
       $output = "";
       foreach($this->avail_tags as $tag)
       {
@@ -654,23 +680,23 @@ class PHPFSPOT {
       /* return a search result */
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
          $query_str = "
-            SELECT DISTINCT photo_id
-               FROM photo_tags pt
+            SELECT DISTINCT pt1.photo_id
+               FROM photo_tags pt1
+            INNER JOIN photo_tags pt2
+               ON pt1.photo_id=pt2.photo_id
+            INNER JOIN tags t1
+               ON pt1.tag_id=t1.id
             INNER JOIN photos p
-               ON p.id=pt.photo_id
-            INNER JOIN tags t
-               ON pt.tag_id=t.id
-            WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'";
+               ON pt1.photo_id=p.id
+            INNER JOIN tags t2
+               ON pt2.tag_id=t2.id
+            WHERE t1.name LIKE '%". $_SESSION['searchfor'] ."%' ";
 
          if(isset($additional_where_cond))
             $query_str.= "AND ". $additional_where_cond ." ";
 
          if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
-            $query_str.= "AND t.name IN (";
-            foreach($this->cfg->show_tags as $tag) {
-               $query_str.= "'". $tag ."',";
-            }
-            $query_str = substr($query_str, 0, strlen($query_str)-1) . ")";
+            $query_str.= "AND t2.name IN ('".implode("','",$this->cfg->show_tags)."')";
          }
          
          if(isset($order_str))
@@ -690,30 +716,30 @@ class PHPFSPOT {
             $selected.= $tag .",";
          $selected = substr($selected, 0, strlen($selected)-1);
 
+         /* photo has to match at least on of the selected tags */
          if($_SESSION['tag_condition'] == 'or') {
             $query_str = "
-               SELECT DISTINCT pt.photo_id
-                  FROM photo_tags pt
-               INNER JOIN photos p
-                  ON p.id=pt.photo_id
+               SELECT DISTINCT pt1.photo_id
+                  FROM photo_tags pt1
+               INNER JOIN photo_tags pt2
+                  ON pt1.photo_id=pt2.photo_id
                INNER JOIN tags t
-                  ON pt.tag_id=t.id
-               WHERE pt.tag_id IN (". $selected .")
+                  ON pt2.tag_id=t.id
+               INNER JOIN photos p
+                  ON pt1.photo_id=p.id
+               WHERE pt1.tag_id IN (". $selected .")
             ";
             if(isset($additional_where_cond)) 
                $query_str.= "AND ". $additional_where_cond ." ";
 
             if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
-               $query_str.= "AND t.name IN (";
-               foreach($this->cfg->show_tags as $tag) {
-                  $query_str.= "'". $tag ."',";
-               }
-               $query_str = substr($query_str, 0, strlen($query_str)-1) . ")";
+               $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags)."')";
             }
 
             if(isset($order_str))
                $query_str.= $order_str;
          }
+         /* photo has to match all selected tags */
          elseif($_SESSION['tag_condition'] == 'and') {
 
             if(count($_SESSION['selected_tags']) >= 32) {
@@ -753,21 +779,17 @@ class PHPFSPOT {
                INNER JOIN photos p
                   ON pt1.photo_id=p.id
             ";
-            $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
+            $query_str.= "WHERE pt2.tag_id=". $_SESSION['selected_tags'][0]." ";
             for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
                $query_str.= "
-                  AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
+                  AND pt". ($i+2) .".tag_id=". $_SESSION['selected_tags'][$i] ."
                "; 
             }
             if(isset($additional_where_cond)) 
                $query_str.= "AND ". $additional_where_cond;
 
             if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
-               $query_str.= "AND t.name IN (";
-               foreach($this->cfg->show_tags as $tag) {
-                  $query_str.= "'". $tag ."',";
-               }
-               $query_str = substr($query_str, 0, strlen($query_str)-1) . ")";
+               $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
             }
 
             if(isset($order_str))
@@ -795,11 +817,7 @@ class PHPFSPOT {
          $query_str.= "WHERE ". $additional_where_cond ." ";
 
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
-         $query_str.= "AND t.name IN (";
-         foreach($this->cfg->show_tags as $tag) {
-            $query_str.= "'". $tag ."',";
-         }
-         $query_str = substr($query_str, 0, strlen($query_str)-1) . ")";
+         $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
       }
  
       if(isset($order_str))
@@ -1251,6 +1269,8 @@ class PHPFSPOT {
 
       $this->_debug("Image [". $idx ."] ". $this->shrink_text($details['name'], 20) ." Thumbnails:");
 
+      $changes = false;
+
       foreach($resolutions as $resolution) {
 
          $thumb_sub_path = substr($file_md5, 0, 2);
@@ -1266,6 +1286,8 @@ class PHPFSPOT {
             $this->_debug(" ". $resolution ."px");
             if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
                $error = 1;
+
+            $changes = true;
          }
          /* if the file hasn't changed there is no need to regen the thumb */
          elseif($file_md5 != $this->getMD5($idx) || $force) {
@@ -1274,9 +1296,14 @@ class PHPFSPOT {
             if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
                $error = 1;
 
+            $changes = true;
          }
       }
 
+      if(!$changes) {
+         $this->_debug(" already exist");
+      }
+
       /* set the new/changed MD5 sum for the current photo */
       if(!$error) {
          $this->setMD5($idx, $file_md5);
@@ -1343,6 +1370,8 @@ class PHPFSPOT {
     */
    public function startSearch($searchfor, $sort_order, $from = 0, $to = 0)
    {
+      $this->get_tags();
+
       $_SESSION['searchfor'] = $searchfor;
       $_SESSION['sort_order'] = $sort_order;
       if($from != 0)
@@ -1997,14 +2026,15 @@ class PHPFSPOT {
     */
    public function get_thumb_path($width, $photo)
    {
-      $sub_path = substr($this->getMD5($photo), 0, 2);
+      $md5 = $this->getMD5($photo);
+      $sub_path = substr($md5, 0, 2);
       return $this->cfg->thumb_path
          . "/"
          . $sub_path
          . "/"
          . $width
          . "_"
-         . $this->getMD5($photo);
+         . $md5;
 
    } // get_thumb_path()
 
@@ -2061,6 +2091,26 @@ class PHPFSPOT {
 
    } // check_readable()
 
+   /**
+    * check if all needed indices are present
+    *
+    * this function checks, if some needed indices are already
+    * present, or if not, create them on the fly. they are
+    * necessary to speed up some queries like that one look for
+    * all tags, when show_tags is specified in the configuration.
+    */
+   private function checkDbIndices()
+   {
+      $result = $this->db->db_exec("
+         CREATE INDEX IF NOT EXISTS
+            phototag
+         ON
+            photo_tags
+               (photo_id, tag_id)
+      ");
+
+   } // checkDbIndices()
+
 }
 
 ?>