Gallery Most Recent Comments
These files add two functionalities to
Gallery version 1.4.3-pl2:
- list the most recently added comments (eg. http://gallery.complexfission.com/gallery/recent_comments.php)
- list photos sorted by the number of comments (eg. http://gallery.complexfission.com/gallery/most_comments.php)
This package may work for Gallery versions earlier than 1.4.3, for example, 1.4.2, but I have not tested it. It definitely does not work for 1.3.4.
Files
Let
$GALLERY be the directory where Gallyer is installed. Let
$ALBUMS be the directory where Gallery keeps all its photos and data files. In my case, these are:
$GALLERY = /web/complexfission/gallery
$ALBUMS = /web/complexfission/albums
This extension to Gallery consists of 5 new files in the directory
$GALLERY, one modification to an existing Gallery file, and one dynamically generated data file in $ALBUMS:
scan_comments.php: This script does a recursive scan through all the albums in the gallery and collects the most recently added comment for each photo. It saves the scan results in the file
recent_photos.dat.
recent_comments.php: This script reads in the data in
recent_photos.dat and displays the photos sorted by descending order in time of the most recently added comment.
most_comments.php: This file is
identical to
recent_comments.php file. When invoked as
most_comments.php, it prints out the photos in decreasing order of the
total number of comments attached to the photo. I suppose I could have used GET parameters to pass the information directly to the script. I reason I do it this way is because I can't quite figure out Gallery's convention for passing arguments to its scripts, either through the GET parameter, or through session variables.
RecentPhotos.php: This is a PHP class that is used by
recent_comments.php and
most_comments.php to parse the
recent_photos.dat file. It supports only
read operations, but I have thought of adding write operations to unify the interface to the
recent_photos.dat file.
albums.php: This is a file provided by Gallery. A small addition adds a hyperlink to
recent_comments.php. I did not include hyperlink to
scan_comments.php on this page, because I don't want web robots to run this script.
recent_photos.dat: This file is created by
scan_comments.php using the PHP
serialize() function. It is in text form, but not very readable.
Installation and Usage
Download the files attached to this page (appearing below and above), and copy them into the
$GALLERY directory of your Gallery installation. Rename the
.txt extension to the original
.php extension. (TWiki does not allow
.php files to be uploaded because it would create a security hole.)
Point your browser to
http://gallery.complexfission.com/gallery/scan_comments.php, where you replace "gallery.complexfission.com" with the URL of your Gallery site. It should produce a page containing the list of all albums on your site. You do not have to logged in as administrator or anyone else to do this. I don't think this is a security problem, but let me know if you think it is.
Point your browser to
http://gallery.complexfission.com/gallery/recent_comments.php. You should see a list of photos with comments, sorted by the date of the most recent comments.
Point your browser to
http://gallery.complexfission.com/gallery/most_comments.php. You should see a list of photos with comments, sorted by the number of comments added to the photo.
To scan for comments automatically, on a Linux box, create a
cron job that loads the
scan_comments.php script periodically. You can create the cron job using any user; it does not have to be
root. For example, the following
3 1 * * * /usr/bin/wget -O /dev/null -o /dev/null http://gallery.complexfission.com/gallery/scan_comments.php
will run the script
scan_comments.php at 1:03 am every night, dumping the output into
/dev/null so that an email won't be generated.
How It Works
A number of changes to Gallery occurred between version 1.3.4 and 1.4.3 which caused this script to stop working.
GallerySourceCode has some notes to help me remember these for the next time I have to modify these files. This section contains some notes about how
scan_comments.php and these other scripts work.
scan_comments.php, scan_utils.php
The main work is done in the function
scanAlbums() which has been moved to
scan_utils.php. It collects the list of all top level albums using the Gallery object
AlbumDB. Note that Gallery has mutliple root albums.
AlbumDB provides an interface method
getAlbum() which takes the currently logged in user (
$gallery->user) as an argument. Presumeable it returns only those albums that the user is allowed to see. For each top-level album, it calls the function
scanComments() with the Album object as argument.
The function
scanComments() is responsible for building an array that contains information about photos and their comments. It is called recursively for each sub-albums. An Album object provides methods for accessing information about the album, in particular, the list of photos within the album. There seems to be a round-about method for accessing photos, as explained next.
-
$album->getAlbumName($i) returns the name of the album at position $i if it is a sub-album, but returns FALSE if the element at position $i is not an album. The function scanComments() is called recursively for sub-albums.
-
$album->getPhotos(1) gets the number of photos in the album. (I don't remember what the "1" argument is for.)
-
$album->getPhotoId($i) takes an integer argument from 1 to N, where N is the number of photos in the album, and it returns a $photoId (perhaps $photoId is not numeric).
-
$album->getPhotoIndex($photoId) returns the $index of the particular photo which is used in other methods to retrieve information about the photo.
Once you have the photo
$index, you can use the following methods to get further.
-
$album->getComment($index, $i) retrieves the i'th comment. The method returns a Comment object which has methods for accessing its properties.
-
$album->getThumbnailTag($index) returns the URL of the thumbnail image of the photo which is identified by $index (above).
After getting back a valid Comment object, the following methods are used to access information about the comment:
-
$comment->getName() returns the author of the comment.
-
$comment->datePosted is not a method, but an object variable.
-
$comment->getCommentText() returns the comment text.
The global function
getNextPhoto($i, $album) (in
util.php) returns the next photo in the given album.
For each photo that has an attached comment, the
scanComments() function builds a 2-D array (
$commentArray)
whose element is a hash map (
$commentInfo):
$commentArray[$albumName][$photoId] = $commentInfo;
$commentInfo = array (
'time' => $timeOfMostRecentComment,
'url' => $photoURL,
'thumb' => $thumbNailTag,
'numComments' => $numComments,
'author' => $author,
'text' => $text,
);
There is only one entry per photo. I don't remember why I used a 2D array to build this. I might have been worried
about duplicate entries.
This 2-D array is flattened by
scanComments() into a 1-D array of hash maps:
$recentPhotos[] = array(
'time' => $commentInfo[ 'time' ],
'albumName' => $albumName,
'photoId' => $photoId,
'photoURL' => $commentInfo[ 'url' ],
'photoThumb' => $commentInfo[ 'thumb' ],
'numComments' => $commentInfo[ 'numComments' ],
'author' => $commentInfo[ 'author' ],
'text' => $commentInfo[ 'text' ],
);
It is this array that is serialized into the file
recent_photos.dat by the function
saveRecentPhotos().
RecentPhoto class
The
RecentPhoto.php file defines the
RecentPhoto class which provides read access to the array
$recentPhotos. It also provides a method for reading the serialized data in the file
recent_photos.dat into memory. Here are some methods:
-
RecentPhoto($fileName) - contructor takes the file name of the serialized data, in this case, recent_photos.dat
-
load() - unserialize the data from $fileName
-
sortByTime() - sort the 1D array by time of stamp of the comment
-
sortByNumComments() - sort the 1D array by the number of comments in the photo
-
getPhotos() - return the 1D array (sorted by time or number of comments)
-
getNumPhotos() - return the total number of photos
-
getScanTime() - return the time stamp of the recent_photos.dat file, which is the last time the scan was done
The two auxillary functions
compareByNumComments() and
compareByTime() is used by the two sorting methods to provide the correct comparision operator to the PHP
usort() function.
albums.php
Only a small addition was made to the 1.4.3-pl2 version of
albums.php. Here is the context diff:
> diff -u /web/gallery/gallery-1.4.3-pl2/albums.php albums.php
--- /web/gallery/gallery-1.4.3-pl2/albums.php Sun Apr 11 16:01:58 2004
+++ albums.php Fri Jun 18 09:14:43 2004
@@ -180,6 +180,10 @@
}
}
+// Add link to recent_comments.php
+//
+$adminCommands .= "<a href=\"recent_comments.php\">[recent comments]</a>\n";
+
if ($gallery->user->isLoggedIn() && !$gallery->session->offline) {
if ($gallery->userDB->canModifyUser()) {
$adminCommands .= popup_link("[". _("preferences") ."]",
Known Bugs and Potential Problems
An earlier version I created for Gallery 1.3.2 stopped working when I upgraded to 1.4.2. This version has been tested and working on 1.4.3-pl2. There is no guarantee that a future version of Gallery won't break it again. I have a weak understanding of Gallery's internal PHP code, so I used lots of trial-and-error.
I am particularly concerned about security holes, but Gallery has gotten more difficult to read with each successive revision, and I don't have time to go through their code.
I definitely do not understand their HTML rendering archicture, so these scripts may break when used with certain skins and layout options.
I do not run Gallery in embedded mode (eg. PostNuke) so I don't know if these scripts will work under that kind of environment.
Permissions
This program is Copyright (c) 2004 Brian Park. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License.
--
BrianPark - 17 Jun 2004