Moved functions out of boot.php into class

- z_root() => $a->getBaseURL()
- absurl() => removed because no usage
- is_ajax() => $a->isAjax()
- current_load() => System::currentLoad()
- argc() => $a->argc
- argv($x) => $a->getArgumentValue($x)
This commit is contained in:
Philipp Holzer 2018-10-13 18:57:31 +02:00
parent 14e7686df4
commit 2c541afd47
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
8 changed files with 66 additions and 90 deletions

View file

@ -475,44 +475,6 @@ function defaults() {
return $return; return $return;
} }
/**
* @brief Returns the baseurl.
*
* @see System::baseUrl()
*
* @return string
* @TODO Function is deprecated and only used in some addons
*/
function z_root()
{
return System::baseUrl();
}
/**
* @brief Return absolut URL for given $path.
*
* @param string $path given path
*
* @return string
*/
function absurl($path)
{
if (strpos($path, '/') === 0) {
return z_path() . $path;
}
return $path;
}
/**
* @brief Function to check if request was an AJAX (xmlhttprequest) request.
*
* @return boolean
*/
function is_ajax()
{
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}
/** /**
* @brief Function to check if request was an AJAX (xmlhttprequest) request. * @brief Function to check if request was an AJAX (xmlhttprequest) request.
* *
@ -1175,46 +1137,6 @@ function validate_include(&$file)
return $valid; return $valid;
} }
function current_load()
{
if (!function_exists('sys_getloadavg')) {
return false;
}
$load_arr = sys_getloadavg();
if (!is_array($load_arr)) {
return false;
}
return max($load_arr[0], $load_arr[1]);
}
/**
* @brief get c-style args
*
* @return int
*/
function argc()
{
return get_app()->argc;
}
/**
* @brief Returns the value of a argv key
*
* @param int $x argv key
* @return string Value of the argv key
*/
function argv($x)
{
if (array_key_exists($x, get_app()->argv)) {
return get_app()->argv[$x];
}
return '';
}
/** /**
* @brief Get the data which is needed for infinite scroll * @brief Get the data which is needed for infinite scroll
* *

View file

@ -77,7 +77,7 @@ function admin_post(App $a)
break; break;
case 'themes': case 'themes':
if ($a->argc < 2) { if ($a->argc < 2) {
if (is_ajax()) { if ($a->isAjax()) {
return; return;
} }
goaway('admin/'); goaway('admin/');
@ -107,7 +107,7 @@ function admin_post(App $a)
} }
info(L10n::t('Theme settings updated.')); info(L10n::t('Theme settings updated.'));
if (is_ajax()) { if ($a->isAjax()) {
return; return;
} }
$return_path = 'admin/themes/' . $theme; $return_path = 'admin/themes/' . $theme;
@ -286,7 +286,7 @@ function admin_content(App $a)
$o = admin_page_summary($a); $o = admin_page_summary($a);
} }
if (is_ajax()) { if ($a->isAjax()) {
echo $o; echo $o;
killme(); killme();
return ''; return '';
@ -2536,7 +2536,7 @@ function admin_page_features_post(App $a)
*/ */
function admin_page_features(App $a) function admin_page_features(App $a)
{ {
if ((argc() > 1) && (argv(1) === 'features')) { if (($a->argc > 1) && ($a->argv[1] === 'features')) {
$arr = []; $arr = [];
$features = Feature::get(false); $features = Feature::get(false);

View file

@ -36,12 +36,12 @@ function help_content(App $a)
$path = ''; $path = '';
// looping through the argv keys bigger than 0 to build // looping through the argv keys bigger than 0 to build
// a path relative to /help // a path relative to /help
for ($x = 1; $x < argc(); $x ++) { for ($x = 1; $x < $a->argc; $x ++) {
if (strlen($path)) { if (strlen($path)) {
$path .= '/'; $path .= '/';
} }
$path .= argv($x); $path .= $a->getArgumentValue($x);
} }
$title = basename($path); $title = basename($path);
$filename = $path; $filename = $path;

View file

@ -876,13 +876,13 @@ function item_content(App $a)
$o = ''; $o = '';
if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) { if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
if (is_ajax()) { if ($a->isAjax()) {
$o = Item::deleteForUser(['id' => $a->argv[2]], local_user()); $o = Item::deleteForUser(['id' => $a->argv[2]], local_user());
} else { } else {
$o = drop_item($a->argv[2]); $o = drop_item($a->argv[2]);
} }
if (is_ajax()) { if ($a->isAjax()) {
// ajax return: [<item id>, 0 (no perm) | <owner id>] // ajax return: [<item id>, 0 (no perm) | <owner id>]
echo json_encode([intval($a->argv[2]), intval($o)]); echo json_encode([intval($a->argv[2]), intval($o)]);
killme(); killme();

View file

@ -26,7 +26,7 @@ function viewsrc_content(App $a)
$item = Item::selectFirst(['body'], ['uid' => local_user(), 'id' => $item_id]); $item = Item::selectFirst(['body'], ['uid' => local_user(), 'id' => $item_id]);
if (DBA::isResult($item)) { if (DBA::isResult($item)) {
if (is_ajax()) { if ($a->isAjax()) {
echo str_replace("\n", '<br />', $item['body']); echo str_replace("\n", '<br />', $item['body']);
killme(); killme();
} else { } else {

View file

@ -110,6 +110,11 @@ class App
*/ */
private $currentTheme; private $currentTheme;
/**
* @var bool check if request was an AJAX (xmlhttprequest) request
*/
private $isAjax;
/** /**
* Register a stylesheet file path to be included in the <head> tag of every page. * Register a stylesheet file path to be included in the <head> tag of every page.
* Inclusion is done in App->initHead(). * Inclusion is done in App->initHead().
@ -322,6 +327,8 @@ class App
$this->is_mobile = $mobile_detect->isMobile(); $this->is_mobile = $mobile_detect->isMobile();
$this->is_tablet = $mobile_detect->isTablet(); $this->is_tablet = $mobile_detect->isTablet();
$this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest';
// Register template engines // Register template engines
$this->registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); $this->registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
} }
@ -1221,7 +1228,7 @@ class App
} }
} }
$load = current_load(); $load = System::currentLoad();
if ($load) { if ($load) {
if (intval($load) > $maxsysload) { if (intval($load) > $maxsysload) {
logger('system: load ' . $load . ' for ' . $process . ' tasks (' . $maxsysload . ') too high.'); logger('system: load ' . $load . ' for ' . $process . ' tasks (' . $maxsysload . ') too high.');
@ -1572,4 +1579,32 @@ class App
{ {
return Core\Theme::getStylesheetPath($this->getCurrentTheme()); return Core\Theme::getStylesheetPath($this->getCurrentTheme());
} }
/**
* Check if request was an AJAX (xmlhttprequest) request.
*
* @return boolean true if it was an AJAX request
*/
public function isAjax()
{
return $this->isAjax;
}
/**
* Returns the value of a argv key
* TODO there are a lot of $a->argv usages in combination with defaults() which can be replaced with this method
*
* @param int $position the position of the argument
* @param mixed $default the default value if not found
*
* @return mixed returns the value of the argument
*/
public function getArgumentValue($position, $default = '')
{
if (array_key_exists($position, $this->argv)) {
return $this->argv[$position];
}
return $default;
}
} }

View file

@ -216,6 +216,26 @@ class System extends BaseObject
return substr($trailer . uniqid('') . mt_rand(), 0, 26); return substr($trailer . uniqid('') . mt_rand(), 0, 26);
} }
/**
* Returns the current Load of the System
*
* @return integer
*/
public static function currentLoad()
{
if (!function_exists('sys_getloadavg')) {
return false;
}
$load_arr = sys_getloadavg();
if (!is_array($load_arr)) {
return false;
}
return max($load_arr[0], $load_arr[1]);
}
/// @todo Move the following functions from boot.php /// @todo Move the following functions from boot.php
/* /*
function killme() function killme()
@ -232,6 +252,5 @@ class System extends BaseObject
function get_cachefile($file, $writemode = true) function get_cachefile($file, $writemode = true)
function get_itemcachepath() function get_itemcachepath()
function get_spoolpath() function get_spoolpath()
function current_load()
*/ */
} }

View file

@ -618,7 +618,7 @@ class Worker
$active = self::activeWorkers(); $active = self::activeWorkers();
// Decrease the number of workers at higher load // Decrease the number of workers at higher load
$load = current_load(); $load = System::currentLoad();
if ($load) { if ($load) {
$maxsysload = intval(Config::get("system", "maxloadavg", 50)); $maxsysload = intval(Config::get("system", "maxloadavg", 50));