diskcache: unify naming

master
Andrew Dolgov 4 years ago
parent 8e79f1717d
commit 166f2d4666

@ -348,7 +348,7 @@ class API extends Handler {
},
$hook_object);
$article['content'] = DiskCache::rewriteUrls($article['content']);
$article['content'] = DiskCache::rewrite_urls($article['content']);
array_push($articles, $article);
@ -792,7 +792,7 @@ class API extends Handler {
},
$hook_object);
$headline_row["content"] = DiskCache::rewriteUrls($headline_row['content']);
$headline_row["content"] = DiskCache::rewrite_urls($headline_row['content']);
}
array_push($headlines, $headline_row);

@ -508,7 +508,7 @@ class Article extends Handler_Protected {
while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
if ($cache->exists(sha1($line["content_url"]))) {
$line["content_url"] = $cache->getUrl(sha1($line["content_url"]));
$line["content_url"] = $cache->get_url(sha1($line["content_url"]));
}
array_push($rv, $line);
@ -678,10 +678,10 @@ class Article extends Handler_Protected {
$cache = new DiskCache("images");
if ($article_image && $cache->exists(sha1($article_image)))
$article_image = $cache->getUrl(sha1($article_image));
$article_image = $cache->get_url(sha1($article_image));
if ($article_stream && $cache->exists(sha1($article_stream)))
$article_stream = $cache->getUrl(sha1($article_stream));
$article_stream = $cache->get_url(sha1($article_stream));
return [$article_image, $article_stream, $article_kind];
}

@ -194,20 +194,20 @@ class DiskCache {
$this->dir = CACHE_DIR . "/" . basename(clean($dir));
}
public function getDir() {
public function get_dir() {
return $this->dir;
}
public function makeDir() {
public function make_dir() {
if (!is_dir($this->dir)) {
return mkdir($this->dir);
}
}
public function isWritable($filename = "") {
public function is_writable($filename = "") {
if ($filename) {
if (file_exists($this->getFullPath($filename)))
return is_writable($this->getFullPath($filename));
if (file_exists($this->get_full_path($filename)))
return is_writable($this->get_full_path($filename));
else
return is_writable($this->dir);
} else {
@ -216,44 +216,44 @@ class DiskCache {
}
public function exists($filename) {
return file_exists($this->getFullPath($filename));
return file_exists($this->get_full_path($filename));
}
public function getSize($filename) {
public function get_size($filename) {
if ($this->exists($filename))
return filesize($this->getFullPath($filename));
return filesize($this->get_full_path($filename));
else
return -1;
}
public function getFullPath($filename) {
public function get_full_path($filename) {
return $this->dir . "/" . basename(clean($filename));
}
public function put($filename, $data) {
return file_put_contents($this->getFullPath($filename), $data);
return file_put_contents($this->get_full_path($filename), $data);
}
public function touch($filename) {
return touch($this->getFullPath($filename));
return touch($this->get_full_path($filename));
}
public function get($filename) {
if ($this->exists($filename))
return file_get_contents($this->getFullPath($filename));
return file_get_contents($this->get_full_path($filename));
else
return null;
}
public function getMimeType($filename) {
public function get_mime_type($filename) {
if ($this->exists($filename))
return mime_content_type($this->getFullPath($filename));
return mime_content_type($this->get_full_path($filename));
else
return null;
}
public function getFakeExtension($filename) {
$mimetype = $this->getMimeType($filename);
public function get_fake_extension($filename) {
$mimetype = $this->get_mime_type($filename);
if ($mimetype)
return isset($this->mimeMap[$mimetype]) ? $this->mimeMap[$mimetype] : "";
@ -262,17 +262,17 @@ class DiskCache {
}
public function send($filename) {
$fake_extension = $this->getFakeExtension($filename);
$fake_extension = $this->get_fake_extension($filename);
if ($fake_extension)
$fake_extension = ".$fake_extension";
header("Content-Disposition: inline; filename=\"${filename}${fake_extension}\"");
return $this->send_local_file($this->getFullPath($filename));
return $this->send_local_file($this->get_full_path($filename));
}
public function getUrl($filename) {
public function get_url($filename) {
return get_self_url_prefix() . "/public.php?op=cached_url&file=" . basename($this->dir) . "/" . basename($filename);
}
@ -280,7 +280,7 @@ class DiskCache {
// this is called separately after sanitize() and plugin render article hooks to allow
// plugins work on original source URLs used before caching
// NOTE: URLs should be already absolutized because this is called after sanitize()
static public function rewriteUrls($str)
static public function rewrite_urls($str)
{
$res = trim($str);
if (!$res) return '';
@ -301,7 +301,7 @@ class DiskCache {
$cached_filename = sha1($url);
if ($cache->exists($cached_filename)) {
$url = $cache->getUrl($cached_filename);
$url = $cache->get_url($cached_filename);
$entry->setAttribute($attr, $url);
$entry->removeAttribute("srcset");
@ -318,7 +318,7 @@ class DiskCache {
$cached_filename = sha1($matches[$i]["url"]);
if ($cache->exists($cached_filename)) {
$matches[$i]["url"] = $cache->getUrl($cached_filename);
$matches[$i]["url"] = $cache->get_url($cached_filename);
$need_saving = true;
}

@ -260,7 +260,7 @@ class Feeds extends Handler_Protected {
$this->_mark_timestamp(" hook_render_cdm");
$line['content'] = DiskCache::rewriteUrls($line['content']);
$line['content'] = DiskCache::rewrite_urls($line['content']);
$this->_mark_timestamp(" disk_cache_rewrite");

@ -106,7 +106,7 @@ class Handler_Public extends Handler {
$content = Sanitizer::sanitize($line["content"], false, $owner_uid,
$feed_site_url, false, $line["id"]);
$content = DiskCache::rewriteUrls($content);
$content = DiskCache::rewrite_urls($content);
if ($line['note']) {
$content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
@ -354,7 +354,7 @@ class Handler_Public extends Handler {
},
$line);
$line['content'] = DiskCache::rewriteUrls($line['content']);
$line['content'] = DiskCache::rewrite_urls($line['content']);
header("Content-Type: text/html");

@ -1281,7 +1281,7 @@ class RSSUtils {
static function cache_enclosures($enclosures, $site_url) {
$cache = new DiskCache("images");
if ($cache->isWritable()) {
if ($cache->is_writable()) {
foreach ($enclosures as $enc) {
if (preg_match("/(image|audio|video)/", $enc[1])) {
@ -1335,7 +1335,7 @@ class RSSUtils {
} else {
Debug::log("cache_media: failed with $fetch_last_error_code: $fetch_last_error");
}
} else if ($cache->isWritable($local_filename)) {
} else if ($cache->is_writable($local_filename)) {
$cache->touch($local_filename);
}
}
@ -1344,7 +1344,7 @@ class RSSUtils {
static function cache_media($html, $site_url) {
$cache = new DiskCache("images");
if ($html && $cache->isWritable()) {
if ($html && $cache->is_writable()) {
$doc = new DOMDocument();
if (@$doc->loadHTML($html)) {
$xpath = new DOMXPath($doc);

@ -59,14 +59,14 @@ class Af_Proxy_Http extends Plugin {
$local_filename = sha1($url);
if ($this->cache->exists($local_filename)) {
header("Location: " . $this->cache->getUrl($local_filename));
header("Location: " . $this->cache->get_url($local_filename));
return;
} else {
$data = UrlHelper::fetch(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]);
if ($data) {
if ($this->cache->put($local_filename, $data)) {
header("Location: " . $this->cache->getUrl($local_filename));
header("Location: " . $this->cache->get_url($local_filename));
return;
}
} else {

@ -17,18 +17,18 @@ class Cache_Starred_Images extends Plugin {
$this->host = $host;
$this->cache = new DiskCache("starred-images");
if ($this->cache->makeDir())
chmod($this->cache->getDir(), 0777);
if ($this->cache->make_dir())
chmod($this->cache->get_dir(), 0777);
if (!$this->cache->exists(".no-auto-expiry"))
$this->cache->touch(".no-auto-expiry");
if ($this->cache->isWritable()) {
if ($this->cache->is_writable()) {
$host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
$host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
$host->add_hook($host::HOOK_SANITIZE, $this);
} else {
user_error("Starred cache directory ".$this->cache->getDir()." is not writable.", E_USER_WARNING);
user_error("Starred cache directory ".$this->cache->get_dir()." is not writable.", E_USER_WARNING);
}
}
@ -69,9 +69,9 @@ class Cache_Starred_Images extends Plugin {
/* actual housekeeping */
Debug::log("expiring " . $this->cache->getDir() . "...");
Debug::log("expiring " . $this->cache->get_dir() . "...");
$files = glob($this->cache->getDir() . "/*.{png,mp4,status}", GLOB_BRACE);
$files = glob($this->cache->get_dir() . "/*.{png,mp4,status}", GLOB_BRACE);
$last_article_id = 0;
$article_exists = 1;
@ -98,7 +98,7 @@ class Cache_Starred_Images extends Plugin {
$local_filename = $article_id . "-" . sha1($enc["content_url"]);
if ($this->cache->exists($local_filename)) {
$enc["content_url"] = $this->cache->getUrl($local_filename);
$enc["content_url"] = $this->cache->get_url($local_filename);
}
return $enc;
@ -117,7 +117,7 @@ class Cache_Starred_Images extends Plugin {
$local_filename = $article_id . "-" . sha1($src);
if ($this->cache->exists($local_filename)) {
$entry->setAttribute("src", $this->cache->getUrl($local_filename));
$entry->setAttribute("src", $this->cache->get_url($local_filename));
$entry->removeAttribute("srcset");
}
}
@ -151,7 +151,7 @@ class Cache_Starred_Images extends Plugin {
$status_filename = $article_id . "-" . sha1($site_url) . ".status";
/* housekeeping might run as a separate user, in this case status/media might not be writable */
if (!$this->cache->isWritable($status_filename)) {
if (!$this->cache->is_writable($status_filename)) {
Debug::log("status not writable: $status_filename", Debug::$LOG_VERBOSE);
return false;
}

Loading…
Cancel
Save