Completed
Push — logging ( 5b784a...cad4fb )
by Andreas
05:47 queued 03:07
created

admin_plugin_logviewer::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
use dokuwiki\Logger;
4
5
/**
6
 * DokuWiki Plugin logviewer (Admin Component)
7
 *
8
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9
 * @author  Andreas Gohr <[email protected]>
10
 */
11
class admin_plugin_logviewer extends DokuWiki_Admin_Plugin
12
{
13
14
    protected $facilities;
15
    protected $facility;
16
    protected $date;
17
18
    /** @inheritDoc */
19
    public function forAdminOnly()
20
    {
21
        return true;
22
    }
23
24
    /** @inheritDoc */
25
    public function handle()
26
    {
27
        global $INPUT;
28
29
        $this->facilities = $this->getFacilities();
30
        $this->facility = $INPUT->str('facility');
31
        if (!in_array($this->facility, $this->facilities)) {
32
            $this->facility = $this->facilities[0];
33
        }
34
35
        $this->date = $INPUT->str('date');
36
        if (!preg_match('/^\d\d\d\d-\d\d-\d\d$/', $this->date)) {
37
            $this->date = gmdate('Y-m-d');
38
        }
39
    }
40
41
    /** @inheritDoc */
42
    public function html()
43
    {
44
        echo '<div id="plugin__logviewer">';
45
        echo $this->locale_xhtml('intro');
46
        $this->displayTabs();
47
        $this->displayLog();
48
        echo '</div>';
49
    }
50
51
    /**
52
     * Show the navigational tabs and date picker
53
     */
54
    protected function displayTabs()
55
    {
56
        global $ID;
57
58
        $form = new dokuwiki\Form\Form(['method'=>'GET']);
59
        $form->setHiddenField('do', 'admin');
60
        $form->setHiddenField('page', 'logviewer');
61
        $form->setHiddenField('facility', $this->facility);
62
        $form->addTextInput('date','Date*')->attr('type','date')->val($this->date)->addClass('quickselect');
63
        $form->addButton('submit','>')->attr('type','submit');
64
        echo $form->toHTML();
65
66
        echo '<ul class="tabs">';
67
        foreach ($this->facilities as $facility) {
68
            echo '<li>';
69
            if ($facility == $this->facility) {
70
                echo '<strong>' . hsc($facility) . '</strong>';
71
            } else {
72
                $link = wl($ID,
73
                    ['do' => 'admin', 'page' => 'logviewer', 'date' => $this->date, 'facility' => $facility]);
74
                echo '<a href="' . $link . '">' . hsc($facility) . '</a>';
75
            }
76
            echo '</li>';
77
        }
78
        echo '</ul>';
79
80
    }
81
82
    /**
83
     * Output the logfile contents
84
     */
85
    protected function displayLog()
86
    {
87
        $logfile = Logger::getInstance($this->facility)->getLogfile($this->date);
88
        if (!file_exists($logfile)) {
89
            echo $this->locale_xhtml('nolog');
90
            return;
91
        }
92
93
        // loop through the file an print it
94
        echo '<dl>';
95
        $lines = file($logfile);
96
        $cnt = count($lines);
97
        for ($i = 0; $i < $cnt; $i++) {
98
            $line = $lines[$i];
99
100
            if ($line[0] === ' ' && $line[1] === ' ') {
101
                // lines indented by two spaces are details, aggregate them
102
                echo '<dd>';
103
                while ($line[0] === ' ' && $line[1] === ' ') {
104
                    echo hsc(substr($line, 2)) . '<br />';
105
                    $line = $lines[$i++];
106
                }
107
                echo '</dd>';
108
                $i -= 2; // rewind the counter
109
            } else {
110
                // other lines are actual log lines in three parts
111
                list($dt, $file, $msg) = explode("\t", $line, 3);
112
                echo '<dt>';
113
                echo '<span class="datetime">' . hsc($dt) . '</span>';
114
                echo '<span class="log">';
115
                echo '<span class="msg">' . hsc($msg) . '</span>';
116
                echo '<span class="file">' . hsc($file) . '</span>';
117
                echo '</span>';
118
                echo '</dt>';
119
            }
120
        }
121
        echo '</dl>';
122
    }
123
124
    /**
125
     * Get the available logging facilities
126
     *
127
     * @return array
128
     */
129
    protected function getFacilities()
130
    {
131
        global $conf;
132
        $conf['logdir'];
133
134
        // default facilities first
135
        $facilities = [
136
            Logger::LOG_ERROR,
137
            Logger::LOG_DEPRECATED,
138
            Logger::LOG_DEBUG,
139
        ];
140
141
        // add all other dirs
142
        $dirs = glob($conf['logdir'] . '/*', GLOB_ONLYDIR);
143
        foreach ($dirs as $dir) {
144
            $facilities[] = basename($dir);
145
        }
146
        $facilities = array_unique($facilities);
147
148
        return $facilities;
149
    }
150
151
}
152
153