Skip to content

Commit

Permalink
Fixes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 10, 2019
1 parent d622556 commit 3d2fe58
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 26 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Expand Up @@ -5,7 +5,7 @@
<name>Cookbook</name>
<summary>An integrated cookbook using schema.org JSON files as recipes</summary>
<description><![CDATA[A library for all your recipes. It uses JSON files following the schema.org recipe format. To add a recipe to the collection, you can paste in the URL of the recipe, and the provided web page will be parsed and downloaded to whichever folder you specify in the app settings.]]></description>
<version>0.3.3</version>
<version>0.4.0</version>
<licence>agpl</licence>
<author mail="mrzapp@users.noreply.github.com" >Jeppe Zapp</author>
<namespace>Cookbook</namespace>
Expand Down
1 change: 0 additions & 1 deletion lib/Controller/RecipeController.php
Expand Up @@ -107,7 +107,6 @@ public function get() {
* @NoCSRFRequired
*/
public function update() {

$json = $_POST;

if(isset($_GET['id'])) {
Expand Down
54 changes: 38 additions & 16 deletions lib/Db/RecipeDb.php
Expand Up @@ -48,12 +48,14 @@ public function deleteRecipeById(int $id) {
$qb->execute();
}

public function findAllRecipes() {
public function findAllRecipes(string $userId) {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('cookbook_recipes', 'r')
->where('user_id = :user')
->orderBy('r.name');
$qb->setParameter('user', $userId, TYPE::STRING);

$cursor = $qb->execute();
$result = $cursor->fetchAll();
Expand All @@ -62,12 +64,14 @@ public function findAllRecipes() {
return $result;
}

public function findAllKeywords() {
public function findAllKeywords(string $userId) {
$qb = $this->db->getQueryBuilder();

$qb->select('k.name')
->from('cookbook_keywords', 'k')
->where('user_id = :user')
->groupBy('k.name');
$qb->setParameter('user', $userId, TYPE::STRING);

$cursor = $qb->execute();
$result = $cursor->fetchAll();
Expand All @@ -81,19 +85,19 @@ public function findAllKeywords() {
/**
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
*/
public function findRecipes($keywords) {
public function findRecipes(array $keywords, string $userId) {
$has_keywords = $keywords && is_array($keywords) && sizeof($keywords) > 0 && $keywords[0];

if(!$has_keywords) { return $this->findAllRecipes(); }
if(!$has_keywords) { return $this->findAllRecipes($userId); }

$qb = $this->db->getQueryBuilder();

$qb->select(['r.recipe_id', 'r.name'])
->from('cookbook_keywords', 'k');

$paramIdx = 1;
$params = array();
$types = array();
$params = [];
$types = [];

foreach ($keywords as $keyword)
{
Expand All @@ -104,11 +108,14 @@ public function findRecipes($keywords) {

$params["keyword$paramIdx"] = "%$lowerKeyword%";
$types["keyword$paramIdx"] = Type::STRING;
$paramIdx ++;
$paramIdx++;

}


$qb->andWhere('k.user_id = :user');

$qb->setParameters($params, $types);
$qb->setParameter('user', $userId, TYPE::STRING);

$qb->join('k', 'cookbook_recipes', 'r', 'k.recipe_id = r.recipe_id');

Expand All @@ -122,22 +129,29 @@ public function findRecipes($keywords) {
return $result;
}

public function emptySearchIndex() {
public function emptySearchIndex(string $userId) {
$qb = $this->db->getQueryBuilder();

$qb->delete('cookbook_recipes');
$qb->delete('cookbook_recipes')
->where('user_id = :user')
->orWhere('user_id = :empty');
$qb->setParameter('user', $userId, TYPE::STRING);
$qb->setParameter('empty', 'empty', TYPE::STRING);

$qb->execute();

$qb->delete('cookbook_keywords');
$qb->delete('cookbook_keywords')
->where('user_id = :user')
->orWhere('user_id = :empty');
$qb->setParameter('user', $userId, TYPE::STRING);
$qb->setParameter('empty', 'empty', TYPE::STRING);

$qb->execute();
}

private function isRecipeEmpty($json) {
}
private function isRecipeEmpty($json) {}

public function indexRecipeFile($file) {
public function indexRecipeFile($file, string $userId) {
$json = json_decode($file->getContent(), true);

if(!$json || !isset($json['name']) || $json['name'] === 'No name') { return; }
Expand All @@ -148,24 +162,30 @@ public function indexRecipeFile($file) {

// Insert recipe
$qb->delete('cookbook_recipes')
->where('recipe_id = :id');
->where('recipe_id = :id')
->andWhere('user_id = :user');
$qb->setParameter('id', $id, IQueryBuilder::PARAM_INT);
$qb->setParameter('user', $userId, TYPE::STRING);

$qb->execute();

$qb->insert('cookbook_recipes')
->values([
'recipe_id' => ':id',
'name' => ':name',
'user_id' => ':user',
]);
$qb->setParameter('id', $id, IQueryBuilder::PARAM_INT);
$qb->setParameter('name', $json['name'], Type::STRING);
$qb->setParameter('user', $userId, Type::STRING);
$qb->execute();

// Insert keywords
$qb->delete('cookbook_keywords')
->where('recipe_id = :id');
->where('recipe_id = :id')
->andWhere('user_id = :user');
$qb->setParameter('id', $id, IQueryBuilder::PARAM_INT);
$qb->setParameter('user', $userId, TYPE::STRING);
$qb->execute();

if(isset($json['keywords'])) {
Expand All @@ -177,9 +197,11 @@ public function indexRecipeFile($file) {
->values([
'recipe_id' => ':id',
'name' => ':keyword',
'user_id' => ':user',
]);
$qb->setParameter('id', $id, IQueryBuilder::PARAM_INT);
$qb->setParameter('keyword', $keyword, Type::STRING);
$qb->setParameter('user', $userId, Type::STRING);
$qb->execute();
}
}
Expand Down
46 changes: 46 additions & 0 deletions lib/Migration/Version000000Date20190910100911.php
@@ -0,0 +1,46 @@
<?php

namespace OCA\Cookbook\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use Doctrine\DBAL\Types\Type;

class Version000000Date20190910100911 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$recipes_table = $schema->getTable('cookbook_recipes');

if(!$recipes_table->hasColumn('user_id')) {
$recipes_table->addColumn('user_id', Type::STRING, [
'notnull' => true,
'length' => 64,
'default' => 'empty',
]);
}

$keywords_table = $schema->getTable('cookbook_keywords');

if(!$keywords_table->hasColumn('user_id')) {
$keywords_table->addColumn('user_id', Type::STRING, [
'notnull' => true,
'length' => 64,
'default' => 'empty',
]);
}

return $schema;
}
}

?>
26 changes: 18 additions & 8 deletions lib/Service/RecipeService.php
Expand Up @@ -264,7 +264,7 @@ private function parseRecipeHtml($html) {
$json_matches = [];

// Parse JSON
preg_match_all('/<script type="application\/ld\+json">([\s\S]*?)<\/script>/', $html, $json_matches, PREG_SET_ORDER);
preg_match_all('/<script type=["|\']application\/ld\+json["|\'][^>]*>([\s\S]*?)<\/script>/', $html, $json_matches, PREG_SET_ORDER);
foreach($json_matches as $json_match) {
if(!$json_match || !isset($json_match[1])) { continue; }

Expand All @@ -274,7 +274,17 @@ private function parseRecipeHtml($html) {

$json = json_decode($string, true);

if(!$json || $json['@type'] !== 'Recipe') { continue; }
// Look through @graph field for recipe
if($json && isset($json['@graph']) && is_array($json['@graph'])) {
foreach($json['@graph'] as $graph_item) {
if(!isset($graph_item['@type']) || $graph_item['@type'] !== 'Recipe') { continue; }

$json = $graph_item;
break;
}
}

if(!$json || !isset($json['@type']) || $json['@type'] !== 'Recipe') { continue; }

return $this->checkRecipe($json);
}
Expand Down Expand Up @@ -426,7 +436,7 @@ public function addRecipe($json) {

$file->putContent(json_encode($json));

$this->db->indexRecipeFile($file);
$this->db->indexRecipeFile($file, $this->userId);

$cache_folder = $this->getFolderForCache($file->getId());

Expand Down Expand Up @@ -493,10 +503,10 @@ public function rebuildSearchIndex() {
$cache_folder = $this->getFolderForCache();
$cache_folder->delete();

$this->db->emptySearchIndex();
$this->db->emptySearchIndex($this->userId);

foreach($this->getRecipeFiles() as $file) {
$this->db->indexRecipeFile($file);
$this->db->indexRecipeFile($file, $this->userId);
}
}

Expand All @@ -506,7 +516,7 @@ public function rebuildSearchIndex() {
* @return array
*/
public function getAllKeywordsInSearchIndex() {
return $this->db->findAllKeywords();
return $this->db->findAllKeywords($this->userId);
}

/**
Expand All @@ -515,7 +525,7 @@ public function getAllKeywordsInSearchIndex() {
* @return array
*/
public function getAllRecipesInSearchIndex() {
return $this->db->findAllRecipes();
return $this->db->findAllRecipes($this->userId);
}

/**
Expand All @@ -534,7 +544,7 @@ public function findRecipesInSearchIndex($keywords_string) {
$keywords_array = $keywords_array[0];
}

return $this->db->findRecipes($keywords_array);
return $this->db->findRecipes($keywords_array, $this->userId);
}

/**
Expand Down

0 comments on commit 3d2fe58

Please sign in to comment.