1<?php
2if (!defined('DOKU_INC')) die();
3
4use splitbrain\phpcli\Options;
5
6class cli_plugin_newpagetemplate extends DokuWiki_CLI_Plugin
7{
8    // register options and arguments
9    public $newpgdbg = false;
10
11    protected function setup(Options $options)
12    {
13        // $this->colors->disable();
14        $config = $this->colors->wrap('config', 'cyan');
15        $pg = $this->colors->wrap('page', 'cyan');
16        $ini = $this->colors->wrap('ini', 'cyan');
17        $browser = $this->colors->wrap('browser', 'cyan');
18        $cmdLine = $this->colors->wrap('cmdLine', 'cyan');
19        $browser = $this->colors->wrap('browser', 'cyan');
20        $nosave = $this->colors->wrap('true', 'cyan');
21        $false = $this->colors->wrap('false', 'cyan');
22        $existing = $this->colors->wrap('existing', 'cyan');
23        $options->setHelp(
24    "[[-p|--page] page_id] | [[-i|--ini] path-to-ini|config] | [[-t|--tpl] template_id] | [[-u|usrrepl] <macros>]\n|[[-s|--screen][cmdLine|browser]]|[[--nosave|-n]true]\n" .
25        "\n\nThis plugin helps to automate the processing of pages that use new page templates. " .
26            "The first command line option must be either '$pg' or '$ini'.  For a complete description see" .
27            " the newpagetemplate documentation at https://www.dokuwiki.org/plugin:newpagetemplate"
28            );
29        $options->registerOption('version', 'print version', 'v');
30        $options->registerOption('page', 'Apply the template to the named page id', 'p');
31        $options->registerOption('usrrepl', 'newpagevars: Macro/Replacent string: @MACRO@,replacement;@MACRO_2@. . . ', 'u');
32        $options->registerOption('tmpl', 'Template to apply to the specified page ', 't');
33        $options->registerOption('cliuser', 'User of CLI process', 'c');
34
35        $options->registerOption('nosave',
36             "If this option is set to '$nosave', then none of the the pages will be saved; if set to '$existing', " .
37             "then existing pages will not be over-written, but all other pages will be created and saved. " .
38             " If set to '$false' all pages will saved and existing pages over-written. " .
39             "In any case, the output will still be printed to the screen, " .
40             "if the screen option is set to $browser.", 'n');
41
42        $options->registerOption('screen', "Output results to screen; this option takes one of two parameters: either '$browser' or '$cmdLine'."
43        . " The command line output consists of the command line options in raw and parsed format. The browser output" .
44        " consists of the parsed page data.", 's');
45
46        $options->registerOption('ini', "Name of an ini file. This file must be stored in the root directory" .
47            " of the newpagetemplate plugin. The ini files make it possible to parse and save data to multiple pages. " .
48            " Its format and functioning are described on the newpagetemplate" .
49            " plugin page. To use the ini file specified in the plugin's Configuration Settings, this option must be set to '$config'.", 'i');
50
51    }
52
53    protected function main(Options $options)
54    {
55        print_r($options->getArgs(), 1);
56
57        if ($options->getOpt('page')) {
58            $opts = $options->getArgs();
59            $clopts = $this->get_commandLineOptions($opts);
60            if (!$clopts['page']) {
61                $this->commandLineErr('page');
62            }
63            $this->raw_commandLineOpts($opts, $clopts);
64            $helper = plugin_load('helper', 'newpagetemplate', 1, $false);
65            $helper->init($clopts, $options, $this);
66        } else if ($options->getOpt('ini')) {
67            $opts = $options->getArgs();
68            $clopts = $this->get_commandLineOptions($opts, 1);
69            if (!$clopts['ini']) {
70                $this->commandLineErr('ini');
71            }
72            $this->raw_commandLineOpts($opts, $clopts);
73            $helper = plugin_load('helper', 'newpagetemplate', 1, $false);
74            $helper->init($clopts, $options, $this);
75        } else if ($options->getOpt('version')) {
76            $info = $this->getInfo();
77            $this->success($info['date']);
78        } else {
79            echo $options->help();
80        }
81
82    }
83
84    function get_commandLineOptions($opts, $config = false)
85    {
86        if (function_exists('is_countable') && !is_countable($opts)) return;
87
88        $page = "";
89        $usrrepl = "";
90        $user = "";
91        $ini = false;
92        $screen = "";
93        if ($config) {
94            $ini = array_shift($opts);
95        } else $page = array_shift($opts);
96        for ($i = 0; $i < count($opts); $i++) {
97            $cl_switch = trim($opts[$i], '-');
98            switch ($cl_switch) { // leaves space for additional command line options
99                case 'u':
100                case 'usrrepl':
101                    $usrrepl = $opts[$i + 1];
102                    break;
103                case 't':
104                case 'templ':
105                    $tmpl = $opts[$i + 1];
106                    break;
107                case 'o':
108                case 'owner':
109                    $user = $opts[$i + 1];
110                    break;
111                case 'cliuser':
112                case 'c':
113                    $user = $opts[$i + 1];
114                    break;
115                case 'screen':
116                case 's':
117                     $screen =  $opts[$i + 1];
118                     break;
119                case 'nosave':
120                case 'n':
121                     $nosave =  $opts[$i + 1];
122                     break;
123            }
124        }
125        if (!$user) {
126            $processUser = posix_getpwuid(posix_geteuid());
127            if (isset($processUser) && !empty($processUser['name'])) {
128                $user = $processUser['name'];
129            }
130        }
131        if(empty($nosave)) {
132            $nosave = 'true';
133        }
134        return array('page' => $page, 'usrrepl' => $usrrepl, 'tmpl' => $tmpl, 'user' => $user, 'ini' => $ini, 'screen' => $screen,
135                     'nosave' => $nosave);
136    }
137
138    public function raw_commandLineOpts($opts = "", $clopts = "", $dbg = false)
139    {
140        if (!$this->newpgdbg && !$dbg) return;
141        global $argv;
142        echo "argv = " . print_r($argv, 1);
143        print_r($opts);
144        print_r($clopts);
145    }
146
147    function commandLineErr($type)
148    {
149        switch ($type) {
150            case 'page':
151                $this->colors->ptln("$type error: page id required", 'cyan');
152                exit;
153            case 'ini':
154                $this->colors->ptln("$type error: ini configuration file required", 'cyan');
155                exit;
156            default:
157        }
158        return 1;
159    }
160
161    /**
162     * @param array $array
163     * @param int|string $position
164     * @param mixed $insert
165     */
166    function array_insert(&$array, $position, $insert)
167    {
168        if (is_int($position)) {
169            array_splice($array, $position, 0, $insert);
170        } else {
171            $pos = array_search($position, array_keys($array));
172            $array = array_merge(
173                array_slice($array, 0, $pos),
174                $insert,
175                array_slice($array, $pos)
176            );
177        }
178    }
179
180}
181