-
Notifications
You must be signed in to change notification settings - Fork 17
/
assetfix.php
372 lines (313 loc) · 11.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
<?php
/**
* A JApplicationWeb application built on the Joomla Platform.
*
* To run this place it in the root of your Joomla CMS installation.
* This application is buil
*
* @package Joomla.AssetFix
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
//ini_set('display_errors','1');
error_reporting(-1);
ini_set('display_errors', 'On');
// Set flag that this is a parent file.
// We are a valid Joomla entry point.
define('_JEXEC', 1);
// Setup the base path related constants.
define('JPATH_BASE', dirname(__FILE__));
define('JPATH_SITE', JPATH_BASE);
define('JPATH_CONFIGURATION',JPATH_BASE);
define('JPATH_CACHE',JPATH_BASE . '/cache');
// Bootstrap the application.
require JPATH_BASE . '/libraries/import.php';
// Import the JApplicationWeb class from the platform.
// IS_MAC is not defined in the CMS 3 version of the platform.
if (!defined('IS_MAC'))
{
define('JPATH_LIBRARIES', JPATH_PLATFORM);
require JPATH_BASE . '/libraries/import.legacy.php';
require JPATH_BASE . '/libraries/cms.php';
// Import the JApplicationWeb class from the platform.
JLoader::import('joomla.application.web');
JLoader::import('cms.helper.tags');
JLoader::import('cms.table.corecontent');
JLoader::import('joomla.observer.mapper');
// Categories is in legacy for CMS 3 so we have to check there.
JLoader::registerPrefix('J', JPATH_PLATFORM . '/legacy');
JLoader::Register('J', JPATH_PLATFORM . '/cms');
}
/**
* This class checks some common situations that occur when the asset table is corrupted.
*/
// Instantiate the application.
class Assetfix extends JApplicationWeb
{
/**
* Overrides the parent doExecute method to run the web application.
*
* This method should include your custom code that runs the application.
*
* @return void
*
* @since 11.3
*/
public function __construct()
{
// Call the parent __construct method so it bootstraps the application class.
parent::__construct();
require_once JPATH_CONFIGURATION.'/configuration.php';
jimport('joomla.database.database');
// System configuration.
$config = JFactory::getConfig();
// Note, this will throw an exception if there is an error
// Creating the database connection.
$this->dbo = JDatabase::getInstance(
array(
'driver' => $config->get('dbtype'),
'host' => $config->get('host'),
'user' => $config->get('user'),
'password' => $config->get('password'),
'database' => $config->get('db'),
'prefix' => $config->get('dbprefix'),
)
);
}
protected function doExecute()
{
// Initialise the body with the DOCTYPE.
$this->setBody(
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
);
$this->appendBody('<html>')
->appendBody('<head>')
->appendBody('</head>')
->appendBody('<body style="font-family:verdana; margin-left: 30px; width: 500px;">');
$this->appendBody('<h1>Asset Fix</h1>
<p>This is an unofficial way of fixing the asset table for extensions, categories and articles</p>
<p>It attempts to fix some of the reported issues in asset tables, but is not guaranteed to fix everything</p>'
);
$this->_db = JFactory::getDBO();
$contenttable = JTable::getInstance('Content');
$asset = JTable::getInstance('Asset');
$asset->loadByName('root.1');
if ($asset)
{
$rootId = (int) $asset->id;
}
if ($rootId && ($asset->level != 0 || $asset->parent_id != 0))
{
self::fixRoot($rootId);
}
if (!$asset->id)
{
$rootId = self::getAssetRootId();
self::fixRoot($rootId);
}
if ($rootId === false)
{
// Should the row just be inserted here?
$this->appendBody('<p>There is no valid root. Please manually create a root asset and rerun.</p>');
}
if ($rootId)
{
// Now let's make sure that the components make sense
$query = $this->_db->getQuery(true);
$query->select('extension_id, name');
$query->from($this->_db->quoteName('#__extensions'));
$query->where($this->_db->quoteName('type') . ' = ' . $this->_db->quote('component'));
$this->_db->setQuery($query);
$components = $this->dbo->loadObjectList();
foreach ($components as $component)
{
$asset->reset();
$asset->loadByName($component->name);
if ($asset && ($asset->parent_id != $rootId || $asset->level != 1))
{
self::fixExtensionAsset($asset,$rootId);
$this->appendBody('<p>This asset for this extension was fixed: ' . $component->name . '</p>');
}
elseif (!$asset)
{
$this->appendBody('<p>This extension is missing an asset: ' . $component->name . '</p>');
}
}
// Let's rebuild the categories tree
JTable::getInstance('Category')->rebuild();
// Although we have rebuilt it may not have fully worked. Let's do some extra checks.
$asset = JTable::getInstance('Asset');
$assetTree = $asset->getTree(1);
// Get all the categories as objects
$queryc = $this->_db->getQuery(true);
$queryc->select('id, asset_id, parent_id');
$queryc->from('#__categories');
$this->_db->setQuery($queryc);
$categories = $this->dbo->loadObjectList();
// Create an array of just level 1 assets that look like the are extensons.
$extensionAssets = array();
foreach ($assetTree as $aid => $assetData)
{
// Now we will make a list of components based on the assets table not the extensions table.
if (substr($assetData->name, 0, 4) === 'com_' && $assetData->level ==1)
{
$extensionAssets[$assetData->title] = $assetData->id;
}
}
foreach ($assetTree as $assetData)
{
// Assume the root asset is valid.
if ($assetData->name != 'root.1')
{
// There have been some reports of misnamed contact assets.
if (strpos($assetData->name, 'contact_details') != false)
{
str_replace($assetData->name, 'contact_details', 'contact');
}
// Now handle categories with parent_id of 0 or 1
if (strpos($assetData->name, 'category') != false)
{
$catFixCount = 0;
$fixedCats = array();
// Just assume that they are top level categories.
// We are also goingto fix parent_id of 1 since some people in the forums did this to temporarily
// fix a problem and also categories should never have a parent_id of 1.
if ($assetData->parent_id == 0 || $assetData->parent_id == 1)
{
$catFixCount += 1;
$explodeAssetName = explode('.', $assetData->name);
$assetData->parent_id = $extensionAssets[$explodeAssetName[0]];
$fixedCats[] = $assetData->id;
$asset->load($assetData->id);
// For categories the ultimate parent is the extension
$asset->parent_id = $extensionAssets[$explodeAssetName[0]];
$asset->store();
$asset->reset();
$this->appendBody('<p>The assets for the following category was fixed:' . $assetData->name . ' You will want to
check the category manager to make sure any nesting you require is in place.');
}
}
}
}
// Rebuild again as a final check to clear up any newly created inconsistencies.
JTable::getInstance('Category')->rebuild();
$this->appendBody('<p>Categories were successfully finished.</p>');
// Now we will start work on the articles
$query = $this->_db->getQuery(true);
$query->select('id, asset_id');
$query->from('#__content');
$this->_db->setQuery($query);
$articles = $this->dbo->loadObjectList();
foreach ($articles as $article)
{
$asset->id = 0;
$asset->reset();
// We're going to load the articles by asset name.
if ($article->id > 0)
{
$asset->loadByName('com_content.article.' . (int) $article->id);
$query = $this->_db->getQuery(true);
$query->update($this->_db->quoteName('#__content'));
$query->set($this->_db->quoteName('asset_id') . ' = ' . (int) $asset->id);
$query->where('id = ' . (int) $article->id);
$this->dbo->setQuery($query);
$this->dbo->query();
}
// JTableAssets can clean an empty value for asset_id but not a 0 value.
if ($article->asset_id == 0)
{
$article->asset_id = '';
}
$contenttable->load($article->id);
$contenttable->store();
}
$this->appendBody('<p>Article assets successfully finished.</p>');
$this->appendBody('</li>');
$this->appendBody('</li>
</ul>');
}
// Finish up the HTML response.
$this->appendBody('</body>')
->appendBody('</html>');
}
protected function fixRoot($rootId)
{
// Set up the proper nested values for root
$queryr = $this->_db->getQuery(true);
$queryr->update($this->_db->quoteName('#__assets'));
$queryr->set($this->_db->quoteName('parent_id') . ' = 0 ')
->set($this->_db->quoteName('level') . ' = 0 ' )
->set($this->_db->quoteName('lft') . ' = 1 ')
->set($this->_db->quoteName('name') . ' = ' . $this->_db->quote('root.' . (int) $rootId));
$queryr->where('id = ' . (int) $rootId);
$this->dbo->setQuery($queryr);
$this->dbo->query();
return;
}
/**
* Fix the asset record for extensions
*
* @param JTableAsset $asset The asset table object
* @param integer $rootId The primary key value for the root id, usually 1.
*
* @return mixed The primary id of the root row, or false if not found and the internal error is set.
*
* @since 11.1
*/
protected function fixExtensionAsset($asset, $rootId = 1)
{
// Set up the proper nested values for an extension
$querye = $this->_db->getQuery(true);
$querye->update($this->_db->quoteName('#__assets'));
$querye->set($this->_db->quoteName('parent_id') . ' = ' . $rootId )
->set($this->_db->quoteName('level') . ' = 1 ' );
$querye->where('name = ' . $this->_db->quote($asset->name));
$this->dbo->setQuery($querye);
$this->dbo->query();
return;
}
/**
* Gets the ID of the root item in the tree
*
* @return mixed The primary id of the root row, or false if not found and the internal error is set.
*
* @since 11.1
*/
public function getAssetRootId()
{
// Test for a unique record with parent_id = 0
$query = $this->dbo->getQuery(true);
$query->select($this->dbo->quote('id'))
->from($this->dbo->quoteName('#__assets'))
->where($this->dbo->quote('parent_id') .' = 0');
$result = $this->dbo->setQuery($query)->loadColumn();
if (count($result) == 1)
{
return $result[0];
}
// Test for a unique record with lft = 0
$query = $this->dbo->getQuery(true);
$query->select('id')
->from($this->_db->quoteName('#__assets'))
->where($this->_db->quote('lft') . ' = 0');
$result = $this->_db->setQuery($query)->loadColumn();
if (count($result) == 1)
{
return $result[0];
}
// Test for a unique record alias = root
$query = $this->_db->getQuery(true);
$query->select($this->_db->quoteName('id'))
->from($this->_db->quoteName('#__assets'))
->where('name LIKE ' . $this->_db->quote('root%'));
$result = $this->_db->setQuery($query)->loadColumn();
if (count($result) == 1)
{
return $result[0];
}
$e = new UnexpectedValueException(sprintf('%s::getRootId', get_class($this)));
//$this->setError($e);
return false;
}
}
JApplicationWeb::getInstance('Assetfix')->execute();