1<?php
2
3namespace dokuwiki\plugin\farmsync\meta;
4
5
6class MediaUpdates extends EntityUpdates {
7
8    public function updateEntity($medium, $source, $target) {
9
10        $sourceModTime = $this->farm_util->getRemoteFilemtime($source, $medium, true);
11
12        $result = new UpdateResults($medium, $target);
13        if (!$this->farm_util->remoteMediaExists($target, $medium)) {
14            $this->farm_util->saveRemoteMedia($source, $target, $medium);
15            $result->setMergeResult('new file');
16            $this->results[$target]['passed'][] = $result;
17            return;
18        }
19        $targetModTime = $this->farm_util->getRemoteFilemtime($target, $medium, true);
20        if ($targetModTime == $sourceModTime && $this->farm_util->readRemoteMedia($source, $medium) == $this->farm_util->readRemoteMedia($target, $medium)) {
21            $result->setMergeResult('unchanged');
22            $this->results[$target]['passed'][] = $result;
23            return;
24        }
25        if ($this->farm_util->remoteMediaExists($source, $medium, $targetModTime) && $this->farm_util->readRemoteMedia($source, $medium, $targetModTime) == $this->farm_util->readRemoteMedia($target, $medium)) {
26            $this->farm_util->saveRemoteMedia($source, $target, $medium);
27            $result->setMergeResult('file overwritten');
28            $this->results[$target]['passed'][] = $result;
29            return;
30        }
31        if ($this->farm_util->remoteMediaExists($target, $medium, $sourceModTime) && $this->farm_util->readRemoteMedia($source, $medium) == $this->farm_util->readRemoteMedia($target, $medium, $sourceModTime)) {
32            $result->setMergeResult('unchanged');
33            $this->results[$target]['passed'][] = $result;
34            return;
35        }
36        $result = new MediaConflict($medium, $target, $source);
37        $result->setMergeResult('merged with conflicts');
38        $this->results[$target]['failed'][] = $result;
39    }
40
41    protected function preProcessEntities($medialines) {
42        $media = array();
43        foreach ($medialines as $line) {
44            $media = array_merge($media, $this->getDocumentsFromLine($this->source, $line));
45        }
46        array_unique($media);
47        return $media;
48    }
49
50
51    function printProgressLine($target, $i, $total) {
52        echo sprintf($this->getLang('progress:media'), $target, $i, $total) . "</br>";
53    }
54
55    protected function printResultHeading() {
56        echo "<h3>".$this->getLang('heading:media')."</h3>";
57    }
58
59    /**
60     * @param string $source
61     * @param string $line
62     *
63     * @return \string[]
64     *
65     */
66    public function getDocumentsFromLine($source, $line) {
67        if (trim($line) == '') return array();
68        $cleanline = str_replace('/', ':', $line);
69        $namespace = join(':', explode(':', $cleanline, -1));
70        $documentdir = dirname($this->farm_util->getRemoteMediaFilename($source, $cleanline, 0, false));
71
72        $search_algo = 'search_media';
73        $documents = array();
74
75        if (substr($cleanline, -3) == ':**') {
76            $nsfiles = array();
77            search($nsfiles, $documentdir, $search_algo, array());
78            $documents = array_map(function ($elem) use ($namespace) {
79                return $namespace . ':' . $elem['id'];
80            }, $nsfiles);
81        } elseif (substr($cleanline, -2) == ':*') {
82            $nsfiles = array();
83            search($nsfiles, $documentdir, $search_algo, array('depth' => 1));
84            $documents = array_map(function ($elem) use ($namespace) {
85                return $namespace . ':' . $elem['id'];
86            }, $nsfiles);
87        } else {
88            $document = $cleanline;
89            if (!$this->farm_util->remoteMediaExists($source, $document) || is_dir(mediaFN($document))) {
90                msg("Media-file $document does not exist in source wiki!", -1);
91                return array();
92            }
93            $documents[] = cleanID($document);
94        }
95        return $documents;
96    }
97}
98