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
<?php
class RelationCheck extends BEAppModel
{
public $useTable = false;
public function checkRelation($relationName, &$messageBuffer) {
$result = true;
$relationStats = ClassRegistry::init('RelationStats');
$relationData = $relationStats->getRelation($relationName);
if (empty($relationData)) {
throw new BEditaException('Relation data not found for relation ' . $relationName);
}
$leftObjectTypes = $relationStats->objectTypesIdsForObjectNames($relationData,'left');
$rightObjectTypes = $relationStats->objectTypesIdsForObjectNames($relationData,'right');
$countAllowedTypesL = $relationStats->getObjectRelationsCount($relationName, $leftObjectTypes);
$countAllTypesL = $relationStats->getObjectRelationsCount($relationName);
$results[] = $this->checkTypesForRelation($relationName, $countAllowedTypesL, $countAllTypesL, $leftObjectTypes, $messageBuffer);
if (!empty($relationData['inverse']) && ($relationData['inverse'] != $relationName) ) {
$relationInverse = $relationData['inverse'];
$countAllowedTypesR = $relationStats->getObjectRelationsCount($relationInverse, $rightObjectTypes);
$countAllTypesR = $relationStats->getObjectRelationsCount($relationInverse);
$results[] = $this->checkTypesForRelation($relationInverse, $countAllowedTypesR, $countAllTypesR, $rightObjectTypes, $messageBuffer);
$results[] = $this->checkCounts('allowed types', $relationName, $relationInverse, $countAllowedTypesL, $countAllowedTypesR, $messageBuffer);
$results[] = $this->checkCounts('all types', $relationName, $relationInverse, $countAllTypesL, $countAllTypesR, $messageBuffer);
} else {
$messageBuffer[] = 'No inverse: no checks';
}
foreach ($results as $r) {
if ($r === false) {
$result = false;
}
}
$messageBuffer[] = ($result) ? 'ok' : 'corrupted data';
return $result;
}
private function checkTypesForRelation($relationName, $countRelAllowedTypes, $countRelAllTypes, $allowedTypes, &$messageBuffer) {
if ($countRelAllowedTypes != $countRelAllTypes) {
$messageBuffer[] = abs($countRelAllTypes - $countRelAllowedTypes) . ' records refer to a type not related to relation "' . $relationName . '" (MODEL-RELATED-TYPES-COUNT: ' . $countRelAllowedTypes . ' / ALL-TYPES-COUNT: ' . $countRelAllTypes . ')';
$relationStats = ClassRegistry::init('RelationStats');
$countRelationsGroupByType = $relationStats->getObjectRelationsCountGroupByType($relationName);
foreach ($countRelationsGroupByType as $objectTypeId => $data) {
if (!in_array($objectTypeId, $allowedTypes)) {
$ot = Configure::read('objectTypes.'.$objectTypeId.'.name');
$messageBuffer[] = '|=> ' . $data . ' records refer to the object type ' . $ot . ' (the object type is not related to relation ' . $relationName . ')';
}
}
}
}
private function checkCounts($info, $relationName, $relationInverse, $countRelation, $countInverse, &$messageBuffer) {
if ($countRelation === $countInverse) {
$messageBuffer[] = "$info [left]->'$relationName'->[right]: $countRelation";
$messageBuffer[] = "$info [right]->'$relationInverse'->[left]: $countInverse";
return true;
} else if ($countRelation < $countInverse) {
$messageBuffer[] = "$info [left]->'$relationName'->[right]: $countRelation => should be $countInverse";
$messageBuffer[] = "$info [right]->'$relationInverse'->[left]: $countInverse";
return false;
} else {
$messageBuffer[] = "$info [left]->'$relationName'->[right]: $countRelation";
$messageBuffer[] = "$info [right]->'$relationInverse'->[left]: $countInverse => should be $countRelation";
return false;
}
return true;
}
}