Skip to content

Commit

Permalink
Moving quickjump cache to use new cache module.
Browse files Browse the repository at this point in the history
  • Loading branch information
reines committed Feb 28, 2011
1 parent c6a1eb4 commit d0afe45
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 117 deletions.
10 changes: 2 additions & 8 deletions admin_categories.php
Expand Up @@ -77,10 +77,7 @@
$db->query('DELETE FROM '.$db->prefix.'categories WHERE id='.$cat_to_delete) or error('Unable to delete category', __FILE__, __LINE__, $db->error());

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

generate_quickjump_cache();
$cache->delete('quickjump');

redirect('admin_categories.php', $lang_admin_categories['Category deleted redirect']);
}
Expand Down Expand Up @@ -145,10 +142,7 @@
}

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

generate_quickjump_cache();
$cache->delete('quickjump');

redirect('admin_categories.php', $lang_admin_categories['Categories updated redirect']);
}
Expand Down
25 changes: 5 additions & 20 deletions admin_forums.php
Expand Up @@ -32,10 +32,7 @@
$db->query('INSERT INTO '.$db->prefix.'forums (forum_name, cat_id) VALUES(\''.$db->escape($lang_admin_forums['New forum']).'\', '.$add_to_cat.')') or error('Unable to create forum', __FILE__, __LINE__, $db->error());

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

generate_quickjump_cache();
$cache->delete('quickjump');

redirect('admin_forums.php', $lang_admin_forums['Forum added redirect']);
}
Expand Down Expand Up @@ -76,10 +73,7 @@
$db->query('DELETE FROM '.$db->prefix.'forum_subscriptions WHERE forum_id='.$forum_id) or error('Unable to delete subscriptions', __FILE__, __LINE__, $db->error());

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

generate_quickjump_cache();
$cache->delete('quickjump');

redirect('admin_forums.php', $lang_admin_forums['Forum deleted redirect']);
}
Expand Down Expand Up @@ -135,10 +129,7 @@
}

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

generate_quickjump_cache();
$cache->delete('quickjump');

redirect('admin_forums.php', $lang_admin_forums['Forums updated redirect']);
}
Expand Down Expand Up @@ -200,10 +191,7 @@
}

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

generate_quickjump_cache();
$cache->delete('quickjump');

redirect('admin_forums.php', $lang_admin_forums['Forum updated redirect']);
}
Expand All @@ -214,10 +202,7 @@
$db->query('DELETE FROM '.$db->prefix.'forum_perms WHERE forum_id='.$forum_id) or error('Unable to delete group forum permissions', __FILE__, __LINE__, $db->error());

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

generate_quickjump_cache();
$cache->delete('quickjump');

redirect('admin_forums.php?edit_forum='.$forum_id, $lang_admin_forums['Perms reverted redirect']);
}
Expand Down
6 changes: 1 addition & 5 deletions admin_groups.php
Expand Up @@ -293,11 +293,7 @@
}

// Regenerate the quick jump cache
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';

$group_id = $_POST['mode'] == 'add' ? $new_group_id : intval($_POST['group_id']);
generate_quickjump_cache($group_id);
$cache->delete('quickjump');

if ($_POST['mode'] == 'edit')
redirect('admin_groups.php', $lang_admin_groups['Group edited redirect']);
Expand Down
59 changes: 51 additions & 8 deletions footer.php
Expand Up @@ -68,17 +68,60 @@
// Display the "Jump to" drop list
if ($pun_config['o_quickjump'] == '1')
{
// Load cached quick jump
if (file_exists(FORUM_CACHE_DIR.'cache_quickjump_'.$pun_user['g_id'].'.php'))
include FORUM_CACHE_DIR.'cache_quickjump_'.$pun_user['g_id'].'.php';
$quickjump = $cache->get('quickjump');
if ($quickjump === Cache::NOT_FOUND)
{
$quickjump = array();

// Generate the quick jump cache for all groups
$group_result = $db->query('SELECT g_id FROM '.$db->prefix.'groups WHERE g_read_board=1') or error('Unable to fetch user group list', __FILE__, __LINE__, $db->error());
while ($cur_group = $db->fetch_assoc($group_result))
{
$quickjump[$cur_group['g_id']] = array();

if (!defined('PUN_QJ_LOADED'))
$result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$cur_group['g_id'].') WHERE fp.read_forum IS NULL OR fp.read_forum=1 ORDER BY c.disp_position, c.id, f.disp_position') or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());
while ($cur_forum = $db->fetch_assoc($result))
$quickjump[$cur_group['g_id']][] = $cur_forum;
}

$cache->set('quickjump', $quickjump);
}

if (!empty($quickjump[$pun_user['g_id']]))
{
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
require PUN_ROOT.'include/cache.php';
?>
<form id="qjump" method="get" action="viewforum.php">
<div>
<label>
<span><?php echo $lang_common['Jump to'] ?><br /></span>
<select name="id" onchange="window.location=('viewforum.php?id='+this.options[this.selectedIndex].value)">
<?php

$cur_category = 0;
foreach ($quickjump[$pun_user['g_id']] as $cur_forum)
{
if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
{
if ($cur_category)
echo "\t\t\t\t\t\t\t\t".'</optgroup>'."\n";

echo "\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
$cur_category = $cur_forum['cid'];
}

$redirect_tag = ($cur_forum['redirect_url'] != '') ? ' &gt;&gt;&gt;' : '';
echo "\t\t\t\t\t\t\t".'<option value="'.$cur_forum['fid'].'"'. ($forum_id == $cur_forum['fid'] ? ' selected="selected"' : '').'>'.pun_htmlspecialchars($cur_forum['forum_name']).$redirect_tag.'</option>'."\n";
}

?>
</optgroup>
</select>
<input type="submit" value="<?php echo $lang_common['Go'] ?>" accesskey="g" />
</label>
</div>
</form>
<?php

generate_quickjump_cache($pun_user['g_id']);
require FORUM_CACHE_DIR.'cache_quickjump_'.$pun_user['g_id'].'.php';
}
}

Expand Down
76 changes: 0 additions & 76 deletions include/cache.php
Expand Up @@ -11,82 +11,6 @@
exit;


//
// Generate quick jump cache PHP scripts
//
function generate_quickjump_cache($group_id = false)
{
global $db, $lang_common, $pun_user;

$groups = array();

// If a group_id was supplied, we generate the quick jump cache for that group only
if ($group_id !== false)
{
// Is this group even allowed to read forums?
$result = $db->query('SELECT g_read_board FROM '.$db->prefix.'groups WHERE g_id='.$group_id) or error('Unable to fetch user group read permission', __FILE__, __LINE__, $db->error());
$read_board = $db->result($result);

$groups[$group_id] = $read_board;
}
else
{
// A group_id was not supplied, so we generate the quick jump cache for all groups
$result = $db->query('SELECT g_id, g_read_board FROM '.$db->prefix.'groups') or error('Unable to fetch user group list', __FILE__, __LINE__, $db->error());
$num_groups = $db->num_rows($result);

while ($row = $db->fetch_row($result))
$groups[$row[0]] = $row[1];
}

// Loop through the groups in $groups and output the cache for each of them
foreach ($groups as $group_id => $read_board)
{
// Output quick jump as PHP code
$fh = @fopen(FORUM_CACHE_DIR.'cache_quickjump_'.$group_id.'.php', 'wb');
if (!$fh)
error('Unable to write quick jump cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);

$output = '<?php'."\n\n".'if (!defined(\'PUN\')) exit;'."\n".'define(\'PUN_QJ_LOADED\', 1);'."\n".'$forum_id = isset($forum_id) ? $forum_id : 0;'."\n\n".'?>';

if ($read_board == '1')
{
$result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$group_id.') WHERE fp.read_forum IS NULL OR fp.read_forum=1 ORDER BY c.disp_position, c.id, f.disp_position') or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());

if ($db->num_rows($result))
{
$output .= "\t\t\t\t".'<form id="qjump" method="get" action="viewforum.php">'."\n\t\t\t\t\t".'<div><label><span><?php echo $lang_common[\'Jump to\'] ?>'.'<br /></span>'."\n\t\t\t\t\t".'<select name="id" onchange="window.location=(\'viewforum.php?id=\'+this.options[this.selectedIndex].value)">'."\n";

$cur_category = 0;
while ($cur_forum = $db->fetch_assoc($result))
{
if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
{
if ($cur_category)
$output .= "\t\t\t\t\t\t".'</optgroup>'."\n";

$output .= "\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
$cur_category = $cur_forum['cid'];
}

$redirect_tag = ($cur_forum['redirect_url'] != '') ? ' &gt;&gt;&gt;' : '';
$output .= "\t\t\t\t\t\t\t".'<option value="'.$cur_forum['fid'].'"<?php echo ($forum_id == '.$cur_forum['fid'].') ? \' selected="selected"\' : \'\' ?>>'.pun_htmlspecialchars($cur_forum['forum_name']).$redirect_tag.'</option>'."\n";
}

$output .= "\t\t\t\t\t\t".'</optgroup>'."\n\t\t\t\t\t".'</select>'."\n\t\t\t\t\t".'<input type="submit" value="<?php echo $lang_common[\'Go\'] ?>" accesskey="g" />'."\n\t\t\t\t\t".'</label></div>'."\n\t\t\t\t".'</form>'."\n";
}
}

fwrite($fh, $output);

fclose($fh);

if (function_exists('apc_delete_file'))
@apc_delete_file(FORUM_CACHE_DIR.'cache_quickjump_'.$group_id.'.php');
}
}


//
// Load some information about the latest registered users
//
Expand Down

0 comments on commit d0afe45

Please sign in to comment.