FOF30\Update\Update::refreshUpdateSite PHP Метод

refreshUpdateSite() публичный Метод

Refreshes the Joomla! update sites for this extension as needed
public refreshUpdateSite ( ) : void
Результат void
    public function refreshUpdateSite()
    {
        if (empty($this->extension_id)) {
            return;
        }
        // Create the update site definition we want to store to the database
        $update_site = array('name' => $this->updateSiteName, 'type' => 'extension', 'location' => $this->updateSite, 'enabled' => 1, 'last_check_timestamp' => 0, 'extra_query' => $this->extraQuery);
        // Get a reference to the db driver
        $db = $this->container->db;
        // Get the #__update_sites columns
        $columns = $db->getTableColumns('#__update_sites', true);
        if (version_compare(JVERSION, '3.0.0', 'lt') || !array_key_exists('extra_query', $columns)) {
            unset($update_site['extra_query']);
        }
        // Get the update sites for our extension
        $updateSiteIds = $this->getUpdateSiteIds();
        if (empty($updateSiteIds)) {
            $updateSiteIds = array();
        }
        /** @var boolean $needNewUpdateSite Do I need to create a new update site? */
        $needNewUpdateSite = true;
        /** @var int[] $deleteOldSites Old Site IDs to delete */
        $deleteOldSites = array();
        // Loop through all update sites
        foreach ($updateSiteIds as $id) {
            $query = $db->getQuery(true)->select('*')->from($db->qn('#__update_sites'))->where($db->qn('update_site_id') . ' = ' . $db->q($id));
            $db->setQuery($query);
            $aSite = $db->loadObject();
            if (empty($aSite)) {
                // Update site is now up-to-date, don't need to refresh it anymore.
                continue;
            }
            // We have an update site that looks like ours
            if ($needNewUpdateSite && $aSite->name == $update_site['name'] && $aSite->location == $update_site['location']) {
                $needNewUpdateSite = false;
                $mustUpdate = false;
                // Is it enabled? If not, enable it.
                if (!$aSite->enabled) {
                    $mustUpdate = true;
                    $aSite->enabled = 1;
                }
                // Do we have the extra_query property (J 3.2+) and does it match?
                if (property_exists($aSite, 'extra_query') && isset($update_site['extra_query']) && $aSite->extra_query != $update_site['extra_query']) {
                    $mustUpdate = true;
                    $aSite->extra_query = $update_site['extra_query'];
                }
                // Update the update site if necessary
                if ($mustUpdate) {
                    $db->updateObject('#__update_sites', $aSite, 'update_site_id', true);
                }
                continue;
            }
            // In any other case we need to delete this update site, it's obsolete
            $deleteOldSites[] = $aSite->update_site_id;
        }
        if (!empty($deleteOldSites)) {
            try {
                $obsoleteIDsQuoted = array_map(array($db, 'quote'), $deleteOldSites);
                // Delete update sites
                $query = $db->getQuery(true)->delete('#__update_sites')->where($db->qn('update_site_id') . ' IN (' . implode(',', $obsoleteIDsQuoted) . ')');
                $db->setQuery($query)->execute();
                // Delete update sites to extension ID records
                $query = $db->getQuery(true)->delete('#__update_sites_extensions')->where($db->qn('update_site_id') . ' IN (' . implode(',', $obsoleteIDsQuoted) . ')');
                $db->setQuery($query)->execute();
            } catch (\Exception $e) {
                // Do nothing on failure
                return;
            }
        }
        // Do we still need to create a new update site?
        if ($needNewUpdateSite) {
            // No update sites defined. Create a new one.
            $newSite = (object) $update_site;
            $db->insertObject('#__update_sites', $newSite);
            $id = $db->insertid();
            $updateSiteExtension = (object) array('update_site_id' => $id, 'extension_id' => $this->extension_id);
            $db->insertObject('#__update_sites_extensions', $updateSiteExtension);
        }
    }

Usage Example

Пример #1
0
 /**
  * Refreshes the update sites, removing obsolete update sites in the process
  */
 public function refreshUpdateSite()
 {
     // Remove any update sites for the old com_akeeba package
     $this->removeObsoleteComponentUpdateSites();
     // Refresh our update sites
     parent::refreshUpdateSite();
 }