Skip to content

Commit

Permalink
replace cachedata
Browse files Browse the repository at this point in the history
  • Loading branch information
tontof committed May 2, 2013
1 parent 244ae7a commit 635305f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
29 changes: 12 additions & 17 deletions src/class/Feed.php
Expand Up @@ -1196,7 +1196,7 @@ public function readHeader($url, $str) {
*
* @return DOMDocument DOMDocument corresponding to the XML URL
*/
public function loadXml($xmlUrl, &$cachedata)
public function loadXml($xmlUrl, &$etag, &$lastModified)
{
// reinitialize cache headers
$this->_headers = array();
Expand All @@ -1214,28 +1214,23 @@ public function loadXml($xmlUrl, &$cachedata)
);

// http headers
if (isset($cachedata)) {
if (empty($opts['http']['headers'])) {
$opts['http']['headers'] = array();
}

if (!empty($cachedata['last-modified'])) {
$opts['http']['headers'][] = 'If-Modified-Since: ' . $cachedata['last-modified'];
}
if (!empty($cachedata['etag'])) {
$opts['http']['headers'][] = 'If-None-Match: ' . $cachedata['etag'];
}
$opts['http']['headers'] = array();
if (!empty($lastModified)) {
$opts['http']['headers'][] = 'If-Modified-Since: ' . $lastModified;
}
if (!empty($etag)) {
$opts['http']['headers'][] = 'If-None-Match: ' . $etag;
}

$document = new DOMDocument();

if (in_array('curl', get_loaded_extensions())) {
$output = $this->loadUrl($xmlUrl, $opts);
if ($output['isnew']) {
$cachedata['etag'] = $output['etag'];
$cachedata['last-modified'] = $output['last-modified'];
$etag = $output['etag'];
$lastModified = $output['last-modified'];
}
$document->loadXml($output['data']);
$document->loadXML($output['data']);
} else {
// try using libxml
$context = stream_context_create($opts);
Expand All @@ -1261,7 +1256,7 @@ public function addChannel($xmlUrl)
{
$feedHash = MyTool::smallHash($xmlUrl);
if (!isset($this->_data['feeds'][$feedHash])) {
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['cachedata']);
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['etag'], $this->_data['feeds'][$feedHash]['last-modified']);

if (!$xml) {
return false;
Expand Down Expand Up @@ -1371,7 +1366,7 @@ public function updateChannel($feedHash)

unset($this->_data['feeds'][$feedHash]['error']);
$xmlUrl = $this->_data['feeds'][$feedHash]['xmlUrl'];
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['cachedata']);
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['etag'], $this->_data['feeds'][$feedHash]['last-modified']);

if (!$xml) {
if (file_exists($this->cacheDir.'/'.$feedHash.'.php')) {
Expand Down
31 changes: 13 additions & 18 deletions src/index.php
Expand Up @@ -6329,6 +6329,7 @@ public function curl_exec_follow(&$ch, $redirects = 20, $curloptHeader = false)
curl_setopt($ch, CURLOPT_HEADER, $curloptHeader);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects);

$data = curl_exec($ch);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
Expand Down Expand Up @@ -6380,7 +6381,7 @@ public function readHeader($url, $str) {
return strlen($str);
}

public function loadXml($xmlUrl, &$cachedata)
public function loadXml($xmlUrl, &$etag, &$lastModified)
{
// reinitialize cache headers
$this->_headers = array();
Expand All @@ -6398,29 +6399,23 @@ public function loadXml($xmlUrl, &$cachedata)
);

// http headers
if (isset($cachedata)) {
if (empty($opts['http']['headers'])) {
$opts['http']['headers'] = array();
}

if (!empty($cachedata['last-modified'])) {
$opts['http']['headers'][] = 'If-Modified-Since: ' . $cachedata['last-modified'];
}
if (!empty($cachedata['etag'])) {
$opts['http']['headers'][] = 'If-None-Match: ' . $cachedata['etag'];
}
$opts['http']['headers'] = array();
if (!empty($lastModified)) {
$opts['http']['headers'][] = 'If-Modified-Since: ' . $lastModified;
}
if (!empty($etag)) {
$opts['http']['headers'][] = 'If-None-Match: ' . $etag;
}

$document = new DOMDocument();

if (in_array('curl', get_loaded_extensions())) {
$output = $this->loadUrl($xmlUrl, $opts);
if ($output['isnew']) {
$cachedata['xml'] = $output['data'];
$cachedata['etag'] = $output['etag'];
$cachedata['last-modified'] = $output['last-modified'];
$etag = $output['etag'];
$lastModified = $output['last-modified'];
}
$document->loadXml($cachedata['xml']);
$document->loadXML($output['data']);
} else {
// try using libxml
$context = stream_context_create($opts);
Expand All @@ -6439,7 +6434,7 @@ public function addChannel($xmlUrl)
{
$feedHash = MyTool::smallHash($xmlUrl);
if (!isset($this->_data['feeds'][$feedHash])) {
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['cachedata']);
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['etag'], $this->_data['feeds'][$feedHash]['last-modified']);

if (!$xml) {
return false;
Expand Down Expand Up @@ -6527,7 +6522,7 @@ public function updateChannel($feedHash)

unset($this->_data['feeds'][$feedHash]['error']);
$xmlUrl = $this->_data['feeds'][$feedHash]['xmlUrl'];
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['cachedata']);
$xml = $this->loadXml($xmlUrl, $this->_data['feeds'][$feedHash]['etag'], $this->_data['feeds'][$feedHash]['last-modified']);

if (!$xml) {
if (file_exists($this->cacheDir.'/'.$feedHash.'.php')) {
Expand Down

0 comments on commit 635305f

Please sign in to comment.