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