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 = [];  // 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
40        // Extract parameters if given
41        $filtering = false;  // First initialisation of the variable
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                    default:
54                        // Move straight to next parameter
55                        continue 2;
56                        break;
57                    // Pass full text ending in : straight to config
58                    case $p[-1] == ':' ? $p : '':
59                        if (in_array($p, ['filter', 'where', 'filterand', 'and', 'filteror', 'or'])) {
60                            $filtering = true;
61                        }
62                        $lines[] = $p . ' ' . trim($parameters[$i + 1]);
63                        $i++;
64                        break;
65                    // Short alias for filterand
66                    case '&':
67                        $filtering = true;
68                        $lines[] = 'filterand: ' . trim($parameters[$i + 1]);
69                        $i++;
70                        break;
71                    // Short alias for filteror
72                    case '|':
73                        $filtering = true;
74                        $lines[] = 'filteror: ' . trim($parameters[$i + 1]);
75                        $i++;
76                        break;
77                }
78            }
79        }
80
81        // Check whether a page was specified
82        if (count($components) == 3) {
83            // At least page, schema and field supplied
84            $lines[] = 'schema: ' . trim($components[1]);
85            $lines[] = 'field: ' . trim($components[2]);
86            $lines[] = 'filter: %pageid% = ' . trim($components[0]);
87        } elseif (count($components) == 2) {
88            // At least schema and field supplied
89            $lines[] = 'schema: ' . trim($components[0]);
90            $lines[] = 'field: ' . trim($components[1]);
91            if (!$filtering) {
92                $lines[] = 'filter: %pageid% = $ID$';
93            }
94        }
95
96        // Call original ConfigParser's constructor
97        parent::__construct($lines);
98    }
99}
100