From 78c08b8857d0283bd9b90e00181a3c91d8a214f7 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 28 May 2019 10:44:08 +0200 Subject: [PATCH] Fixes fetching part of issue #1 --- lib/Service/RecipeService.php | 127 ++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 61 deletions(-) diff --git a/lib/Service/RecipeService.php b/lib/Service/RecipeService.php index 238c2549..ee3fe5cd 100644 --- a/lib/Service/RecipeService.php +++ b/lib/Service/RecipeService.php @@ -21,8 +21,8 @@ class RecipeService { $this->root = $root; $this->db = new RecipeDb($db); $this->config = $config; - } - + } + /** * @param int $id * @@ -44,9 +44,11 @@ class RecipeService { public function getRecipeFileById($id) { $folder = $this->getFolderForUser(); $file = $folder->getById($id); + if(count($file) <= 0 || !$this->isRecipeFile($file[0])) { return null; } + return $file[0]; } @@ -65,7 +67,7 @@ class RecipeService { $json['@context'] = 'http://schema.org'; $json['@type'] = 'Recipe'; - // Make sur that "name" doesn't have any funky characters in it + // Make sure that "name" doesn't have any funky characters in it $json['name'] = $this->cleanUpString($json['name']); // Make sure that "dailyDozen" is a comma-separated string @@ -92,7 +94,7 @@ class RecipeService { if(is_array($img) && isset($img['url'])) { $img = $img['url']; } - + if(!$result || strlen($img) < strlen($json['image'])) { $json['image'] = $img; } @@ -104,7 +106,7 @@ class RecipeService { } else { $json['image'] = ''; } - + // Make sure that "recipeYield" is an integer which is at least 1 if(isset($json['recipeYield']) && $json['recipeYield']) { $yield = filter_var($json['recipeYield'], FILTER_SANITIZE_NUMBER_INT); @@ -150,7 +152,7 @@ class RecipeService { } $json['recipeIngredients'] = array_filter($json['recipeIngredients']); - + // Make sure that "recipeInstructions" is an array of strings if(isset($json['recipeInstructions'])) { if(is_array($json['recipeInstructions'])) { @@ -163,20 +165,23 @@ class RecipeService { $json['recipeInstructions'][$i] = ''; } } + } else if(is_string($json['recipeInstructions'])) { + $json['recipeInstructions'] = html_entity_decode($json['recipeInstructions']); + $regex_matches = []; - preg_match_all('/

(.*?)<\/p>/', $this->cleanUpString($json['recipeInstructions']), $regex_matches, PREG_SET_ORDER); + preg_match_all('/<(p|li)>(.*?)<\/(p|li)>/', $json['recipeInstructions'], $regex_matches, PREG_SET_ORDER); $instructions = []; foreach($regex_matches as $regex_match) { - if(!$regex_match || !isset($regex_match[1])) { continue; } + if(!$regex_match || !isset($regex_match[2])) { continue; } - $string = $this->cleanUpString(regex_match[1]); - - if(!$string) { continue; } + $step = $this->cleanUpString($regex_match[2]); - array_push($instructions, $string); + if(!$step) { continue; } + + array_push($instructions, $step); } if(sizeof($instructions) > 0) { @@ -201,19 +206,19 @@ class RecipeService { */ private function parseRecipeHtml($html) { if(!$html) { return null; } - + //$html = str_replace(["\r", "\n", "\t"], '', $html); $json_matches = []; - + // Parse JSON preg_match_all('/