diff --git a/core/polyfills.php b/core/polyfills.php index d4299a28..33be6cec 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -556,17 +556,41 @@ function to_shorthand_int(int $int): string return (string)$int; } } - -const TIME_UNITS = ["s"=>60,"m"=>60,"h"=>24,"d"=>365,"y"=>PHP_INT_MAX]; -function format_milliseconds(int $input): string +abstract class TIME_UNITS +{ + public const MILLISECONDS = "ms"; + public const SECONDS = "s"; + public const MINUTES = "m"; + public const HOURS = "h"; + public const DAYS = "d"; + public const YEARS = "y"; + public const CONVERSION = [ + self::MILLISECONDS=>1000, + self::SECONDS=>60, + self::MINUTES=>60, + self::HOURS=>24, + self::DAYS=>365, + self::YEARS=>PHP_INT_MAX + ]; +} +function format_milliseconds(int $input, string $min_unit = TIME_UNITS::SECONDS): string { $output = ""; - $remainder = floor($input / 1000); + $remainder = $input; - foreach (TIME_UNITS as $unit=>$conversion) { + $found = false; + + foreach (TIME_UNITS::CONVERSION as $unit=>$conversion) { $count = $remainder % $conversion; $remainder = floor($remainder / $conversion); + + if ($found||$unit==$min_unit) { + $found = true; + } else { + continue; + } + if ($count==0&&$remainder<1) { break; } @@ -575,6 +599,32 @@ function format_milliseconds(int $input): string return trim($output); } +function parse_to_milliseconds(string $input): int +{ + $output = 0; + $current_multiplier = 1; + + if (preg_match('/^([0-9]+)$/i', $input, $match)) { + // If just a number, then we treat it as milliseconds + $length = $match[0]; + if (is_numeric($length)) { + $length = floatval($length); + $output += $length; + } + } else { + foreach (TIME_UNITS::CONVERSION as $unit=>$conversion) { + if (preg_match('/([0-9]+)'.$unit.'/i', $input, $match)) { + $length = $match[1]; + if (is_numeric($length)) { + $length = floatval($length); + $output += $length * $current_multiplier; + } + } + $current_multiplier *= $conversion; + } + } + return intval($output); +} /** * Turn a date into a time, a date, an "X minutes ago...", etc diff --git a/core/tests/polyfills.test.php b/core/tests/polyfills.test.php index 9ca56d33..de44f4c3 100644 --- a/core/tests/polyfills.test.php +++ b/core/tests/polyfills.test.php @@ -105,6 +105,13 @@ class PolyfillsTest extends TestCase $this->assertEquals("1y 213d 16h 53m 20s", format_milliseconds(50000000000)); } + public function test_parse_to_milliseconds() + { + $this->assertEquals(10, parse_to_milliseconds("10")); + $this->assertEquals(5000, parse_to_milliseconds("5s")); + $this->assertEquals(50000000000, parse_to_milliseconds("1y 213d 16h 53m 20s")); + } + public function test_autodate() { $this->assertEquals( diff --git a/ext/index/main.php b/ext/index/main.php index d85da537..cd73aa16 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -231,6 +231,10 @@ class Index extends Extension } elseif (preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $event->add_querylet(new Querylet("height $cmp :height{$event->id}", ["height{$event->id}"=>int_escape($matches[2])])); + } elseif (preg_match("/^length([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(.+)$/i", $event->term, $matches)) { + $value = parse_to_milliseconds($matches[2]); + $cmp = ltrim($matches[1], ":") ?: "="; + $event->add_querylet(new Querylet("length $cmp :length{$event->id}", ["length{$event->id}"=>$value])); } elseif (preg_match("/^order[=|:](id|width|height|length|filesize|filename)[_]?(desc|asc)?$/i", $event->term, $matches)) { $ord = strtolower($matches[1]); $default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC"; diff --git a/ext/index/theme.php b/ext/index/theme.php index 1b5c0d11..6fc3352b 100644 --- a/ext/index/theme.php +++ b/ext/index/theme.php @@ -47,7 +47,7 @@ and of course start organising your images :-) if (count($images) > 0) { $this->display_page_images($page, $images); } else { - $this->display_error(404, "No Images Found", "No images were found to match the search criteria"); + $this->display_error(404, "No posts Found", "No posts were found to match the search criteria"); } } @@ -181,94 +181,94 @@ and of course start organising your images :-) public function get_help_html() { - return '

Searching is largely based on tags, with a number of special keywords available that allow searching based on properties of the images.

+ return '

Searching is largely based on tags, with a number of special keywords available that allow searching based on properties of the posts.

tagname
-

Returns images that are tagged with "tagname".

+

Returns posts that are tagged with "tagname".

tagname othertagname
-

Returns images that are tagged with "tagname" and "othertagname".

+

Returns posts that are tagged with "tagname" and "othertagname".

-

Most tags and keywords can be prefaced with a negative sign (-) to indicate that you want to search for images that do not match something.

+

Most tags and keywords can be prefaced with a negative sign (-) to indicate that you want to search for posts that do not match something.

-tagname
-

Returns images that are not tagged with "tagname".

+

Returns posts that are not tagged with "tagname".

-tagname -othertagname
-

Returns images that are not tagged with "tagname" and "othertagname". This is different than without the negative sign, as images with "tagname" or "othertagname" can still be returned as long as the other one is not present.

+

Returns posts that are not tagged with "tagname" and "othertagname". This is different than without the negative sign, as posts with "tagname" or "othertagname" can still be returned as long as the other one is not present.

tagname -othertagname
-

Returns images that are tagged with "tagname", but are not tagged with "othertagname".

+

Returns posts that are tagged with "tagname", but are not tagged with "othertagname".

Wildcard searches are possible as well using * for "any one, more, or none" and ? for "any one".

tagn*
-

Returns images that are tagged with "tagname", "tagnot", or anything else that starts with "tagn".

+

Returns posts that are tagged with "tagname", "tagnot", or anything else that starts with "tagn".

tagn?me
-

Returns images that are tagged with "tagname", "tagnome", or anything else that starts with "tagn", has one character, and ends with "me".

+

Returns posts that are tagged with "tagname", "tagnome", or anything else that starts with "tagn", has one character, and ends with "me".

tags=1
-

Returns images with exactly 1 tag.

+

Returns posts with exactly 1 tag.

tags>0
-

Returns images with 1 or more tags.

+

Returns posts with 1 or more tags.

Can use <, <=, >, >=, or =.


-

Search for images by aspect ratio

+

Search for posts by aspect ratio

ratio=4:3
-

Returns images with an aspect ratio of 4:3.

+

Returns posts with an aspect ratio of 4:3.

ratio>16:9
-

Returns images with an aspect ratio greater than 16:9.

+

Returns posts with an aspect ratio greater than 16:9.

Can use <, <=, >, >=, or =. The relation is calculated by dividing width by height.


-

Search for images by file size

+

Search for posts by file size

filesize=1
-

Returns images exactly 1 byte in size.

+

Returns posts exactly 1 byte in size.

filesize>100mb
-

Returns images greater than 100 megabytes in size.

+

Returns posts greater than 100 megabytes in size.

Can use <, <=, >, >=, or =. Supported suffixes are kb, mb, and gb. Uses multiples of 1024.


-

Search for images by MD5 hash

+

Search for posts by MD5 hash

hash=0D3512CAA964B2BA5D7851AF5951F33B
@@ -277,65 +277,86 @@ and of course start organising your images :-)
-

Search for images by file name

+

Search for posts by file name

filename=picasso.jpg
-

Returns images that are named "picasso.jpg".

+

Returns posts that are named "picasso.jpg".


-

Search for images by source

+

Search for posts by source

source=http://google.com/
-

Returns images with a source of "http://google.com/".

+

Returns posts with a source of "http://google.com/".

source=any
-

Returns images with a source set.

+

Returns posts with a source set.

source=none
-

Returns images without a source set.

+

Returns posts without a source set.


-

Search for images by date posted.

+

Search for posts by date posted.

posted>=07-19-2019
-

Returns images posted on or after 07-19-2019.

+

Returns posts posted on or after 07-19-2019.

Can use <, <=, >, >=, or =. Date format is mm-dd-yyyy. Date posted includes time component, so = will not work unless the time is exact.


-

Search for images by image dimensions

+

Search for posts by length.

+ +
+
length>=1h
+

Returns posts that are longer than an hour.

+
+ +
+
length<=10h15m
+

Returns posts that are shorter than 10 hours and 15 minutes.

+
+ +
+
length>=10000
+

Returns posts that are longer than 10,000 milliseconds, or 10 seconds.

+
+ +

Can use <, <=, >, >=, or =. Available suffixes are ms, s, m, h, d, and y. A number by itself will be interpreted as milliseconds. Searches using = are not likely to work unless time is specified down to the millisecond.

+ +
+ +

Search for posts by dimensions

size=640x480
-

Returns images exactly 640 pixels wide by 480 pixels high.

+

Returns posts exactly 640 pixels wide by 480 pixels high.

size>1920x1080
-

Returns images with a width larger than 1920 and a height larger than 1080.

+

Returns posts with a width larger than 1920 and a height larger than 1080.

width=1000
-

Returns images exactly 1000 pixels wide.

+

Returns posts exactly 1000 pixels wide.

height=1000
-

Returns images exactly 1000 pixels high.

+

Returns posts exactly 1000 pixels high.

Can use <, <=, >, >=, or =.

@@ -346,12 +367,12 @@ and of course start organising your images :-)
order:id_asc
-

Returns images sorted by ID, smallest first.

+

Returns posts sorted by ID, smallest first.

order:width_desc
-

Returns images sorted by width, largest first.

+

Returns posts sorted by width, largest first.

These fields are supported: