[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/extension/helper/ -> list.php (source)

   1  <?php
   2  /**
   3   * DokuWiki Plugin extension (Helper Component)
   4   *
   5   * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
   6   * @author  Michael Hamann <michael@content-space.de>
   7   */
   8  
   9  /**
  10   * Class helper_plugin_extension_list takes care of creating a HTML list of extensions
  11   */
  12  class helper_plugin_extension_list extends DokuWiki_Plugin
  13  {
  14      protected $form = '';
  15      /** @var  helper_plugin_extension_gui */
  16      protected $gui;
  17  
  18      /**
  19       * Constructor
  20       *
  21       * loads additional helpers
  22       */
  23      public function __construct()
  24      {
  25          $this->gui = plugin_load('helper', 'extension_gui');
  26      }
  27  
  28      /**
  29       * Initialize the extension table form
  30       */
  31      public function startForm()
  32      {
  33          $this->form .= '<form id="extension__list" accept-charset="utf-8" method="post" action="">';
  34          $hidden = array(
  35              'do'=>'admin',
  36              'page'=>'extension',
  37              'sectok'=>getSecurityToken()
  38          );
  39          $this->addHidden($hidden);
  40          $this->form .= '<ul class="extensionList">';
  41      }
  42  
  43      /**
  44       * Build single row of extension table
  45       *
  46       * @param helper_plugin_extension_extension  $extension The extension that shall be added
  47       * @param bool                               $showinfo  Show the info area
  48       */
  49      public function addRow(helper_plugin_extension_extension $extension, $showinfo = false)
  50      {
  51          $this->startRow($extension);
  52          $this->populateColumn('legend', $this->makeLegend($extension, $showinfo));
  53          $this->populateColumn('actions', $this->makeActions($extension));
  54          $this->endRow();
  55      }
  56  
  57      /**
  58       * Adds a header to the form
  59       *
  60       * @param string $id     The id of the header
  61       * @param string $header The content of the header
  62       * @param int    $level  The level of the header
  63       */
  64      public function addHeader($id, $header, $level = 2)
  65      {
  66          $this->form .='<h'.$level.' id="'.$id.'">'.hsc($header).'</h'.$level.'>'.DOKU_LF;
  67      }
  68  
  69      /**
  70       * Adds a paragraph to the form
  71       *
  72       * @param string $data The content
  73       */
  74      public function addParagraph($data)
  75      {
  76          $this->form .= '<p>'.hsc($data).'</p>'.DOKU_LF;
  77      }
  78  
  79      /**
  80       * Add hidden fields to the form with the given data
  81       *
  82       * @param array $data key-value list of fields and their values to add
  83       */
  84      public function addHidden(array $data)
  85      {
  86          $this->form .= '<div class="no">';
  87          foreach ($data as $key => $value) {
  88              $this->form .= '<input type="hidden" name="'.hsc($key).'" value="'.hsc($value).'" />';
  89          }
  90          $this->form .= '</div>'.DOKU_LF;
  91      }
  92  
  93      /**
  94       * Add closing tags
  95       */
  96      public function endForm()
  97      {
  98          $this->form .= '</ul>';
  99          $this->form .= '</form>'.DOKU_LF;
 100      }
 101  
 102      /**
 103       * Show message when no results are found
 104       */
 105      public function nothingFound()
 106      {
 107          global $lang;
 108          $this->form .= '<li class="notfound">'.$lang['nothingfound'].'</li>';
 109      }
 110  
 111      /**
 112       * Print the form
 113       */
 114      public function render()
 115      {
 116          echo $this->form;
 117      }
 118  
 119      /**
 120       * Start the HTML for the row for the extension
 121       *
 122       * @param helper_plugin_extension_extension $extension The extension
 123       */
 124      private function startRow(helper_plugin_extension_extension $extension)
 125      {
 126          $this->form .= '<li id="extensionplugin__'.hsc($extension->getID()).
 127              '" class="'.$this->makeClass($extension).'">';
 128      }
 129  
 130      /**
 131       * Add a column with the given class and content
 132       * @param string $class The class name
 133       * @param string $html  The content
 134       */
 135      private function populateColumn($class, $html)
 136      {
 137          $this->form .= '<div class="'.$class.' col">'.$html.'</div>'.DOKU_LF;
 138      }
 139  
 140      /**
 141       * End the row
 142       */
 143      private function endRow()
 144      {
 145          $this->form .= '</li>'.DOKU_LF;
 146      }
 147  
 148      /**
 149       * Generate the link to the plugin homepage
 150       *
 151       * @param helper_plugin_extension_extension $extension The extension
 152       * @return string The HTML code
 153       */
 154      public function makeHomepageLink(helper_plugin_extension_extension $extension)
 155      {
 156          $text = $this->getLang('homepage_link');
 157          $url = hsc($extension->getURL());
 158          return '<a href="'.$url.'" title="'.$url.'" class ="urlextern">'.$text.'</a> ';
 159      }
 160  
 161      /**
 162       * Generate the class name for the row of the extensio
 163       *
 164       * @param helper_plugin_extension_extension $extension The extension object
 165       * @return string The class name
 166       */
 167      public function makeClass(helper_plugin_extension_extension $extension)
 168      {
 169          $class = ($extension->isTemplate()) ? 'template' : 'plugin';
 170          if ($extension->isInstalled()) {
 171              $class.=' installed';
 172              $class.= ($extension->isEnabled()) ? ' enabled':' disabled';
 173              if ($extension->updateAvailable()) $class .= ' updatable';
 174          }
 175          if (!$extension->canModify()) $class.= ' notselect';
 176          if ($extension->isProtected()) $class.=  ' protected';
 177          //if($this->showinfo) $class.= ' showinfo';
 178          return $class;
 179      }
 180  
 181      /**
 182       * Generate a link to the author of the extension
 183       *
 184       * @param helper_plugin_extension_extension $extension The extension object
 185       * @return string The HTML code of the link
 186       */
 187      public function makeAuthor(helper_plugin_extension_extension $extension)
 188      {
 189          if ($extension->getAuthor()) {
 190              $mailid = $extension->getEmailID();
 191              if ($mailid) {
 192                  $url = $this->gui->tabURL('search', array('q' => 'authorid:'.$mailid));
 193                  return '<bdi><a href="'.$url.'" class="author" title="'.$this->getLang('author_hint').'" >'.
 194                      '<img src="//www.gravatar.com/avatar/'.$mailid.'?s=20&amp;d=mm" width="20" height="20" alt="" /> '.
 195                      hsc($extension->getAuthor()).'</a></bdi>';
 196              } else {
 197                  return '<bdi><span class="author">'.hsc($extension->getAuthor()).'</span></bdi>';
 198              }
 199          }
 200          return "<em class=\"author\">".$this->getLang('unknown_author')."</em>".DOKU_LF;
 201      }
 202  
 203      /**
 204       * Get the link and image tag for the screenshot/thumbnail
 205       *
 206       * @param helper_plugin_extension_extension $extension The extension object
 207       * @return string The HTML code
 208       */
 209      public function makeScreenshot(helper_plugin_extension_extension $extension)
 210      {
 211          $screen = $extension->getScreenshotURL();
 212          $thumb = $extension->getThumbnailURL();
 213  
 214          if ($screen) {
 215              // use protocol independent URLs for images coming from us #595
 216              $screen = str_replace('http://www.dokuwiki.org', '//www.dokuwiki.org', $screen);
 217              $thumb = str_replace('http://www.dokuwiki.org', '//www.dokuwiki.org', $thumb);
 218  
 219              $title = sprintf($this->getLang('screenshot'), hsc($extension->getDisplayName()));
 220              $img = '<a href="'.hsc($screen).'" target="_blank" class="extension_screenshot">'.
 221                  '<img alt="'.$title.'" width="120" height="70" src="'.hsc($thumb).'" />'.
 222                  '</a>';
 223          } elseif ($extension->isTemplate()) {
 224              $img = '<img alt="" width="120" height="70" src="'.DOKU_BASE.
 225                  'lib/plugins/extension/images/template.png" />';
 226          } else {
 227              $img = '<img alt="" width="120" height="70" src="'.DOKU_BASE.
 228                  'lib/plugins/extension/images/plugin.png" />';
 229          }
 230          return '<div class="screenshot" >'.$img.'<span></span></div>'.DOKU_LF;
 231      }
 232  
 233      /**
 234       * Extension main description
 235       *
 236       * @param helper_plugin_extension_extension $extension The extension object
 237       * @param bool                              $showinfo  Show the info section
 238       * @return string The HTML code
 239       */
 240      public function makeLegend(helper_plugin_extension_extension $extension, $showinfo = false)
 241      {
 242          $return  = '<div>';
 243          $return .= '<h2>';
 244          $return .= sprintf(
 245              $this->getLang('extensionby'),
 246              '<bdi>'.hsc($extension->getDisplayName()).'</bdi>',
 247              $this->makeAuthor($extension)
 248          );
 249          $return .= '</h2>'.DOKU_LF;
 250  
 251          $return .= $this->makeScreenshot($extension);
 252  
 253          $popularity = $extension->getPopularity();
 254          if ($popularity !== false && !$extension->isBundled()) {
 255              $popularityText = sprintf($this->getLang('popularity'), round($popularity*100, 2));
 256              $return .= '<div class="popularity" title="'.$popularityText.'">'.
 257                  '<div style="width: '.($popularity * 100).'%;">'.
 258                  '<span class="a11y">'.$popularityText.'</span>'.
 259                  '</div></div>'.DOKU_LF;
 260          }
 261  
 262          if ($extension->getDescription()) {
 263              $return .= '<p><bdi>';
 264              $return .=  hsc($extension->getDescription()).' ';
 265              $return .= '</bdi></p>'.DOKU_LF;
 266          }
 267  
 268          $return .= $this->makeLinkbar($extension);
 269  
 270          if ($showinfo) {
 271              $url = $this->gui->tabURL('');
 272              $class = 'close';
 273          } else {
 274              $url = $this->gui->tabURL('', array('info' => $extension->getID()));
 275              $class = '';
 276          }
 277          $return .= ' <a href="'.$url.'#extensionplugin__'.$extension->getID().
 278              '" class="info '.$class.'" title="'.$this->getLang('btn_info').
 279              '" data-extid="'.$extension->getID().'">'.$this->getLang('btn_info').'</a>';
 280  
 281          if ($showinfo) {
 282              $return .= $this->makeInfo($extension);
 283          }
 284          $return .= $this->makeNoticeArea($extension);
 285          $return .= '</div>'.DOKU_LF;
 286          return $return;
 287      }
 288  
 289      /**
 290       * Generate the link bar HTML code
 291       *
 292       * @param helper_plugin_extension_extension $extension The extension instance
 293       * @return string The HTML code
 294       */
 295      public function makeLinkbar(helper_plugin_extension_extension $extension)
 296      {
 297          $return  = '<div class="linkbar">';
 298          $return .= $this->makeHomepageLink($extension);
 299          if ($extension->getBugtrackerURL()) {
 300              $return .= ' <a href="'.hsc($extension->getBugtrackerURL()).
 301                  '" title="'.hsc($extension->getBugtrackerURL()).'" class ="bugs">'.
 302                  $this->getLang('bugs_features').'</a> ';
 303          }
 304          if ($extension->getTags()) {
 305              $first = true;
 306              $return .= '<span class="tags">'.$this->getLang('tags').' ';
 307              foreach ($extension->getTags() as $tag) {
 308                  if (!$first) {
 309                      $return .= ', ';
 310                  } else {
 311                      $first = false;
 312                  }
 313                  $url = $this->gui->tabURL('search', array('q' => 'tag:'.$tag));
 314                  $return .= '<bdi><a href="'.$url.'">'.hsc($tag).'</a></bdi>';
 315              }
 316              $return .= '</span>';
 317          }
 318          $return .= '</div>'.DOKU_LF;
 319          return $return;
 320      }
 321  
 322      /**
 323       * Notice area
 324       *
 325       * @param helper_plugin_extension_extension $extension The extension
 326       * @return string The HTML code
 327       */
 328      public function makeNoticeArea(helper_plugin_extension_extension $extension)
 329      {
 330          $return = '';
 331          $missing_dependencies = $extension->getMissingDependencies();
 332          if (!empty($missing_dependencies)) {
 333              $return .= '<div class="msg error">' .
 334                  sprintf(
 335                      $this->getLang('missing_dependency'),
 336                      '<bdi>' . implode(', ', $missing_dependencies) . '</bdi>'
 337                  ) .
 338                  '</div>';
 339          }
 340          if ($extension->isInWrongFolder()) {
 341              $return .= '<div class="msg error">' .
 342                  sprintf(
 343                      $this->getLang('wrong_folder'),
 344                      '<bdi>' . hsc($extension->getInstallName()) . '</bdi>',
 345                      '<bdi>' . hsc($extension->getBase()) . '</bdi>'
 346                  ) .
 347                  '</div>';
 348          }
 349          if (($securityissue = $extension->getSecurityIssue()) !== false) {
 350              $return .= '<div class="msg error">'.
 351                  sprintf($this->getLang('security_issue'), '<bdi>'.hsc($securityissue).'</bdi>').
 352                  '</div>';
 353          }
 354          if (($securitywarning = $extension->getSecurityWarning()) !== false) {
 355              $return .= '<div class="msg notify">'.
 356                  sprintf($this->getLang('security_warning'), '<bdi>'.hsc($securitywarning).'</bdi>').
 357                  '</div>';
 358          }
 359          if ($extension->updateAvailable()) {
 360              $return .=  '<div class="msg notify">'.
 361                  sprintf($this->getLang('update_available'), hsc($extension->getLastUpdate())).
 362                  '</div>';
 363          }
 364          if ($extension->hasDownloadURLChanged()) {
 365              $return .= '<div class="msg notify">' .
 366                  sprintf(
 367                      $this->getLang('url_change'),
 368                      '<bdi>' . hsc($extension->getDownloadURL()) . '</bdi>',
 369                      '<bdi>' . hsc($extension->getLastDownloadURL()) . '</bdi>'
 370                  ) .
 371                  '</div>';
 372          }
 373          return $return.DOKU_LF;
 374      }
 375  
 376      /**
 377       * Create a link from the given URL
 378       *
 379       * Shortens the URL for display
 380       *
 381       * @param string $url
 382       * @return string  HTML link
 383       */
 384      public function shortlink($url)
 385      {
 386          $link = parse_url($url);
 387  
 388          $base = $link['host'];
 389          if (!empty($link['port'])) $base .= $base.':'.$link['port'];
 390          $long = $link['path'];
 391          if (!empty($link['query'])) $long .= $link['query'];
 392  
 393          $name = shorten($base, $long, 55);
 394  
 395          return '<a href="'.hsc($url).'" class="urlextern">'.hsc($name).'</a>';
 396      }
 397  
 398      /**
 399       * Plugin/template details
 400       *
 401       * @param helper_plugin_extension_extension $extension The extension
 402       * @return string The HTML code
 403       */
 404      public function makeInfo(helper_plugin_extension_extension $extension)
 405      {
 406          $default = $this->getLang('unknown');
 407          $return = '<dl class="details">';
 408  
 409          $return .= '<dt>'.$this->getLang('status').'</dt>';
 410          $return .= '<dd>'.$this->makeStatus($extension).'</dd>';
 411  
 412          if ($extension->getDonationURL()) {
 413              $return .= '<dt>'.$this->getLang('donate').'</dt>';
 414              $return .= '<dd>';
 415              $return .= '<a href="'.$extension->getDonationURL().'" class="donate">'.
 416                  $this->getLang('donate_action').'</a>';
 417              $return .= '</dd>';
 418          }
 419  
 420          if (!$extension->isBundled()) {
 421              $return .= '<dt>'.$this->getLang('downloadurl').'</dt>';
 422              $return .= '<dd><bdi>';
 423              $return .= ($extension->getDownloadURL() ? $this->shortlink($extension->getDownloadURL()) : $default);
 424              $return .= '</bdi></dd>';
 425  
 426              $return .= '<dt>'.$this->getLang('repository').'</dt>';
 427              $return .= '<dd><bdi>';
 428              $return .= ($extension->getSourcerepoURL() ? $this->shortlink($extension->getSourcerepoURL()) : $default);
 429              $return .= '</bdi></dd>';
 430          }
 431  
 432          if ($extension->isInstalled()) {
 433              if ($extension->getInstalledVersion()) {
 434                  $return .= '<dt>'.$this->getLang('installed_version').'</dt>';
 435                  $return .= '<dd>';
 436                  $return .= hsc($extension->getInstalledVersion());
 437                  $return .= '</dd>';
 438              }
 439              if (!$extension->isBundled()) {
 440                  $return .= '<dt>'.$this->getLang('install_date').'</dt>';
 441                  $return .= '<dd>';
 442                  $return .= ($extension->getUpdateDate() ? hsc($extension->getUpdateDate()) : $this->getLang('unknown'));
 443                  $return .= '</dd>';
 444              }
 445          }
 446          if (!$extension->isInstalled() || $extension->updateAvailable()) {
 447              $return .= '<dt>'.$this->getLang('available_version').'</dt>';
 448              $return .= '<dd>';
 449              $return .= ($extension->getLastUpdate() ? hsc($extension->getLastUpdate()) : $this->getLang('unknown'));
 450              $return .= '</dd>';
 451          }
 452  
 453          $return .= '<dt>'.$this->getLang('provides').'</dt>';
 454          $return .= '<dd><bdi>';
 455          $return .= ($extension->getTypes() ? hsc(implode(', ', $extension->getTypes())) : $default);
 456          $return .= '</bdi></dd>';
 457  
 458          if (!$extension->isBundled() && $extension->getCompatibleVersions()) {
 459              $return .= '<dt>'.$this->getLang('compatible').'</dt>';
 460              $return .= '<dd>';
 461              foreach ($extension->getCompatibleVersions() as $date => $version) {
 462                  $return .= '<bdi>'.$version['label'].' ('.$date.')</bdi>, ';
 463              }
 464              $return = rtrim($return, ', ');
 465              $return .= '</dd>';
 466          }
 467          if ($extension->getDependencies()) {
 468              $return .= '<dt>'.$this->getLang('depends').'</dt>';
 469              $return .= '<dd>';
 470              $return .= $this->makeLinkList($extension->getDependencies());
 471              $return .= '</dd>';
 472          }
 473  
 474          if ($extension->getSimilarExtensions()) {
 475              $return .= '<dt>'.$this->getLang('similar').'</dt>';
 476              $return .= '<dd>';
 477              $return .= $this->makeLinkList($extension->getSimilarExtensions());
 478              $return .= '</dd>';
 479          }
 480  
 481          if ($extension->getConflicts()) {
 482              $return .= '<dt>'.$this->getLang('conflicts').'</dt>';
 483              $return .= '<dd>';
 484              $return .= $this->makeLinkList($extension->getConflicts());
 485              $return .= '</dd>';
 486          }
 487          $return .= '</dl>'.DOKU_LF;
 488          return $return;
 489      }
 490  
 491      /**
 492       * Generate a list of links for extensions
 493       *
 494       * @param array $ext The extensions
 495       * @return string The HTML code
 496       */
 497      public function makeLinkList($ext)
 498      {
 499          $return = '';
 500          foreach ($ext as $link) {
 501              $return .= '<bdi><a href="'.
 502                  $this->gui->tabURL('search', array('q'=>'ext:'.$link)).'">'.hsc($link).'</a></bdi>, ';
 503          }
 504          return rtrim($return, ', ');
 505      }
 506  
 507      /**
 508       * Display the action buttons if they are possible
 509       *
 510       * @param helper_plugin_extension_extension $extension The extension
 511       * @return string The HTML code
 512       */
 513      public function makeActions(helper_plugin_extension_extension $extension)
 514      {
 515          global $conf;
 516          $return = '';
 517          $errors = '';
 518  
 519          if ($extension->isInstalled()) {
 520              if (($canmod = $extension->canModify()) === true) {
 521                  if (!$extension->isProtected()) {
 522                      $return .= $this->makeAction('uninstall', $extension);
 523                  }
 524                  if ($extension->getDownloadURL()) {
 525                      if ($extension->updateAvailable()) {
 526                          $return .= $this->makeAction('update', $extension);
 527                      } else {
 528                          $return .= $this->makeAction('reinstall', $extension);
 529                      }
 530                  }
 531              } else {
 532                  $errors .= '<p class="permerror">'.$this->getLang($canmod).'</p>';
 533              }
 534  
 535              if (!$extension->isProtected() && !$extension->isTemplate()) { // no enable/disable for templates
 536                  if ($extension->isEnabled()) {
 537                      $return .= $this->makeAction('disable', $extension);
 538                  } else {
 539                      $return .= $this->makeAction('enable', $extension);
 540                  }
 541              }
 542  
 543              if ($extension->isGitControlled()) {
 544                  $errors .= '<p class="permerror">'.$this->getLang('git').'</p>';
 545              }
 546  
 547              if ($extension->isEnabled() &&
 548                  in_array('Auth', $extension->getTypes()) &&
 549                  $conf['authtype'] != $extension->getID()
 550              ) {
 551                  $errors .= '<p class="permerror">'.$this->getLang('auth').'</p>';
 552              }
 553          } else {
 554              if (($canmod = $extension->canModify()) === true) {
 555                  if ($extension->getDownloadURL()) {
 556                      $return .= $this->makeAction('install', $extension);
 557                  }
 558              } else {
 559                  $errors .= '<div class="permerror">'.$this->getLang($canmod).'</div>';
 560              }
 561          }
 562  
 563          if (!$extension->isInstalled() && $extension->getDownloadURL()) {
 564              $return .= ' <span class="version">'.$this->getLang('available_version').' ';
 565              $return .= ($extension->getLastUpdate()
 566                      ? hsc($extension->getLastUpdate())
 567                      : $this->getLang('unknown')).'</span>';
 568          }
 569  
 570          return $return.' '.$errors.DOKU_LF;
 571      }
 572  
 573      /**
 574       * Display an action button for an extension
 575       *
 576       * @param string                            $action    The action
 577       * @param helper_plugin_extension_extension $extension The extension
 578       * @return string The HTML code
 579       */
 580      public function makeAction($action, $extension)
 581      {
 582          $title = '';
 583  
 584          switch ($action) {
 585              case 'install':
 586              case 'reinstall':
 587                  $title = 'title="'.hsc($extension->getDownloadURL()).'"';
 588                  break;
 589          }
 590  
 591          $classes = 'button '.$action;
 592          $name    = 'fn['.$action.']['.hsc($extension->getID()).']';
 593  
 594          return '<button class="'.$classes.'" name="'.$name.'" type="submit" '.$title.'>'.
 595              $this->getLang('btn_'.$action).'</button> ';
 596      }
 597  
 598      /**
 599       * Plugin/template status
 600       *
 601       * @param helper_plugin_extension_extension $extension The extension
 602       * @return string The description of all relevant statusses
 603       */
 604      public function makeStatus(helper_plugin_extension_extension $extension)
 605      {
 606          $status = array();
 607  
 608  
 609          if ($extension->isInstalled()) {
 610              $status[] = $this->getLang('status_installed');
 611              if ($extension->isProtected()) {
 612                  $status[] = $this->getLang('status_protected');
 613              } else {
 614                  $status[] = $extension->isEnabled()
 615                      ? $this->getLang('status_enabled')
 616                      : $this->getLang('status_disabled');
 617              }
 618          } else {
 619              $status[] = $this->getLang('status_not_installed');
 620          }
 621          if (!$extension->canModify()) $status[] = $this->getLang('status_unmodifiable');
 622          if ($extension->isBundled()) $status[] = $this->getLang('status_bundled');
 623          $status[] = $extension->isTemplate() ? $this->getLang('status_template') : $this->getLang('status_plugin');
 624          return join(', ', $status);
 625      }
 626  }