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 pathdelete_articles.php
265 lines (229 loc) · 6.76 KB
/
delete_articles.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
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
<?php
/**
* UCOZ to Joomla data converter
*
* @copyright Copyright (C) 2020 Libra.ms. All rights reserved.
* @license GNU General Public License version 2 or later
* @url http://киноархив.com
*/
/**
* This is a script to delete all imported articles in Joomla which should be called from the
* command-line, not the web.
* Example: /path/to/php /path/to/site/ucoz_converter/delete_articles.php --blog
*
* Required parameters:
* --blog - convert blog.
* --news - convert news.
* --publ - convert publications.
* --loads - convert loads.
*/
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\Cache\Cache;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
// This will prevent 'Failed to start application' error.
try
{
$app = Factory::getApplication('site');
}
catch (Exception $e)
{
jexit($e->getMessage());
}
/**
* Class for articles management.
*
* @since 0.1
* @noinspection PhpUnused
*/
Class ConverterDeleteArticles extends JApplicationCli
{
/**
* The context used for the associations table
*
* @var string
* @since 3.4.4
*/
protected $associationsContext = 'com_content.item';
/**
* Delete all previously imported articles.
*
* @return void
* @since 0.1
* @throws Exception
*/
public function doExecute()
{
$lang = Factory::getLanguage();
$lang->load('lib_joomla');
$lang->load('com_content');
// Get cmd options
$opts = getopt("",
array(
'blog' => 'blog::',
'news' => 'news::',
'publ' => 'publ::',
'loads' => 'loads::'
)
);
if (array_key_exists('blog', $opts))
{
$type = 'blog';
}
elseif (array_key_exists('news', $opts))
{
$type = 'news';
}
elseif (array_key_exists('publ', $opts))
{
$type = 'publ';
}
elseif (array_key_exists('loads', $opts))
{
$type = 'loads';
}
else
{
$msg = 'Wrong content type! Available types: --news, --blog, --publ, --loads';
die($msg . "\n\nExample: /usr/bin/php /path/to/site/ucoz_converter/delete_articles.php --blog\n");
}
$articlesIDs = ConverterHelper::getAssocData(__DIR__ . '/imports/' . $type . '_ids.json');
// Load IDs file.
if (empty($articlesIDs) || count($articlesIDs) < 1)
{
jexit('Could not load file at ' . $type . '_ids.json. Maybe file is empty.' . "\n");
}
$table = JTable::getInstance('Content', 'JTable', array());
$db = Factory::getDbo();
$totalRows = count($articlesIDs);
$totalDeleted = 0;
$totalErrors = 0;
$featured = array();
$outputLog = "======= " . date('Y-m-d H:i:s', time()) . " =======\n";
echo $outputLog;
foreach ($articlesIDs as $ucozID => $joomlaID)
{
if ($table->load($joomlaID))
{
// Multilanguage: if associated, delete the item in the _associations table
if ($this->associationsContext && JLanguageAssociations::isEnabled())
{
$query = $db->getQuery(true)
->select('COUNT(*) as count, ' . $db->quoteName('as1.key'))
->from($db->quoteName('#__associations') . ' AS as1')
->join('LEFT', $db->quoteName('#__associations') . ' AS as2 ON ' . $db->quoteName('as1.key') . ' = ' . $db->quoteName('as2.key'))
->where($db->quoteName('as1.context') . ' = ' . $db->quote($this->associationsContext))
->where($db->quoteName('as1.id') . ' = ' . (int) $joomlaID)
->group($db->quoteName('as1.key'));
$db->setQuery($query);
$row = $db->loadAssoc();
if (!empty($row['count']))
{
$query = $db->getQuery(true)
->delete($db->quoteName('#__associations'))
->where($db->quoteName('context') . ' = ' . $db->quote($this->associationsContext))
->where($db->quoteName('key') . ' = ' . $db->quote($row['key']));
if ($row['count'] > 2)
{
$query->where($db->quoteName('id') . ' = ' . (int) $joomlaID);
}
$db->setQuery($query);
$db->execute();
}
}
if (!$table->delete($joomlaID))
{
$totalErrors++;
$msg = 'Article ID: ' . $joomlaID . '. ' . Text::_('JERROR_AN_ERROR_HAS_OCCURRED') . ' ' . $table->getError();
}
else
{
$featured[] = $joomlaID;
$totalDeleted++;
$msg = 'Article with ID: ' . $joomlaID . ' - deleted.' . "\n";
unset($articlesIDs[$ucozID]);
}
}
else
{
$totalErrors++;
$msg = 'Article ID: ' . $joomlaID . '. ' . Text::_('JERROR_AN_ERROR_HAS_OCCURRED') . ' ' . $table->getError();
}
$outputLog .= $msg;
echo $msg;
}
// Remove featured items
$query = $db->getQuery(true)
->delete($db->quoteName('#__content_frontpage'))
->where('content_id IN (' . implode(',', array_values($featured)) . ')');
$db->setQuery($query);
$db->execute();
// Clear the component's cache
try
{
$this->cleanCache('com_content');
$this->cleanCache('mod_articles_archive');
$this->cleanCache('mod_articles_categories');
$this->cleanCache('mod_articles_category');
$this->cleanCache('mod_articles_latest');
$this->cleanCache('mod_articles_news');
$this->cleanCache('mod_articles_popular');
}
catch (Exception $e)
{
echo $e->getMessage() . "\n";
}
ConverterHelper::saveAssocData(__DIR__ . '/imports/' . $type . '_ids.json', $articlesIDs);
$succMsg = "\n" . 'Total articles: ' . $totalRows . '.' .
"\n" . 'Articles deleted: ' . $totalDeleted . '.' .
"\n" . 'Errors found: ' . $totalErrors . "\n";
$outputLog .= $succMsg;
file_put_contents(__DIR__ . '/imports/' . $type . '_delete.log', $outputLog . "\n\n", FILE_APPEND);
echo $succMsg;
}
/**
* Clean the cache
*
* @param string $group The cache group
* @param integer $clientID The ID of the client
*
* @return void
*
* @throws Exception
* @since 0.1
*/
private function cleanCache($group = null, $clientID = 0)
{
$config = Factory::getConfig();
$option = 'com_content';
$options = array(
'defaultgroup' => $group ?: (isset($option) ? $option : Factory::getApplication()->input->get('option')),
'cachebase' => $clientID ? JPATH_ADMINISTRATOR . '/cache' : $config->get('cache_path', JPATH_SITE . '/cache'),
'result' => true,
);
/** @var JCacheControllerCallback $cache */
$cache = Cache::getInstance('callback', $options);
$cache->clean();
// Trigger the onContentCleanCache event.
JEventDispatcher::getInstance()->trigger('onContentCleanCache', $options);
}
}
JApplicationCli::getInstance('ConverterDeleteArticles')->execute();