1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
<?php
require_once 'bedita_base.php';
App::import('Component', 'Transaction');
class RelationShell extends BeditaBaseShell {
public function ls() {
$relationStats = ClassRegistry::init('RelationStats');
$this->out($relationStats->getRelationNames());
}
public function check() {
if (!isset($this->params['name']) && !isset($this->params['all'])) {
$this->out('Missing param(s)');
$this->out(' Usage: check -name <relation-name>');
$this->out(' Usage: check -all');
exit;
}
$relationStats = ClassRegistry::init('RelationStats');
$relationCheck = ClassRegistry::init('RelationCheck');
$relationRepair = ClassRegistry::init('RelationRepair');
$relationNames = (isset($this->params['name'])) ? array($this->params['name']) : $relationStats->getRelationNames();
foreach ($relationNames as $relationName) {
$this->out("-----------------------------------------------------------------");
$this->in("Relation $relationName (press enter to continue)");
$relationData = $relationStats->getRelation($relationName);
if ($relationData == null) {
$this->out('Relation "' . $relationName . '" not found');
exit;
}
$this->out($relationStats->getDescription($relationName));
$this->out("-----------------------------------------------------------------");
$messages = array();
$result = $relationCheck->checkRelation($relationName, $messages);
$this->outMessages($messages);
if ($result === false) {
if (!empty($relationData['inverse'])) {
$countAllTypesL = $relationStats->getObjectRelationsCount($relationName);
$countAllTypesR = $relationStats->getObjectRelationsCount($relationData['inverse']);
if ($countAllTypesL != $countAllTypesR) {
$ans = $this->in('Do you want to try to fix relation data? [y/n]');
if ($ans === 'y') {
$repaired = $relationRepair->repair($relationName);
$this->out($repaired . ' ObjectRelation records repaired');
}
}
}
}
$this->out("-----------------------------------------------------------------");
}
}
public function fix() {
if (!isset($this->params['name'])) {
$this->out('Missing param(s)');
$this->out(' Usage: fix -name <relation-name>');
exit;
}
$relationName = $this->params['name'];
$relationStats = ClassRegistry::init('RelationStats');
$relationData = $relationStats->getRelation($relationName);
if (empty($relationData)) {
$this->out('Relation "' . $relationName . '" not found');
exit;
}
$this->out($relationStats->getDescription($relationName));
$transaction = new TransactionComponent('default');
$repaired = 0;
try {
$transaction->begin();
$repaired = ClassRegistry::init('RelationRepair')->repair($relationName);
$transaction->commit();
} catch(Exception $e) {
$transaction->rollback();
}
$this->out($repaired . ' ObjectRelation records repaired');
}
public function help() {
$this->out('Available functions:');
$this->out(' ');
$this->out('0. ls: relations list');
$this->out(' Usage: ls');
$this->out(' ');
$this->out('1. check: relations data check');
$this->out(' Usage: check -name <relation-name>');
$this->out(' Usage: check -all');
$this->out(' ');
$this->out(" -name <relation-name> \t name of relation you want to check");
$this->out(" -all \t all relations");
$this->out(' ');
$this->out('2. fix: relations data fix');
$this->out(' Usage: fix -name <relation-name>');
$this->out(' ');
$this->out(" -name <relation-name> \t name of relation you want to fix");
$this->out(' ');
}
private function outMessages($messages) {
foreach ($messages as $message) {
$this->out($message);
}
}
}
?>