Back to Changelog class

Method loadFromXml

public bool
loadFromXml
(mixed $url)
Loads an XML file from a URL.
Parameters
  • string $url The URL.
Returns
  • bool True on success
Since
  • 4.0.0
Class: Changelog
Project: Joomla

Method loadFromXml - Source code

/**
 * Loads an XML file from a URL.
 *
 * @param   string  $url  The URL.
 *
 * @return  boolean  True on success
 *
 * @since   4.0.0
 */
public function loadFromXml($url)
{
    $version = new Version();
    $httpOption = new Registry();
    $httpOption->set('userAgent', $version->getUserAgent('Joomla', true, false));
    try {
        $http = HttpFactory::getHttp($httpOption);
        $response = $http->get($url);
    } catch (RuntimeException $e) {
        $response = null;
    }
    if ($response === null || $response->code !== 200) {
        // @todo: Add a 'mark bad' setting here somehow
        Log::add(Text::sprintf('JLIB_UPDATER_ERROR_EXTENSION_OPEN_URL', $url), Log::WARNING, 'jerror');
        return false;
    }
    $this->currentChangelog = new \stdClass();
    $this->xmlParser = xml_parser_create('');
    xml_set_object($this->xmlParser, $this);
    xml_set_element_handler($this->xmlParser, 'startElement', 'endElement');
    xml_set_character_data_handler($this->xmlParser, 'characterData');
    if (!xml_parse($this->xmlParser, $response->body)) {
        Log::add(sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($this->xmlParser)), xml_get_current_line_number($this->xmlParser)), Log::WARNING, 'updater');
        return false;
    }
    xml_parser_free($this->xmlParser);
    return true;
}