calorie
Active member
Well, now that I've finally had a chance to debug this thing, after having to be told that I was missing the <?php on a file (still laughing about that one), I bring to you lightboxed attachments on the forum homepage with no template edits.
What it does is it queries for attachments, selects up to twelve of those files, and forms a thumbnail strip for display over the forum list on the forum homepage. The thumbnail strip shows to all visitors, and permissions should be taken into account for larger images that are called when thumbnails are clicked. I say should as this is still new to me and if it barfs, well, I said should, not will, lol.
This add-on adds four queries to the forum homepage: two queries for two templates, and two queries to get something random-like but is not an 'order by rand' clause. As you might know, 'order by rand' is just not all that optimal, especially on large tables.
My first thought was to use the PHP glob function to traverse the attachments directory, but it traverses the whole directory, which after several hundred attachments, would likely produce a noticeable slowdown. Next I thought about using PHP's opendir, readdir, and closedir functions, with some code to 'seek' in order to avoid traversing the whole directory.
Well, there isn't yet a PHP seekdir function, or at least I couldn't find one that'd work, so I then thought about using the PHP shell_exec function with a shell script, but then, oh, the incompatibility issues and so forth. So that lead me to what you see in the code today. There is no 'order by rand' and, although I haven't checked, I doubt you'll find 'order by rand' in the XF code.
Anyway, four good queries in and of themselves are not necessarily a big deal, as most should be optimized in some way. The two queries for the two templates are done with XF's $template->create and of the other two queries, the first one fetches count, min, and max, which might be better handled using a query on a now non-existent summary table handled via a cron job.
Right now though the attachment table is queried for count, min, and max. If it turns out to cause a noticeable slowdown, then I can look into whether a summary table would be a better way to handle it. So that leaves the last of the four queries, which makes use of attachment ID 'between X and Y' so it is keyed, where X and Y are determined using some math,
The downsize to the math is that if there is a large enough gap in the attachment ID sequence, you won't necessarily see Z thumbnails every time the page loads. Alas, the point of this blurb I guess is to talk about what a PITA it is to get something random without it being a total resource hog on bigger boards. So if you have anything to contribute, please feel free to share if you want.
Personally I'm okay with getting less than Z images in the thumbnail strip on occasion if it means that a full table scan is avoided, so at this point, the other improvement that I'd make would be to have a cron job update a summary table and then query said table for count, min, and max to avoid potential slowdowns there.
So how do you handle it when you want something random?
BTW, enjoy the add-on.
Edit: Seems I inadvertently wiped out the hover shadow on the thumbnails, oops, so to get it back, in the XML file find <blockquote class="messageText"> and replace with <blockquote class="messageText ugc"> and then import. Also seems like one template query could be removed, but it is not yet clear if the exchange of query for code is worth it, but will try to keep you posted if anything changes. Right now though, sleep is required.
What it does is it queries for attachments, selects up to twelve of those files, and forms a thumbnail strip for display over the forum list on the forum homepage. The thumbnail strip shows to all visitors, and permissions should be taken into account for larger images that are called when thumbnails are clicked. I say should as this is still new to me and if it barfs, well, I said should, not will, lol.
This add-on adds four queries to the forum homepage: two queries for two templates, and two queries to get something random-like but is not an 'order by rand' clause. As you might know, 'order by rand' is just not all that optimal, especially on large tables.
My first thought was to use the PHP glob function to traverse the attachments directory, but it traverses the whole directory, which after several hundred attachments, would likely produce a noticeable slowdown. Next I thought about using PHP's opendir, readdir, and closedir functions, with some code to 'seek' in order to avoid traversing the whole directory.
Well, there isn't yet a PHP seekdir function, or at least I couldn't find one that'd work, so I then thought about using the PHP shell_exec function with a shell script, but then, oh, the incompatibility issues and so forth. So that lead me to what you see in the code today. There is no 'order by rand' and, although I haven't checked, I doubt you'll find 'order by rand' in the XF code.
Anyway, four good queries in and of themselves are not necessarily a big deal, as most should be optimized in some way. The two queries for the two templates are done with XF's $template->create and of the other two queries, the first one fetches count, min, and max, which might be better handled using a query on a now non-existent summary table handled via a cron job.
Right now though the attachment table is queried for count, min, and max. If it turns out to cause a noticeable slowdown, then I can look into whether a summary table would be a better way to handle it. So that leaves the last of the four queries, which makes use of attachment ID 'between X and Y' so it is keyed, where X and Y are determined using some math,
The downsize to the math is that if there is a large enough gap in the attachment ID sequence, you won't necessarily see Z thumbnails every time the page loads. Alas, the point of this blurb I guess is to talk about what a PITA it is to get something random without it being a total resource hog on bigger boards. So if you have anything to contribute, please feel free to share if you want.
Personally I'm okay with getting less than Z images in the thumbnail strip on occasion if it means that a full table scan is avoided, so at this point, the other improvement that I'd make would be to have a cron job update a summary table and then query said table for count, min, and max to avoid potential slowdowns there.
So how do you handle it when you want something random?
BTW, enjoy the add-on.
Edit: Seems I inadvertently wiped out the hover shadow on the thumbnails, oops, so to get it back, in the XML file find <blockquote class="messageText"> and replace with <blockquote class="messageText ugc"> and then import. Also seems like one template query could be removed, but it is not yet clear if the exchange of query for code is worth it, but will try to keep you posted if anything changes. Right now though, sleep is required.