Back to Updater class

Method getUpdateObjectsForSite

private array
getUpdateObjectsForSite
(mixed $updateSite, mixed $minimumStability = self::STABILITY_STABLE, mixed $includeCurrent = false)
Loads the contents of an update site record $updateSite and returns the update objects
Parameters
  • array $updateSite The update site record to process
  • int $minimumStability Minimum stability for the returned update records
  • bool $includeCurrent Should I also include the current version?
Returns
  • array The update records. Empty array if no updates are found.
Since
  • 3.6.0
Class: Updater
Project: Joomla

Method getUpdateObjectsForSite - Source code

/**
 * Loads the contents of an update site record $updateSite and returns the update objects
 *
 * @param   array  $updateSite        The update site record to process
 * @param   int    $minimumStability  Minimum stability for the returned update records
 * @param   bool   $includeCurrent    Should I also include the current version?
 *
 * @return  array  The update records. Empty array if no updates are found.
 *
 * @since   3.6.0
 */
private function getUpdateObjectsForSite($updateSite, $minimumStability = self::STABILITY_STABLE, $includeCurrent = false)
{
    $retVal = array();
    $this->setAdapter($updateSite['type']);
    if (!isset($this->_adapters[$updateSite['type']])) {
        // Ignore update sites requiring adapters we don't have installed
        return $retVal;
    }
    $updateSite['minimum_stability'] = $minimumStability;
    // Get the update information from the remote update XML document
    /** @var UpdateAdapter $adapter */
    $adapter = $this->_adapters[$updateSite['type']];
    $update_result = $adapter->findUpdate($updateSite);
    // Version comparison operator.
    $operator = $includeCurrent ? 'ge' : 'gt';
    if (\is_array($update_result)) {
        // If we have additional update sites in the remote (collection) update XML document, parse them
        if (\array_key_exists('update_sites', $update_result) && \count($update_result['update_sites'])) {
            $thisUrl = trim($updateSite['location']);
            $thisId = (int) $updateSite['update_site_id'];
            foreach ($update_result['update_sites'] as $extraUpdateSite) {
                $extraUrl = trim($extraUpdateSite['location']);
                $extraId = (int) $extraUpdateSite['update_site_id'];
                // Do not try to fetch the same update site twice
                if ($thisId == $extraId || $thisUrl == $extraUrl) {
                    continue;
                }
                $extraUpdates = $this->getUpdateObjectsForSite($extraUpdateSite, $minimumStability);
                if (\count($extraUpdates)) {
                    $retVal = array_merge($retVal, $extraUpdates);
                }
            }
        }
        if (\array_key_exists('updates', $update_result) && \count($update_result['updates'])) {
            /** @var \Joomla\CMS\Table\Update $current_update */
            foreach ($update_result['updates'] as $current_update) {
                $current_update->extra_query = $updateSite['extra_query'];
                /** @var \Joomla\CMS\Table\Update $update */
                $update = Table::getInstance('update');
                /** @var \Joomla\CMS\Table\Extension $extension */
                $extension = Table::getInstance('extension');
                $uid = $update->find(array('element' => $current_update->get('element'), 'type' => $current_update->get('type'), 'client_id' => $current_update->get('client_id'), 'folder' => $current_update->get('folder')));
                $eid = $extension->find(array('element' => $current_update->get('element'), 'type' => $current_update->get('type'), 'client_id' => $current_update->get('client_id'), 'folder' => $current_update->get('folder')));
                if (!$uid) {
                    // Set the extension id
                    if ($eid) {
                        // We have an installed extension, check the update is actually newer
                        $extension->load($eid);
                        $data = json_decode($extension->manifest_cache, true);
                        if (version_compare($current_update->version, $data['version'], $operator) == 1) {
                            $current_update->extension_id = $eid;
                            $retVal[] = $current_update;
                        }
                    } else {
                        // A potentially new extension to be installed
                        $retVal[] = $current_update;
                    }
                } else {
                    $update->load($uid);
                    // We already have an update in the database lets check whether it has an extension_id
                    if ((int) $update->extension_id === 0 && $eid) {
                        // The current update does not have an extension_id but we found one. Let's use it.
                        $current_update->extension_id = $eid;
                    }
                    // If there is an update, check that the version is newer then replaces
                    if (version_compare($current_update->version, $update->version, $operator) == 1) {
                        $retVal[] = $current_update;
                    }
                }
            }
        }
    }
    return $retVal;
}