[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/logviewer/ -> admin.php (source)

   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 <andi@splitbrain.org>
  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',$this->getLang('date'))
  63               ->attr('type','date')->val($this->date)->addClass('quickselect');
  64          $form->addButton('submit','>')->attr('type','submit');
  65          echo $form->toHTML();
  66  
  67          echo '<ul class="tabs">';
  68          foreach ($this->facilities as $facility) {
  69              echo '<li>';
  70              if ($facility == $this->facility) {
  71                  echo '<strong>' . hsc($facility) . '</strong>';
  72              } else {
  73                  $link = wl($ID,
  74                      ['do' => 'admin', 'page' => 'logviewer', 'date' => $this->date, 'facility' => $facility]);
  75                  echo '<a href="' . $link . '">' . hsc($facility) . '</a>';
  76              }
  77              echo '</li>';
  78          }
  79          echo '</ul>';
  80  
  81      }
  82  
  83      /**
  84       * Output the logfile contents
  85       */
  86      protected function displayLog()
  87      {
  88          $logfile = Logger::getInstance($this->facility)->getLogfile($this->date);
  89          if (!file_exists($logfile)) {
  90              echo $this->locale_xhtml('nolog');
  91              return;
  92          }
  93  
  94          // loop through the file an print it
  95          echo '<dl>';
  96          $lines = file($logfile);
  97          $cnt = count($lines);
  98          for ($i = 0; $i < $cnt; $i++) {
  99              $line = $lines[$i];
 100  
 101              if ($line[0] === ' ' && $line[1] === ' ') {
 102                  // lines indented by two spaces are details, aggregate them
 103                  echo '<dd>';
 104                  while ($line[0] === ' ' && $line[1] === ' ') {
 105                      echo hsc(substr($line, 2)) . '<br />';
 106                      $line = $lines[$i++];
 107                  }
 108                  echo '</dd>';
 109                  $i -= 2; // rewind the counter
 110              } else {
 111                  // other lines are actual log lines in three parts
 112                  list($dt, $file, $msg) = explode("\t", $line, 3);
 113                  echo '<dt>';
 114                  echo '<span class="datetime">' . hsc($dt) . '</span>';
 115                  echo '<span class="log">';
 116                  echo '<span class="msg">' . hsc($msg) . '</span>';
 117                  echo '<span class="file">' . hsc($file) . '</span>';
 118                  echo '</span>';
 119                  echo '</dt>';
 120              }
 121          }
 122          echo '</dl>';
 123      }
 124  
 125      /**
 126       * Get the available logging facilities
 127       *
 128       * @return array
 129       */
 130      protected function getFacilities()
 131      {
 132          global $conf;
 133          $conf['logdir'];
 134  
 135          // default facilities first
 136          $facilities = [
 137              Logger::LOG_ERROR,
 138              Logger::LOG_DEPRECATED,
 139              Logger::LOG_DEBUG,
 140          ];
 141  
 142          // add all other dirs
 143          $dirs = glob($conf['logdir'] . '/*', GLOB_ONLYDIR);
 144          foreach ($dirs as $dir) {
 145              $facilities[] = basename($dir);
 146          }
 147          $facilities = array_unique($facilities);
 148  
 149          return $facilities;
 150      }
 151  
 152  }
 153