function updateNext() { $db = JFactory::getDBO(); // Load URLs to update $query = "SELECT `id`, `sefurl`, `origurl`, `Itemid` FROM `#__sefurls` WHERE `dateadd` = '0000-00-00' AND `locked` = '0' AND `flag` = '1' LIMIT 25"; $db->setQuery($query); $rows = $db->loadObjectList(); // Check that there's anything to update if (is_null($rows) || count($rows) == 0) { // Done echo json_encode(array('type' => 'completed', 'updated' => 0)); jexit(); } // OK, we've got some data, let's update them // First, we need to delete the URLs to be updated $ids = array(); $count = count($rows); for ($i = 0; $i < $count; $i++) { $ids[] = $rows[$i]->id; } $ids = implode(',', $ids); $query = "DELETE FROM `#__sefurls` WHERE `id` IN ({$ids})"; $db->setQuery($query); if (!$db->query()) { echo json_encode(array('type' => 'error', 'msg' => $db->stderr(true))); jexit(); } // Suppress all the normal output ob_start(); // Loop through URLs and update them one by one $mainframe = JFactory::getApplication(); $router = $mainframe->getRouter(); $sefRouter = new JRouterJoomsef(); for ($i = 0; $i < $count; $i++) { $row =& $rows[$i]; $url = $row->origurl; $oldSef = $row->sefurl; if (!empty($row->Itemid)) { if (strpos($url, '?') !== false) { $url .= '&'; } else { $url .= '?'; } $url .= 'Itemid=' . $row->Itemid; } $oldUri = new JURI($url); $newSefUri = $sefRouter->build($router, $oldUri); // JURI::toString() returns bad results when used with some UTF characters! $newSefUrl = JoomSefUri::getUri($newSefUri); $newSef = ltrim(str_replace(JURI::root(), '', $newSefUrl), '/'); // If the SEF URL changed, we need to add it to 301 redirection table if ($oldSef != $newSef) { // Check that the redirect does not already exist $query = "SELECT `id` FROM `#__sefmoved` WHERE `old` = " . $db->quote($oldSef) . " AND `new` = " . $db->quote($newSef) . " LIMIT 1"; $db->setQuery($query); $id = $db->loadResult(); if (!$id) { $query = "INSERT INTO `#__sefmoved` (`old`, `new`) VALUES (" . $db->quote($oldSef) . ", " . $db->quote($newSef) . ")"; $db->setQuery($query); if (!$db->query()) { echo json_encode(array('type' => 'error', 'msg' => $db->stderr(true))); jexit(); } } } } ob_end_clean(); echo json_encode(array('type' => 'updatestep', 'updated' => $count)); jexit(); }
function updateNext() { $db =& JFactory::getDBO(); $sefConfig =& SEFConfig::getConfig(); // Load all the URLs $query = "SELECT `id`, `sefurl`, `origurl`, `Itemid` FROM `#__sefurls` WHERE `locked` = '0' AND `trashed` = '0' AND `flag` = '1' LIMIT 25"; $db->setQuery($query); $rows = $db->loadObjectList(); // Check that there's anything to update if (is_null($rows) || count($rows) == 0) { // Done //$db->setQuery("SELECT COUNT(`id`) FROM `#__sefurls` WHERE `dateadd` = '0000-00-00' AND `locked` = '0' AND `trashed` = '0'"); //$count = $db->loadResult(); $obj = new stdClass(); $obj->type = 'completed'; $obj->updated = 0; return json_encode($obj); } // Load the needed classes jimport('joomla.application.router'); require_once JPATH_ROOT . DS . 'includes' . DS . 'application.php'; require_once JPATH_ROOT . DS . 'components' . DS . 'com_sef' . DS . 'sef.router.php'; if (SEFTools::JoomFishInstalled()) { require_once JPATH_ROOT . DS . 'components' . DS . 'com_joomfish' . DS . 'helpers' . DS . 'defines.php'; JLoader::register('JoomfishManager', JOOMFISH_ADMINPATH . DS . 'classes' . DS . 'JoomfishManager.class.php'); JLoader::register('JoomFishVersion', JOOMFISH_ADMINPATH . DS . 'version.php'); JLoader::register('JoomFish', JOOMFISH_PATH . DS . 'helpers' . DS . 'joomfish.class.php'); } // OK, we've got some data, let's update them // First, we need to trash all the URLs to be updated without the meta tags loss $ids = array(); $count = count($rows); for ($i = 0; $i < $count; $i++) { $ids[] = $rows[$i]->id; } $ids = implode(',', $ids); $query = "UPDATE `#__sefurls` SET `trashed` = '1', `flag` = '0' WHERE `id` IN ({$ids})"; $db->setQuery($query); if (!$db->query()) { return "{ type: 'error' }"; } // Create an instance of JoomSEF router $router = new JRouterJoomsef(); // Check if JoomFish is present if (SEFTools::JoomFishInstalled()) { // We need to fool JoomFish to think we're running in frontend $mainframe =& JFactory::getApplication(); $mainframe->_clientId = 0; // Load and initialize JoomFish plugin if (!class_exists('plgSystemJFDatabase')) { require JPATH_PLUGINS . DS . 'system' . DS . 'jfdatabase.php'; } $params =& JPluginHelper::getPlugin('system', 'jfdatabase'); $dispatcher =& JDispatcher::getInstance(); $plugin = new plgSystemJFDatabase($dispatcher, (array) $params); $plugin->onAfterInitialise(); // Set the mainframe back to its original state $mainframe->_clientId = 1; } // Suppress all the normal output ob_start(); // Loop through URLs and update them one by one for ($i = 0; $i < $count; $i++) { $row =& $rows[$i]; $url = $row->origurl; $oldSef = $row->sefurl; if (!empty($row->Itemid)) { if (strpos($url, '?') !== false) { $url .= '&'; } else { $url .= '?'; } $url .= 'Itemid=' . $row->Itemid; } $newSefUri = $router->build($url); // JURI::toString() returns bad results when used with some UTF characters! $newSef = ltrim(str_replace(JURI::root(), '', $newSefUri->_uri), '/'); // If the SEF URL changed, we need to add it to 301 redirection table if ($oldSef != $newSef) { // Check that the redirect does not already exist $query = "SELECT `id` FROM `#__sefmoved` WHERE `old` = '{$oldSef}' AND `new` = '{$newSef}' LIMIT 1"; $db->setQuery($query); $id = $db->loadResult(); if (!$id) { $query = "INSERT INTO `#__sefmoved` (`old`, `new`) VALUES ('{$oldSef}', '{$newSef}')"; $db->setQuery($query); $db->query(); } } } ob_end_clean(); $obj = new stdClass(); $obj->type = 'updatestep'; $obj->updated = $count; return json_encode($obj); }