-
Notifications
You must be signed in to change notification settings - Fork 2
/
assetfix.php
582 lines (483 loc) · 16.2 KB
/
assetfix.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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/**
* @package Joomla.Cli
*
* @copyright Copyright (C) 2013 AtomTech, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// We are a valid entry point.
const _JEXEC = 1;
// 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';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
// Configure error reporting to maximum for CLI output.
error_reporting(E_ALL);
ini_set('display_errors', 1);
/**
* This class checks some common situations that occur when the asset table is corrupted.
*
* @package Joomla.Cli
* @since 3.1
*/
class AssetFixCli extends JApplicationCli
{
/**
* Overrides the parent doExecute method to run the web application.
*
* This method should include your custom code that runs the application.
*
* @since 3.1
*/
public function __construct()
{
// Import the dependencies.
jimport('joomla.table.asset');
// Call the parent __construct method so it bootstraps the application class.
parent::__construct();
// Add the logger.
JLog::addLogger(array('text_file' => 'assetfix.php'));
}
/**
* Entry point for CLI script.
*
* @return void
*
* @since 3.1
*/
protected function doExecute()
{
// Backup the tables to modify.
$this->out('Creating Backup...');
$tables = array('#__assets', '#__categories', '#__content');
$this->doBackup($tables);
// Cleanup the asset table.
$this->out('Populate database with default assets table...')
->populateDatabase('./sql/assets.sql');
// Fixing the extensions assets.
$this->out('Creating extensions assets...')
->fixExtensionsAssets();
// Fixing the categories assets.
$this->out('Creating category assets...')
->fixCategoryAssets();
// Fixing the content assets.
$this->out('Creating content assets...')
->fixContentAssets();
$this->out();
// End message.
$this->out('Finished assets updates!');
}
/**
* Backup tables.
*
* @param array $tables Array with the name of tables to backup.
*
* @return boolean
*
* @since 3.1
* @throws Exception
*/
protected function doBackup($tables)
{
$count = count($tables);
for ($i = 0; $i < $count; $i++)
{
// Rename the tables.
$table = $tables[$i];
$rename = $tables[$i] . "_backup";
$exists = $this->_existsTable($rename);
if ($exists == 0)
{
$this->_copyTable($table, $rename);
}
}
}
/**
* Copy table to old site to new site.
*
* @param string $from The old table structure.
* @param string $to The new table structure.
*
* @return boolean
*
* @since 3.1
* @throws Exception
*/
protected function _copyTable($from, $to = null)
{
// Initialiase variables.
$db = JFactory::getDbo();
if (!$to)
{
$to = $from;
}
if ($this->_cloneTable($from, $to))
{
// Set the query.
$db->setQuery('INSERT INTO ' . $to . ' SELECT * FROM ' . $from);
try
{
$db->execute();
}
catch (Exception $e)
{
// Display the error.
$this->out($e->getMessage(), true);
// Close the app.
$this->close($e->getCode());
}
}
return true;
}
/**
* Clone old table structure from.
*
* @param string $from The old table structure.
* @param string $to The new table structure.
* @param string $drop Drop the table.
*
* @return boolean
*
* @since 3.1
* @throws Exception
*/
protected function _cloneTable($from, $to = null, $drop = true)
{
// Initialiase variables.
$db = JFactory::getDbo();
if (!$to)
{
$to = $from;
}
if ($this->_existsTable($from) == 0)
{
return false;
}
else
{
// Set the query.
$db->setQuery('CREATE TABLE ' . $to . ' LIKE ' . $from);
try
{
$db->execute();
}
catch (Exception $e)
{
// Display the error.
$this->out($e->getMessage(), true);
// Close the app.
$this->close($e->getCode());
}
}
return true;
}
/**
* Verify if exists table.
*
* @param string $table The table name.
*
* @return boolean
*
* @since 3.1
* @throws Exception
*/
function _existsTable($table)
{
// System configuration.
$config = JFactory::getConfig();
$database = $config->get('db');
// Initialiase variables.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$table = preg_replace('/#__/', $db->getPrefix(), $table);
// Prepare query.
$query->select('COUNT(*) AS count')
->from('information_schema.tables')
->where('table_schema = "' . $database . '"')
->where('table_name = "' . $table . '"');
// Inject the query and load the result.
$db->setQuery($query);
$result = $db->loadResult();
// Check for a database error.
if ($db->getErrorNum())
{
JError::raiseWarning(500, $db->getErrorMsg());
return null;
}
return $result;
}
/**
* Populate database.
*
* @param string $sqlfile The sql file.
*
* @return boolean
*
* @since 3.1
* @throws Exception
*/
function populateDatabase($sqlfile)
{
// Initialiase variables.
$db = JFactory::getDbo();
$buffer = file_get_contents($sqlfile);
if (!$buffer)
{
return -1;
}
$queries = $db->splitSql($buffer);
foreach ($queries as $query)
{
$query = trim($query);
if ($query != '' && $query {0} != '#')
{
// Set the query.
$db->setQuery($query);
try
{
$db->execute();
}
catch (Exception $e)
{
// Display the error.
$this->out($e->getMessage(), true);
// Close the app.
$this->close($e->getCode());
}
}
}
return true;
}
/**
* Fix the assets of extensions table.
*
* @return void
*
* @since 3.1
*/
protected function fixExtensionsAssets()
{
// Initialiase variables.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Prepare query.
$query->select('name, element')
->from('#__extensions')
->where('type = "component"')
->where('protected = 0')
->group('element');
// Inject the query and load the extensions.
$db->setQuery($query);
$extensions = $db->loadObjectList();
// Get an instance of the asset table.
$asset = JTable::getInstance('Asset');
foreach ($extensions as $extension)
{
$asset->id = 0;
// Reset class properties.
$asset->reset();
// Load an asset by name.
$asset->loadByName($extension->element);
if ($asset->id == 0)
{
// Setting the name and title.
$asset->name = $extension->element;
$asset->title = $extension->name;
// Getting the original rules.
$query = $db->getQuery(true);
// Prepare query.
$query->select('rules')
->from('#__assets_backup')
->where('name = "' . $extension->element . '"');
// Inject the query and load the result.
$db->setQuery($query);
$rules = $db->loadResult();
// Add the rules.
$asset->rules = $rules !== null ? $rules : '{"core.admin":{"7":1},"core.manage":{"6":1},"core.create":[],"core.delete":[],"core.edit":[],"core.edit.state":[],"core.edit.own":[]}';
// Setting the location of the new extension.
$asset->setLocation(1, 'last-child');
// Store the row.
$asset->store();
}
}
}
/**
* Fix the assets of category table.
*
* @return void
*
* @since 3.1
*/
protected function fixCategoryAssets()
{
// Initialiase variables.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Prepare query.
$query->select('*');
$query->from('#__categories');
$query->where('id != 1');
// Inject the query and load the categories.
$db->setQuery($query);
$categories = $db->loadObjectList();
foreach ($categories as $category)
{
// Fixing name of the extension.
$category->extension = $category->extension == 'com_contact_details' ? 'com_contact' : $category->extension;
// Get an instance of the asset table.
$asset = JTable::getInstance('Asset');
// Load an asset by name.
$name = $category->extension . '.category.' . (int) $category->id;
$asset->loadByName($name);
$asset->title = $category->title;
$asset->name = $name;
// Getting the original rules.
$query = $db->getQuery(true);
// Prepare query.
$query->select('rules')
->from('#__assets_backup')
->where('name = "' . $asset->name . '"');
// Inject the query and load the result.
$db->setQuery($query);
$rules = $db->loadResult();
if ($category->parent_id !== false)
{
// Setting the parent.
$parent = 0;
if ($category->parent_id == 1)
{
// Get an instance of the asset table.
$parentAsset = JTable::getInstance('Asset');
$parentAsset->loadByName($category->extension);
$parent = $parentAsset->id;
}
elseif ($category->parent_id > 1)
{
// Getting the correct parent.
$query = $db->getQuery(true);
// Prepare query.
$query->select('a.id')
->from('#__categories AS c')
->join('LEFT', '#__assets AS a ON a.title = c.title')
->where('c.id = ' . (int) $category->parent_id);
// Inject the query and load the result.
$db->setQuery($query);
$parent = $db->loadResult();
}
// Setting the location of the new category.
$asset->setLocation($parent, 'last-child');
}
// Add the rules.
$asset->rules = $rules !== null ? $rules : '{"core.admin":{"7":1},"core.manage":{"6":1},"core.create":[],"core.delete":[],"core.edit":[],"core.edit.state":[]}';
// Store the row.
$asset->store();
// Fixing the category asset_id.
$query = $db->getQuery(true);
// Prepare query.
$query->update($db->quoteName('#__categories'))
->set($db->quoteName('asset_id') . ' = ' . (int) $asset->id)
->where('id = ' . (int) $category->id);
// Inject the query and load the result.
$db->setQuery($query)
->query();
}
}
/**
* Fix the assets of content table.
*
* @return void
*
* @since 3.1
*/
protected function fixContentAssets()
{
// Initialiase variables.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
//Some performance fix for big data sites, btw it's better to make while loop with fetching next rows in it, but it also works fine
$query->select('COUNT(*)')->from('#__content');
$db->setQuery($query);
$count = $db->loadResult();
$count = (int) $count - 1;
$limit = 100;
$start = 0;
while($start < $count) {
// Prepare query.
$query = $db->getQuery(true);
$query->select('*')->from('#__content');
// Inject the query and load the result.
$db->setQuery($query, $start, $limit);
$contents = $db->loadObjectList();
foreach ($contents as $article)
{
// Get an instance of the asset table.
$table = JTable::getInstance('Asset');
$table->title = $article->title;
$table->name = 'com_content.article.' . $article->id;
// Getting the original rules.
$query = $db->getQuery(true);
// Prepare query.
$query->select('rules')
->from('#__assets_backup')
->where('name = "' . $table->name . '"');
// Inject the query and load the result.
$db->setQuery($query);
$rules = $db->loadResult();
// Setting the parent.
$parent = 0;
if ($article->catid !== false)
{
if ($article->catid == 1)
{
// Get an instance of the asset table.
$parentAsset = JTable::getInstance('Asset');
$parentAsset->loadByName('com_content');
$parent = $parentAsset->id;
}
elseif ($article->catid > 1)
{
// Getting the correct parent.
$query = $db->getQuery(true);
// Prepare query.
$query->select('a.id')
->from('#__categories AS c')
->join('LEFT', '#__assets AS a ON a.title = c.title')
->where('c.id = ' . (int) $article->catid);
// Inject the query and load the result.
$db->setQuery($query);
$parent = $db->loadResult();
}
// Setting the location of the new content.
$table->setLocation($parent, 'last-child');
}
// Add the rules.
$table->rules = $rules !== null ? $rules : '{"core.delete":{"6":1},"core.edit":{"6":1,"4":1},"core.edit.state":{"6":1,"5":1}}';
// Store the row.
$table->store();
// Fixing the content asset_id.
$query = $db->getQuery(true);
// Prepare query.
$query->update($db->quoteName('#__content'))
->set($db->quoteName('asset_id') . ' = ' . (int) $table->id)
->where('id = ' . (int) $article->id);
// Inject the query and load the result.
$db->setQuery($query)
->query();
}
$start += $limit;
}
}
}
// Instantiate the application object, passing the class name to JCli::getInstance
// and use chaining to execute the application.
JApplicationCli::getInstance('AssetFixCli')->execute();