Skip to content

Instantly share code, notes, and snippets.

@mbabker
Created April 29, 2014 21:17
Show Gist options
  • Save mbabker/c06c5261d292cc62194d to your computer and use it in GitHub Desktop.
Save mbabker/c06c5261d292cc62194d to your computer and use it in GitHub Desktop.
CLI Script to resave all modules
<?php
/**
* Module Resave Script
*
* @copyright Copyright (C) 2014 Michael Babker. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/
// Set flag that this is a parent file.
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 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';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
// Import the configuration.
require_once JPATH_CONFIGURATION . '/configuration.php';
/**
* Module Resave Script
*
* @since 1.0
*/
class ModuleResave extends JApplicationCli
{
/**
* Method to run the application routines.
*
* @return void
*
* @since 1.0
*/
public function doExecute()
{
$db = JFactory::getDbo();
$db->setQuery(
$db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__modules'))
);
try
{
$modules = $db->loadObjectList();
}
catch (RuntimeException $e)
{
$this->out('Error retrieving module IDs: ' . $e->getMessage());
$this->close();
}
foreach ($modules as $module)
{
/** @type JTableModule $table */
$table = JTable::getInstance('Module');
$table->load($module->id);
try
{
$table->store();
}
catch (Exception $e)
{
$this->out('Error storing module ID ' . $module->id . ': ' . $e->getMessage());
exit();
}
}
}
}
// Global exception handler, this way we can get decent messages if need be
try
{
JApplicationCli::getInstance('ModuleResave')->execute();
}
catch (Exception $e)
{
fwrite(STDOUT, "\nERROR: " . $e->getMessage() . "\n");
fwrite(STDOUT, "\n" . $e->getTraceAsString() . "\n");
exit($e->getCode() ? : 255);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment