[pLog-svn] r6459 - plog/trunk/class/gallery/dao

mark at devel.lifetype.net mark at devel.lifetype.net
Wed May 14 03:13:25 EDT 2008


Author: mark
Date: 2008-05-14 03:13:25 -0400 (Wed, 14 May 2008)
New Revision: 6459

Modified:
   plog/trunk/class/gallery/dao/galleryresources.class.php
Log:
1. Add order arguments to getUserResources, for backward compatibility, I have put it in last argument.

2. Implement getUserResourcesFromQuery( $query ) for developer get to get resource with thier own sql easily.

3.Fixed addResourceFromDisk(). No one complains about this, it is really weird. That means no one use moblog and xmlrpc. 

Without this fix, the moblog and xmlrpc won't get the right image size and right album counter.

4. Re-implement deleteUserResources, the new way has better performance when delete large amounts of resurces.

Modified: plog/trunk/class/gallery/dao/galleryresources.class.php
===================================================================
--- plog/trunk/class/gallery/dao/galleryresources.class.php	2008-05-14 07:04:04 UTC (rev 6458)
+++ plog/trunk/class/gallery/dao/galleryresources.class.php	2008-05-14 07:13:25 UTC (rev 6459)
@@ -12,7 +12,8 @@
 	 * via the getID3 library.
 	 */
 
-	
+	define( "RESOURCES_OLDEST_FIRST_ORDER", 0 );
+	define( "RESOURCES_NEWEST_FIRST_ORDER", 1 );	
     
     
 	
@@ -194,7 +195,8 @@
 								   $searchTerms = "",
 								   $locationId = -1,
                                    $page = DEFAULT_PAGING_ENABLED, 
-                                   $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
+                                   $itemsPerPage = DEFAULT_ITEMS_PER_PAGE,
+                                   $order = RESOURCES_OLDEST_FIRST_ORDER )
         {
 
             $resources = Array();
@@ -214,7 +216,12 @@
             $query = trim( $query );
             if( substr( $query, -3, 3 ) == "AND" )
                 $query = substr( $query, 0, strlen( $query ) - 3 );
-            
+
+			if( $order == RESOURCES_NEWEST_FIRST_ORDER )
+				$query .= "ORDER BY date DESC";
+			else
+            	$query .= "ORDER BY date ASC";
+
             $result = $this->Execute( $query, $page, $itemsPerPage );
             if( !$result )
                 return $resources;
@@ -226,6 +233,37 @@
             
             return $resources;
         }
+
+        /**
+         * @private
+         */
+        function _getUserResourcesFromQuery( $query )
+        {
+            // we send the query and then fetch the first array with the result
+            $result = $this->Execute( $query );
+
+            if( $result == false )
+                return false;
+
+            if ( $result->RecordCount() == 0){
+                $result->Close();
+                return false;
+            }
+
+			$resources = Array();
+            while( $row = $result->FetchRow()) {
+				// map the row to an object
+				$resource = $this->mapRow( $row );
+				$resources[] = $resource;
+				// and cache it for later use, we might need it
+	        	$this->_cache->setData( $resource->getId(), DaoCacheConstants::CACHE_RESOURCES, $resource );
+				$this->_cache->setData( $resource->getFileName(), DaoCacheConstants::CACHE_RESOURCES_BY_NAME, $resource );
+            }
+			            
+            $result->Close();  
+            
+            return $resources;
+        }
         
         /**
          * @private
@@ -642,22 +680,23 @@
                         
             $getId3 = new GetID3();
             $metadata = $getId3->analyze( $fullFilePath );
+
             // nifty helper method from the getid3 package
             getid3_lib::CopyTagsToComments($metadata);                      
     
             $resourceType = $this->_getResourceType( $fullFilePath, $metadata );
-            $info = $this->_filterMetadata( $metadata, $resourceType );            		
 			    
             // set the flags
             $flags = 0;
             if( $resourceType == GalleryConstants::GALLERY_RESOURCE_IMAGE )
                 $flags = $flags|GalleryConstants::GALLERY_RESOURCE_PREVIEW_AVAILABLE;
     
+            $info = $this->_filterMetadata( $metadata, $resourceType );  
+      		
             // add the record to the database
             $fileName = basename( $fullFilePath );
 			$duplicated = $this->isDuplicatedFilename( $fileName );
             $filePath = "";
-        
 			$resourceId = $this->addResourceToDatabase( $ownerId, $albumId, $description, $flags, $resourceType, $filePath, $fileName, $info );
             if( !$resourceId )
                 return false;
@@ -676,36 +715,61 @@
             // move the file to disk
 		                
             $storage = new GalleryResourceStorage();
-            $resFile = $storage->storeFile( $resourceId, 
-			                                $ownerId, 
-											$fullFilePath,
-                                            RESOURCE_STORAGE_STORE_MOVE );
+            $resFile = $storage->storeFile( $resourceId, $ownerId, $fullFilePath, RESOURCE_STORAGE_STORE_MOVE );
             
             // if the file cannot be read, we will also remove the record from the
             // database so that we don't screw up
             $fileReadable = File::isReadable( $resFile );
+			
             if( !$resFile || $resFile < 0 || !$fileReadable ) {
                 // if something went wrong, we should not keep the record in the db
-                $query = "DELETE FROM ".$this->getPrefix()."gallery_resources
-                          WHERE id = $resourceId";
-    
+                $query = "DELETE FROM ".$this->getPrefix()."gallery_resources WHERE id = $resourceId";
                 $this->Execute( $query );
-                
                 return $resFile;
             }
     
+						
+			$albums = new GalleryAlbums();
+			$album = $albums->getAlbum( $albumId );
+			$album->setNumResources( $album->getNumResources() + 1 );
+			$albums->updateAlbum( $album );			
+			
             // and finally, we can generate the thumbnail only if the file is an image, of course :)
             if( $resourceType == GalleryConstants::GALLERY_RESOURCE_IMAGE ) {
             	            
-                GalleryThumbnailGenerator::generateResourceThumbnail( $resFile, $resourceId, $ownerId );
-				GalleryThumbnailGenerator::generateResourceMediumSizeThumbnail( $resFile, $resourceId, $ownerId );
-				// call this method only if the settings are right
+
 				
 				$config =& Config::getConfig();
-				$previewHeight = $config->getValue( "final_size_thumbnail_height", 0 );
-				$previewWidth  = $config->getValue( "final_size_thumbnail_width", 0 );				
-				if( $previewHeight != 0 && $previewWidth != 0 ) {
-					GalleryThumbnailGenerator::generateResourceFinalSizeThumbnail( $resFile, $resourceId, $ownerId );
+            	
+            	$imgWidth = $info["video"]["resolution_x"];
+            	$imgHeight = $info["video"]["resolution_y"];
+	
+				$previewHeight = $config->getValue( "thumbnail_height", GalleryConstants::GALLERY_DEFAULT_THUMBNAIL_HEIGHT );
+				$previewWidth  = $config->getValue( "thumbnail_width", GalleryConstants::GALLERY_DEFAULT_THUMBNAIL_WIDTH );
+				$thumbHeight = ( $imgHeight > $previewHeight ? $previewHeight : $imgHeight );
+				$thumbWidth = ( $imgWidth > $previewWidth ? $previewWidth : $imgWidth );
+                GalleryThumbnailGenerator::generateResourceThumbnail( $resFile, $resourceId, $ownerId, $thumbHeight, $thumbWidth );                
+
+				$medPreviewHeight = $config->getValue( "medium_size_thumbnail_height", GalleryConstants::GALLERY_DEFAULT_MEDIUM_SIZE_THUMBNAIL_HEIGHT );
+				$medPreviewWidth  = $config->getValue( "medium_size_thumbnail_width", GalleryConstants::GALLERY_DEFAULT_MEDIUM_SIZE_THUMBNAIL_WIDTH );
+				$thumbHeight = ( $imgHeight > $medPreviewHeight ? $medPreviewHeight : $imgHeight );
+				$thumbWidth = ( $imgWidth > $medPreviewWidth ? $medPreviewWidth : $imgWidth );				
+				GalleryThumbnailGenerator::generateResourceMediumSizeThumbnail( $resFile, $resourceId, $ownerId, $thumbHeight, $thumbWidth );
+
+				// call this method only if the settings are right and the image is bigger than the final size(s)
+				$finalPreviewHeight = $config->getValue( "final_size_thumbnail_height", 0 );
+				$finalPreviewWidth  = $config->getValue( "final_size_thumbnail_width", 0 );
+				
+				if( $finalPreviewHeight > 0 )
+					if( $imgHeight < $finalPreviewHeight )
+						$finalPreviewHeight = $imgHeight;
+						
+				if( $finalPreviewWidth > 0 )
+					if( $imgWidth < $finalPreviewWidth )
+						$finalPreviewWidth = $imgWidth;
+				
+				if( $finalPreviewHeight != 0 && $finalPreviewWidth != 0 ) {
+					GalleryThumbnailGenerator::generateResourceFinalSizeThumbnail( $resFile, $resourceId, $ownerId, $finalPreviewHeight, $finalPreviewWidth );
 					// we have to recalculate the metadata because the image could be different... This is a bit cumbersome
 					// and repeats code. We know, thanks.
 					$getId3 = new GetID3();
@@ -817,10 +881,12 @@
 			
 			$albums = new GalleryAlbums();
 			$album = $resource->getAlbum();
-			$album->setNumResources( $album->getNumResources() - 1 );
-			$albums->updateAlbum( $album );		
+			if( $album ) {
+				$album->setNumResources( $album->getNumResources() - 1 );
+				$albums->updateAlbum( $album );		
+			}
+	
 	        // proceed and remove the file from disk
-			
         	$storage = new GalleryResourceStorage();
             return $storage->remove( $resource );            	
         }
@@ -837,13 +903,21 @@
                                    					  GalleryConstants::GALLERY_RESOURCE_ANY,
 								   					  "",
                                    					  -1);
+            
+            if( $this->delete( "owner_id", $ownerId ) )
+            {
+            	// remove resources belong to the owner one by one
+	            foreach( $userResources as $resource ) {
+			        $this->_cache->removeData( $resource->getId(), DaoCacheConstants::CACHE_RESOURCES );
+					$this->_cache->removeData( $resource->getOwnerId(), DaoCacheConstants::CACHE_RESOURCES_USER );
+					$this->_cache->removeData( $resource->getFileName(), DaoCacheConstants::CACHE_RESOURCES_BY_NAME );
+	            }
+	            $storage = new GalleryResourceStorage();
+	            $storage->removeOwnerFolder( $ownerId );
 
-            // remove resources belong to the owner one by one
-            foreach( $userResources as $resource ) {
-                $this->deleteResource( $resource->getId(), $resource->getOwnerId() );
-            }
-
-            return true;
+	            return true;
+	        } else
+	        	return false;
         }
 
 		/**



More information about the pLog-svn mailing list