1<?php
2
3use dokuwiki\Extension\Plugin;
4
5/**
6 * DokuWiki Plugin structpublish (Helper Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Anna Dabrowska <dokuwiki@cosmocode.de>
10 */
11class helper_plugin_structpublish_assignments extends Plugin
12{
13    /**
14     * Check if the given pattern matches the given page
15     *
16     * @param string $pattern the pattern to check against
17     * @param string $page the cleaned pageid to check
18     * @param string|null $pns optimization, the colon wrapped namespace of the page, set null for automatic
19     * @return bool
20     * @author Andreas Gohr
21     */
22    public function matchPagePattern($pattern, $page, $pns = null)
23    {
24        if (trim($pattern, ':') == '**') {
25            return true;
26        } // match all
27
28        // regex patterns
29        if ($pattern[0] == '/') {
30            return (bool) preg_match($pattern, ":$page");
31        }
32
33        if (is_null($pns)) {
34            $pns = ':' . getNS($page) . ':';
35        }
36
37        $ans = ':' . cleanID($pattern) . ':';
38        if (substr($pattern, -2) == '**') {
39            // upper namespaces match
40            if (strpos($pns, $ans) === 0) {
41                return true;
42            }
43        } elseif (substr($pattern, -1) == '*') {
44            // namespaces match exact
45            if ($ans == $pns) {
46                return true;
47            }
48        } elseif (cleanID($pattern) == $page) {
49            // exact match
50            return true;
51        }
52
53        return false;
54    }
55}
56