Commit 8f65dc18 by pupi1985

Avoid checking for plugin updates in each page refresh

parent 7ed3f6a0
......@@ -148,6 +148,13 @@ function qa_version_check(uri, version, elem, isCore)
);
}
function qa_version_check_array(versionChecks)
{
for (var i = 0; i < versionChecks.length; i++) {
qa_version_check(versionChecks[i].uri, versionChecks[i].version, versionChecks[i].elem, false)
}
}
function qa_get_enabled_plugins_hashes()
{
var hashes = [];
......
......@@ -25,6 +25,8 @@ class Q2A_Plugin_PluginManager
{
const PLUGIN_DELIMITER = ';';
const OPT_ENABLED_PLUGINS = 'enabled_plugins';
const OPT_LAST_UPDATE_CHECK = 'last_plugin_update_check';
const NUMBER_OF_DAYS_TO_CHECK_FOR_UPDATE = 14;
private $loadBeforeDbInit = array();
private $loadAfterDbInit = array();
......@@ -180,4 +182,27 @@ class Q2A_Plugin_PluginManager
$this->setEnabledPluginsOption($finalEnabledPlugins);
}
/**
* @return bool
*/
public function shouldCheckForUpdate()
{
$lastUpdateCheck = (int)qa_opt(self::OPT_LAST_UPDATE_CHECK);
$currentTime = (int)qa_opt('db_time');
return $currentTime - $lastUpdateCheck > self::NUMBER_OF_DAYS_TO_CHECK_FOR_UPDATE * 24 * 60 * 60;
}
/**
* @param int|null $time
*/
public function performUpdateCheck($time = null)
{
if ($time === null) {
$time = time();
}
qa_opt(self::OPT_LAST_UPDATE_CHECK, $time);
}
}
......@@ -273,6 +273,7 @@ return array(
'users_registered' => 'Registered users:',
'users_title' => 'Users',
'users_voted' => 'Users who voted:',
'version_check' => 'Check for updates',
'version_get_x' => 'get ^',
'version_latest' => 'latest',
'version_latest_unknown' => 'latest unknown',
......
......@@ -62,7 +62,9 @@ if (qa_is_http_post()) {
$enabledPluginHashesArray = explode(';', $enabledPluginHashes);
$pluginDirectories = array_keys(array_intersect($pluginHashes, $enabledPluginHashesArray));
$pluginManager->setEnabledPlugins($pluginDirectories);
qa_redirect('admin/plugins');
} else if (qa_clicked('doversioncheck')) {
$pluginManager->performUpdateCheck(0);
qa_redirect('admin/plugins');
}
}
......@@ -138,6 +140,9 @@ if (!empty($fileSystemPlugins)) {
qa_sort_by($sortedPluginFiles, 'name');
$versionChecks = array();
$shouldCheckForUpdate = $pluginManager->shouldCheckForUpdate();
$pluginIndex = -1;
foreach ($sortedPluginFiles as $pluginDirectory => $metadata) {
$pluginIndex++;
......@@ -168,14 +173,16 @@ if (!empty($fileSystemPlugins)) {
} else
$authorhtml = '';
if ($metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
if ($shouldCheckForUpdate && $metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
$elementid = 'version_check_' . md5($pluginDirectory);
$updatehtml = '(<span id="' . $elementid . '">...</span>)';
$qa_content['script_onloads'][] = array(
"qa_version_check(" . qa_js($metadata['update_uri']) . ", " . qa_js($metadata['version'], true) . ", " . qa_js($elementid) . ", false);"
$versionChecks[] = array(
'uri' => $metadata['update_uri'],
'version' => $metadata['version'],
'elem' => $elementid,
);
$updatehtml = '(<span id="' . $elementid . '">...</span>)';
}
else
$updatehtml = '';
......@@ -246,6 +253,14 @@ if (!empty($fileSystemPlugins)) {
}
}
}
if ($shouldCheckForUpdate) {
$pluginManager->performUpdateCheck();
$qa_content['script_onloads'][] = array(
sprintf('qa_version_check_array(%s);', json_encode($versionChecks)),
);
}
}
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
......@@ -260,6 +275,10 @@ $qa_content['form'] = array(
'tags' => 'name="dosave"',
'label' => qa_lang_html('admin/save_options_button'),
),
'doversioncheck' => array(
'tags' => 'name="doversioncheck"',
'label' => qa_lang_html('admin/version_check'),
),
),
'hidden' => array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment