Failed Conditions
Push — master ( b2c9cd...43d3f0 )
by Andreas
12:59 queued 09:00
created

bin/indexer.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
#!/usr/bin/php
2
<?php
3
4
use splitbrain\phpcli\CLI;
5
use splitbrain\phpcli\Options;
6
7
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
8
define('NOSESSION', 1);
9
require_once(DOKU_INC . 'inc/init.php');
10
11
/**
12
 * Update the Search Index from command line
13
 */
14
class IndexerCLI extends CLI {
15
16
    private $quiet = false;
17
    private $clear = false;
18
19
    /**
20
     * Register options and arguments on the given $options object
21
     *
22
     * @param Options $options
23
     * @return void
24
     */
25
    protected function setup(Options $options) {
26
        $options->setHelp(
27
            'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
28
            'given the index is cleared first.'
29
        );
30
31
        $options->registerOption(
32
            'clear',
33
            'clear the index before updating',
34
            'c'
35
        );
36
        $options->registerOption(
37
            'quiet',
38
            'don\'t produce any output',
39
            'q'
40
        );
41
    }
42
43
    /**
44
     * Your main program
45
     *
46
     * Arguments and options have been parsed when this is run
47
     *
48
     * @param Options $options
49
     * @return void
50
     */
51
    protected function main(Options $options) {
52
        $this->clear = $options->getOpt('clear');
0 ignored issues
show
Documentation Bug introduced by
It seems like $options->getOpt('clear') can also be of type string or array<integer,string>. However, the property $clear is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
53
        $this->quiet = $options->getOpt('quiet');
0 ignored issues
show
Documentation Bug introduced by
It seems like $options->getOpt('quiet') can also be of type string or array<integer,string>. However, the property $quiet is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54
55
        if($this->clear) $this->clearindex();
56
57
        $this->update();
58
    }
59
60
    /**
61
     * Update the index
62
     */
63
    function update() {
64
        global $conf;
65
        $data = array();
66
        $this->quietecho("Searching pages... ");
67
        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
68
        $this->quietecho(count($data) . " pages found.\n");
69
70
        foreach($data as $val) {
71
            $this->index($val['id']);
72
        }
73
    }
74
75
    /**
76
     * Index the given page
77
     *
78
     * @param string $id
79
     */
80
    function index($id) {
81
        $this->quietecho("$id... ");
82
        idx_addPage($id, !$this->quiet, $this->clear);
83
        $this->quietecho("done.\n");
84
    }
85
86
    /**
87
     * Clear all index files
88
     */
89
    function clearindex() {
90
        $this->quietecho("Clearing index... ");
91
        idx_get_indexer()->clear();
92
        $this->quietecho("done.\n");
93
    }
94
95
    /**
96
     * Print message if not supressed
97
     *
98
     * @param string $msg
99
     */
100
    function quietecho($msg) {
101
        if(!$this->quiet) echo $msg;
102
    }
103
}
104
105
// Main
106
$cli = new IndexerCLI();
107
$cli->run();
108