[TASK] Cleanup update wizard in Install Tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / FilemountUpdateWizard.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Tolleiv Nietsch <typo3@tolleiv.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Upgrade wizard which checks all existing filemounts
29 * and upgrades this them in case we have:
30 * a) absolute filemounts (base = 0) without related storage
31 * b) relative filemounts (base = 1) which aren't related to a storage
32 * further we assume that all other filemounts (base > 1) are already related to a storage
33 *
34 * @author Tolleiv Nietsch <typo3@tolleiv.de>
35 */
36 class FilemountUpdateWizard extends AbstractUpdate {
37
38 /**
39 * @var string
40 */
41 protected $title = 'Migrate existing filemounts to be file abstraction layer compatible.';
42
43 /**
44 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
45 */
46 protected $db;
47
48 /**
49 * @var array
50 */
51 protected $sqlQueries = array();
52
53 /**
54 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
55 */
56 protected $storage;
57
58 /**
59 * @var \TYPO3\CMS\Core\Resource\StorageRepository
60 */
61 protected $storageRepository;
62
63 /**
64 * Creates this object
65 */
66 public function __construct() {
67 $this->db = $GLOBALS['TYPO3_DB'];
68 }
69
70 /**
71 * Initialize the storage repository.
72 */
73 public function init() {
74 $this->storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
75 $storages = $this->storageRepository->findAll();
76 $this->storage = $storages[0];
77 }
78
79 /**
80 * Checks if an update is needed.
81 *
82 * @param string &$description The description for the update
83 * @return boolean TRUE if an update is needed, FALSE otherwise
84 */
85 public function checkForUpdate(&$description) {
86 $description = 'Migrate all filemounts to be based on file abstraction layer storages.';
87 $filemountCount = $this->db->exec_SELECTcountRows(
88 '*',
89 'sys_filemounts',
90 'base IN (0,1) ' . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('sys_filemounts')
91 );
92 return $filemountCount > 0 && !$this->isWizardDone();
93 }
94
95 /**
96 * Performs the database update.
97 *
98 * @param array &$dbQueries Queries done in this update
99 * @param mixed &$customMessages Custom messages
100 * @return boolean TRUE on success, FALSE on error
101 */
102 public function performUpdate(array &$dbQueries, &$customMessages) {
103 $this->init();
104 $this->migrateAbsoluteFilemounts();
105 $this->migrateRelativeFilemounts();
106 if (is_array($this->sqlQueries) && is_array($dbQueries)) {
107 $dbQueries = array_merge($dbQueries, $this->sqlQueries);
108 }
109 $this->markWizardAsDone();
110 return TRUE;
111 }
112
113 /**
114 * Takes the existing absolute filemounts (base=0) and migrates them to use
115 * the existing fileadmin/ storage or a new storage.
116 *
117 * @return void
118 */
119 protected function migrateAbsoluteFilemounts() {
120 $description = 'This is the local %s directory. This storage mount has been created by the TYPO3 upgrade wizards.';
121 $fileadminDir = PATH_site . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'];
122 $absoluteFilemounts = $this->db->exec_SELECTgetRows(
123 '*',
124 'sys_filemounts',
125 'base = 0' . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('sys_filemounts')
126 );
127 foreach ($absoluteFilemounts as $filemount) {
128 if (stristr($filemount['path'], $fileadminDir)) {
129 $storageId = $this->storage->getUid();
130 $storagePath = str_replace($fileadminDir, '', $filemount['path']);
131 } else {
132 $storageId = $this->storageRepository->createLocalStorage(
133 $filemount['title'] . ' (auto-created)',
134 $filemount['path'],
135 'absolute',
136 sprintf($description, $filemount['path'])
137 );
138 $storagePath = '/';
139 $this->sqlQueries[] = $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
140 }
141 $this->db->exec_UPDATEquery(
142 'sys_filemounts',
143 'uid=' . intval($filemount['uid']),
144 array('base' => $storageId, 'path' => $storagePath)
145 );
146 $this->sqlQueries[] = $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
147 }
148 }
149
150 /**
151 * Relative filemounts are transformed to relate to our fileadmin/ storage
152 * and their path is modified to be a valid resource location
153 *
154 * @return void
155 */
156 protected function migrateRelativeFilemounts() {
157 $relativeFilemounts = $this->db->exec_SELECTgetRows(
158 '*',
159 'sys_filemounts',
160 'base = 1' . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('sys_filemounts')
161 );
162 foreach ($relativeFilemounts as $filemount) {
163 $this->db->exec_UPDATEquery(
164 'sys_filemounts',
165 'uid=' . intval($filemount['uid']),
166 array('base' => $this->storage->getUid(), 'path' => '/' . ltrim($filemount['path'], '/'))
167 );
168 $this->sqlQueries[] = $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
169 }
170 }
171
172 }
173
174 ?>