+
+/**
+ * @brief update the "last-item" field in the "self" contact
+ *
+ * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
+ *
+ * @return bool "true" when the job is done
+ */
+function post_update_1206() {
+ // Was the script completed?
+ if (Config::get("system", "post_update_version") >= 1206)
+ return true;
+
+ logger("Start", LOGGER_DEBUG);
+ $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
+ (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
+ FROM `user`
+ INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
+
+ if (!DBM::is_result($r)) {
+ return false;
+ }
+ foreach ($r AS $user) {
+ if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
+ q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
+ dbesc($user["lastitem_date"]),
+ intval($user["id"]));
+ }
+ }
+
+ Config::set("system", "post_update_version", 1206);
+ logger("Done", LOGGER_DEBUG);
+ return true;
+}