Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ public function getRules() : array {
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true
];
return array_merge(['@PSR12' => true], $parentRules, $additionalRules);
$ret = array_merge(['@PSR12' => true], $parentRules, $additionalRules);
// print_r($ret);
return $ret;
}
}

$config = new CookbookConfig();
$config
$finder = $config
->getFinder()
->ignoreVCSIgnored(true)
// ->ignoreVCSIgnored(true)
->exclude('build')
->exclude('l10n')
// ->notPath('lib/Vendor')
->exclude('src')
->exclude('node_modules')
->exclude('vendor')
->exclude('.github')
->in(__DIR__);
->in(__DIR__ );

$config->setFinder($finder);

// foreach($finder as $f) {
// print_r($f);
// }

// print_r($config);

return $config;

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

### Fixed
- Prevent slow loading of recipes due to iteration over all files
[#1072](https://github.com/nextcloud/cookbook/pull/1072) @christianlupus


## 0.9.13 - 2022-07-02

### Added
Expand Down
12 changes: 10 additions & 2 deletions cookbook.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@
"**/vendor/**/{Tests,tests}/**",
"**/.history/**",
"**/vendor/**/vendor/**",
"3rdparty/**"
// "3rdparty/**"
],
"cSpell.words": [
"Nextcloud"
],
"path-intellisense.mappings": {
"cookbook": "${workspaceFolder}/src",
}
},
"editor.quickSuggestions": {
"other": "on",
"comments": "on",
"strings": "on"
},
"intelephense.environment.includePaths": [
"${workspaceFolder:base}/3rdparty/doctrine/dbal/src"
]
}
}
12 changes: 12 additions & 0 deletions lib/Db/DbTypesPolyfillHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ class DbTypesPolyfillHelper {
*/
private $string;

/** @var string */
private $date;

public function __construct(Util $util) {
switch ($util->getVersion()[0]) {
case 18:
case 19:
case 20:
$this->int = \Doctrine\DBAL\Types\Type::INTEGER;
$this->string = \Doctrine\DBAL\Types\Type::STRING;
$this->date = \Doctrine\DBAL\Types\Type::DATE;
break;

default:
$this->int = \OCP\DB\Types::INTEGER;
$this->string = \OCP\DB\Types::STRING;
$this->date = \OCP\DB\Types::DATE;
break;
}
}
Expand All @@ -43,4 +48,11 @@ final public function INT() {
final public function STRING() {
return $this->string;
}

/**
* @return string
*/
final public function DATE() {
return $this->date;
}
}
95 changes: 75 additions & 20 deletions lib/Db/RecipeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\Cookbook\Db;

use DateTimeImmutable;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -58,6 +59,8 @@ public function findRecipeById(int $id) {
$ret = [];
$ret['name'] = $row['name'];
$ret['id'] = $row['recipe_id'];
$ret['dateCreated'] = $row['date_created'];
$ret['dateModified'] = $row['date_modified'];

return $ret;
}
Expand Down Expand Up @@ -91,7 +94,7 @@ public function deleteRecipeById(int $id) {
public function findAllRecipes(string $user_id) {
$qb = $this->db->getQueryBuilder();

$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.date_created', 'r.date_modified', 'k.name AS keywords', 'c.name AS category'])
->from(self::DB_TABLE_RECIPES, 'r')
->where('r.user_id = :user')
->orderBy('r.name');
Expand All @@ -102,11 +105,19 @@ public function findAllRecipes(string $user_id) {
'k.user_id = :user'
)
);
$qb->leftJoin('r', self::DB_TABLE_CATEGORIES, 'c',
$qb->expr()->andX(
'c.recipe_id = r.recipe_id',
'c.user_id = r.user_id'
)
);

$cursor = $qb->execute();
$result = $cursor->fetchAll();
$cursor->closeCursor();

$result = $this->mapDbNames($result);

$result = $this->sortRecipes($result);

// group recipes, convert keywords to comma-separated list
Expand All @@ -115,6 +126,17 @@ public function findAllRecipes(string $user_id) {
return $this->unique($recipesGroupedTags);
}

private function mapDbNames($results) {
return array_map(function ($x) {
$x['dateCreated'] = $x['date_created'];
$x['dateModified'] = $x['date_modified'];
unset($x['date_created']);
unset($x['date_modified']);

return $x;
}, $results);
}

public function unique(array $result) {
// NOTE: This post processing shouldn't be necessary
// When sharing recipes with other users, they are occasionally returned twice
Expand Down Expand Up @@ -197,7 +219,7 @@ public function findAllCategories(string $user_id) {
)
)
->where(
$qb->expr()->eq('r.user_id', $qb->expr()->literal($user_id)),
$qb->expr()->eq('r.user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)),
$qb->expr()->isNull('c.name')
);

Expand Down Expand Up @@ -229,7 +251,7 @@ public function getRecipesByCategory(string $category, string $user_id) {
// for the recipe, but those don't seem to work:
// $qb->select(['r.recipe_id', 'r.name', 'GROUP_CONCAT(k.name) AS keywords']) // not working
// $qb->select(['r.recipe_id', 'r.name', DB::raw('GROUP_CONCAT(k.name) AS keywords')]) // not working
$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.date_created', 'r.date_modified', 'k.name AS keywords'])
->from(self::DB_TABLE_CATEGORIES, 'c')
->where('c.name = :category')
->andWhere('c.user_id = :user')
Expand All @@ -242,7 +264,7 @@ public function getRecipesByCategory(string $category, string $user_id) {
$qb->groupBy(['r.name', 'r.recipe_id', 'k.name']);
$qb->orderBy('r.name');
} else {
$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.date_created', 'r.date_modified', 'k.name AS keywords'])
->from(self::DB_TABLE_RECIPES, 'r')
->leftJoin('r', self::DB_TABLE_KEYWORDS, 'k', 'r.recipe_id = k.recipe_id')
->leftJoin(
Expand All @@ -255,7 +277,7 @@ public function getRecipesByCategory(string $category, string $user_id) {
)
)
->where(
$qb->expr()->eq('r.user_id', $qb->expr()->literal($user_id)),
$qb->expr()->eq('r.user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)),
$qb->expr()->isNull('c.name')
);
}
Expand All @@ -264,6 +286,8 @@ public function getRecipesByCategory(string $category, string $user_id) {
$result = $cursor->fetchAll();
$cursor->closeCursor();

$result = $this->mapDbNames($result);

// group recipes, convert keywords to comma-separated list
$recipesGroupedTags = $this->groupKeywordInResult($result);

Expand All @@ -279,7 +303,7 @@ public function getRecipesByKeywords(string $keywords, string $user_id) {

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

$qb->select(['r.recipe_id', 'r.name', 'kk.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.date_created', 'r.date_modified', 'kk.name AS keywords'])
->from(self::DB_TABLE_KEYWORDS, 'k')
->where('k.name IN (:keywords)')
->andWhere('k.user_id = :user')
Expand All @@ -296,6 +320,8 @@ public function getRecipesByKeywords(string $keywords, string $user_id) {
$result = $cursor->fetchAll();
$cursor->closeCursor();

$result = $this->mapDbNames($result);

// group recipes, convert keywords to comma-separated list
$recipesGroupedTags = $this->groupKeywordInResult($result);

Expand All @@ -314,7 +340,7 @@ public function findRecipes(array $keywords, string $user_id) {

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

$qb->select(['r.recipe_id', 'r.name', 'k.name AS keywords'])
$qb->select(['r.recipe_id', 'r.name', 'r.date_created', 'r.date_modified', 'k.name AS keywords', 'c.name AS category'])
->from(self::DB_TABLE_RECIPES, 'r');

$qb->leftJoin('r', self::DB_TABLE_KEYWORDS, 'k', 'k.recipe_id = r.recipe_id');
Expand Down Expand Up @@ -348,6 +374,8 @@ public function findRecipes(array $keywords, string $user_id) {
$result = $cursor->fetchAll();
$cursor->closeCursor();

$result = $this->mapDbNames($result);

// group recipes, convert keywords to comma-separated list
$recipesGroupedTags = $this->groupKeywordInResult($result);

Expand Down Expand Up @@ -432,8 +460,8 @@ public function deleteRecipes(array $ids, string $userId) {
foreach ($ids as $id) {
$qb->orWhere(
$qb->expr()->andX(
"recipe_id = $id",
$qb->expr()->eq("user_id", $qb->expr()->literal($userId))
$qb->expr()->eq('recipe_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)),
$qb->expr()->eq("user_id", $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
));
}

Expand All @@ -454,7 +482,9 @@ public function insertRecipes(array $recipes, string $userId) {
->values([
'recipe_id' => ':id',
'user_id' => ':userid',
'name' => ':name'
'name' => ':name',
'date_created' => ':dateCreated',
'date_modified' => ':dateModified',
]);

$qb->setParameter('userid', $userId);
Expand All @@ -463,6 +493,12 @@ public function insertRecipes(array $recipes, string $userId) {
$qb->setParameter('id', $recipe['id'], $this->types->INT());
$qb->setParameter('name', $recipe['name'], $this->types->STRING());

$dateCreated = $this->parseDate($recipe['dateCreated']);
$qb->setParameter('dateCreated', $dateCreated, $this->types->DATE());

$dateModified = $this->parseDate($recipe['dateModified']);
$qb->setParameter('dateModified', $dateModified, $this->types->DATE());

$qb->execute();
}
}
Expand All @@ -472,20 +508,27 @@ public function updateRecipes(array $recipes, string $userId) {
return;
}

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

foreach ($recipes as $recipe) {
$qb = $this->db->getQueryBuilder();
$qb->update(self::DB_TABLE_RECIPES)
->where('recipe_id = :id', 'user_id = :uid');

$literal = [];
$literal['name'] = $qb->expr()->literal($recipe['name'], IQueryBuilder::PARAM_STR);
$qb->set('name', $literal['name']);
$qb->set('name', $qb->createNamedParameter($recipe['name'], IQueryBuilder::PARAM_STR));

$dateCreated = $this->parseDate($recipe['dateCreated']);
$dateModified = $this->parseDate($recipe['dateModified']);

$qb->set('date_created', $qb->createNamedParameter($dateCreated, IQueryBuilder::PARAM_DATE));
$qb->set('date_modified', $qb->createNamedParameter($dateModified, IQueryBuilder::PARAM_DATE));

$qb->setParameter('id', $recipe['id']);
$qb->setParameter('uid', $userId);

$qb->execute();
try {
$qb->execute();
} catch (\Exception $ex) {
throw $ex;
}
}
}

Expand Down Expand Up @@ -536,7 +579,7 @@ public function updateCategoryOfRecipe(int $recipeId, string $categoryName, stri
$qb = $this->db->getQueryBuilder();
$qb->update(self::DB_TABLE_CATEGORIES)
->where('recipe_id = :rid', 'user_id = :user');
$qb->set('name', $qb->expr()->literal($categoryName, IQueryBuilder::PARAM_STR));
$qb->set('name', $qb->createNamedParameter($categoryName, IQueryBuilder::PARAM_STR));
$qb->setParameter('rid', $recipeId, $this->types->INT());
$qb->setParameter('user', $userId, $this->types->STRING());
$qb->execute();
Expand Down Expand Up @@ -608,13 +651,25 @@ public function removeKeywordPairs(array $pairs, string $userId) {
foreach ($pairs as $p) {
$qb->orWhere(
$qb->expr()->andX(
$qb->expr()->eq('user_id', $qb->expr()->literal($userId)),
$qb->expr()->eq('recipe_id', $qb->expr()->literal($p['recipeId'])),
$qb->expr()->eq('name', $qb->expr()->literal($p['name']))
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->eq('recipe_id', $qb->createNamedParameter($p['recipeId'], IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('name', $qb->createNamedParameter($p['name'], IQueryBuilder::PARAM_STR))
)
);
}

$qb->execute();
}

private function parseDate(?string $date) {
if (is_null($date)) {
return null;
}

try {
return new DateTimeImmutable($date);
} catch (\Exception $ex) {
return new DateTimeImmutable();
}
}
}
26 changes: 26 additions & 0 deletions lib/Helper/Filter/AbstractRecipeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace OCA\Cookbook\Helper\Filter;

use OCA\Cookbook\Exception\InvalidRecipeException;
use OCP\Files\File;

/**
* An abstract filter on a recipe.
*
* A filter should have a single purpose that is serves and implement this interface
*/
interface AbstractRecipeFilter {
/**
* Filter the given recipe according to the filter class specification.
*
* This function can make changes to the recipe array to carry out the needed changes.
* In order to signal if the JSON file needs updating, the return value must be true if and only if the recipe was changed.
*
* @param array $json The recipe data as array
* @param File $recipe The file containing the recipe for further processing
* @return bool true, if and only if the recipe was changed
* @throws InvalidRecipeException if the recipe was not correctly filtered
*/
public function apply(array &$json, File $recipe): bool;
}
Loading