]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Add defaults() function
[friendica.git] / boot.php
index 0fb8c01b9eb460e4814db392408c045eadd75942..a9d5ffdb883ac9d77b5198aa033c6276ca7e3743 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -37,7 +37,6 @@ require_once 'include/datetime.php';
 require_once 'include/pgettext.php';
 require_once 'include/nav.php';
 require_once 'include/identity.php';
-require_once 'update.php';
 
 define('FRIENDICA_PLATFORM',     'Friendica');
 define('FRIENDICA_CODENAME',     'Asparagus');
@@ -573,6 +572,51 @@ function x($s, $k = null)
        }
 }
 
+/**
+ * Return the provided variable value if it exists and is truthy or the provided
+ * default value instead.
+ *
+ * Works with initialized variables and potentially uninitialized array keys
+ *
+ * Usages:
+ * - defaults($var, $default)
+ * - defaults($array, 'key', $default)
+ *
+ * @brief Returns a defaut value if the provided variable or array key is falsy
+ * @see x()
+ * @return mixed
+ */
+function defaults() {
+       $args = func_get_args();
+
+       if (count($args) < 2) {
+               throw new BadFunctionCallException('defaults() requires at least 2 parameters');
+       }
+       if (count($args) > 3) {
+               throw new BadFunctionCallException('defaults() cannot use more than 3 parameters');
+       }
+       if (count($args) === 3 && !is_array($args[0])) {
+               throw new BadFunctionCallException('defaults($arr, $key, $def) requires an array as first parameter');
+       }
+       if (count($args) === 3 && is_null($args[1])) {
+               throw new BadFunctionCallException('defaults($arr, $key, $def) $key is null');
+       }
+
+       $default = array_pop($args);
+
+       if (call_user_func_array('x', $args)) {
+               if (count($args) === 1) {
+                       $return = $args[0];
+               } else {
+                       $return = $args[0][$args[1]];
+               }
+       } else {
+               $return = $default;
+       }
+
+       return $return;
+}
+
 /**
  * @brief Returns the baseurl.
  *
@@ -673,10 +717,13 @@ function update_db(App $a)
        $build = Config::get('system', 'build');
 
        if (empty($build)) {
-               $build = Config::set('system', 'build', DB_UPDATE_VERSION);
+               Config::set('system', 'build', DB_UPDATE_VERSION);
+               $build = DB_UPDATE_VERSION;
        }
 
        if ($build != DB_UPDATE_VERSION) {
+               require_once 'update.php';
+
                $stored = intval($build);
                $current = intval(DB_UPDATE_VERSION);
                if ($stored < $current) {
@@ -704,7 +751,7 @@ function update_db(App $a)
                        }
 
                        // run any left update_nnnn functions in update.php
-                       for ($x = $stored; $x < $current; $x ++) {
+                       for ($x = $stored + 1; $x <= $current; $x++) {
                                $r = run_update_function($x);
                                if (!$r) {
                                        break;