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 >
* < 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 >
* < li > filesize > 1024 -- no images under 1 KB
* < li > filesize <= 3 MB -- shorthand filesizes are supported too
* </ 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 >
* < li > user = Username , eg
* < ul >
* < li > user = Shish -- find all of Shish ' s posts
* </ ul >
* < li > hash = md5sum , eg
* < ul >
* < li > hash = bf5b59173f16b6937a4021713dbfaa72 -- find the " Taiga want up! " image
* </ ul >
* < li > filetype = type , eg
* < ul >
* < li > filetype = png -- find all PNG images
* </ ul >
* < li > filename = blah , eg
* < ul >
2010-02-27 02:09:10 +00:00
* < li > filename = kitten -- find all images with " kitten " in the original filename
2010-01-04 12:41:04 +00:00
* </ ul >
* < li > posted = date , eg
* < ul >
* < li > posted = 2009 - 12 - 25 -- find images posted on the 25 th December
* </ 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 .
2010-01-05 09:40:26 +00:00
* < p > Some search methods provided by extensions :
* < ul >
* < li > Danbooru API
* < ul >
* < li > md5 : [ hash ] -- same as " hash= " , but the API calls it by a different name
* </ 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
* < li > favourited_by = Username -- search for a user ' s choices
* </ ul >
* < li > Notes
* < ul >
* < li > notes ( = , & lt ;, & gt ;, & lt ; = , & gt ; = ) number -- search by the number of notes an image has
* </ 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 ;
2009-05-11 14:04:33 +00:00
public function __construct ( $search ) {
2007-07-17 18:07:18 +00:00
$this -> search_terms = $search ;
}
}
2009-05-11 21:09:24 +00:00
class Index extends SimpleExtension {
2012-02-02 08:07:57 +00:00
public function onInitExt ( InitExtEvent $event ) {
2009-05-11 21:09:24 +00:00
global $config ;
$config -> set_default_int ( " index_width " , 3 );
$config -> set_default_int ( " index_height " , 4 );
$config -> set_default_bool ( " index_tips " , true );
}
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' ])) {
2007-08-01 15:52:46 +00:00
$search = url_escape ( trim ( $_GET [ 'search' ]));
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 ();
2010-05-04 19:10:37 +00:00
try {
$total_pages = Image :: count_pages ( $search_terms );
$images = Image :: find_images (( $page_number - 1 ) * $page_size , $page_size , $search_terms );
}
catch ( SearchTermParseException $stpe ) {
// FIXME: display the error somewhere
$total_pages = 0 ;
$images = array ();
}
2007-07-17 18:07:18 +00:00
2009-07-07 15:52:09 +00:00
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
}
2009-12-30 09:29:25 +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 {
2009-05-11 14:04:33 +00:00
send_event ( new PostListBuildingEvent ( $search_terms ));
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 );
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
2009-05-11 21:09:24 +00:00
$sb -> add_label ( " Index table size " );
$sb -> add_int_option ( " index_width " );
$sb -> add_label ( " x " );
$sb -> add_int_option ( " index_height " );
$sb -> add_label ( " images " );
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-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.
if ( preg_match ( " /tags(<|>|<=|>=|=)( \ d+)/ " , $event -> term , $matches )) {
2009-05-11 21:09:24 +00:00
$cmp = $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
}
else if ( preg_match ( " /^ratio(<|>|<=|>=|=)( \ d+):( \ d+) $ / " , $event -> term , $matches )) {
$cmp = $matches [ 1 ];
2011-02-22 15:03:16 +00:00
$args = array ( " width " => int_escape ( $matches [ 2 ]), " height " => int_escape ( $matches [ 3 ]));
2012-01-16 04:03:27 +00:00
$event -> add_querylet ( new Querylet ( 'width / height ' . $cmp . ' :width / :height' , $args ));
2009-05-11 21:09:24 +00:00
}
else if ( preg_match ( " /^(filesize|id)(<|>|<=|>=|=)( \ d+[kmg]?b?) $ /i " , $event -> term , $matches )) {
$col = $matches [ 1 ];
$cmp = $matches [ 2 ];
$val = parse_shorthand_int ( $matches [ 3 ]);
2011-02-22 15:03:16 +00:00
$event -> add_querylet ( new Querylet ( " images. $col $cmp :val " , array ( " val " => $val )));
2009-05-11 21:09:24 +00:00
}
2009-08-02 07:57:30 +00:00
else if ( preg_match ( " /^(hash|md5)=([0-9a-fA-F]*) $ /i " , $event -> term , $matches )) {
$hash = strtolower ( $matches [ 2 ]);
2012-01-16 04:03:27 +00:00
$event -> add_querylet ( new Querylet ( 'images.hash = "' . $hash . '"' ));
2009-05-11 21:09:24 +00:00
}
else if ( preg_match ( " /^(filetype|ext)=([a-zA-Z0-9]*) $ /i " , $event -> term , $matches )) {
$ext = strtolower ( $matches [ 2 ]);
2012-01-16 04:03:27 +00:00
$event -> add_querylet ( new Querylet ( 'images.ext = "' . $ext . '"' ));
2009-05-11 21:09:24 +00:00
}
else if ( preg_match ( " /^(filename|name)=([a-zA-Z0-9]*) $ /i " , $event -> term , $matches )) {
$filename = strtolower ( $matches [ 2 ]);
2012-01-16 04:03:27 +00:00
$event -> add_querylet ( new Querylet ( 'images.filename LIKE "%' . $filename . '%"' ));
2009-05-11 21:09:24 +00:00
}
else if ( preg_match ( " /^posted=(([0-9 \ *]*)?(-[0-9 \ *]*)?(-[0-9 \ *]*)?) $ / " , $event -> term , $matches )) {
$val = str_replace ( " * " , " % " , $matches [ 1 ]);
2012-01-16 04:03:27 +00:00
$event -> add_querylet ( new Querylet ( 'images.posted LIKE "%' . $val . '%"' ));
2008-02-06 17:24:08 +00:00
}
2012-01-16 04:03:27 +00:00
else if ( preg_match ( " /^size(<|>|<=|>=|=)( \ d+)x( \ d+) $ / " , $event -> term , $matches )) {
2010-01-05 09:05:16 +00:00
$cmp = $matches [ 1 ];
2012-01-16 04:03:27 +00:00
$args = array ( " width " => int_escape ( $matches [ 2 ]), " height " => int_escape ( $matches [ 3 ]));
$event -> add_querylet ( new Querylet ( 'width ' . $cmp . ' :width AND height ' . $cmp . ' :height' , $args ));
2010-01-05 09:05:16 +00:00
}
2007-04-16 11:58:25 +00:00
}
}
?>