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