summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2009-02-26 23:38:17 +0000
committerTim Almdal <tnalmdal@shaw.ca>2009-02-26 23:38:17 +0000
commitd097b4fc027ff558bf5f580298c7bed3696dcd3d (patch)
treeaf67745f12cdb7ebeeff9b615fbf8ec3f5cd6881
parent960c4248deb43eb824dda14670ea1f759a709013 (diff)
Update to image_block based on bharat's feedback
1) move the rand_key column into core 2) don't do a max rand, just try to a get a random number less than the current random number if that doesn't successd look the other way
-rw-r--r--core/helpers/album.php1
-rw-r--r--core/helpers/core_installer.php6
-rw-r--r--core/helpers/movie.php1
-rw-r--r--core/helpers/photo.php1
-rw-r--r--installer/install.sql6
-rw-r--r--modules/image_block/helpers/image_block_event.php25
-rw-r--r--modules/image_block/helpers/image_block_installer.php6
-rw-r--r--modules/image_block/helpers/image_block_theme.php18
8 files changed, 17 insertions, 47 deletions
diff --git a/core/helpers/album.php b/core/helpers/album.php
index 620d5f14..c75095be 100644
--- a/core/helpers/album.php
+++ b/core/helpers/album.php
@@ -45,6 +45,7 @@ class album_Core {
$album->owner_id = $owner_id;
$album->thumb_dirty = 1;
$album->resize_dirty = 1;
+ $album->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
while (ORM::factory("item")
->where("parent_id", $parent->id)
diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php
index 68b03a30..bf83c339 100644
--- a/core/helpers/core_installer.php
+++ b/core/helpers/core_installer.php
@@ -77,9 +77,11 @@ class core_installer {
`updated` int(9) default NULL,
`view_count` int(9) default 0,
`width` int(9) default NULL,
+ `rand_key` float default NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`),
- KEY `type` (`type`))
+ KEY `type` (`type`),
+ KEY `random` (`rand_key` DESC))
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE `logs` (
@@ -119,7 +121,7 @@ class core_installer {
PRIMARY KEY (`id`),
UNIQUE KEY(`name`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
-
+
$db->query("CREATE TABLE `permissions` (
`id` int(9) NOT NULL auto_increment,
`name` varchar(64) default NULL,
diff --git a/core/helpers/movie.php b/core/helpers/movie.php
index 22a1c62d..b964e383 100644
--- a/core/helpers/movie.php
+++ b/core/helpers/movie.php
@@ -63,6 +63,7 @@ class movie_Core {
$movie->mime_type = strtolower($pi["extension"]) == "mp4" ? "video/mp4" : "video/x-flv";
$movie->thumb_dirty = 1;
$movie->resize_dirty = 1;
+ $movie->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
// Randomize the name if there's a conflict
while (ORM::Factory("item")
diff --git a/core/helpers/photo.php b/core/helpers/photo.php
index 423c1973..c98af2f9 100644
--- a/core/helpers/photo.php
+++ b/core/helpers/photo.php
@@ -63,6 +63,7 @@ class photo_Core {
$photo->mime_type = empty($image_info['mime']) ? "application/unknown" : $image_info['mime'];
$photo->thumb_dirty = 1;
$photo->resize_dirty = 1;
+ $photo->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
// Randomize the name if there's a conflict
while (ORM::Factory("item")
diff --git a/installer/install.sql b/installer/install.sql
index 06f2bede..72416653 100644
--- a/installer/install.sql
+++ b/installer/install.sql
@@ -138,12 +138,14 @@ CREATE TABLE `items` (
`width` int(9) default NULL,
`view_1` tinyint(2) NOT NULL default '0',
`view_2` tinyint(2) NOT NULL default '0',
+ `rand_key` float default NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`),
- KEY `type` (`type`)
+ KEY `type` (`type`),
+ KEY `random` (`rand_key` DESC)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;
-INSERT INTO `items` VALUES (NULL,1234232381,'Welcome to your Gallery3',NULL,1,1,1,NULL,NULL,2,0,NULL,NULL,1,2,NULL,NULL,1,'Gallery','album',1234232381,0,NULL,1,1);
+INSERT INTO `items` VALUES (NULL,1234232381,'Welcome to your Gallery3',NULL,1,1,1,NULL,NULL,2,0,NULL,NULL,1,2,NULL,NULL,1,'Gallery','album',1234232381,0,NULL,1,1,NULL);
DROP TABLE IF EXISTS `items_tags`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
diff --git a/modules/image_block/helpers/image_block_event.php b/modules/image_block/helpers/image_block_event.php
deleted file mode 100644
index 0783773b..00000000
--- a/modules/image_block/helpers/image_block_event.php
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php defined("SYSPATH") or die("No direct script access.");
-/**
- * Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2008 Bharat Mediratta
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
- */
-class image_block_event_Core {
- static function item_created($item) {
- $db = Database::instance();
- $db->query("UPDATE `items` SET `rand_key` = RAND() WHERE id = {$item->id}");
- }
-}
diff --git a/modules/image_block/helpers/image_block_installer.php b/modules/image_block/helpers/image_block_installer.php
index 6b0ec20b..386ad2f5 100644
--- a/modules/image_block/helpers/image_block_installer.php
+++ b/modules/image_block/helpers/image_block_installer.php
@@ -24,18 +24,12 @@ class image_block_installer {
if ($version == 0) {
$db = Database::instance();
- $db->query("ALTER TABLE `items` ADD `rand_key` FLOAT DEFAULT NULL");
- $db->query("UPDATE `items` SET `rand_key` = RAND()");
- $db->query("CREATE INDEX `random_index` ON `items` (rand_key DESC)");
-
module::set_version("image_block", 1);
}
}
static function uninstall() {
$db = Database::instance();
- // Dropping the column should drop the index as well.
- $db->query("ALTER TABLE `items` DROP `rand_key`");
module::delete("image_block");
}
diff --git a/modules/image_block/helpers/image_block_theme.php b/modules/image_block/helpers/image_block_theme.php
index 0a103add..96ee58e2 100644
--- a/modules/image_block/helpers/image_block_theme.php
+++ b/modules/image_block/helpers/image_block_theme.php
@@ -19,11 +19,12 @@
*/
class image_block_theme_Core {
static function sidebar_blocks($theme) {
- $result = ORM::factory("item")
+ // Leave as a count so we can filter based on viewable items.
+ $viewable_items = ORM::factory("item")
->viewable()
->select("COUNT(*) AS C")
- ->find();
- if (empty($result->C)) {
+ ->find()->C;
+ if (empty($viewable_items)) {
return "";
}
@@ -32,18 +33,12 @@ class image_block_theme_Core {
$block->title = t("Random Image");
$block->content = new View("image_block_block.html");
- $result = ORM::factory("item")
- ->viewable()
- ->select("MAX(rand_key) AS max_random")
- ->find();
-
- $max_rand = $result->max_random;
$random = ((float)mt_rand()) / (float)mt_getrandmax();
$items = ORM::factory("item")
->viewable()
->where("type !=", "album")
- ->where("rand_key < ", $max_rand * $random)
+ ->where("rand_key < ", $random)
->orderby(array("rand_key" => "DESC"))
->find_all(1);
@@ -51,10 +46,9 @@ class image_block_theme_Core {
$items = ORM::factory("item")
->viewable()
->where("type !=", "album")
- ->where("rand_key > ", $max_rand * $random)
+ ->where("rand_key >= ", $random)
->orderby(array("rand_key" => "DESC"))
->find_all(1);
-
}
$block->content->item = $items->current();