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