From 2c9cf944164372e01e5be5d61596a25fa21d15e7 Mon Sep 17 00:00:00 2001 From: Phil Burton Date: Wed, 9 Dec 2015 13:35:37 +0000 Subject: moved everything into functions and structered things a little better --- search.php | 238 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 147 insertions(+), 91 deletions(-) diff --git a/search.php b/search.php index c3e2d6d..b57c1f3 100644 --- a/search.php +++ b/search.php @@ -1,9 +1,9 @@ 1) { - throw new Exception("Too many page choices. Enter only one `#`"); - } +function read_stdin() +{ + $fr = fopen("php://stdin", "r"); + $input = fgets($fr, 128); + $input = rtrim($input); + fclose($fr); + return $input; +} + +function getPaginationBounds($page, $pagesize) +{ + return [ + "start" => ($page - 1) * $pagesize, + "end" => (($page - 1) * $pagesize) + ($pagesize - 1) + ]; +} - if (!empty($pages)) { - // Set the page - $page = substr($pages[0], 1); +function getFormattedArguments($config) +{ + $args = explode(" ", read_stdin()); + + if (!isset($args) || empty($args)) { + throw new Exception("No arguments found"); + } + // Loop the raw arguments and create our formated argument array + $formatArgs = []; + $formatArgs["search"] = []; + foreach ($args as $arg) { + // Switch to check for special arguments + // output should exit but incase it errors we double break + switch ($arg) { + case "--version": + case "-v": // Flow through + output(getVersion($config)); + break 2; + case "--help": + case "-h": // Flow through + output(getHelp($config)); + break 2; } - if (!is_int((int)$page) || $page <= 0) { - throw new Exception("Page `" . $page . "` is not valid"); + + // Check to see if we have a page specified + if (strpos($arg, "#") === 0) { + $formatArgs["page"] = substr($arg, 1); + continue; } + // If we reach here, then the argument is a search term + $formatArgs["search"][] = $arg; + } - // remove the pagination from the search args - $pos = array_search("#" . $page, $args); - if ($pos) { - $args[$pos] = null; - } + // If we reach this point we must have at least one search term + if (empty($formatArgs["search"])) { + throw new Exception("Please Enter a search term"); + } - // Really slow search - $results = []; - foreach ($output as $upload) { - $match = false; - foreach ($args as $arg) { - if (!is_null($arg) && stripos($upload->filename, $arg) === false) { - $match = false; - break; - } - $match = true; - } - if ($match) { - $results[] = $config["url"] . "/" . rawurlencode($upload->filename); + if (!isset($formatArgs["page"])) { + $formatArgs["page"] = 1; + } elseif (!is_int((int)$formatArgs["page"]) || $formatArgs["page"] < 1) { + throw new Exception("Page number `" . $formatArgs["page"] . "` is not valid"); + } + + return $formatArgs; +} + +try { + $config = getConfig(); + $content = getJsonFromUrl($config); + $arguments = getFormattedArguments($config); + + // parse the arguments + $page = $arguments["page"]; + + $out = []; + + // Really slow search + $results = []; + foreach ($content as $upload) { + $match = false; + foreach ($arguments["search"] as $arg) { + if (!is_null($arg) && stripos($upload->filename, $arg) === false) { + $match = false; + break; } + $match = true; + } + if ($match) { + $results[] = $config["url"] . "/" . rawurlencode($upload->filename); } + } - // Return results - if (empty($results)) { - echo "No results found\n"; - } else { - $count = count($results); - $i = 0; - - $start = ($page-1)*3; - $end = $start+2; - if ($start > ($count-1)) { - throw new Exception("No results for this page"); - } - foreach($results as $result) { - if ($i < $start || $i > $end) { - $i++; - continue; - } - $out[] = $i+1 . ": " . $result; - $i++; - } - if ($count > 3) { - $out[] = "results (" . $count . ")"; - } + if (empty($results)) { + output("No results found"); + } + + $count = count($results); + $i = 0; + $bounds = getPaginationBounds($page, $config["pagesize"]); + + if ($bounds["start"] > ($count - 1)) { + throw new Exception("No results for this page"); + } + + foreach ($results as $result) { + if ($i < $bounds["start"] || $i > $bounds["end"]) { + $i++; + continue; } + $out[] = $i + 1 . ": " . $result; + $i++; } - echo implode(" - ", $out) . "\n"; + if ($count > 3) { + $out[] = "results (" . $count . ")"; + } -} catch (Exception $e) { - echo $e->getMessage() . "\n";die(); -} + output($out, " - "); -function read_stdin() -{ - $fr=fopen("php://stdin","r"); // open our file pointer to read from stdin - $input = fgets($fr,128); // read a maximum of 128 characters - $input = rtrim($input); // trim any trailing spaces. - fclose ($fr); // close the file handle - return $input; // return the text entered +} catch (Exception $e) { + echo $e->getMessage() . "\n"; + die(); } -- cgit v1.2.3