forked from clickstorm/cs_seo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.ext_update.php
More file actions
212 lines (184 loc) · 5.82 KB
/
class.ext_update.php
File metadata and controls
212 lines (184 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Update class for the extension manager.
*
* @package TYPO3
* @subpackage tx_csseo
*/
class ext_update {
const FOLDER_CATEGORY_IMAGES = '/_migrated/news_categories';
/**
* Array of flash messages (params) array[][status,title,message]
*
* @var array
*/
protected $messageArray = [];
/**
* @var \TYPO3\CMS\Core\Database\DatabaseConnection
*/
protected $databaseConnection;
/**
* Constructor
*/
public function __construct() {
$this->databaseConnection = $GLOBALS['TYPO3_DB'];
}
/**
* Main update function called by the extension manager.
*
* @return string
*/
public function main() {
$this->processUpdates();
return $this->generateOutput();
}
/**
* Called by the extension manager to determine if the update menu entry
* should by showed.
*
* @return bool
* @todo find a better way to determine if update is needed or not.
*/
public function access() {
return TRUE;
}
/**
* The actual update function. Add your update task in here.
*
* @return void
*/
protected function processUpdates() {
$this->migrateFromMetaseo();
$this->migrateFromSeoBasics();
}
/**
* Check if metaseo was installed and then transfer the properties from pages and pages_language_overlay
*
* @return void
*/
protected function migrateFromMetaseo() {
// check if tx_metaseo fields exists
$metaseoTableFields = $this->databaseConnection->admin_get_fields('pages');
if (!array_key_exists('tx_metaseo_pagetitle', $metaseoTableFields)) {
$status = FlashMessage::NOTICE;
$title = 'metaseo not found';
$message = 'No MetaSeo properties, so no update needed';
$this->messageArray[] = [$status, $title, $message];
return;
}
// update title only if absolute title
$this->updateTitleOnly('tx_metaseo_pagetitle', 'pages');
// migrate pages
$fieldsToMigrate = [
'tx_metaseo_pagetitle' => 'tx_csseo_title',
'tx_metaseo_pagetitle_rel' => 'tx_csseo_title',
'tx_metaseo_canonicalurl' => 'tx_csseo_canonical',
'tx_metaseo_is_exclude' => 'tx_csseo_no_index',
];
$this->migrateFields($fieldsToMigrate, 'pages');
// update title only if absolute title
$this->updateTitleOnly('tx_metaseo_pagetitle', 'pages_language_overlay');
// migrate pages_language_overlay
$fieldsToMigrate = [
'tx_metaseo_pagetitle' => 'tx_csseo_title',
'tx_metaseo_pagetitle_rel' => 'tx_csseo_title',
];
$this->migrateFields($fieldsToMigrate, 'pages_language_overlay');
/**
* Finished migration from metaseo
*/
$message = 'Title, Canonical and NoIndex are migrated. Run <strong>DB compare</strong> in the install tool to remove the fields from metaseo and run the <strong>DB check</strong> to update the reference index.';
$status = FlashMessage::OK;
$title = 'Migrated all metaseo fields!';
$this->messageArray[] = [$status, $title, $message];
}
/**
* Check if seo_basics was installed and then transfer the properties from pages and pages_language_overlay
*
* @return void
*/
protected function migrateFromSeoBasics() {
// check if seo_basics fields exists
$metaseoTableFields = $this->databaseConnection->admin_get_fields('pages');
if (!array_key_exists('tx_seo_titletag', $metaseoTableFields)) {
$status = FlashMessage::NOTICE;
$title = 'seo_basics not found';
$message = 'No seo_basics properties, so no update needed';
$this->messageArray[] = [$status, $title, $message];
return;
}
// migrate pages
$fieldsToMigrate = [
'tx_seo_titletag' => 'tx_csseo_title',
'tx_seo_canonicaltag' => 'tx_csseo_canonical',
];
$this->migrateFields($fieldsToMigrate, 'pages');
$this->migrateFields($fieldsToMigrate, 'pages_language_overlay');
/**
* Finished migration from seo_basics
*/
$message = 'Title and Canonical are migrated. Run <strong>DB compare</strong> in the install tool to remove the fields from metaseo and run the <strong>DB check</strong> to update the reference index.';
$status = FlashMessage::OK;
$title = 'Migrated all seo_basics fields!';
$this->messageArray[] = [$status, $title, $message];
}
/**
* migrate the title only field, if absolute title-tag is set
* @param string $field
* @param string $table
*/
protected function updateTitleOnly($field, $table) {
// Settings from page
$query = 'UPDATE ' . $table . ' SET tx_csseo_title_only = 1';
$query .= ' WHERE ' . $field . ' > \'\'';
$GLOBALS['TYPO3_DB']->sql_query($query);
}
/**
* Migrate fields from one column to another of a table
*
* @param array $fieldsToMigrate
* @param string $table
*
* @return void
*/
protected function migrateFields($fieldsToMigrate, $table) {
foreach($fieldsToMigrate as $oldField => $newField) {
// Settings from page
$query = 'UPDATE ' . $table . ' SET '.$newField.' = '.$oldField;
$query .= ' WHERE ' . $newField . ' = "" OR ' . $newField . ' IS NULL ';
$GLOBALS['TYPO3_DB']->sql_query($query);
}
}
/**
* Generates output by using flash messages
*
* @return string
*/
protected function generateOutput() {
$output = '';
foreach ($this->messageArray as $messageItem) {
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
$flashMessage = GeneralUtility::makeInstance(
'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
$messageItem[2],
$messageItem[1],
$messageItem[0]);
$output .= $flashMessage->render();
}
return $output;
}
}