-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate.php
More file actions
451 lines (451 loc) · 16.7 KB
/
migrate.php
File metadata and controls
451 lines (451 loc) · 16.7 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
/**
* Bludit migration script for v2.3.4 to v3.0
* Tested on PHP7+
*/
// Set PHP max execution time to infinite, since script might take a lot of time depending on no. of posts/pages.
ini_set('max_execution_time', 0);
/**
* Config
*/
define('FILENAME', 'index.txt'); // Set the filename used by your Bludit installation
define('DS', DIRECTORY_SEPARATOR);
define('CHARSET', 'UTF-8');
$migrationDirectoryName = 'migrations';
$contentDirectoryName = 'bl-content';
$migratedContentPath = $migrationDirectoryName . DS . $contentDirectoryName;
function msg($string)
{
// Determine if CLI and set end of line according to the way script is called.
// This is for logging output.
$breakLine = (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) ? PHP_EOL : '<br>';
echo $string . $breakLine;
}
// Core plugins/databases only.
// Not available in v3: discovery
// Available only in v3: robots, simple-stats, updater
$pluginsWhiteList = [
'about',
'api',
'backup',
'categories',
'disqus',
'html-code',
'links',
'maintenance-mode',
'navigation',
'opengraph',
'rss',
'simplemde',
'sitemap',
'static-pages',
'tags',
'tinymce',
'twitter-cards',
'version'
];
$databasesWhiteList = [
'categories.php',
'pages.php',
'security.php',
'site.php',
'tags.php',
'users.php',
'syslog.php'
];
/**
* Helper Functions
* 1. Recursive Copy. Thanks http://php.net/manual/en/function.copy.php
* 2. Recusive Delete recurse_delete() rm -rf. Thanks https://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir
* 3. dd() Pretty print_r
* 4. stripFirstLine() : Removes mandatory BLUDIT string
* 5. insert() : Inserts mandatory BLUDIT string + encoded Json back to database
*/
function recurse_copy($src, $dst)
{
$dir = opendir($src);
@mkdir($dst);
while (false !== ( $file = readdir($dir))) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($src . '/' . $file)) {
recurse_copy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
// Recursive delete
function recurse_delete($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object)) {
recurse_delete($dir."/".$object);
} else {
unlink($dir."/".$object);
}
}
}
rmdir($dir);
}
}
/* Dump and die */
function dd($variable, $die = false)
{
echo "<pre>";
($die) ? die(print_r($variable)) : print_r($variable);
echo "</pre>";
}
// Remove first line function. Helper function to remove string "<?php defined('BLUDIT')" from db
// Thanks ComFreek http://stackoverflow.com/questions/7740405/php-delete-the-first-line-of-a-text-and-return-the-rest
function stripFirstLine($text)
{
return substr($text, strpos($text, "\n")+1);
}
// Insert to database
function insert($filePath, $data)
{
$compulsoryBluditLine = "<?php defined('BLUDIT') or die('Bludit CMS.'); ?>".PHP_EOL;
$content = $compulsoryBluditLine . json_encode($data, JSON_PRETTY_PRINT);
file_put_contents($filePath, $content);
}
// Title
msg('================================');
msg('Bludit Migration from v2.3.4 to v3');
msg('================================');
/**
* First we check if script was copied to the right directory.
*/
if (!file_exists($contentDirectoryName)) {
die("Failed: Script was copied to the wrong directory. Make sure it is copied where $contentDirectoryName exists.");
}
/**
* Create migrations directory (Delete if it already exists)
*/
if (file_exists($migrationDirectoryName)) {
/**
* Empty the folder recursively
* Thanks https://stackoverflow.com/questions/4594180/deleting-all-files-from-a-folder-using-php
*/
recurse_delete($migrationDirectoryName);
}
if (!mkdir('migrations', 0755)) {
die('Failed to create directory. No write permission available.');
} else {
msg("Created Migration Directory...");
}
/**
* Copy everything from bl-content to migrations/
*/
recurse_copy($contentDirectoryName, $migratedContentPath);
$allPages = array_diff(scandir($migratedContentPath . '/pages'), array('.', '..'));
// Delete plugins/databases not present in white list
$allDatabases = array_diff(scandir($migratedContentPath . '/databases'), array('.', '..', 'plugins')); // Ignore plugins directory
$allPlugins = array_diff(scandir($migratedContentPath . '/databases/plugins'), array('.', '..'));
// dd($allDatabases);
// dd($allPlugins);
/* Delete non bludit core plugin/database configs */
foreach ($allDatabases as $database) {
if (! in_array($database, $databasesWhiteList)) {
if (is_dir($migratedContentPath . '/databases/' . $database)) {
recurse_delete($migratedContentPath . '/databases/' . $database);
} else {
unlink($migratedContentPath . '/databases/' . $database);
}
msg("Removed non Bludit core database config: $database");
}
}
foreach ($allPlugins as $plugin) {
if (! in_array($plugin, $pluginsWhiteList)) {
recurse_delete($migratedContentPath . '/databases/plugins/' . $plugin);
msg("Removed non Bludit core plugin config: $plugin");
}
}
/**
* Delete old v2 backup and timemachine-x directories to prevent potential problems in v3
*/
if (file_exists($migratedContentPath . '/backup')) {
recurse_delete($migratedContentPath . '/backup');
msg('Removed old v2 backups from backup plugin...');
}
if (file_exists($migratedContentPath . '/timemachine-x')) {
recurse_delete($migratedContentPath . '/timemachine-x');
msg('Removed old v2 timemachine-x points...');
}
/**
* Create workspaces directory (New v3 feature for plugins)
*/
mkdir($migratedContentPath . '/workspaces', 0755);
// Migrate Core Plugins
// Get Fresh List
msg('Migrating core plugin databases...');
$allPlugins = array_diff(scandir($migratedContentPath . '/databases/plugins'), array('.', '..'));
foreach ($allPlugins as $plugin) {
$tmp = $migratedContentPath . '/databases/plugins/' . $plugin . '/';
switch ($plugin) {
/**
* amountOfItems was changed to numberOfItems
*/
case 'api':
case 'navigation':
case 'rss':
if (file_exists($tmp . 'db.php')) {
$data = stripFirstLine(file_get_contents($tmp . 'db.php'));
$json = json_decode($data);
$json->numberOfItems = $json->amountOfItems;
unset($json->amountOfItems);
// Insert to db
insert($tmp . 'db.php', $json);
/**
* Create a rss workspace and move rss.xml to it.
*/
if ($plugin === 'rss') {
mkdir($migratedContentPath . '/workspaces/rss', 0755);
if (file_exists($tmp . 'rss.xml')) {
rename($tmp . 'rss.xml', $migratedContentPath . '/workspaces/rss/rss.xml');
}
}
} else {
msg("Plugin $plugin db.php not found");
}
// dd($json);
break;
case 'sitemap':
if (file_exists($tmp . 'db.php')) {
/**
* Create a sitemap workspace and move sitemap.xml to it.
*/
mkdir($migratedContentPath . '/workspaces/sitemap', 0755);
if (file_exists($tmp . 'sitemap.xml')) {
rename($tmp . 'sitemap.xml', $migratedContentPath . '/workspaces/sitemap/sitemap.xml');
}
} else {
msg("Plugin $plugin db.php not found");
}
case 'tinymce':
if (file_exists($tmp . 'db.php')) {
$data = stripFirstLine(file_get_contents($tmp . 'db.php'));
$json = json_decode($data);
// New items
$json->toolbar1 = "formatselect bold italic bullist numlist blockquote alignleft aligncenter alignright link pagebreak image removeformat code";
$json->toolbar2 = "";
$json->mobileToolbar = "bold italic bullist formatselect";
$json->plugins = "code autolink image link pagebreak advlist lists textcolor colorpicker textpattern";
// Insert to db
insert($tmp . 'db.php', $json);
} else {
msg("Plugin $plugin db.php not found");
}
// dd($json);
break;
case 'simplemde':
if (file_exists($tmp . 'db.php')) {
$data = stripFirstLine(file_get_contents($tmp . 'db.php'));
$json = json_decode($data);
// Obsolete
unset($json->autosave);
// Insert to db
insert($tmp . 'db.php', $json);
} else {
msg("Plugin $plugin db.php not found");
}
// dd($json);
break;
}
}
/**
* Enable simple-stats and robots plugin (Available only on v3)
*/
mkdir($migratedContentPath . '/databases/plugins/robots', 0755);
insert($migratedContentPath . '/databases/plugins/robots/db.php', [
'position' => 1
]);
mkdir($migratedContentPath . '/databases/plugins/simple-stats', 0755);
// Create workspace
mkdir($migratedContentPath . '/workspaces/simple-stats', 0755);
insert($migratedContentPath . '/databases/plugins/simple-stats/db.php', [
'numberOfDays' => 7,
'label' => 'Visits',
'excludeAdmins'=> false,
'position' => 1
]);
// Migrate Core Databases to v3.0
// Get Fresh List
$allDatabases = array_diff(scandir($migratedContentPath . '/databases'), array('.', '..', 'plugins')); // Ignore plugins directory
foreach ($allDatabases as $database) {
$tmp = $migratedContentPath . '/databases/' . $database;
switch ($database) {
case 'categories.php':
if (file_exists($tmp)) {
$data = stripFirstLine(file_get_contents($tmp));
$json = json_decode($data);
foreach ($json as $category) {
// New values
$category->description = '';
$category->template = '';
}
// Insert to db
insert($tmp, $json);
} else {
msg("Database $database not found");
}
break;
case 'pages.php':
if (file_exists($tmp)) {
$data = stripFirstLine(file_get_contents($tmp));
$json = json_decode($data);
$count = 0;
foreach ($json as $key => $page) {
$pageObject = new Page($key);
// Get title from index.txt
$page->title = $pageObject->vars['title'];
if (empty($page->title)) {
msg('WARNING: Page with empty title found.');
}
$page->type = $page->status;
unset($page->status);
// Insert rawContent to index.txt
file_put_contents($migratedContentPath.DS.'pages'.DS.$key.DS.FILENAME, $pageObject->vars['contentRaw']);
// Recalculate new index.txt md5
$page->md5file = md5_file($migratedContentPath.DS.'pages'.DS.$key.DS.FILENAME);
$page->noindex = false;
$page->nofollow = false;
$page->noarchive = false;
$count++;
}
// Insert to db
insert($tmp, $json);
msg("Migrated $count pages...");
} else {
msg("Database $database not found");
}
break;
case 'site.php':
if (file_exists($tmp)) {
$data = stripFirstLine(file_get_contents($tmp));
$json = json_decode($data);
// New values
// Safer compat
$json->theme = 'blogx';
$json->adminTheme = 'booty';
$json->currentBuild = 20180821;
$json->instagram = '';
$json->gitlab = '';
$json->linkedin = '';
$json->extremeFriendly = true;
$json->autosaveInterval = 2;
$json->titleFormatHomepage = '{{site-slogan}} | {{site-title}}';
$json->titleFormatPages = '{{page-title}} | {{site-title}}';
$json->titleFormatCategory = '{{category-name}} | {{site-title}}';
$json->titleFormatTag = '{{tag-name}} | {{site-title}}';
// Set uriCategory if not available.
$json->uriCategory = isset($json->uriCategory) ? $json->uriCategory : '/category/';
// Insert to db
insert($tmp, $json);
} else {
msg("Database $database not found");
}
break;
case 'users.php':
if (file_exists($tmp)) {
$data = stripFirstLine(file_get_contents($tmp));
$json = json_decode($data);
foreach ($json as $user) {
// New values
$user->nickname = ($user === 'admin') ? 'Admin' : $user->firstName;
$user->codepen = '';
$user->linkedin = '';
$user->github = '';
$user->gitlab = '';
}
// Insert to db
insert($tmp, $json);
} else {
msg("Database $database not found");
}
break;
// No changes in security.php, syslog.php, tags.php
}
}
msg("Fixing Permissions...");
chmod($contentDirectoryName, 0755);
msg("Successfuly migrated.");
/**
* Customised Bludit v2.3.4 page.class.php for migration purpose
*/
class Page
{
public $vars;
public function __construct($key)
{
$this->vars = false;
if ($this->build($key)) {
$this->vars['key'] = $key;
}
}
// Parse the content from the file index.txt
public function build($key)
{
// Get path from script.
global $migratedContentPath;
$filePath = $migratedContentPath.DS.'pages'.DS.$key.DS.FILENAME;
// Check if the file exists
if (!is_file($filePath)) {
return false;
}
$tmp = 0;
$file = file($filePath);
foreach ($file as $lineNumber => $line) {
// Split the line in 2 parts, limiter by :
$parts = explode(':', $line, 2);
$field = $parts[0]; // title, date, slug
$value = isset($parts[1])?$parts[1]:false; // value of title, value of date
// Remove all characters except letters and dash - from field
$field = preg_replace('/[^A-Za-z\-]/', '', $field);
// Field to lowercase
$field = mb_strtolower($field, CHARSET);
// Check if the current line start the content of the page
// We have two breakers, the word content or 3 dash ---
if ($field==='content') {
$tmp = $lineNumber;
$styleTypeUsed = 'Content:';
break;
}
if ($field==='---') {
$tmp = $lineNumber;
$styleTypeUsed = '---';
break;
}
if (!empty($field) && !empty($value)) {
// Remove missing dashs -
$field = preg_replace('/[^A-Za-z]/', '', $field);
// Remove <-- and -->
$value = preg_replace('/<\-\-/', '', $value);
$value = preg_replace('/\-\->/', '', $value);
// Remove empty spaces on borders
$value = trim($value);
// Position accepts only integers
if ($field=='position') {
$value = preg_replace('/[^0-9]/', '', $value);
}
// Sanitize all fields, except the content
$this->vars[$field] = $value;
}
}
// Process the content
if ($tmp!==0) {
// Get all lines starting from "Content:" or "---"
$content = array_slice($file, $tmp);
// Remove "Content:" or "---" and keep next characters if there are
$content[0] = substr($content[0], strpos($content[0], $styleTypeUsed) + strlen($styleTypeUsed));
$content[0] = ltrim($content[0]);
// Join lines in one variable, this is RAW content from file
$this->vars['contentRaw'] = implode($content);
}
return true;
}
}