1<?php
2
3namespace dokuwiki\plugin\farmsync\meta;
4require_once(DOKU_INC . 'inc/DifferenceEngine.php');
5
6abstract class EntityUpdates {
7    protected $source;
8    protected $targets;
9    protected $results;
10    protected $entities;
11    public $farm_util;
12
13    public function getLang($key) {
14        $helper = plugin_load('helper', 'farmsync');
15        return $helper->getLang($key);
16    }
17
18    public function __construct($source, $targets, $entities) {
19        $this->source = $source;
20        $this->targets = $targets;
21        $this->farm_util = new FarmSyncUtil();
22        $this->entities = $this->preProcessEntities($entities);
23    }
24
25    public function updateEntities() {
26        $total = count($this->targets);
27        $i = 0;
28        foreach ($this->targets as $target) {
29            $this->results[$target]['passed'] = array();
30            $this->results[$target]['failed'] = array();
31            foreach ($this->entities as $entity) {
32                $this->updateEntity($entity, $this->source, $target);
33            }
34            $i += 1;
35            $this->doPerTargetAction($target);
36            $this->farm_util->clearAnimalCache($target);
37            $this->printProgressLine($target, $i, $total);
38        }
39    }
40
41    public function getNumberOfAnimalConflicts($animal) {
42        return count($this->results[$animal]['failed']);
43    }
44
45    public function printAnimalResultHTML($animal) {
46        $this->printResultHeading();
47
48        if (!empty($this->results[$animal]['failed'])) {
49            echo "<ul>";
50            /** @var UpdateResults $result */
51            foreach ($this->results[$animal]['failed'] as $result) {
52                echo "<li class='level1'>";
53                echo "<div class='li'>" . $result->getResultLine() . "</div>";
54                echo "</li>";
55            }
56            echo "</ul>";
57        }
58
59        if (!empty($this->results[$animal]['passed'])) {
60            echo '<a class="show_noconflicts wikilink1">' . $this->getLang('link:nocoflictitems') . '</a>';
61            echo "<ul class='noconflicts'>";
62            /** @var UpdateResults $result */
63            foreach ($this->results[$animal]['passed'] as $result) {
64                echo "<li class='level1'>";
65                echo "<div class='li'>" . $result->getResultLine() . "</div>";
66                echo "</li>";
67            }
68            echo "</ul>";
69        }
70    }
71
72    abstract protected function printResultHeading();
73
74    public function doPerTargetAction($target) {}
75
76    abstract protected function printProgressLine($target, $i, $total);
77
78    abstract protected function updateEntity($entity, $source, $target);
79
80    /**
81     * @return mixed
82     */
83    public function getResults() {
84        return $this->results;
85    }
86
87    protected function preProcessEntities($entities) {
88        return $entities;
89    }
90
91    /**
92     * @param string $page
93     * @return string
94     */
95    protected function handleStartpage($source, $page) {
96        global $conf;
97        if ($this->farm_util->remotePageExists($source, $page . $conf['start'])) {
98            $page = $page . $conf['start'];
99            return $page;
100        } elseif ($this->farm_util->remotePageExists($source, $page . noNS(cleanID($page)))) {
101            $page = $page . noNS(cleanID($page));
102            return $page;
103        } elseif ($this->farm_util->remotePageExists($source, $page)) {
104            return cleanID($page);
105        } else {
106            $page = $page . $conf['start'];
107            return $page;
108        }
109    }
110
111
112}
113