2007-04-16 11:58:25 +00:00
< ? php
2010-01-04 12:41:04 +00:00
/**
2010-01-05 13:13:11 +00:00
* Name : Image List
2010-01-04 12:41:04 +00:00
* Author : Shish < webmaster @ shishnet . org >
2010-01-04 12:44:20 +00:00
* Link : http :// code . shishnet . org / shimmie2 /
2010-01-04 12:41:04 +00:00
* License : GPLv2
* Description : Show a list of uploaded images
* Documentation :
* Here is a list of the search methods available out of the box ;
* Shimmie extensions may provide other filters :
* < ul >
* < li > by tag , eg
* < ul >
* < li > cat
* < li > pie
* < li > somethi * -- wildcards are supported
* </ ul >
* < li > size ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) width x height , eg
* < ul >
* < li > size = 1024 x768 -- a specific wallpaper size
* < li > size & gt ; = 500 x500 -- no small images
* < li > size & lt ; 1000 x1000 -- no large images
* </ ul >
2014-01-02 14:10:08 +00:00
* < li > width ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) width , eg
* < ul >
* < li > width = 1024 -- find images with 1024 width
* < li > width > 2000 -- find images bigger than 2000 width
* </ ul >
* < li > height ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) height , eg
* < ul >
* < li > height = 768 -- find images with 768 height
* < li > height > 1000 -- find images bigger than 1000 height
* </ ul >
2010-01-04 12:41:04 +00:00
* < li > ratio ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) width : height , eg
* < ul >
* < li > ratio = 4 : 3 , ratio = 16 : 9 -- standard wallpaper
* < li > ratio = 1 : 1 -- square images
* < li > ratio < 1 : 1 -- tall images
* < li > ratio > 1 : 1 -- wide images
* </ ul >
* < li > filesize ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) size , eg
* < ul >
2012-02-17 01:45:55 +00:00
* < li > filesize & gt ; 1024 -- no images under 1 KB
* < li > filesize & lt = 3 MB -- shorthand filesizes are supported too
2010-01-04 12:41:04 +00:00
* </ ul >
* < li > id ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) number , eg
* < ul >
* < li > id < 20 -- search only the first few images
* < li > id >= 500 -- search later images
* </ ul >
2014-01-02 14:00:24 +00:00
* < li > user = Username & poster = Username , eg
2010-01-04 12:41:04 +00:00
* < ul >
* < li > user = Shish -- find all of Shish ' s posts
2014-01-02 14:00:24 +00:00
* < li > poster = Shish -- same as above
2010-01-04 12:41:04 +00:00
* </ ul >
2014-01-02 14:00:24 +00:00
* < li > user_id = userID & poster_id = userID , eg
* < ul >
* < li > user_id = 2 -- find all posts by user id 2
* < li > poster_id = 2 -- same as above
* </ ul >
* < li > hash = md5sum & md5 = md5sum , eg
2010-01-04 12:41:04 +00:00
* < ul >
* < li > hash = bf5b59173f16b6937a4021713dbfaa72 -- find the " Taiga want up! " image
2014-01-02 14:00:24 +00:00
* < li > md5 = bf5b59173f16b6937a4021713dbfaa72 -- same as above
2010-01-04 12:41:04 +00:00
* </ ul >
2014-01-02 14:00:24 +00:00
* < li > filetype = type & ext = type , eg
2010-01-04 12:41:04 +00:00
* < ul >
* < li > filetype = png -- find all PNG images
2014-01-02 14:00:24 +00:00
* < li > ext = png -- same as above
2010-01-04 12:41:04 +00:00
* </ ul >
2014-01-02 14:00:24 +00:00
* < li > filename = blah & name = blah , eg
2010-01-04 12:41:04 +00:00
* < ul >
2010-02-27 02:09:10 +00:00
* < li > filename = kitten -- find all images with " kitten " in the original filename
2014-01-02 14:00:24 +00:00
* < li > name = kitten -- same as above
2010-01-04 12:41:04 +00:00
* </ ul >
2012-02-17 01:45:55 +00:00
* < li > posted ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) date , eg
2010-01-04 12:41:04 +00:00
* < ul >
2012-02-17 01:45:55 +00:00
* < li > posted & gt ; = 2009 - 12 - 25 posted & lt ; = 2010 - 01 - 01 -- find images posted between christmas and new year
2010-01-04 12:41:04 +00:00
* </ ul >
2014-01-02 14:00:24 +00:00
* < li > tags ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) count , eg
* < ul >
* < li > tags = 1 -- search for images with only 1 tag
* < li > tags >= 10 -- search for images with 10 or more tags
* < li > tags < 25 -- search for images with less than 25 tags
* </ ul >
* < li > source = url , eg
* < ul >
* < li > source = http :// example . com -- find all images with " http://example.com " in the source
* </ ul >
2014-01-03 01:24:55 +00:00
* < li > order ( id , width , height , filesize , filename ) _ ( ASC , DESC ), eg
* < ul >
* < li > order = width -- find all images sorted from highest > lowest width
* < li > order = filesize_asc -- find all images sorted from lowest > highest filesize
* </ ul >
2010-01-05 09:40:26 +00:00
* </ ul >
2010-01-04 12:41:04 +00:00
* < p > Search items can be combined to search for images which match both ,
* or you can stick " - " in front of an item to search for things that don ' t
* match it .
2014-01-02 14:00:24 +00:00
* < p > Metatags can be followed by " : " rather than " = " if you prefer .
* < br /> I . E : " posted:2014-01-01 " , " id:>=500 " etc .
2010-01-05 09:40:26 +00:00
* < p > Some search methods provided by extensions :
* < ul >
* < li > Numeric Score
* < ul >
* < li > score ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) number -- seach by score
* < li > upvoted_by = Username -- search for a user ' s likes
* < li > downvoted_by = Username -- search for a user ' s dislikes
* </ ul >
* < li > Image Rating
* < ul >
* < li > rating = se -- find safe and explicit images , ignore questionable and unknown
* </ ul >
* < li > Favorites
* < ul >
* < li > favorites ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) number -- search for images favourited a certain number of times
2014-01-02 14:00:24 +00:00
* < li > favourited_by = Username -- search for a user ' s choices by username
* < li > favorited_by_userno = UserID -- search for a user ' s choice by userID
2010-01-05 09:40:26 +00:00
* </ ul >
* < li > Notes
* < ul >
* < li > notes ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) number -- search by the number of notes an image has
2014-01-02 14:00:24 +00:00
* < li > notes_by = Username -- search for a notes created by username
* < li > notes_by_userno = UserID -- search for a notes created by userID
2010-01-05 09:40:26 +00:00
* </ ul >
* </ ul >
2010-01-04 12:41:04 +00:00
*/
2009-01-03 21:00:09 +00:00
/*
* SearchTermParseEvent :
* Signal that a search term needs parsing
*/
class SearchTermParseEvent extends Event {
var $term = null ;
var $context = null ;
var $querylets = array ();
public function SearchTermParseEvent ( $term , $context ) {
$this -> term = $term ;
$this -> context = $context ;
}
public function is_querylet_set () {
return ( count ( $this -> querylets ) > 0 );
}
public function get_querylets () {
return $this -> querylets ;
}
public function add_querylet ( $q ) {
$this -> querylets [] = $q ;
}
}
2007-04-16 11:58:25 +00:00
2009-08-13 18:26:23 +00:00
class SearchTermParseException extends SCoreException {
}
2007-07-17 18:07:18 +00:00
class PostListBuildingEvent extends Event {
var $search_terms = null ;
2012-08-18 18:23:11 +00:00
var $parts = array ();
2007-07-17 18:07:18 +00:00
2009-05-11 14:04:33 +00:00
public function __construct ( $search ) {
2007-07-17 18:07:18 +00:00
$this -> search_terms = $search ;
}
2012-08-18 18:23:11 +00:00
public function add_control ( /*string*/ $html , /*int*/ $position = 50 ) {
while ( isset ( $this -> parts [ $position ])) $position ++ ;
$this -> parts [ $position ] = $html ;
}
2007-07-17 18:07:18 +00:00
}
2012-02-08 12:07:01 +00:00
class Index extends Extension {
2013-06-16 11:20:12 +00:00
var $stpen = 0 ; // search term parse event number
2012-04-05 15:18:41 +00:00
2012-02-02 08:07:57 +00:00
public function onInitExt ( InitExtEvent $event ) {
2009-05-11 21:09:24 +00:00
global $config ;
2012-04-01 16:47:39 +00:00
$config -> set_default_int ( " index_images " , 24 );
2009-05-11 21:09:24 +00:00
$config -> set_default_bool ( " index_tips " , true );
2014-01-03 08:24:47 +00:00
$config -> set_default_string ( " index_order " , " id DESC " );
2009-05-11 21:09:24 +00:00
}
2007-07-10 22:33:49 +00:00
2012-02-02 08:07:57 +00:00
public function onPageRequest ( PageRequestEvent $event ) {
2009-05-11 14:04:33 +00:00
global $config , $database , $page , $user ;
2009-05-11 21:09:24 +00:00
if ( $event -> page_matches ( " post/list " )) {
2007-07-19 11:46:43 +00:00
if ( isset ( $_GET [ 'search' ])) {
2013-08-04 17:13:50 +00:00
$search = url_escape ( Tag :: implode ( Tag :: resolve_aliases ( Tag :: explode ( $_GET [ 'search' ], false ))));
2007-08-01 15:52:46 +00:00
if ( empty ( $search )) {
2009-05-11 14:04:33 +00:00
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " post/list/1 " ));
2007-08-01 15:52:46 +00:00
}
else {
2009-05-11 14:04:33 +00:00
$page -> set_mode ( " redirect " );
2012-01-16 04:03:27 +00:00
$page -> set_redirect ( make_link ( 'post/list/' . $search . '/1' ));
2007-08-01 15:52:46 +00:00
}
2007-07-19 11:46:43 +00:00
return ;
}
2010-04-26 01:36:05 +00:00
$search_terms = $event -> get_search_terms ();
$page_number = $event -> get_page_number ();
$page_size = $event -> get_page_size ();
2013-10-04 21:17:42 +00:00
$count_search_terms = count ( $search_terms );
2010-05-04 19:10:37 +00:00
try {
2013-08-29 23:19:38 +00:00
#log_debug("index", "Search for ".implode(" ", $search_terms), false, array("terms"=>$search_terms));
2010-05-04 19:10:37 +00:00
$total_pages = Image :: count_pages ( $search_terms );
2013-10-04 21:17:42 +00:00
if ( SPEED_HAX && $count_search_terms === 0 && ( $page_number < 10 )) { // extra caching for the first few post/list pages
2012-06-24 01:07:59 +00:00
$images = $database -> cache -> get ( " post-list- $page_number " );
if ( ! $images ) {
$images = Image :: find_images (( $page_number - 1 ) * $page_size , $page_size , $search_terms );
$database -> cache -> set ( " post-list- $page_number " , $images , 600 );
}
}
else {
$images = Image :: find_images (( $page_number - 1 ) * $page_size , $page_size , $search_terms );
}
2010-05-04 19:10:37 +00:00
}
catch ( SearchTermParseException $stpe ) {
// FIXME: display the error somewhere
$total_pages = 0 ;
$images = array ();
}
2007-07-17 18:07:18 +00:00
2013-10-04 21:17:42 +00:00
$count_images = count ( $images );
if ( $count_search_terms === 0 && $count_images === 0 && $page_number === 1 ) {
2009-05-11 14:04:33 +00:00
$this -> theme -> display_intro ( $page );
2009-07-07 15:52:09 +00:00
send_event ( new PostListBuildingEvent ( $search_terms ));
2009-04-22 05:41:49 +00:00
}
2013-10-04 21:17:42 +00:00
else if ( $count_search_terms > 0 && $count_images === 1 && $page_number === 1 ) {
2009-05-11 14:04:33 +00:00
$page -> set_mode ( " redirect " );
2012-01-16 04:03:27 +00:00
$page -> set_redirect ( make_link ( 'post/view/' . $images [ 0 ] -> id ));
2009-01-22 09:39:44 +00:00
}
else {
2012-08-18 18:23:11 +00:00
$plbe = new PostListBuildingEvent ( $search_terms );
send_event ( $plbe );
2009-04-22 05:41:49 +00:00
$this -> theme -> set_page ( $page_number , $total_pages , $search_terms );
2009-05-11 14:04:33 +00:00
$this -> theme -> display_page ( $page , $images );
2012-08-18 18:23:11 +00:00
if ( count ( $plbe -> parts ) > 0 ) {
$this -> theme -> display_admin_block ( $plbe -> parts );
}
2009-01-22 09:39:44 +00:00
}
2007-04-16 11:58:25 +00:00
}
2009-05-11 21:09:24 +00:00
}
2007-04-16 11:58:25 +00:00
2012-02-02 08:07:57 +00:00
public function onSetupBuilding ( SetupBuildingEvent $event ) {
2009-05-11 21:09:24 +00:00
$sb = new SetupBlock ( " Index Options " );
$sb -> position = 20 ;
2009-01-04 19:18:37 +00:00
2012-04-01 16:47:39 +00:00
$sb -> add_label ( " Show " );
$sb -> add_int_option ( " index_images " );
$sb -> add_label ( " images on the post list " );
2007-04-16 11:58:25 +00:00
2009-05-11 21:09:24 +00:00
$event -> panel -> add_block ( $sb );
}
2008-02-06 17:24:08 +00:00
2012-06-24 01:07:59 +00:00
public function onImageAddition ( ImageAdditionEvent $event ) {
global $database ;
if ( SPEED_HAX ) {
for ( $i = 1 ; $i < 10 ; $i ++ ) {
$database -> cache -> delete ( " post-list- $i " );
}
}
}
public function onImageDeletion ( ImageDeletionEvent $event ) {
global $database ;
if ( SPEED_HAX ) {
for ( $i = 1 ; $i < 10 ; $i ++ ) {
$database -> cache -> delete ( " post-list- $i " );
}
}
}
2012-02-02 08:07:57 +00:00
public function onSearchTermParse ( SearchTermParseEvent $event ) {
2009-05-11 21:09:24 +00:00
$matches = array ();
2012-01-16 04:03:27 +00:00
// check for tags first as tag based searches are more common.
2014-01-02 14:00:24 +00:00
if ( preg_match ( " /^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])( \ d+) $ / " , $event -> term , $matches )) {
$cmp = ltrim ( $matches [ 1 ], " : " ) ? : " = " ;
2012-01-16 04:03:27 +00:00
$tags = $matches [ 2 ];
$event -> add_querylet ( new Querylet ( 'images.id IN (SELECT DISTINCT image_id FROM image_tags GROUP BY image_id HAVING count(image_id) ' . $cmp . ' ' . $tags . ')' ));
2009-05-11 21:09:24 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])( \ d+):( \ d+) $ / " , $event -> term , $matches )) {
$cmp = preg_replace ( '/^:/' , '=' , $matches [ 1 ]);
2013-06-16 11:20:12 +00:00
$args = array ( " width { $this -> stpen } " => int_escape ( $matches [ 2 ]), " height { $this -> stpen } " => int_escape ( $matches [ 3 ]));
$event -> add_querylet ( new Querylet ( " width / height $cmp :width { $this -> stpen } / :height { $this -> stpen } " , $args ));
2009-05-11 21:09:24 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^(filesize|id)([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])( \ d+[kmg]?b?) $ /i " , $event -> term , $matches )) {
2009-05-11 21:09:24 +00:00
$col = $matches [ 1 ];
2014-01-02 14:00:24 +00:00
$cmp = ltrim ( $matches [ 2 ], " : " ) ? : " = " ;
2009-05-11 21:09:24 +00:00
$val = parse_shorthand_int ( $matches [ 3 ]);
2013-06-16 11:20:12 +00:00
$event -> add_querylet ( new Querylet ( " images. $col $cmp :val { $this -> stpen } " , array ( " val { $this -> stpen } " => $val )));
2009-05-11 21:09:24 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^(hash|md5)[=|:]([0-9a-fA-F]*) $ /i " , $event -> term , $matches )) {
2009-08-02 07:57:30 +00:00
$hash = strtolower ( $matches [ 2 ]);
2013-06-16 11:20:12 +00:00
$event -> add_querylet ( new Querylet ( 'images.hash = :hash' , array ( " hash " => $hash )));
2009-05-11 21:09:24 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^(filetype|ext)[=|:]([a-zA-Z0-9]*) $ /i " , $event -> term , $matches )) {
2009-05-11 21:09:24 +00:00
$ext = strtolower ( $matches [ 2 ]);
2013-06-16 11:20:12 +00:00
$event -> add_querylet ( new Querylet ( 'images.ext = :ext' , array ( " ext " => $ext )));
2009-05-11 21:09:24 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^(filename|name)[=|:]([a-zA-Z0-9]*) $ /i " , $event -> term , $matches )) {
2009-05-11 21:09:24 +00:00
$filename = strtolower ( $matches [ 2 ]);
2013-06-16 11:20:12 +00:00
$event -> add_querylet ( new Querylet ( " images.filename LIKE :filename { $this -> stpen } " , array ( " filename { $this -> stpen } " => " % $filename % " )));
2012-04-05 16:33:50 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^(source)[=|:]([a-zA-Z0-9]*) $ /i " , $event -> term , $matches )) {
$source = strtolower ( $matches [ 2 ]);
$event -> add_querylet ( new Querylet ( 'images.source LIKE :src' , array ( " src " => " % $source % " )));
2009-05-11 21:09:24 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*) $ / " , $event -> term , $matches )) {
$cmp = ltrim ( $matches [ 1 ], " : " ) ? : " = " ;
2012-02-17 01:43:49 +00:00
$val = $matches [ 2 ];
2013-06-16 11:20:12 +00:00
$event -> add_querylet ( new Querylet ( " images.posted $cmp :posted { $this -> stpen } " , array ( " posted { $this -> stpen } " => $val )));
2008-02-06 17:24:08 +00:00
}
2014-01-02 14:00:24 +00:00
else if ( preg_match ( " /^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])( \ d+)x( \ d+) $ / " , $event -> term , $matches )) {
$cmp = ltrim ( $matches [ 1 ], " : " ) ? : " = " ;
2013-06-16 11:20:12 +00:00
$args = array ( " width { $this -> stpen } " => int_escape ( $matches [ 2 ]), " height { $this -> stpen } " => int_escape ( $matches [ 3 ]));
$event -> add_querylet ( new Querylet ( " width $cmp :width { $this -> stpen } AND height $cmp :height { $this -> stpen } " , $args ));
2010-01-05 09:05:16 +00:00
}
2014-01-02 14:10:08 +00:00
else if ( preg_match ( " /^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])( \ d+) $ / " , $event -> term , $matches )) {
$cmp = ltrim ( $matches [ 1 ], " : " ) ? : " = " ;
$event -> add_querylet ( new Querylet ( " width $cmp :width { $this -> stpen } " , array ( " width { $this -> stpen } " => int_escape ( $matches [ 2 ]))));
}
else if ( preg_match ( " /^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])( \ d+) $ / " , $event -> term , $matches )) {
$cmp = ltrim ( $matches [ 1 ], " : " ) ? : " = " ;
$event -> add_querylet ( new Querylet ( " height $cmp :height { $this -> stpen } " , array ( " height { $this -> stpen } " => int_escape ( $matches [ 2 ]))));
}
2014-01-03 01:24:55 +00:00
else if ( preg_match ( " /^order[=|:](id|width|height|filesize|filename)[_]?(desc|asc)? $ /i " , $event -> term , $matches )){
global $order_sql ;
2014-01-03 08:24:47 +00:00
$ord = strtolower ( $matches [ 1 ]);
2014-01-03 01:24:55 +00:00
$sort = isset ( $matches [ 2 ]) ? strtoupper ( $matches [ 2 ]) : ( preg_match ( " /^(id|filename) $ / " , $matches [ 1 ]) ? " ASC " : " DESC " );
2014-01-03 08:24:47 +00:00
$order_sql = " $ord $sort " ;
2014-01-03 01:24:55 +00:00
$event -> add_querylet ( new Querylet ( " 1=1 " )); //small hack to avoid metatag being treated as normal tag
}
2013-06-16 11:20:12 +00:00
$this -> stpen ++ ;
2007-04-16 11:58:25 +00:00
}
}
?>