Skip to content

Commit

Permalink
[#512] Frequently written entities can now be ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
JanVoracek committed Jan 12, 2016
1 parent d6bf065 commit 3cd4d25
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 14 deletions.
63 changes: 63 additions & 0 deletions plugins/versionpress/src/Database/EntityInfo.php
Expand Up @@ -129,6 +129,8 @@ class EntityInfo {

private $virtualReferences = array();

private $frequentlyWritten = array();

/**
* Does the parsing and sets all properties
*
Expand Down Expand Up @@ -196,9 +198,70 @@ public function __construct($entitySchema) {
}
$this->hasReferences = true;
}

if (isset($schemaInfo['frequently-written'])) {
$this->frequentlyWritten = $schemaInfo['frequently-written'];
}
}

public function isVirtualReference($reference) {
return isset($this->virtualReferences[$reference]);
}

public function isFrequentlyWrittenEntity($entity) {
$rules = $this->getRulesForFrequentlyWrittenEntities();

foreach ($rules as $rule) {
foreach ($rule as $field => $value) { // check all parts of rule
if (!isset($entity[$field]) || $entity[$field] != $value) {
continue;
}
return true;
}
}
return false;
}

public function getRulesForFrequentlyWrittenEntities() {
// https://regex101.com/r/wT6zG3/4 (query language)
$re = "/(-)?(?:(\\S+):\\s*)?(?:'((?:[^'\\\\]|\\\\.)*)'|\"((?:[^\"\\\\]|\\\\.)*)\"|(\\S+))/";
$rules = array();

foreach ($this->frequentlyWritten as $rule) {
preg_match_all($re, $rule, $matches);
$isValidRule = count($matches[0]) > 0;
if (!$isValidRule) {
continue;
}

$keys = $matches[2];

/* value can be in 3rd, 4th or 5th group
*
* 3rd group => value is in single quotes
* 4th group => value is in double quotes
* 5th group => value is without quotes
*
*/
$possibleValues[] = $matches[3];
$possibleValues[] = $matches[4];
$possibleValues[] = $matches[5];

// we need to join all groups together
$values = array();
foreach ($possibleValues as $possibleValue) {
foreach ($possibleValue as $index => $value) {
if ($value !== '') {
$values[$index] = $value;
}
}
}

ksort($values);
$rules[] = array_combine($keys, $values);

}

return $rules;
}
}
2 changes: 2 additions & 0 deletions plugins/versionpress/src/Database/wordpress-schema.neon
Expand Up @@ -61,3 +61,5 @@ option:
value-references:
option_name@option_value:
site_icon: post
frequently-written:
- 'option_name: akismet_spam_count'
8 changes: 1 addition & 7 deletions plugins/versionpress/src/Storages/DirectoryStorage.php
Expand Up @@ -27,12 +27,10 @@ abstract class DirectoryStorage extends Storage {
/** @var string */
private $directory;

/** @var EntityInfo */
private $entityInfo;

private $uncommittedEntities = array();

public function __construct($directory, $entityInfo) {
parent::__construct($entityInfo);
$this->directory = $directory;
$this->entityInfo = $entityInfo;
}
Expand Down Expand Up @@ -118,10 +116,6 @@ public function loadAll() {
return $entities;
}

public function shouldBeSaved($data) {
return true;
}

public function prepareStorage() {
FileSystem::mkdir($this->directory);
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/versionpress/src/Storages/MetaEntityStorage.php
Expand Up @@ -27,6 +27,7 @@ abstract class MetaEntityStorage extends Storage {
private $parentStorage;

function __construct(Storage $parentStorage, EntityInfo $entityInfo, $keyName, $valueName) {
parent::__construct($entityInfo);
$this->parentStorage = $parentStorage;
$this->keyName = $keyName;
$this->valueName = $valueName;
Expand Down Expand Up @@ -258,7 +259,7 @@ private function extractEntityFromParent($parentEntity, $joinedKey) {
}

function shouldBeSaved($data) {
return isset($data[$this->parentReferenceName]) && $this->parentStorage->exists($data[$this->parentReferenceName], null);
return parent::shouldBeSaved($data) && isset($data[$this->parentReferenceName]) && $this->parentStorage->exists($data[$this->parentReferenceName], null);
}

function prepareStorage() {
Expand Down
5 changes: 1 addition & 4 deletions plugins/versionpress/src/Storages/OptionStorage.php
Expand Up @@ -22,16 +22,13 @@ class OptionStorage extends DirectoryStorage {
'auto_core_update_notified',
);

/** @var EntityInfo */
private $entityInfo;
/** @var string */
private $tablePrefix;
/** @var string[] */
private $taxonomies;

public function __construct($directory, $entityInfo, $tablePrefix, $taxonomies) {
parent::__construct($directory, $entityInfo);
$this->entityInfo = $entityInfo;
$this->tablePrefix = $tablePrefix;
$this->taxonomies = $taxonomies;
}
Expand Down Expand Up @@ -61,7 +58,7 @@ protected function deserializeEntity($serializedEntity) {

public function shouldBeSaved($data) {
$id = $data[$this->entityInfo->idColumnName];
return !($this->isTransientOption($id) || $this->isTaxonomyChildren($id) || in_array($id, self::$optionsBlacklist));
return parent::shouldBeSaved($data) && !($this->isTransientOption($id) || $this->isTaxonomyChildren($id) || in_array($id, self::$optionsBlacklist));
}

private function isTransientOption($id) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/versionpress/src/Storages/PostStorage.php
Expand Up @@ -36,7 +36,7 @@ public function shouldBeSaved($data) {
if (!$isExistingEntity && isset($data['post_type']) && $data['post_type'] === 'attachment' && !isset($data['post_title']))
return false;

return true;
return parent::shouldBeSaved($data);
}

protected function removeUnwantedColumns($entity) {
Expand Down
18 changes: 17 additions & 1 deletion plugins/versionpress/src/Storages/Storage.php
Expand Up @@ -14,6 +14,16 @@ abstract class Storage {
/** @var bool */
protected $isTransaction;

/** @var bool */
public $ignoreFrequentlyWrittenEntities = true;

/** @var EntityInfo */
protected $entityInfo;

public function __construct(EntityInfo $entityInfo) {
$this->entityInfo = $entityInfo;
}

/**
* Saves data to a storage
*
Expand Down Expand Up @@ -58,7 +68,13 @@ public abstract function loadAll();
* @param array $data
* @return bool
*/
public abstract function shouldBeSaved($data);
public function shouldBeSaved($data) {
if (!$this->ignoreFrequentlyWrittenEntities) {
return true;
}

return !$this->entityInfo->isFrequentlyWrittenEntity($data);
}

/**
* Called from {@link VersionPress\Initialization\Initializer} to give storage a chance to prepare itself.
Expand Down

0 comments on commit 3cd4d25

Please sign in to comment.