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(-) (limited to 'search.php') 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 From 83102b75ce9876cf247814f857ee44483b5a19e6 Mon Sep 17 00:00:00 2001 From: Phil Burton Date: Wed, 9 Dec 2015 13:57:11 +0000 Subject: Add docblocking to functions --- search.php | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) (limited to 'search.php') diff --git a/search.php b/search.php index b57c1f3..04e12f4 100644 --- a/search.php +++ b/search.php @@ -1,5 +1,17 @@ + * @throws Exception when file is not readable + * @throws Exception when ini file fails to parse + * @throws Exception when url not found in ini file + * @throws Exception when command not found in ini file + * @throws Exception when pagesize is set incorrectly + */ function getConfig() { $config = false; @@ -30,6 +42,14 @@ function getConfig() return $config; } +/** + * Use curl to find get the json file for a url + * Using the sha256 hashed password if it's been set in the config + * + * @param string[] $config + * @return StdClass[] + * @author Phil Burton + */ function getJsonFromUrl($config) { $curl = curl_init($config["url"] . "?format=json"); @@ -39,9 +59,25 @@ function getJsonFromUrl($config) curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $output = json_decode(curl_exec($curl)); curl_close($curl); + + if (!isset($output)) { + throw new Exception( + "Could not retrive JSON from http server `" + . $config["url"] + . "?format=json` using password=" + . (isset($config["hash"]) ? "true" : "false") + ); + } return $output; } +/** + * Return the help text + * + * @param StdClass[] $config + * @return string + * @author Phil Burton + */ function getHelp($config) { return "Usage: !" . $config["command"] . " [query] #[page] | " @@ -50,11 +86,28 @@ function getHelp($config) . "!" . $config["command"] . " intense marcus #2 (returns the second page of results)"; } +/** + * Get the version string + * + * @param stdClass[] $config + * @return string + * @author Phil Burton + */ function getVersion($config) { return $config["command"] . " v" . "2.0"; } +/** + * Output the given string, adding a new line at the end + * If pieces is an array, use the glue to implode and output a single string + * + * EXIT + * + * @param string|string[] $pieces + * @param string $glue + * @author Phil Burton + */ function output($pieces, $glue = " - ") { if (!is_array($pieces)) { @@ -66,6 +119,12 @@ function output($pieces, $glue = " - ") exit; } +/** + * Read stdin and return as a string + * + * @return string + * @author Phil Burton + */ function read_stdin() { $fr = fopen("php://stdin", "r"); @@ -75,6 +134,15 @@ function read_stdin() return $input; } +/** + * Get the start and end index positions for the required page and + * page size + * + * @param int $page + * @param int $pagesize + * @return int[] + * @author Phil Burton + */ function getPaginationBounds($page, $pagesize) { return [ @@ -83,6 +151,15 @@ function getPaginationBounds($page, $pagesize) ]; } +/** + * Loop through the stdin and parse the arguments given + * If we hit a "special" argument then run the specific code and exit + * Otherwise we set the page and search terms and return them as an array + * + * @param $config + * @return mixed[] + * @author Phil Burton + */ function getFormattedArguments($config) { $args = explode(" ", read_stdin()); @@ -131,6 +208,9 @@ function getFormattedArguments($config) return $formatArgs; } +/********************************* + * ENTRY POINT FOR FUNCTION HERE * + *********************************/ try { $config = getConfig(); $content = getJsonFromUrl($config); @@ -185,6 +265,5 @@ try { output($out, " - "); } catch (Exception $e) { - echo $e->getMessage() . "\n"; - die(); + output($e->getMessage()); } -- cgit v1.2.3