Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / package / plugin / TemplatePackageInstallationPlugin.class.php
CommitLineData
11ade432
AE
1<?php
2namespace wcf\system\package\plugin;
04727c8b
MS
3use wcf\data\application\Application;
4use wcf\data\package\Package;
870b0938 5use wcf\system\package\PackageArchive;
11ade432
AE
6use wcf\system\package\TemplatesFileHandler;
7use wcf\system\WCF;
11ade432
AE
8
9/**
a17de04e 10 * Installs, updates and deletes templates.
9f959ced 11 *
04727c8b 12 * @author Alexander Ebert, Matthias Schmidt
ca4ba303 13 * @copyright 2001-2014 WoltLab GmbH
11ade432
AE
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package com.woltlab.wcf
16 * @subpackage system.package.plugin
9f959ced 17 * @category Community Framework
11ade432 18 */
2f7488d7 19class TemplatePackageInstallationPlugin extends AbstractPackageInstallationPlugin {
b5be6fb0 20 /**
0ad90fc3 21 * @see \wcf\system\package\plugin\AbstractPackageInstallationPlugin::$tableName
9f959ced 22 */
11ade432
AE
23 public $tableName = 'template';
24
25 /**
0ad90fc3 26 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::install()
11ade432
AE
27 */
28 public function install() {
29 parent::install();
a17de04e 30
04727c8b
MS
31 $abbreviation = 'wcf';
32 if (isset($this->instruction['attributes']['application'])) {
33 $abbreviation = $this->instruction['attributes']['application'];
34 }
35 else if ($this->installation->getPackage()->isApplication) {
36 $abbreviation = Package::getAbbreviation($this->installation->getPackage()->package);
37 }
38
39 // absolute path to package dir
40 $packageDir = Application::getDirectory($abbreviation);
41
11ade432
AE
42 // extract files.tar to temp folder
43 $sourceFile = $this->installation->getArchive()->extractTar($this->instruction['value'], 'templates_');
44
45 // create file handler
04727c8b 46 $fileHandler = new TemplatesFileHandler($this->installation, $abbreviation);
11ade432 47
04727c8b 48 $this->installation->extractFiles($packageDir.'templates/', $sourceFile, $fileHandler);
11ade432
AE
49
50 // delete temporary sourceArchive
51 @unlink($sourceFile);
52 }
53
54 /**
55 * Uninstalls the templates of this package.
56 */
57 public function uninstall() {
04727c8b 58 // fetch templates from log
8ea5b730
MW
59 $sql = "SELECT template.templateName, template.application,
60 template_group.templateGroupFolderName
61 FROM wcf".WCF_N."_template template
62 LEFT JOIN wcf".WCF_N."_template_group template_group
63 ON (template_group.templateGroupID = template.templateGroupID)
64 WHERE packageID = ?";
11ade432
AE
65 $statement = WCF::getDB()->prepareStatement($sql);
66 $statement->execute(array($this->installation->getPackageID()));
04727c8b
MS
67
68 $templates = array();
11ade432 69 while ($row = $statement->fetchArray()) {
04727c8b
MS
70 if (!isset($templates[$row['application']])) {
71 $templates[$row['application']] = array();
72 }
73
8ea5b730 74 $templates[$row['application']][] = 'templates/'.$row['templateGroupFolderName'].$row['templateName'].'.tpl';
11ade432
AE
75 }
76
04727c8b
MS
77 foreach ($templates as $application => $templateNames) {
78 $this->installation->deleteFiles(Application::getDirectory($application), $templateNames, false, $this->installation->getPackage()->isApplication);
11ade432
AE
79
80 // delete log entries
81 parent::uninstall();
82 }
83 }
870b0938
AE
84
85 /**
86 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::isValid()
87 */
88 public static function isValid(PackageArchive $archive, $instruction) {
89 if (preg_match('~\.(tar(\.gz)?|tgz)$~', $instruction)) {
90 // check if file actually exists
91 try {
92 if ($archive->getTar()->getIndexByFilename($instruction) === false) {
93 return false;
94 }
95 }
96 catch (\SystemException $e) {
97 return false;
98 }
99
100 return true;
101 }
102
103 return false;
104 }
11ade432 105}