1<?php
2
3class helper_plugin_recommend_assignment
4{
5    public static $confFile = DOKU_CONF . 'recommend_snippets.json';
6
7    public static function getAssignments()
8    {
9        return @jsonToArray(self::$confFile);
10    }
11
12    public function addAssignment($assignment)
13    {
14        $assignments = self::getAssignments();
15        $assignments[] = $assignment;
16        return (bool)file_put_contents(self::$confFile, json_encode($assignments, JSON_PRETTY_PRINT));
17    }
18
19    public function removeAssignment($assignment)
20    {
21        if (empty($assignment['pattern'])) {
22            return false;
23        }
24
25        $assignments = self::getAssignments();
26        $remaining = array_filter($assignments, function($data) use ($assignment) {
27            return !(
28                $assignment['pattern'] === $data['pattern']
29                && $assignment['user'] === $data['user']
30                && $assignment['subject'] === $data['subject']
31                && $assignment['message'] === $data['message']
32            );
33        });
34
35        if (count($remaining) < count($assignments)) {
36            return (bool)file_put_contents(self::$confFile, json_encode($remaining, JSON_PRETTY_PRINT));
37        }
38        return false;
39    }
40
41    /**
42     * Returns the last matching template.
43     *
44     * @return array
45     */
46    public function loadMatchingTemplate()
47    {
48        $assignments = self::getAssignments();
49        $hlp = $this;
50        $matches = array_filter($assignments, function ($data) use ($hlp) {
51            return $hlp::matchPagePattern($data['pattern']);
52        });
53
54        $template = array_pop($matches);
55        return $template;
56    }
57
58    /**
59     * Check if the given pattern matches the given page
60     *
61     * @param string $pattern the pattern to check against
62     * @param string|null $page the cleaned pageid to check
63     * @param string|null $pns optimization, the colon wrapped namespace of the page, set null for automatic
64     * @return bool
65     * @author Andreas Gohr
66     *
67     */
68    public static function matchPagePattern($pattern, $page = null, $pns = null)
69    {
70        if (is_null($page)) $page = getID();
71
72        if (trim($pattern, ':') == '**') {
73            return true;
74        } // match all
75
76        // regex patterns
77        if ($pattern[0] == '/') {
78            return (bool) preg_match($pattern, ":$page");
79        }
80
81        if (is_null($pns)) {
82            $pns = ':' . getNS($page) . ':';
83        }
84
85        $ans = ':' . cleanID($pattern) . ':';
86        if (substr($pattern, -2) == '**') {
87            // upper namespaces match
88            if (strpos($pns, $ans) === 0) {
89                return true;
90            }
91        } elseif (substr($pattern, -1) == '*') {
92            // namespaces match exact
93            if ($ans == $pns) {
94                return true;
95            }
96        } else {
97            // exact match
98            if (cleanID($pattern) == $page) {
99                return true;
100            }
101        }
102
103        return false;
104    }
105}
106