From 4392858662214959f649b68f6b2eabe041c98d81 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sat, 20 Oct 2018 18:19:55 +0200 Subject: [PATCH] Move Global Functions - Part 3 - Replaced every CACHE definition - Moved check_url to App->checkURL() - Removed unused definition "ZCURL_TIMEOUT" --- boot.php | 46 ------------------------ index.php | 2 +- mod/ping.php | 2 +- mod/search.php | 4 +-- src/App.php | 19 ++++++++++ src/Content/OEmbed.php | 4 +-- src/Model/GContact.php | 4 +-- src/Model/Photo.php | 4 +-- src/Model/Profile.php | 4 +-- src/Network/Probe.php | 2 +- src/Protocol/ActivityPub/Transmitter.php | 2 +- src/Protocol/Diaspora.php | 6 ++-- src/Protocol/OStatus.php | 2 +- src/Util/JsonLD.php | 2 +- src/Worker/DiscoverPoCo.php | 2 +- src/Worker/Queue.php | 8 ++--- 16 files changed, 43 insertions(+), 70 deletions(-) diff --git a/boot.php b/boot.php index 5d4b7c147..d55f4b7bc 100644 --- a/boot.php +++ b/boot.php @@ -121,24 +121,6 @@ define('LOGGER_DATA', 4); define('LOGGER_ALL', 5); /* @}*/ -/** - * @name Cache - * @deprecated since version 3.6 - * @see Cache - * - * Cache levels - * @{ - */ -define('CACHE_MONTH', Cache::MONTH); -define('CACHE_WEEK', Cache::WEEK); -define('CACHE_DAY', Cache::DAY); -define('CACHE_HOUR', Cache::HOUR); -define('CACHE_HALF_HOUR', Cache::HALF_HOUR); -define('CACHE_QUARTER_HOUR', Cache::QUARTER_HOUR); -define('CACHE_FIVE_MINUTES', Cache::FIVE_MINUTES); -define('CACHE_MINUTE', Cache::MINUTE); -/* @}*/ - /** * @name Register * @@ -212,11 +194,6 @@ $netgroup_ids = [ */ define('MAX_LIKERS', 75); -/** - * Communication timeout - */ -define('ZCURL_TIMEOUT', (-1)); - /** * @name Notify * @@ -502,29 +479,6 @@ function check_db($via_worker) } } -/** - * Sets the base url for use in cmdline programs which don't have - * $_SERVER variables - * - * @param object $a App - */ -function check_url(App $a) -{ - $url = Config::get('system', 'url'); - - // if the url isn't set or the stored url is radically different - // than the currently visited url, store the current value accordingly. - // "Radically different" ignores common variations such as http vs https - // and www.example.com vs example.com. - // We will only change the url to an ip address if there is no existing setting - - if (empty($url) || (!link_compare($url, System::baseUrl())) && (!preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $a->getHostName()))) { - Config::set('system', 'url', System::baseUrl()); - } - - return; -} - /** * @brief Automatic database updates * @param object $a App diff --git a/index.php b/index.php index 6ab405c63..884b06732 100644 --- a/index.php +++ b/index.php @@ -172,7 +172,7 @@ if ($a->getMode()->isInstall() && $a->module != 'view') { } elseif (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED) && $a->module != 'view') { $a->module = 'maintenance'; } else { - check_url($a); + $a->checkURL(); check_db(false); Addon::check(); } diff --git a/mod/ping.php b/mod/ping.php index 5ea75727a..6f5b5b4a9 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -221,7 +221,7 @@ function ping_init(App $a) DBA::escape(DateTimeFormat::utcNow()) ); if (DBA::isResult($ev)) { - Cache::set($cachekey, $ev, CACHE_HOUR); + Cache::set($cachekey, $ev, Cache::HOUR); } } diff --git a/mod/search.php b/mod/search.php index b6e1a7e7f..80b1c184f 100644 --- a/mod/search.php +++ b/mod/search.php @@ -136,9 +136,9 @@ function search_content(App $a) { "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]); killme(); } - Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR); + Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR); } else - Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR); + Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR); } Nav::setSelected('search'); diff --git a/src/App.php b/src/App.php index 97c193b3b..ddffa867a 100644 --- a/src/App.php +++ b/src/App.php @@ -1607,4 +1607,23 @@ class App return $default; } + + /** + * Sets the base url for use in cmdline programs which don't have + * $_SERVER variables + */ + public function checkURL() + { + $url = Config::get('system', 'url'); + + // if the url isn't set or the stored url is radically different + // than the currently visited url, store the current value accordingly. + // "Radically different" ignores common variations such as http vs https + // and www.example.com vs example.com. + // We will only change the url to an ip address if there is no existing setting + + if (empty($url) || (!link_compare($url, $this->getBaseURL())) && (!preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $this->getHostName()))) { + Config::set('system', 'url', $this->getBaseURL()); + } + } } diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index 116120ab5..cfe9468ad 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -120,9 +120,9 @@ class OEmbed 'content' => $json_string, 'created' => DateTimeFormat::utcNow() ], true); - $cache_ttl = CACHE_DAY; + $cache_ttl = Cache::DAY; } else { - $cache_ttl = CACHE_FIVE_MINUTES; + $cache_ttl = Cache::FIVE_MINUTES; } Cache::set($cache_key, $json_string, $cache_ttl); diff --git a/src/Model/GContact.php b/src/Model/GContact.php index 26cb11175..681c1c370 100644 --- a/src/Model/GContact.php +++ b/src/Model/GContact.php @@ -468,7 +468,7 @@ class GContact * Uncommented because the result of the queries are to big to store it in the cache. * We need to decide if we want to change the db column type or if we want to delete it. */ - //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, CACHE_FIVE_MINUTES); + //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, Cache::FIVE_MINUTES); return $r; } @@ -509,7 +509,7 @@ class GContact * Uncommented because the result of the queries are to big to store it in the cache. * We need to decide if we want to change the db column type or if we want to delete it. */ - //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, CACHE_FIVE_MINUTES); + //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, Cache::FIVE_MINUTES); return $list; } diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 372e875ea..6b094ded6 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -253,7 +253,7 @@ class Photo DBA::escape(L10n::t('Contact Photos')) ); } - Cache::set($key, $albums, CACHE_DAY); + Cache::set($key, $albums, Cache::DAY); } return $albums; } @@ -265,7 +265,7 @@ class Photo public static function clearAlbumCache($uid) { $key = "photo_albums:".$uid.":".local_user().":".remote_user(); - Cache::set($key, null, CACHE_DAY); + Cache::set($key, null, Cache::DAY); } /** diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 1f0773cd1..f6fb3167d 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -577,7 +577,7 @@ class Profile ); if (DBA::isResult($s)) { $r = DBA::toArray($s); - Cache::set($cachekey, $r, CACHE_HOUR); + Cache::set($cachekey, $r, Cache::HOUR); } } @@ -1037,7 +1037,7 @@ class Profile logger('URL ' . $my_url . ' already tried to authenticate.', LOGGER_DEBUG); return; } else { - Cache::set($cachekey, true, CACHE_MINUTE); + Cache::set($cachekey, true, Cache::MINUTE); } logger('Not authenticated. Invoking reverse magic-auth for ' . $my_url, LOGGER_DEBUG); diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 23b97a5cd..08ec4ae1c 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -381,7 +381,7 @@ class Probe // Only store into the cache if the value seems to be valid if (!in_array($data['network'], [Protocol::PHANTOM, Protocol::MAIL])) { - Cache::set("Probe::uri:".$network.":".$uri, $data, CACHE_DAY); + Cache::set("Probe::uri:".$network.":".$uri, $data, Cache::DAY); /// @todo temporary fix - we need a real contact update function that updates only changing fields /// The biggest problem is the avatar picture that could have a reduced image size. diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index a94b7f59f..2291b68e9 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -545,7 +545,7 @@ class Transmitter $data = ActivityPub\Transmitter::createActivityFromItem($item_id); - Cache::set($cachekey, $data, CACHE_QUARTER_HOUR); + Cache::set($cachekey, $data, Cache::QUARTER_HOUR); return $data; } diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 5792d56b6..1a8054aad 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3205,7 +3205,7 @@ class Diaspora logger("Send participation for ".$item["guid"]." by ".$author, LOGGER_DEBUG); // It doesn't matter what we store, we only want to avoid sending repeated notifications for the same item - Cache::set($cachekey, $item["guid"], CACHE_QUARTER_HOUR); + Cache::set($cachekey, $item["guid"], Cache::QUARTER_HOUR); return self::buildAndTransmit($owner, $contact, "participation", $message); } @@ -3575,7 +3575,7 @@ class Diaspora $msg = ["type" => $type, "message" => $message]; - Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR); + Cache::set($cachekey, $msg, Cache::QUARTER_HOUR); return $msg; } @@ -3702,7 +3702,7 @@ class Diaspora $comment['thread_parent_guid'] = self::getGuidFromUri($item['thr-parent'], $item['uid']); } - Cache::set($cachekey, $comment, CACHE_QUARTER_HOUR); + Cache::set($cachekey, $comment, Cache::QUARTER_HOUR); return($comment); } diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index f589d3827..0e3d5e6c0 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -2210,7 +2210,7 @@ class OStatus $feeddata = trim($doc->saveXML()); $msg = ['feed' => $feeddata, 'last_update' => $last_update]; - Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR); + Cache::set($cachekey, $msg, Cache::QUARTER_HOUR); logger('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created, LOGGER_DEBUG); diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php index 5380c3c54..6f4545565 100644 --- a/src/Util/JsonLD.php +++ b/src/Util/JsonLD.php @@ -43,7 +43,7 @@ class JsonLD } $data = jsonld_default_document_loader($url); - Cache::set('documentLoader:' . $url, $data, CACHE_DAY); + Cache::set('documentLoader:' . $url, $data, Cache::DAY); return $data; } diff --git a/src/Worker/DiscoverPoCo.php b/src/Worker/DiscoverPoCo.php index 497684497..c1583888c 100644 --- a/src/Worker/DiscoverPoCo.php +++ b/src/Worker/DiscoverPoCo.php @@ -258,7 +258,7 @@ class DiscoverPoCo } } } - Cache::set("dirsearch:".$search, time(), CACHE_DAY); + Cache::set("dirsearch:".$search, time(), Cache::DAY); } /** diff --git a/src/Worker/Queue.php b/src/Worker/Queue.php index b27a776c7..6cad9ac53 100644 --- a/src/Worker/Queue.php +++ b/src/Worker/Queue.php @@ -87,7 +87,7 @@ class Queue logger("Check server " . $server . " (" . $contact["network"] . ")"); $vital = PortableContact::checkServer($server, $contact["network"], true); - Cache::set($cachekey_server . $server, $vital, CACHE_MINUTE); + Cache::set($cachekey_server . $server, $vital, Cache::MINUTE); } if (!is_null($vital) && !$vital) { @@ -119,7 +119,7 @@ class Queue QueueModel::removeItem($q_item['id']); } else { QueueModel::updateTime($q_item['id']); - Cache::set($cachekey_deadguy . $contact['notify'], true, CACHE_MINUTE); + Cache::set($cachekey_deadguy . $contact['notify'], true, Cache::MINUTE); } break; @@ -129,7 +129,7 @@ class Queue if ($deliver_status == -1) { QueueModel::updateTime($q_item['id']); - Cache::set($cachekey_deadguy . $contact['notify'], true, CACHE_MINUTE); + Cache::set($cachekey_deadguy . $contact['notify'], true, Cache::MINUTE); } else { QueueModel::removeItem($q_item['id']); } @@ -144,7 +144,7 @@ class Queue QueueModel::removeItem($q_item['id']); } else { QueueModel::updateTime($q_item['id']); - Cache::set($cachekey_deadguy . $contact['notify'], true, CACHE_MINUTE); + Cache::set($cachekey_deadguy . $contact['notify'], true, Cache::MINUTE); } break;