/** * @param Section $section * @param array $ids * * @return array $contributions */ public function compareContributions(Section $section, $ids) { $contributions = $this->contributionRepository->findyBySectionAndIds($section, $ids); $titleDiff = new HtmlDiff($contributions[0]->getTitle(), $contributions[1]->getTitle(), false); $textDiff = new HtmlDiff($contributions[0]->getText(), $contributions[1]->getText(), true); $contribution = new Contribution(); $contribution->setText($textDiff->outputDiff()->toString()); $contribution->setTitle($titleDiff->outputDiff()->toString()); $contribution->setContributor($contributions[1]->getContributor()); $contribution->setCreationDate($contributions[1]->getCreationDate()); $contributions[1] = $contribution; return $contributions; }
function areCachablyDifferent(Guzzle\Http\Message\Response $oldResp, Guzzle\Http\Message\Response $newResp) { if (hasMicroformats($oldResp) and hasMicroformats($newResp)) { // if mf changed, probably significantly different enough to warrant a new cache entry return mfForResponse($oldResp) != mfForResponse($newResp); } else { // look for last-updated header if ($newResp->getLastModified() != null) { $lastModified = new DateTime($newResp->getLastModified()); $lastArchived = new DateTime($oldResp->getDate()); return $lastModified > $lastArchived; } $differ = new HtmlDiff($oldResp->getBody(true), $newResp->getBody(true), true); $mods = $differ->outputDiff()->getModifications(); // to do : figure out a sensible threshold here return max($mods) > 2; } }
/** * @param Objects $repo * @param $objectKey * @param $origItem * @return string */ protected function generateDiff(Objects $repo, $objectKey, $origItem) { $pk = $repo->getObjectPk($objectKey, $origItem); $currentItem = $repo->get($objectKey, $pk); $definition = $repo->getDefinition($objectKey); $changes = []; foreach ($definition->getFields() as $field) { if ($field->hasFieldType() && !$field->getFieldType()->isDiffAllowed()) { //todo, check $field->isDiffAllowed() as well continue; } $k = $field->getId(); if (!isset($origItem[$k]) || !isset($currentItem[$k])) { continue; } if (!is_string($origItem[$k]) && !is_numeric($origItem[$k])) { continue; } if (!is_string($currentItem[$k]) && !is_numeric($currentItem[$k])) { continue; } $from = strip_tags($origItem[$k]); $to = strip_tags($currentItem[$k]); if ($from != $to) { $htmlDiff = new HtmlDiff($from, $to, true); $out = $htmlDiff->outputDiff(); $changes[$k] = $out->toString(); } } $message = []; foreach ($changes as $changedKey => $diff) { if ($field = $definition->getField($changedKey)) { $message[] = ($field->getLabel() ?: $field->getId()) . ': ' . $diff; } } return $message ? '<div class="change">' . implode('</div><div class="change">', $message) . '</div>' : ''; }