Computes the difference between two texts to create a patch. Applies the patch onto another text,
allowing for errors.
This class implements the same API as all other google-diff-match-patch libs. It was created for
compatibility reason only.
public function actionDiff($document_history_id) { $model = History::findOne($document_history_id); if (is_null($model)) { throw new \yii\web\HttpException(404, Yii::t('burivuh', 'Record in the history does not exist')); } $document = Document::findOne($model->document_id); $previous = History::find()->where(['document_id' => $model->document_id])->andWhere('created_at<:created_at', [':created_at' => $model->created_at])->orderBy('created_at DESC')->limit(1)->one(); $diffs = []; if (!is_null($previous)) { $dmp = new DiffMatchPatch(); $diffs = $dmp->diff_main($previous->content, $model->content, false); } return $this->render('diff', ['document' => $document, 'model' => $model, 'previous' => $previous, 'diffs' => $diffs]); }
public function renderDiff($lastEdit) { $dmp = new DiffMatchPatch(); $diffs = $dmp->diff_main($this->content, $lastEdit->content, false); $added = 0; $deleted = 0; foreach ($diffs as $diff) { $length = mb_strlen($diff[1], 'utf-8'); if ($diff[0] == 1) { $added += $length; } if ($diff[0] == -1) { $deleted += $length; } } $this->diff = $added . ':' . $deleted; }
public static function boot() { parent::boot(); static::creating(function ($page) { $slugifier = new Slugify(); $page->setJsonAttribute('metadata', 'author', UserProvider::getCurrentUserId()); $page->setJsonAttribute('metadata', 'slug', $slugifier->slugify($page->title)); }); static::created(function ($page) { $revision = new PageRevision(); $revision->page_id = $page->id; $revision->revision_id = 0; $dmp = new DiffMatchPatch(); // Content patch $contentPatches = $dmp->patch_make("", $page->content); $revision->content_patch = $dmp->patch_toText($contentPatches); // Metadata patch $revision->metadata_patch = json_encode(JsonPatch::diff([], json_decode($page->metadata, 1))); $revision->author = UserProvider::getCurrentUserId(); $revision->created_at = Carbon::now(); $revision->save(); }); static::updating(function ($updatingPage) { // Get the page again from DB so we are sure we have up-to-date content to make the diff from $page = Page::find($updatingPage->id); $lastRevision = PageRevision::where('page_id', $page->id)->orderBy('revision_id', 'DESC')->firstOrFail(); $revision = new PageRevision(); $revision->page_id = $page->id; $revision->revision_id = $lastRevision->revision_id + 1; $dmp = new DiffMatchPatch(); // Content patch $contentPatches = $dmp->patch_make($page->getOriginal('content'), $updatingPage->content); $revision->content_patch = $dmp->patch_toText($contentPatches); // Metadata patch $revision->metadata_patch = json_encode(JsonPatch::diff(json_decode($page->metadata, 1), json_decode($updatingPage->metadata, 1))); $revision->author = UserProvider::getCurrentUserId(); $revision->created_at = Carbon::now(); $revision->save(); }); }