Complex classes like helper_plugin_extension_extension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use helper_plugin_extension_extension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class helper_plugin_extension_extension extends DokuWiki_Plugin |
||
| 16 | { |
||
| 17 | private $id; |
||
| 18 | private $base; |
||
| 19 | private $is_template = false; |
||
| 20 | private $localInfo; |
||
| 21 | private $remoteInfo; |
||
| 22 | private $managerData; |
||
| 23 | /** @var helper_plugin_extension_repository $repository */ |
||
| 24 | private $repository = null; |
||
| 25 | |||
| 26 | /** @var array list of temporary directories */ |
||
| 27 | private $temporary = array(); |
||
| 28 | |||
| 29 | /** @var string where templates are installed to */ |
||
| 30 | private $tpllib = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * helper_plugin_extension_extension constructor. |
||
| 34 | */ |
||
| 35 | public function __construct() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Destructor |
||
| 42 | * |
||
| 43 | * deletes any dangling temporary directories |
||
| 44 | */ |
||
| 45 | public function __destruct() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return bool false, this component is not a singleton |
||
| 54 | */ |
||
| 55 | public function isSingleton() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set the name of the extension this instance shall represents, triggers loading the local and remote data |
||
| 62 | * |
||
| 63 | * @param string $id The id of the extension (prefixed with template: for templates) |
||
| 64 | * @return bool If some (local or remote) data was found |
||
| 65 | */ |
||
| 66 | public function setExtension($id) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * If the extension is installed locally |
||
| 99 | * |
||
| 100 | * @return bool If the extension is installed locally |
||
| 101 | */ |
||
| 102 | public function isInstalled() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * If the extension is under git control |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function isGitControlled() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * If the extension is bundled |
||
| 120 | * |
||
| 121 | * @return bool If the extension is bundled |
||
| 122 | */ |
||
| 123 | public function isBundled() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * If the extension is protected against any modification (disable/uninstall) |
||
| 139 | * |
||
| 140 | * @return bool if the extension is protected |
||
| 141 | */ |
||
| 142 | public function isProtected() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * If the extension is installed in the correct directory |
||
| 156 | * |
||
| 157 | * @return bool If the extension is installed in the correct directory |
||
| 158 | */ |
||
| 159 | public function isInWrongFolder() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * If the extension is enabled |
||
| 166 | * |
||
| 167 | * @return bool If the extension is enabled |
||
| 168 | */ |
||
| 169 | public function isEnabled() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * If the extension should be updated, i.e. if an updated version is available |
||
| 183 | * |
||
| 184 | * @return bool If an update is available |
||
| 185 | */ |
||
| 186 | public function updateAvailable() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * If the extension is a template |
||
| 199 | * |
||
| 200 | * @return bool If this extension is a template |
||
| 201 | */ |
||
| 202 | public function isTemplate() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get the ID of the extension |
||
| 209 | * |
||
| 210 | * This is the same as getName() for plugins, for templates it's getName() prefixed with 'template:' |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getID() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the name of the installation directory |
||
| 221 | * |
||
| 222 | * @return string The name of the installation directory |
||
| 223 | */ |
||
| 224 | public function getInstallName() |
||
| 228 | |||
| 229 | // Data from plugin.info.txt/template.info.txt or the repo when not available locally |
||
| 230 | /** |
||
| 231 | * Get the basename of the extension |
||
| 232 | * |
||
| 233 | * @return string The basename |
||
| 234 | */ |
||
| 235 | public function getBase() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the display name of the extension |
||
| 243 | * |
||
| 244 | * @return string The display name |
||
| 245 | */ |
||
| 246 | public function getDisplayName() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the author name of the extension |
||
| 255 | * |
||
| 256 | * @return string|bool The name of the author or false if there is none |
||
| 257 | */ |
||
| 258 | public function getAuthor() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the email of the author of the extension if there is any |
||
| 267 | * |
||
| 268 | * @return string|bool The email address or false if there is none |
||
| 269 | */ |
||
| 270 | public function getEmail() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the email id, i.e. the md5sum of the email |
||
| 279 | * |
||
| 280 | * @return string|bool The md5sum of the email if there is any, false otherwise |
||
| 281 | */ |
||
| 282 | public function getEmailID() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the description of the extension |
||
| 291 | * |
||
| 292 | * @return string The description |
||
| 293 | */ |
||
| 294 | public function getDescription() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the URL of the extension, usually a page on dokuwiki.org |
||
| 303 | * |
||
| 304 | * @return string The URL |
||
| 305 | */ |
||
| 306 | public function getURL() |
||
| 307 | { |
||
| 308 | if (!empty($this->localInfo['url'])) return $this->localInfo['url']; |
||
| 309 | return 'https://www.dokuwiki.org/'. |
||
| 310 | ($this->isTemplate() ? 'template' : 'plugin').':'.$this->getBase(); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get the installed version of the extension |
||
| 315 | * |
||
| 316 | * @return string|bool The version, usually in the form yyyy-mm-dd if there is any |
||
| 317 | */ |
||
| 318 | public function getInstalledVersion() |
||
| 319 | { |
||
| 320 | if (!empty($this->localInfo['date'])) return $this->localInfo['date']; |
||
| 321 | if ($this->isInstalled()) return $this->getLang('unknownversion'); |
||
| 322 | return false; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the install date of the current version |
||
| 327 | * |
||
| 328 | * @return string|bool The date of the last update or false if not available |
||
| 329 | */ |
||
| 330 | public function getUpdateDate() |
||
| 331 | { |
||
| 332 | if (!empty($this->managerData['updated'])) return $this->managerData['updated']; |
||
| 333 | return $this->getInstallDate(); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the date of the installation of the plugin |
||
| 338 | * |
||
| 339 | * @return string|bool The date of the installation or false if not available |
||
| 340 | */ |
||
| 341 | public function getInstallDate() |
||
| 342 | { |
||
| 343 | if (!empty($this->managerData['installed'])) return $this->managerData['installed']; |
||
| 344 | return false; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the names of the dependencies of this extension |
||
| 349 | * |
||
| 350 | * @return array The base names of the dependencies |
||
| 351 | */ |
||
| 352 | public function getDependencies() |
||
| 353 | { |
||
| 354 | if (!empty($this->remoteInfo['dependencies'])) return $this->remoteInfo['dependencies']; |
||
| 355 | return array(); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get the names of the missing dependencies |
||
| 360 | * |
||
| 361 | * @return array The base names of the missing dependencies |
||
| 362 | */ |
||
| 363 | public function getMissingDependencies() |
||
| 364 | { |
||
| 365 | /* @var PluginController $plugin_controller */ |
||
| 366 | global $plugin_controller; |
||
| 367 | $dependencies = $this->getDependencies(); |
||
| 368 | $missing_dependencies = array(); |
||
| 369 | foreach ($dependencies as $dependency) { |
||
| 370 | if ($plugin_controller->isdisabled($dependency)) { |
||
| 371 | $missing_dependencies[] = $dependency; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | return $missing_dependencies; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the names of all conflicting extensions |
||
| 379 | * |
||
| 380 | * @return array The names of the conflicting extensions |
||
| 381 | */ |
||
| 382 | public function getConflicts() |
||
| 383 | { |
||
| 384 | if (!empty($this->remoteInfo['conflicts'])) return $this->remoteInfo['conflicts']; |
||
| 385 | return array(); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the names of similar extensions |
||
| 390 | * |
||
| 391 | * @return array The names of similar extensions |
||
| 392 | */ |
||
| 393 | public function getSimilarExtensions() |
||
| 394 | { |
||
| 395 | if (!empty($this->remoteInfo['similar'])) return $this->remoteInfo['similar']; |
||
| 396 | return array(); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get the names of the tags of the extension |
||
| 401 | * |
||
| 402 | * @return array The names of the tags of the extension |
||
| 403 | */ |
||
| 404 | public function getTags() |
||
| 405 | { |
||
| 406 | if (!empty($this->remoteInfo['tags'])) return $this->remoteInfo['tags']; |
||
| 407 | return array(); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the popularity information as floating point number [0,1] |
||
| 412 | * |
||
| 413 | * @return float|bool The popularity information or false if it isn't available |
||
| 414 | */ |
||
| 415 | public function getPopularity() |
||
| 416 | { |
||
| 417 | if (!empty($this->remoteInfo['popularity'])) return $this->remoteInfo['popularity']; |
||
| 418 | return false; |
||
| 419 | } |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the text of the security warning if there is any |
||
| 424 | * |
||
| 425 | * @return string|bool The security warning if there is any, false otherwise |
||
| 426 | */ |
||
| 427 | public function getSecurityWarning() |
||
| 428 | { |
||
| 429 | if (!empty($this->remoteInfo['securitywarning'])) return $this->remoteInfo['securitywarning']; |
||
| 430 | return false; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get the text of the security issue if there is any |
||
| 435 | * |
||
| 436 | * @return string|bool The security issue if there is any, false otherwise |
||
| 437 | */ |
||
| 438 | public function getSecurityIssue() |
||
| 439 | { |
||
| 440 | if (!empty($this->remoteInfo['securityissue'])) return $this->remoteInfo['securityissue']; |
||
| 441 | return false; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get the URL of the screenshot of the extension if there is any |
||
| 446 | * |
||
| 447 | * @return string|bool The screenshot URL if there is any, false otherwise |
||
| 448 | */ |
||
| 449 | public function getScreenshotURL() |
||
| 450 | { |
||
| 451 | if (!empty($this->remoteInfo['screenshoturl'])) return $this->remoteInfo['screenshoturl']; |
||
| 452 | return false; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the URL of the thumbnail of the extension if there is any |
||
| 457 | * |
||
| 458 | * @return string|bool The thumbnail URL if there is any, false otherwise |
||
| 459 | */ |
||
| 460 | public function getThumbnailURL() |
||
| 461 | { |
||
| 462 | if (!empty($this->remoteInfo['thumbnailurl'])) return $this->remoteInfo['thumbnailurl']; |
||
| 463 | return false; |
||
| 464 | } |
||
| 465 | /** |
||
| 466 | * Get the last used download URL of the extension if there is any |
||
| 467 | * |
||
| 468 | * @return string|bool The previously used download URL, false if the extension has been installed manually |
||
| 469 | */ |
||
| 470 | public function getLastDownloadURL() |
||
| 471 | { |
||
| 472 | if (!empty($this->managerData['downloadurl'])) return $this->managerData['downloadurl']; |
||
| 473 | return false; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the download URL of the extension if there is any |
||
| 478 | * |
||
| 479 | * @return string|bool The download URL if there is any, false otherwise |
||
| 480 | */ |
||
| 481 | public function getDownloadURL() |
||
| 482 | { |
||
| 483 | if (!empty($this->remoteInfo['downloadurl'])) return $this->remoteInfo['downloadurl']; |
||
| 484 | return false; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * If the download URL has changed since the last download |
||
| 489 | * |
||
| 490 | * @return bool If the download URL has changed |
||
| 491 | */ |
||
| 492 | public function hasDownloadURLChanged() |
||
| 493 | { |
||
| 494 | $lasturl = $this->getLastDownloadURL(); |
||
| 495 | $currenturl = $this->getDownloadURL(); |
||
| 496 | return ($lasturl && $currenturl && $lasturl != $currenturl); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the bug tracker URL of the extension if there is any |
||
| 501 | * |
||
| 502 | * @return string|bool The bug tracker URL if there is any, false otherwise |
||
| 503 | */ |
||
| 504 | public function getBugtrackerURL() |
||
| 505 | { |
||
| 506 | if (!empty($this->remoteInfo['bugtracker'])) return $this->remoteInfo['bugtracker']; |
||
| 507 | return false; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Get the URL of the source repository if there is any |
||
| 512 | * |
||
| 513 | * @return string|bool The URL of the source repository if there is any, false otherwise |
||
| 514 | */ |
||
| 515 | public function getSourcerepoURL() |
||
| 516 | { |
||
| 517 | if (!empty($this->remoteInfo['sourcerepo'])) return $this->remoteInfo['sourcerepo']; |
||
| 518 | return false; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get the donation URL of the extension if there is any |
||
| 523 | * |
||
| 524 | * @return string|bool The donation URL if there is any, false otherwise |
||
| 525 | */ |
||
| 526 | public function getDonationURL() |
||
| 527 | { |
||
| 528 | if (!empty($this->remoteInfo['donationurl'])) return $this->remoteInfo['donationurl']; |
||
| 529 | return false; |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get the extension type(s) |
||
| 534 | * |
||
| 535 | * @return array The type(s) as array of strings |
||
| 536 | */ |
||
| 537 | public function getTypes() |
||
| 538 | { |
||
| 539 | if (!empty($this->remoteInfo['types'])) return $this->remoteInfo['types']; |
||
| 540 | if ($this->isTemplate()) return array(32 => 'template'); |
||
| 541 | return array(); |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get a list of all DokuWiki versions this extension is compatible with |
||
| 546 | * |
||
| 547 | * @return array The versions in the form yyyy-mm-dd => ('label' => label, 'implicit' => implicit) |
||
| 548 | */ |
||
| 549 | public function getCompatibleVersions() |
||
| 550 | { |
||
| 551 | if (!empty($this->remoteInfo['compatible'])) return $this->remoteInfo['compatible']; |
||
| 552 | return array(); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Get the date of the last available update |
||
| 557 | * |
||
| 558 | * @return string|bool The last available update in the form yyyy-mm-dd if there is any, false otherwise |
||
| 559 | */ |
||
| 560 | public function getLastUpdate() |
||
| 561 | { |
||
| 562 | if (!empty($this->remoteInfo['lastupdate'])) return $this->remoteInfo['lastupdate']; |
||
| 563 | return false; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get the base path of the extension |
||
| 568 | * |
||
| 569 | * @return string The base path of the extension |
||
| 570 | */ |
||
| 571 | public function getInstallDir() |
||
| 572 | { |
||
| 573 | if ($this->isTemplate()) { |
||
| 574 | return $this->tpllib.$this->base; |
||
| 575 | } else { |
||
| 576 | return DOKU_PLUGIN.$this->base; |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * The type of extension installation |
||
| 582 | * |
||
| 583 | * @return string One of "none", "manual", "git" or "automatic" |
||
| 584 | */ |
||
| 585 | public function getInstallType() |
||
| 586 | { |
||
| 587 | if (!$this->isInstalled()) return 'none'; |
||
| 588 | if (!empty($this->managerData)) return 'automatic'; |
||
| 589 | if (is_dir($this->getInstallDir().'/.git')) return 'git'; |
||
| 590 | return 'manual'; |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * If the extension can probably be installed/updated or uninstalled |
||
| 595 | * |
||
| 596 | * @return bool|string True or error string |
||
| 597 | */ |
||
| 598 | public function canModify() |
||
| 599 | { |
||
| 600 | if ($this->isInstalled()) { |
||
| 601 | if (!is_writable($this->getInstallDir())) { |
||
| 602 | return 'noperms'; |
||
| 603 | } |
||
| 604 | } |
||
| 605 | |||
| 606 | if ($this->isTemplate() && !is_writable($this->tpllib)) { |
||
| 607 | return 'notplperms'; |
||
| 608 | } elseif (!is_writable(DOKU_PLUGIN)) { |
||
| 609 | return 'nopluginperms'; |
||
| 610 | } |
||
| 611 | return true; |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Install an extension from a user upload |
||
| 616 | * |
||
| 617 | * @param string $field name of the upload file |
||
| 618 | * @throws Exception when something goes wrong |
||
| 619 | * @return array The list of installed extensions |
||
| 620 | */ |
||
| 621 | public function installFromUpload($field) |
||
| 622 | { |
||
| 623 | if ($_FILES[$field]['error']) { |
||
| 624 | throw new Exception($this->getLang('msg_upload_failed').' ('.$_FILES[$field]['error'].')'); |
||
| 625 | } |
||
| 626 | |||
| 627 | $tmp = $this->mkTmpDir(); |
||
| 628 | if (!$tmp) throw new Exception($this->getLang('error_dircreate')); |
||
| 629 | |||
| 630 | // filename may contain the plugin name for old style plugins... |
||
| 631 | $basename = basename($_FILES[$field]['name']); |
||
| 632 | $basename = preg_replace('/\.(tar\.gz|tar\.bz|tar\.bz2|tar|tgz|tbz|zip)$/', '', $basename); |
||
| 633 | $basename = preg_replace('/[\W]+/', '', $basename); |
||
| 634 | |||
| 635 | if (!move_uploaded_file($_FILES[$field]['tmp_name'], "$tmp/upload.archive")) { |
||
| 636 | throw new Exception($this->getLang('msg_upload_failed')); |
||
| 637 | } |
||
| 638 | |||
| 639 | try { |
||
| 640 | $installed = $this->installArchive("$tmp/upload.archive", true, $basename); |
||
| 641 | $this->updateManagerData('', $installed); |
||
| 642 | $this->removeDeletedfiles($installed); |
||
| 643 | // purge cache |
||
| 644 | $this->purgeCache(); |
||
| 645 | } catch (Exception $e) { |
||
| 646 | throw $e; |
||
| 647 | } |
||
| 648 | return $installed; |
||
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Install an extension from a remote URL |
||
| 653 | * |
||
| 654 | * @param string $url |
||
| 655 | * @throws Exception when something goes wrong |
||
| 656 | * @return array The list of installed extensions |
||
| 657 | */ |
||
| 658 | public function installFromURL($url) |
||
| 659 | { |
||
| 660 | try { |
||
| 661 | $path = $this->download($url); |
||
| 662 | $installed = $this->installArchive($path, true); |
||
| 663 | $this->updateManagerData($url, $installed); |
||
| 664 | $this->removeDeletedfiles($installed); |
||
| 665 | |||
| 666 | // purge cache |
||
| 667 | $this->purgeCache(); |
||
| 668 | } catch (Exception $e) { |
||
| 669 | throw $e; |
||
| 670 | } |
||
| 671 | return $installed; |
||
| 672 | } |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Install or update the extension |
||
| 676 | * |
||
| 677 | * @throws \Exception when something goes wrong |
||
| 678 | * @return array The list of installed extensions |
||
| 679 | */ |
||
| 680 | public function installOrUpdate() |
||
| 681 | { |
||
| 682 | $url = $this->getDownloadURL(); |
||
| 683 | $path = $this->download($url); |
||
| 684 | $installed = $this->installArchive($path, $this->isInstalled(), $this->getBase()); |
||
| 685 | $this->updateManagerData($url, $installed); |
||
| 686 | |||
| 687 | // refresh extension information |
||
| 688 | if (!isset($installed[$this->getID()])) { |
||
| 689 | throw new Exception('Error, the requested extension hasn\'t been installed or updated'); |
||
| 690 | } |
||
| 691 | $this->removeDeletedfiles($installed); |
||
| 692 | $this->setExtension($this->getID()); |
||
| 693 | $this->purgeCache(); |
||
| 694 | return $installed; |
||
| 695 | } |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Uninstall the extension |
||
| 699 | * |
||
| 700 | * @return bool If the plugin was sucessfully uninstalled |
||
| 701 | */ |
||
| 702 | public function uninstall() |
||
| 703 | { |
||
| 704 | $this->purgeCache(); |
||
| 705 | return io_rmdir($this->getInstallDir(), true); |
||
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Enable the extension |
||
| 710 | * |
||
| 711 | * @return bool|string True or an error message |
||
| 712 | */ |
||
| 713 | public function enable() |
||
| 714 | { |
||
| 715 | if ($this->isTemplate()) return $this->getLang('notimplemented'); |
||
| 716 | if (!$this->isInstalled()) return $this->getLang('notinstalled'); |
||
| 717 | if ($this->isEnabled()) return $this->getLang('alreadyenabled'); |
||
| 718 | |||
| 719 | /* @var PluginController $plugin_controller */ |
||
| 720 | global $plugin_controller; |
||
| 721 | if ($plugin_controller->enable($this->base)) { |
||
| 722 | $this->purgeCache(); |
||
| 723 | return true; |
||
| 724 | } else { |
||
| 725 | return $this->getLang('pluginlistsaveerror'); |
||
| 726 | } |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Disable the extension |
||
| 731 | * |
||
| 732 | * @return bool|string True or an error message |
||
| 733 | */ |
||
| 734 | public function disable() |
||
| 735 | { |
||
| 736 | if ($this->isTemplate()) return $this->getLang('notimplemented'); |
||
| 737 | |||
| 738 | /* @var PluginController $plugin_controller */ |
||
| 739 | global $plugin_controller; |
||
| 740 | if (!$this->isInstalled()) return $this->getLang('notinstalled'); |
||
| 741 | if (!$this->isEnabled()) return $this->getLang('alreadydisabled'); |
||
| 742 | if ($plugin_controller->disable($this->base)) { |
||
| 743 | $this->purgeCache(); |
||
| 744 | return true; |
||
| 745 | } else { |
||
| 746 | return $this->getLang('pluginlistsaveerror'); |
||
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Purge the cache by touching the main configuration file |
||
| 752 | */ |
||
| 753 | protected function purgeCache() |
||
| 754 | { |
||
| 755 | global $config_cascade; |
||
| 756 | |||
| 757 | // expire dokuwiki caches |
||
| 758 | // touching local.php expires wiki page, JS and CSS caches |
||
| 759 | @touch(reset($config_cascade['main']['local'])); |
||
| 760 | } |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Read local extension data either from info.txt or getInfo() |
||
| 764 | */ |
||
| 765 | protected function readLocalData() |
||
| 766 | { |
||
| 767 | if ($this->isTemplate()) { |
||
| 768 | $infopath = $this->getInstallDir().'/template.info.txt'; |
||
| 769 | } else { |
||
| 770 | $infopath = $this->getInstallDir().'/plugin.info.txt'; |
||
| 771 | } |
||
| 772 | |||
| 773 | if (is_readable($infopath)) { |
||
| 774 | $this->localInfo = confToHash($infopath); |
||
| 775 | } elseif (!$this->isTemplate() && $this->isEnabled()) { |
||
| 776 | $path = $this->getInstallDir().'/'; |
||
| 777 | $plugin = null; |
||
| 778 | |||
| 779 | foreach (PluginController::PLUGIN_TYPES as $type) { |
||
| 780 | if (file_exists($path.$type.'.php')) { |
||
| 781 | $plugin = plugin_load($type, $this->base); |
||
| 782 | if ($plugin) break; |
||
| 783 | } |
||
| 784 | |||
| 785 | if ($dh = @opendir($path.$type.'/')) { |
||
| 786 | while (false !== ($cp = readdir($dh))) { |
||
| 787 | if ($cp == '.' || $cp == '..' || strtolower(substr($cp, -4)) != '.php') continue; |
||
| 788 | |||
| 789 | $plugin = plugin_load($type, $this->base.'_'.substr($cp, 0, -4)); |
||
| 790 | if ($plugin) break; |
||
| 791 | } |
||
| 792 | if ($plugin) break; |
||
| 793 | closedir($dh); |
||
| 794 | } |
||
| 795 | } |
||
| 796 | |||
| 797 | if ($plugin) { |
||
| 798 | /* @var DokuWiki_Plugin $plugin */ |
||
| 799 | $this->localInfo = $plugin->getInfo(); |
||
| 800 | } |
||
| 801 | } |
||
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Save the given URL and current datetime in the manager.dat file of all installed extensions |
||
| 806 | * |
||
| 807 | * @param string $url Where the extension was downloaded from. (empty for manual installs via upload) |
||
| 808 | * @param array $installed Optional list of installed plugins |
||
| 809 | */ |
||
| 810 | protected function updateManagerData($url = '', $installed = null) |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Read the manager.dat file |
||
| 838 | */ |
||
| 839 | protected function readManagerData() |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Write the manager.data file |
||
| 859 | */ |
||
| 860 | protected function writeManagerData() |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Returns a temporary directory |
||
| 872 | * |
||
| 873 | * The directory is registered for cleanup when the class is destroyed |
||
| 874 | * |
||
| 875 | * @return false|string |
||
| 876 | */ |
||
| 877 | protected function mkTmpDir() |
||
| 884 | |||
| 885 | /** |
||
| 886 | * downloads a file from the net and saves it |
||
| 887 | * |
||
| 888 | * - $file is the directory where the file should be saved |
||
| 889 | * - if successful will return the name used for the saved file, false otherwise |
||
| 890 | * |
||
| 891 | * @author Andreas Gohr <[email protected]> |
||
| 892 | * @author Chris Smith <[email protected]> |
||
| 893 | * |
||
| 894 | * @param string $url url to download |
||
| 895 | * @param string $file path to file or directory where to save |
||
| 896 | * @param string $defaultName fallback for name of download |
||
| 897 | * @return bool|string if failed false, otherwise true or the name of the file in the given dir |
||
| 898 | */ |
||
| 899 | protected function downloadToFile($url, $file, $defaultName = '') |
||
| 900 | { |
||
| 901 | global $conf; |
||
| 902 | $http = new DokuHTTPClient(); |
||
| 903 | $http->max_bodysize = 0; |
||
| 904 | $http->timeout = 25; //max. 25 sec |
||
| 905 | $http->keep_alive = false; // we do single ops here, no need for keep-alive |
||
| 906 | $http->agent = 'DokuWiki HTTP Client (Extension Manager)'; |
||
| 907 | |||
| 908 | $data = $http->get($url); |
||
| 909 | if ($data === false) return false; |
||
| 910 | |||
| 911 | $name = ''; |
||
| 912 | if (isset($http->resp_headers['content-disposition'])) { |
||
| 913 | $content_disposition = $http->resp_headers['content-disposition']; |
||
| 914 | $match = array(); |
||
| 915 | if (is_string($content_disposition) && |
||
| 916 | preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match) |
||
| 917 | ) { |
||
| 918 | $name = \dokuwiki\Utf8\PhpString::basename($match[1]); |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Download an archive to a protected path |
||
| 941 | * |
||
| 942 | * @param string $url The url to get the archive from |
||
| 943 | * @throws Exception when something goes wrong |
||
| 944 | * @return string The path where the archive was saved |
||
| 945 | */ |
||
| 946 | public function download($url) |
||
| 976 | |||
| 977 | /** |
||
| 978 | * @param string $file The path to the archive that shall be installed |
||
| 979 | * @param bool $overwrite If an already installed plugin should be overwritten |
||
| 980 | * @param string $base The basename of the plugin if it's known |
||
| 981 | * @throws Exception when something went wrong |
||
| 982 | * @return array list of installed extensions |
||
| 983 | */ |
||
| 984 | public function installArchive($file, $overwrite = false, $base = '') |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * Find out what was in the extracted directory |
||
| 1076 | * |
||
| 1077 | * Correct folders are searched recursively using the "*.info.txt" configs |
||
| 1078 | * as indicator for a root folder. When such a file is found, it's base |
||
| 1079 | * setting is used (when set). All folders found by this method are stored |
||
| 1080 | * in the 'new' key of the $result array. |
||
| 1081 | * |
||
| 1082 | * For backwards compatibility all found top level folders are stored as |
||
| 1083 | * in the 'old' key of the $result array. |
||
| 1084 | * |
||
| 1085 | * When no items are found in 'new' the copy mechanism should fall back |
||
| 1086 | * the 'old' list. |
||
| 1087 | * |
||
| 1088 | * @author Andreas Gohr <[email protected]> |
||
| 1089 | * @param array $result - results are stored here |
||
| 1090 | * @param string $directory - the temp directory where the package was unpacked to |
||
| 1091 | * @param string $default_type - type used if no info.txt available |
||
| 1092 | * @param string $subdir - a subdirectory. do not set. used by recursion |
||
| 1093 | * @return bool - false on error |
||
| 1094 | */ |
||
| 1095 | protected function findFolders(&$result, $directory, $default_type = 'plugin', $subdir = '') |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Decompress a given file to the given target directory |
||
| 1160 | * |
||
| 1161 | * Determines the compression type from the file extension |
||
| 1162 | * |
||
| 1163 | * @param string $file archive to extract |
||
| 1164 | * @param string $target directory to extract to |
||
| 1165 | * @throws Exception |
||
| 1166 | * @return bool |
||
| 1167 | */ |
||
| 1168 | private function decompress($file, $target) |
||
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Determine the archive type of the given file |
||
| 1203 | * |
||
| 1204 | * Reads the first magic bytes of the given file for content type guessing, |
||
| 1205 | * if neither bz, gz or zip are recognized, tar is assumed. |
||
| 1206 | * |
||
| 1207 | * @author Andreas Gohr <[email protected]> |
||
| 1208 | * @param string $file The file to analyze |
||
| 1209 | * @return string|false false if the file can't be read, otherwise an "extension" |
||
| 1210 | */ |
||
| 1211 | private function guessArchiveType($file) |
||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Copy with recursive sub-directory support |
||
| 1226 | * |
||
| 1227 | * @param string $src filename path to file |
||
| 1228 | * @param string $dst filename path to file |
||
| 1229 | * @return bool|int|string |
||
| 1230 | */ |
||
| 1231 | private function dircopy($src, $dst) |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Delete outdated files from updated plugins |
||
| 1260 | * |
||
| 1261 | * @param array $installed |
||
| 1262 | */ |
||
| 1263 | private function removeDeletedfiles($installed) |
||
| 1292 | } |
||
| 1293 | |||
| 1295 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: