new log picture gallery

This commit is contained in:
following
2013-03-12 10:15:04 +01:00
parent ce8f0d18b9
commit 10ce948a93
50 changed files with 2174 additions and 132 deletions

View File

@ -37,4 +37,5 @@
define('USR_OPT_GMZOOM', 1);
define('USR_OPT_SHOWSTATS', 5);
define('USR_OPT_TRANSLANG', 6);
define('USR_OPT_PICSTAT', 11);
?>

View File

@ -16,8 +16,9 @@
sql_escape_backtick($value) ... escape $value for use within backticks
sql_value($sql, $default) ... Query SQL and return first row of first line
sql_fetch_array($rs) ... mysql_fetch_array with charset conversion
sql_fetch_assoc($rs) ... mysql_fetch_assoc with charset conversion
sql_fetch_assoc($rs) ... mysql_fetch_assoc with charset conversion
sql_fetch_row($rs) ... mysql_fetch_row with charset conversion
sql_fetch_assoc_table($rs) ... fetch_assoc for all rows
sql_temp_table($table) ... registers an placeholder for use as temporary
table and drop's temporary tables if
mysql_pconnect is used
@ -524,6 +525,15 @@
return $retval;
}
function sql_fetch_assoc_table($rs)
{
$result = array();
while ($r = sql_fetch_assoc($rs))
$result[] = $r;
sql_free_result($rs);
return $result;
}
function sql_fetch_row($rs)
{
global $opt;

View File

@ -249,7 +249,7 @@ class cache
sql("INSERT INTO `cache_visits` (`cache_id`, `user_id_ip`, `count`) VALUES (&1, '&2', 1)
ON DUPLICATE KEY UPDATE `count`=`count`+1", $nCacheId, $sIdentifier);
// if the previous statement does an INSERT, it was the first visit for this user
// if the previous statement does an INSERT, it was the first visit for this user today
if (sql_affected_rows() == 1)
{
if ($nVisitUserId != sql_value("SELECT `user_id` FROM `caches` WHERE `cache_id`='&1'", 0, $nCacheId))
@ -296,7 +296,7 @@ class cache
while ($rLog = sql_fetch_assoc($rsLogs))
{
$pictures = array();
$rsPictures = sql("SELECT `url`, `title`, `uuid` FROM `pictures` WHERE `object_id`='&1' AND `object_type`=1", $rLog['id']);
$rsPictures = sql("SELECT `url`, `title`, `uuid`, `id`, `spoiler` FROM `pictures` WHERE `object_id`='&1' AND `object_type`=1", $rLog['id']);
while ($rPicture = sql_fetch_assoc($rsPictures))
$pictures[] = $rPicture;
sql_free_result($rsPictures);

View File

@ -46,4 +46,11 @@
define('OBJECT_TRAVELER', 5);
define('OBJECT_PICTURE', 6);
define('OBJECT_REMOVEDOBJECT', 7);
define('MAX_LOGENTRIES_ON_CACHEPAGE', 5);
define('MAX_PICTURES_PER_GALLERY_PAGE', 48); // must be multiple of 6
define('MAX_PICTURES_IN_CACHE_GALLERY', 120); // must be multiple of 6
// As of march 2013, there are a good deal of OC.de caches with 50-100 pics,
// but only one with more (~500 pics). If lots of caches have >100 pics,
// it may pay out to limit the gallery e.g. to 60 pics and implement browsing.
?>

View File

@ -0,0 +1,186 @@
<?php
/***************************************************************************
* For license information see doc/license.txt
*
* Unicode Reminder メモ
*
* retrieves data from pictures table for log picture stats and galleries;
* data is mostly processed within res_logpictures.tpl
***************************************************************************/
// This kind of purpose definition may be suboptimal. If functions need
// to be reused of other purpose, change naming according to the retrieved
// data set instead of destination view.
define('LOGPICS_FOR_STARTPAGE_GALLERY', 1);
define('LOGPICS_FOR_NEWPICS_GALLERY', 2);
define('LOGPICS_FOR_USER_STAT', 3);
define('LOGPICS_FOR_USER_GALLERY', 4); // params: userid, startat
define('LOGPICS_FOR_MYHOME_GALLERY', 5);
define('LOGPICS_FOR_CACHE_STAT', 6); // params: cacheid
define('LOGPICS_FOR_CACHE_GALLERY', 7); // params: userid, cacheid
function get_logpics($purpose, $userid=0, $cacheid=0, $startat=0)
{
global $login;
// Variables to return for res_logpictures are
//
// from pictures: uuid, url and title
// from cache_logs: id, cache_id, date and type (type currently unused)
// from user: user_id and username, only if no $userid param is given
$rs = false;
$fields = "`pics`.`uuid` AS `pic_uuid`, `pics`.`url` AS `pic_url`,
`pics`.`title`, `pics`.`date_created`,
`logs`.`user_id`, `logs`.`cache_id`,
`logs`.`date` AS `logdate`, `pics`.`date_created` < LEFT(NOW(),4) AS `oldyear`,
`logs`.`id` AS `logid`, `logs`.`type` AS `logtype`";
$join_logs = "INNER JOIN `cache_logs` `logs` ON `logs`.`id`=`pics`.`object_id`";
$join_caches = "INNER JOIN `caches` ON `caches`.`cache_id`=`logs`.`cache_id`";
$join_cachestatus =
"INNER JOIN `cache_status` ON `caches`.`status`=`cache_status`.`id` AND `allow_user_view`=1";
$join_user = "INNER JOIN `user` ON `user`.`user_id`=`logs`.`user_id`";
switch ($purpose)
{
case LOGPICS_FOR_STARTPAGE_GALLERY:
// one pic per user and day,
// one pic per cache and day
// no spoilers, no bad data, no invisible or unpublished caches
// The group-by via nested query make this whole thing sufficiently performant.
// Direct group-bys combined with the wheres are awful slow, and no kind of
// index seems to be good enough to speed it up.
// Indexing the for the inner WHERE seems rather useless, as it filters out
// only a few percent of caches. We must rely on fast data caching.
$rs = sql_slave(
"SELECT $fields, `user`.`username`, `pics`.`date_created` AS `picdate`
FROM (SELECT * FROM
(SELECT `uuid`, `url`, `title`, `date_created`, `object_id` FROM `pictures`
WHERE `local`=1 AND `display`=1 AND `spoiler`=0 AND `unknown_format`=0
AND `object_type`=1
ORDER BY `date_created` DESC
LIMIT 120) `piics`
/* 20 times reserve for filtering out user dups, cache dups and invisibles */
GROUP BY `object_id`, LEFT(`date_created`,10)) `pics` /* max. 1 pic per cache and day */
$join_logs
$join_caches
$join_cachestatus
$join_user
GROUP BY `user`.`user_id`, LEFT(`pics`.`date_created`,10) /* max. 1 pic per user and day */
ORDER BY `pics`.`date_created` DESC
LIMIT 6");
break;
case LOGPICS_FOR_NEWPICS_GALLERY:
// like above, without the "one pic per cache and day" condition
// This saves us one grouped subquery.
$rs = sql_slave(
"SELECT $fields, `user`.`username`, `pics`.`date_created` AS `picdate`
FROM (SELECT `uuid`, `url`, `title`, `date_created`, `object_id` FROM `pictures`
WHERE `local`=1 AND `display`=1 AND `spoiler`=0 AND `unknown_format`=0
AND `object_type`=1
ORDER BY `date_created` DESC
LIMIT 480) `pics`
/* 10 times reserve for filtering out user dups and invisibles */
$join_logs
$join_caches
$join_cachestatus
$join_user
GROUP BY `user`.`user_id`, LEFT(`pics`.`date_created`,10)
ORDER BY `date_created` DESC
LIMIT &1", MAX_PICTURES_PER_GALLERY_PAGE);
break;
case LOGPICS_FOR_USER_STAT:
// just count all the logpics of one user
// It's faster, sensible and consistend with cache and log handling to count
// also invisible data here. Actually, it is present, the pic was made and
// uploaded with a log, and it is still visible for the logger himself
// (and hopfully some time for all, independend of the invisible listing!).
$result = sql_value_slave(
"SELECT COUNT(*)
FROM `pictures` `pics`
$join_logs
WHERE `pics`.`object_type`=1 AND `logs`.`user_id`='&1'",
0, $userid);
break;
case LOGPICS_FOR_USER_GALLERY:
// all pics of one user, except spoilers and invisibles
$rs = sql("SELECT $fields, `logs`.`date` AS `picdate`
FROM `pictures` `pics`
$join_logs
$join_caches
$join_cachestatus
WHERE `object_type`=1 AND `logs`.`user_id`='&1' AND NOT `spoiler`
ORDER BY `logs`.`date` DESC
LIMIT &2, &3", $userid, $startat, MAX_PICTURES_PER_GALLERY_PAGE+1);
break;
case LOGPICS_FOR_MYHOME_GALLERY:
// all picture of one user, with the only exception of zombie pix hanging
// by an old log deletion (we should remove those ...)
$rs = sql("SELECT $fields, `logs`.`date` AS `picdate`
FROM `pictures` AS `pics`
$join_logs
WHERE `object_type`=1 AND `logs`.`user_id`='&1'
ORDER BY `logs`.`date` DESC", $login->userid);
break;
case LOGPICS_FOR_CACHE_STAT:
// all pictures for a cache
// need not to exclude invisible caches, as this is only displayed in listing view
$result = sql_value(
"SELECT COUNT(*)
FROM `pictures` AS `pics`
$join_logs
WHERE `object_type`=1 AND `logs`.`cache_id`='&1'",
0, $cacheid);
break;
case LOGPICS_FOR_CACHE_GALLERY:
// all picture for a cache
// for all users except owner: also excluding invisble caches
$rs = sql("SELECT $fields, `user`.`username`, `logs`.`date` AS `picdate`
FROM `pictures` AS `pics`
$join_logs " .
($userid == $login->userid ? "" : "$join_caches $join_cachestatus") . "
$join_user
WHERE `object_type`=1 AND `logs`.`cache_id`='&1'
ORDER BY `logs`.`date` DESC
LIMIT &2", $cacheid, MAX_PICTURES_IN_CACHE_GALLERY+1);
break;
default:
global $tpl;
$tpl->error(ERROR_INVALID_OPERATION);
return null;
}
if ($rs !== false)
{
$result = sql_fetch_assoc_table($rs);
while (count($result) % 6)
// fill up table to full line length, so that < 6 pics do not spread over the table width
$result[] = array('pic_uuid' => '');
}
return $result;
}
?>

View File

@ -233,6 +233,24 @@ class picture
else
return false;
}
function isVisibleOnCachePage()
{
if ($this->getObjectType() != OBJECT_CACHELOG)
return null;
else
$rs = sql("SELECT `id` FROM `cache_logs` WHERE `cache_id`='&1'
ORDER BY `date` DESC
LIMIT &2",
$this->getCacheId(), MAX_LOGENTRIES_ON_CACHEPAGE);
$firstlogs = false;
while ($r = sql_fetch_assoc($rs))
if ($r['id'] == $this->getLogId())
$firstlogs = true;
sql_free_result($rs);
return $firstlogs;
}
function getCacheId()
{
if ($this->getObjectType() == OBJECT_CACHELOG)