Merge branch 'master' of github.com:WoltLab/WCF
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / action / UninstallPackageAction.class.php
1 <?php
2 namespace wcf\acp\action;
3 use wcf\action\AbstractDialogAction;
4 use wcf\data\package\installation\queue\PackageInstallationQueue;
5 use wcf\data\package\installation\queue\PackageInstallationQueueEditor;
6 use wcf\data\package\Package;
7 use wcf\system\exception\IllegalLinkException;
8 use wcf\system\exception\SystemException;
9 use wcf\system\package\PackageUninstallationDispatcher;
10 use wcf\system\WCF;
11 use wcf\util\StringUtil;
12
13 /**
14 * Handles an AJAX-based package uninstallation.
15 *
16 * @author Alexander Ebert
17 * @copyright 2001-2011 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package com.woltlab.wcf
20 * @subpackage acp.action
21 * @category Community Framework
22 */
23 class UninstallPackageAction extends InstallPackageAction {
24 /**
25 * active package id
26 * @var integer
27 */
28 protected $packageID = 0;
29
30 /**
31 * @see wcf\action\AbstractDialogAction::$templateName
32 */
33 public $templateName = 'packageUninstallationStep';
34
35 /**
36 * @see wcf\action\IAction::readParameters()
37 */
38 public function readParameters() {
39 AbstractDialogAction::readParameters();
40
41 if (isset($_POST['node'])) $this->node = StringUtil::trim($_POST['node']);
42
43 if (isset($_POST['packageID'])) {
44 $this->packageID = intval($_POST['packageID']);
45 }
46 else {
47 if (isset($_POST['queueID'])) $this->queueID = intval($_POST['queueID']);
48 $this->queue = new PackageInstallationQueue($this->queueID);
49
50 if (!$this->queue->queueID) {
51 throw new IllegalLinkException();
52 }
53
54 $this->installation = new PackageUninstallationDispatcher($this->queue);
55 }
56 }
57
58 /**
59 * Prepares the uninstallation process.
60 */
61 protected function stepPrepare() {
62 $package = new Package($this->packageID);
63 if (!$package->packageID) {
64 throw new IllegalLinkException();
65 }
66
67 // get new process no
68 $processNo = PackageInstallationQueue::getNewProcessNo();
69
70 // create queue
71 $queue = PackageInstallationQueueEditor::create(array(
72 'processNo' => $processNo,
73 'userID' => WCF::getUser()->userID,
74 'packageName' => $package->getName(),
75 'packageID' => $package->packageID,
76 'action' => 'uninstall',
77 'cancelable' => 0
78 ));
79
80 // initialize uninstallation
81 $this->installation = new PackageUninstallationDispatcher($queue);
82
83 $this->installation->nodeBuilder->purgeNodes();
84 $this->installation->nodeBuilder->buildNodes();
85
86 WCF::getTPL()->assign(array(
87 'queue' => $queue
88 ));
89
90 $queueID = $this->installation->nodeBuilder->getQueueByNode($queue->processNo, $this->installation->nodeBuilder->getNextNode());
91 $this->data = array(
92 'template' => WCF::getTPL()->fetch($this->templateName),
93 'step' => 'uninstall',
94 'node' => $this->installation->nodeBuilder->getNextNode(),
95 'currentAction' => WCF::getLanguage()->get('wcf.package.installation.step.uninstalling'),
96 'progress' => 0,
97 'queueID' => $queueID
98 );
99 }
100
101 /**
102 * Uninstalls node components and returns next node.
103 *
104 * @param string $node
105 * @return string
106 */
107 public function stepUninstall() {
108 $node = $this->installation->uninstall($this->node);
109
110 if ($node == '') {
111 // remove node data
112 $this->installation->nodeBuilder->purgeNodes();
113 $this->finalize($queueID);
114
115 // redirect to application if not already within one
116 if (PACKAGE_ID == 1) {
117 // select first installed application
118 $sql = "SELECT packageID
119 FROM wcf".WCF_N."_package
120 WHERE packageID <> 1
121 AND isApplication = 1
122 ORDER BY installDate ASC";
123 $statement = WCF::getDB()->prepareStatement($sql, 1);
124 $statement->execute();
125 $row = $statement->fetchArray();
126 $packageID = ($row === false) ? 1 : $row['packageID'];
127 }
128 else {
129 $packageID = PACKAGE_ID;
130 }
131
132 // get domain path
133 $sql = "SELECT domainName, domainPath
134 FROM wcf".WCF_N."_application
135 WHERE packageID = ?";
136 $statement = WCF::getDB()->prepareStatement($sql);
137 $statement->execute(array($packageID));
138 $row = $statement->fetchArray();
139
140 // build redirect location
141 $location = $row['domainName'] . $row['domainPath'] . 'acp/index.php/PackageList/' . SID_ARG_1ST;
142
143 // show success
144 $this->data = array(
145 'progress' => 100,
146 'redirectLocation' => $location,
147 'step' => 'success'
148 );
149 return;
150 }
151
152 // continue with next node
153 $queueID = $this->installation->nodeBuilder->getQueueByNode($this->installation->queue->processNo, $this->installation->nodeBuilder->getNextNode($this->node));
154 $this->data = array(
155 'step' => 'uninstall',
156 'node' => $node,
157 'progress' => $this->installation->nodeBuilder->calculateProgress($this->node),
158 'queueID' => $queueID
159 );
160 }
161
162 /**
163 * @see wcf\action\AbstractDialogAction::validateStep()
164 */
165 protected function validateStep() {
166 switch ($this->step) {
167 case 'prepare':
168 case 'uninstall':
169 continue;
170 break;
171
172 default:
173 die(print_r($_POST, true));
174 throw new IllegalLinkException();
175 break;
176 }
177 }
178 }