function on_shutdown() { global $pending; // we were waiting for changes, and this is an internal error if ($pending && !headers_sent()) { http::no_content(); } }
/** * wait for updates * * This script will wait for new updates before providing them to caller. * Because of potential time-outs, you have to care of retries. * * @param string reference to thread (e.g., 'article:123') * @param string timestamp of previous update * @return array attributes including new comments and a timestamp * * @see articles/view_as_chat.php * @see comments/thread.php */ public static function &pull($anchor, $stamp, $count = 100) { global $context; $timer = 1; // some implementations will kill network connections earlier anyway Safe::set_time_limit(max(30, $timer)); // we return formatted text $text = ''; // sanity check if (!$anchor) { return $text; } // the query to get time of last update $query = "SELECT edit_date, edit_name FROM " . SQL::table_name('comments') . " AS comments " . " WHERE comments.anchor LIKE '" . SQL::escape($anchor) . "'" . " ORDER BY comments.edit_date DESC" . " LIMIT 1"; // we may timeout ourself, to be safe with network resources while (!($stat = SQL::query_first($query)) || isset($stat['edit_date']) && $stat['edit_date'] <= $stamp) { // kill the request to avoid repeated transmissions when nothing has changed if (--$timer < 1) { http::no_content(); die; } // preserve server resources sleep(1); } // return an array of variables $response = array(); $response['items'] =& Comments::list_by_thread_for_anchor($anchor, 0, $count, 'thread'); $response['name'] = strip_tags($stat['edit_name']); $response['timestamp'] = SQL::strtotime($stat['edit_date']); // return by reference return $response; }