1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5/**
6 * Class InlineConfigParser
7 *
8 * Wrapper to convert inline syntax to full before instantiating ConfigParser
9 *
10 * {{$schema.field}}
11 * {{$pageid.schema.field}}
12 * {{$... ? filter: ... and: ... or: ...}} or {{$... ? & ... | ...}}
13 * TODO: {{$... ? sum}} or {{$... ? +}}
14 * TODO: {{$... ? default: ...}} or {{$... ? ! ...}}
15 * Colons following key words must have no space preceding them.
16 * If no page ID or filter is supplied, filter: "%pageid% = $ID$" is added.
17 * Any component can be placed in double quotes (needed to allow space, dot or question mark in components).
18 *
19 * @package dokuwiki\plugin\struct\meta
20 */
21class InlineConfigParser extends ConfigParser
22{
23    /**
24     * Parser constructor.
25     *
26     * parses the given inline configuration
27     *
28     * @param string $inline
29     */
30    public function __construct($inline)
31    {
32        // Start to build the main config array
33        $lines = array();  // Config lines to pass to full parser
34
35        // Extract components
36        $parts = explode('?', $inline, 2);
37        $n_parts = count($parts);
38        $components = str_getcsv(trim($parts[0]), '.');
39        $n_components = count($components);
40
41        // Extract parameters if given
42        if ($n_parts == 2) {
43            $filtering = false;  // Whether to filter result to current page
44            $parameters = str_getcsv(trim($parts[1]), ' ');
45            $n_parameters = count($parameters);
46
47            // Process parameters and add to config lines
48            for ($i = 0; $i < $n_parameters; $i++) {
49                $p = trim($parameters[$i]);
50                switch ($p) {
51                    // Empty (due to extra spaces)
52                    case '':
53                        // Move straight to next parameter
54                        continue 2;
55                        break;
56                    // Pass full text ending in : straight to config
57                    case $p[-1] == ':' ? $p : '':
58                        if (in_array($p, ['filter', 'where', 'filterand', 'and', 'filteror', 'or'])) {
59                            $filtering = true;
60                        }
61                        $lines[] = $p . ' ' . trim($parameters[$i + 1]);
62                        $i++;
63                        break;
64                    // Short alias for filterand
65                    case '&':
66                        $filtering = true;
67                        $lines[] = 'filterand: ' . trim($parameters[$i + 1]);
68                        $i++;
69                        break;
70                    // Short alias for filteror
71                    case '|':
72                        $filtering = true;
73                        $lines[] = 'filteror: ' . trim($parameters[$i + 1]);
74                        $i++;
75                        break;
76                    default:
77                        // Move straight to next parameter
78                        continue 2;
79                        break;
80                }
81            }
82        }
83
84        // Check whether a page was specified
85        if (count($components) == 3) {
86            // At least page, schema and field supplied
87            $lines[] = 'schema: ' . trim($components[1]);
88            $lines[] = 'field: ' . trim($components[2]);
89            $lines[] = 'filter: %pageid% = ' . trim($components[0]);
90        } elseif (count($components) == 2) {
91            // At least schema and field supplied
92            $lines[] = 'schema: ' . trim($components[0]);
93            $lines[] = 'field: ' . trim($components[1]);
94            if (!$filtering) {
95                $lines[] = 'filter: %pageid% = $ID$';
96            }
97        }
98
99        // Call original ConfigParser's constructor
100        parent::__construct($lines);
101    }
102}
103