xref: /plugin/elasticsearch/cli.php (revision 46a93d8f4459bd8a343baf545d4ba71795756d5f)
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        $cmd = $options->getCmd();
55        switch ($cmd) {
56            case 'createindex':
57                $result = $this->hlp->createIndex($options->getOpt('clear'));
58                if($result->hasError()){
59                    $this->error($result->getError());
60                } else {
61                    $this->success('Index created');
62                }
63                break;
64            case 'createlangmapping':
65                $result = $this->hlp->createLanguageMapping();
66                if($result->hasError()){
67                    $this->error($result->getError());
68                } else {
69                    $this->success('Mapping created');
70                }
71                break;
72            case 'index':
73                $this->indexAllPages();
74                break;
75            default:
76                $this->error('No command provided');
77                exit(1);
78        }
79
80
81    }
82
83    /**
84     * Index all the pages
85     */
86    protected function indexAllPages() {
87        global $conf, $ID;
88
89        /** @var action_plugin_elasticsearch_indexing $act */
90        $act = plugin_load('action', 'elasticsearch_indexing');
91
92        $data = array();
93        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
94        $pages = count($data);
95        $n     = 0;
96        foreach($data as $val) {
97            $ID = $val['id'];
98            $n++;
99            $this->info(sprintf("Indexing page %s (%d of %d)\n", $ID, $n, $pages));
100            $act->index_page($ID);
101        }
102    }
103
104}
105