ab193cb97de6fc0ff7d651398fd580a823629d49
[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 * Upgrade wizard which checks all existing filemounts
28 * and upgrades this them in case we have:
29 * a) absolute filemounts (base = 0) without related storage
30 * b) relative filemounts (base = 1) which aren't related to a storage
31 * further we assume that all other filemounts (base > 1) are already related to a storage
32 *
33 * @author Tolleiv Nietsch <typo3@tolleiv.de>
34 * @license http://www.gnu.org/copyleft/gpl.html
35 */
36 class FilemountUpdateWizard extends \TYPO3\CMS\Install\Updates\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('*', 'sys_filemounts', 'base IN (0,1) ' . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('sys_filemounts'));
88 return $filemountCount > 0 && !$this->isWizardDone();
89 }
90
91 /**
92 * Performs the database update.
93 *
94 * @param array &$dbQueries: queries done in this update
95 * @param mixed &$customMessages: custom messages
96 * @return boolean TRUE on success, FALSE on error
97 */
98 public function performUpdate(array &$dbQueries, &$customMessages) {
99 $this->init();
100 $this->migrateAbsoluteFilemounts();
101 $this->migrateRelativeFilemounts();
102 if (is_array($this->sqlQueries) && is_array($dbQueries)) {
103 $dbQueries = array_merge($dbQueries, $this->sqlQueries);
104 }
105 $this->markWizardAsDone();
106 return TRUE;
107 }
108
109 /**
110 * Takes the existing absolute filemounts (base=0) and migrates them to use
111 * the existing fileadmin/ storage or a new storage.
112 */
113 protected function migrateAbsoluteFilemounts() {
114 $description = 'This is the local %s directory. This storage mount has been created by the TYPO3 upgrade wizards.';
115 $fileadminDir = PATH_site . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'];
116 $absoluteFilemounts = $this->db->exec_SELECTgetRows('*', 'sys_filemounts', 'base = 0' . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('sys_filemounts'));
117 foreach ($absoluteFilemounts as $filemount) {
118 if (stristr($filemount['path'], $fileadminDir)) {
119 $storageId = $this->storage->getUid();
120 $storagePath = str_replace($fileadminDir, '', $filemount['path']);
121 } else {
122 $storageId = $this->storageRepository->createLocalStorage($filemount['title'] . ' (auto-created)', $filemount['path'], 'absolute', sprintf($description, $filemount['path']));
123 $storagePath = '/';
124 $this->sqlQueries[] = $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
125 }
126 $this->db->exec_UPDATEquery('sys_filemounts', 'uid=' . intval($filemount['uid']), array('base' => $storageId, 'path' => $storagePath));
127 $this->sqlQueries[] = $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
128 }
129 }
130
131 /**
132 * Relative filemounts are transformed to relate to our fileadmin/ storage
133 * and their path is modified to be a valid resource location
134 */
135 protected function migrateRelativeFilemounts() {
136 $relativeFilemounts = $this->db->exec_SELECTgetRows('*', 'sys_filemounts', 'base = 1' . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('sys_filemounts'));
137 foreach ($relativeFilemounts as $filemount) {
138 $this->db->exec_UPDATEquery('sys_filemounts', 'uid=' . intval($filemount['uid']), array('base' => $this->storage->getUid(), 'path' => '/' . ltrim($filemount['path'], '/')));
139 $this->sqlQueries[] = $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
140 }
141 }
142
143 }
144
145
146 ?>