1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of dcRevisions, a plugin for Dotclear. |
---|
4 | # |
---|
5 | # Copyright (c) 2011 Tomtom and contributors |
---|
6 | # http://blog.zenstyle.fr/ |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | class dcRevisionsRestMethods |
---|
14 | { |
---|
15 | public static function getPatch() |
---|
16 | { |
---|
17 | global $core; |
---|
18 | |
---|
19 | $pid = isset($_GET['pid']) ? $_GET['pid'] : null; |
---|
20 | $rid = isset($_GET['rid']) ? $_GET['rid'] : null; |
---|
21 | |
---|
22 | if ($pid === null) { |
---|
23 | throw new Exception(__('No post ID')); |
---|
24 | } |
---|
25 | if ($rid === null) { |
---|
26 | throw new Exception(__('No revision ID')); |
---|
27 | } |
---|
28 | |
---|
29 | $p = $core->blog->getPosts(array('post_id' => $pid)); |
---|
30 | $o = array( |
---|
31 | 'post_excerpt_xhtml' => $p->post_excerpt_xhtml, |
---|
32 | 'post_content_xhtml' => $p->post_content_xhtml |
---|
33 | ); |
---|
34 | |
---|
35 | $n = $core->blog->revisions->getPatch($pid,$rid); |
---|
36 | unset($n['post_excerpt']); |
---|
37 | unset($n['post_content']); |
---|
38 | |
---|
39 | $rsp = new xmlTag(); |
---|
40 | |
---|
41 | foreach ($o as $k => $v) { |
---|
42 | $rsp->insertNode(self::buildNode($v,$n[$k],2,$k)); |
---|
43 | } |
---|
44 | |
---|
45 | return $rsp; |
---|
46 | } |
---|
47 | |
---|
48 | public static function buildNode($src,$dst,$ctx,$root) |
---|
49 | { |
---|
50 | $udiff = diff::uniDiff($src,$dst,$ctx); |
---|
51 | $tdiff = new tidyDiff(htmlspecialchars($udiff),true); |
---|
52 | |
---|
53 | $rev = new xmlTag($root); |
---|
54 | |
---|
55 | foreach ($tdiff->getChunks() as $k => $chunk) { |
---|
56 | foreach ($chunk->getLines() as $line) { |
---|
57 | switch ($line->type) { |
---|
58 | case 'context': |
---|
59 | $node = new xmlTag('context'); |
---|
60 | $node->oline = $line->lines[0]; |
---|
61 | $node->nline = $line->lines[1]; |
---|
62 | $node->insertNode($line->content); |
---|
63 | $rev->insertNode($node); |
---|
64 | break; |
---|
65 | case 'delete': |
---|
66 | $node = new xmlTag('delete'); |
---|
67 | $node->oline = $line->lines[0]; |
---|
68 | $c = str_replace(array('\0','\1'),array('<del>','</del>'),$line->content); |
---|
69 | $node->insertNode($c); |
---|
70 | $rev->insertNode($node); |
---|
71 | break; |
---|
72 | case 'insert': |
---|
73 | $node = new xmlTag('insert'); |
---|
74 | $node->nline = $line->lines[1]; |
---|
75 | $c = str_replace(array('\0','\1'),array('<ins>','</ins>'),$line->content); |
---|
76 | $node->insertNode($c); |
---|
77 | $rev->insertNode($node); |
---|
78 | break; |
---|
79 | } |
---|
80 | } |
---|
81 | if ($k < count($tdiff->getChunks()) - 1) { |
---|
82 | $node = new xmlTag('skip'); |
---|
83 | $rev->insertNode($node); |
---|
84 | } |
---|
85 | } |
---|
86 | return $rev; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | ?> |
---|