1<?php
2/**
3 * DokuWiki Plugin elasticsearch (CLI Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9if (!defined('DOKU_INC')) die();
10
11use splitbrain\phpcli\Options;
12
13class cli_plugin_elasticsearch extends DokuWiki_CLI_Plugin {
14
15    /** @var helper_plugin_elasticsearch_client */
16    protected $hlp;
17
18    /**
19     * Initialize helper plugin
20     */
21    public function __construct(){
22        parent::__construct();
23        $this->hlp = plugin_load('helper', 'elasticsearch_client');
24    }
25
26
27    /**
28     * Register options and arguments on the given $options object
29     *
30     * @param Options $options
31     * @return void
32     * @throws \splitbrain\phpcli\Exception
33     */
34    protected function setup(Options $options) {
35        $options->setHelp('Manage the elastic search index');
36
37        $options->registerCommand('index', 'Index all pages in the wiki');
38
39        $options->registerCommand('createindex', 'Create a simple index named "'.$this->hlp->getConf('indexname').'".');
40        $options->registerOption('clear', 'Remove existing index if any', 'c', false, 'createindex');
41
42        $options->registerCommand('createlangmapping', 'Create the field mapping for multilanguage setup');
43    }
44
45    /**
46     * Your main program
47     *
48     * Arguments and options have been parsed when this is run
49     *
50     * @param Options $options
51     * @return void
52     */
53    protected function main(Options $options) {
54        // manually initialize auth system
55        // see https://github.com/splitbrain/dokuwiki/issues/2823
56        global $AUTH_ACL;
57        if(!$AUTH_ACL) auth_setup();
58
59        $cmd = $options->getCmd();
60        switch ($cmd) {
61            case 'createindex':
62                $result = $this->hlp->createIndex($options->getOpt('clear'));
63                if($result->hasError()){
64                    $this->error($result->getError());
65                } else {
66                    $this->success('Index created');
67                }
68                break;
69            case 'createlangmapping':
70                $result = $this->hlp->createLanguageMapping();
71                if($result->hasError()){
72                    $this->error($result->getError());
73                } else {
74                    $this->success('Mapping created');
75                }
76                break;
77            case 'index':
78                $this->indexAllPages();
79                break;
80            default:
81                $this->error('No command provided');
82                exit(1);
83        }
84
85
86    }
87
88    /**
89     * Index all the pages
90     */
91    protected function indexAllPages() {
92        global $conf, $ID;
93
94        /** @var action_plugin_elasticsearch_indexing $act */
95        $act = plugin_load('action', 'elasticsearch_indexing');
96
97        $data = array();
98        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
99        $pages = count($data);
100        $n     = 0;
101        foreach($data as $val) {
102            $ID = $val['id'];
103            $n++;
104            $this->info(sprintf("Indexing page %s (%d of %d)\n", $ID, $n, $pages));
105            $act->index_page($ID);
106        }
107    }
108
109}
110