This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategories.php
204 lines (175 loc) · 5.12 KB
/
categories.php
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
<?php
/**
* UCOZ to Joomla data converter
*
* @copyright Copyright (C) 2019 Libra.ms. All rights reserved.
* @license GNU General Public License version 2 or later
* @url http://киноархив.com
*/
/**
* This is a script to convert categories from Ucoz to Joomla which should be called from the command-line, not the web.
* Example: /path/to/php /path/to/site/ucoz_converter/categories.php
*/
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
if (PHP_SAPI !== 'cli')
{
die('This script can run only in CLI mode!');
}
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
require_once __DIR__ . '/application/helper.php';
use Joomla\CMS\Factory;
use Joomla\Filesystem\Path;
use Joomla\Filter\InputFilter;
// This will prevent 'Failed to start application' error.
try
{
$app = Factory::getApplication('site');
}
catch (Exception $e)
{
jexit($e->getMessage());
}
/**
* Class for categories.
*
* @since 0.1
* @noinspection PhpUnused
*/
Class ConverterCategories extends JApplicationCli
{
/**
* Convert categories and save in Joomla database.
*
* @return void
* @since 0.1
* @throws Exception
*/
public function doExecute()
{
$outputLog = "======= " . date('Y-m-d H:i:s', time()) . " =======\n";
JLoader::register('CategoriesHelper', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/categories.php');
$filter = new InputFilter;
$lang = Factory::getLanguage();
$lang->load('lib_joomla');
$config = ConverterHelper::loadConfig();
$backupPath = Path::clean($config->get('backupPath') . '/_s1');
$loads = ConverterHelper::loadBackupFile($backupPath . '/ld_ld.txt');
$news = ConverterHelper::loadBackupFile($backupPath . '/nw_nw.txt');
$publ = ConverterHelper::loadBackupFile($backupPath . '/pu_pu.txt');
$categories = array();
if ($loads !== false)
{
foreach ($loads as $rows)
{
$cols = explode('|', $rows);
$categories['loads'][$cols[0]] = $cols[5];
}
}
else
{
$msg = "Could not load backup file $backupPath/ld_ld.txt";
$outputLog .= $msg;
echo "\n$msg\n";
}
if ($news !== false)
{
foreach ($news as $rows)
{
$cols = explode('|', $rows);
$categories['news'][$cols[0]] = $cols[3];
}
}
else
{
$msg = "Could not load backup file $backupPath/nw_nw.txt";
$outputLog .= $msg;
echo "\n$msg\n";
}
if ($publ !== false)
{
foreach ($publ as $rows)
{
$cols = explode('|', $rows);
$categories['publ'][$cols[0]] = $cols[5];
}
}
else
{
$msg = "Could not load backup file $backupPath/pu_pu.txt";
$outputLog .= $msg;
echo "\n$msg\n";
}
$totalRows = array_sum(array_map('count', $categories));
$totalImported = 0;
$totalErrors = 0;
$ids = ConverterHelper::getAssocData(__DIR__ . '/imports/categories_import.json');
foreach ($categories as $type => $titles)
{
$totalTitles = count($titles);
$i = 1;
echo "===============================\n"
. "$type\n"
. "===============================\n";
foreach ($titles as $key => $category)
{
$table = array();
$table['title'] = $filter->clean($category, 'string');
$table['parent_id'] = 1;
$table['extension'] = 'com_content';
$table['language'] = $config->get('categoriesLang');
$table['published'] = 1;
$table['params'] = json_encode(array('category_layout' => '', 'image' => ''));
$table['metadata'] = json_encode(array('author' => '', 'robots' => ''));
$table['created_user_id'] = (int) $config->get('categoriesUserId');
if (!empty($ids) && @array_key_exists($key, $ids[$type]))
{
$table['id'] = $ids[$type][$key];
$isNew = 0;
}
else
{
$isNew = 1;
}
$catid = CategoriesHelper::createCategory($table);
if (!empty($catid))
{
$totalImported++;
$ids[$type][$key] = $catid;
$txt = $isNew ? ' - imported.' : ' - updated.';
$msg = $i . ' of ' . $totalTitles . '. Category: "' . $category . $txt . "\n";
$outputLog .= $msg;
}
else
{
$totalErrors++;
$msg = $i . ' of ' . $totalTitles . '. Category: "' . $category . '" - error(possible duplicate title).' . "\n";
$outputLog .= $msg;
}
echo $msg;
$i++;
}
}
// Store two dimensional array with type, Joomla ID, Ucoz ID as JSON.
// E.g.: array('news' => array('joomla_id' => 'ucoz_id', ...), ...)
ConverterHelper::saveAssocData(__DIR__ . '/imports/categories_import.json', $ids);
$succMsg = "\n" . 'Total categories: ' . $totalRows . '.' .
"\n" . 'Categories imported: ' . $totalImported . '.' .
"\n" . 'Errors found: ' . $totalErrors . "\n";
$outputLog .= $succMsg;
file_put_contents(__DIR__ . '/imports/categories_import.log', $outputLog . "\n\n", FILE_APPEND);
echo $succMsg;
}
}
JApplicationCli::getInstance('ConverterCategories')->execute();