比如我想每隔1天就优化一下数据表,难道每天都要打开论坛执行SQL吗?不用,安装了这个插件就能让你的论坛定时执行SQL了。
后台设置见图。
修改方法:
打开后台,升级数据库,执行:
- INSERT INTO `cdb_settings` VALUES('savetime', '0');
- INSERT INTO `cdb_settings` VALUES('querytime', '0');
- INSERT INTO `cdb_settings` VALUES('lastquery', '0');
复制代码
打开模板文件admincp.lang.php,找:
- 'menu_database_optimize' => '数据表优化',
复制代码
在下面添加:
- 'menu_database_timing' => '定时执行',
复制代码
打开admincp.php文件,找:
在后面加上:
打开admin/menu.php文件,找:
- array('name' => $lang['menu_database_query'], 'url' => 'admincp.php?action=runquery'),
复制代码
在下面添加:
- array('name' => $lang['menu_database_timing'], 'url' => 'admincp.php?action=timing'),
复制代码
打开admin/database.php文件,找最后一个:
改成:
- } else if($action == 'timing') {
- if(!submitcheck('timingsubmit')) {
- if($fp = @fopen('forumdata/autosql.txt', 'r')) {
- $querysql = fread($fp, filesize('forumdata/autosql.txt'));
- fclose($fp);
- }
- $savetime = $db->fetch_array($db->query("SELECT * FROM {$tablepre}settings WHERE variable='savetime'"));
- $savetime = $savetime['value'];
- $querytime = $db->fetch_array($db->query("SELECT * FROM {$tablepre}settings WHERE variable='querytime'"));
- $querytime = $querytime['value'];
- $lastquery = $db->fetch_array($db->query("SELECT * FROM {$tablepre}settings WHERE variable='lastquery'"));
- $lastquery = $lastquery['value'];
- if($querytime == 0) {
- $unit = '<select name="unit"><option value="1">秒钟</option><option value="60">分钟</option><option value="3600">小时</option><option value="86400">天</option></select>';
- } else if($querytime > 0 && $querytime < 60) {
- $unit = '秒钟';
- $querytime = intval($querytime);
- } else if($querytime >= 60 && $querytime < 3600) {
- $unit = '分钟';
- $querytime = intval($querytime / 60);
- } else if($querytime >= 3600 && $querytime < 3600 * 24) {
- $unit = '小时';
- $querytime = intval($querytime / 3600);
- } else {
- $unit = '天';
- $querytime = intval($querytime / (3600 * 24));
- }
- include CP_TPL . 'database_timing.php';
- } else {
- $db->query("UPDATE {$tablepre}settings SET value='$timestamp' WHERE variable='savetime'");
- $query = "UPDATE {$tablepre}settings SET value='" . ($querytime_txt * $unit) . "' WHERE variable='querytime'";
- $db->query($query);
- if($fp = @fopen('forumdata/autosql.txt', 'w')) {
- fwrite($fp, $querysql);
- fclose($fp);
- }
- cpmsg('信息保存完成。');
- }
- }
复制代码
建立一个新文件 admin/tpl/database_timing.php ,内容如下:
- <?php if(!defined('IN_ADMINCP')) exit('Access Denied'); ?>
- <table cellspacing="0" cellpadding="0" border="0" width="90%" align="center"><tr><td bgcolor="<?=BORDERCOLOR?>"><table border="0" cellspacing="<?=BORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="100%"><tr class="header"><td><?=$lang['tips']?></td></tr>
- <tr bgcolor="<?=ALTBG1?>"><td>
- <br><li>请设定下列的信息,Discuz!将会按照您设定的时间自动执行您预定的SQL语句。<br><br>
- </td></tr></table></td></tr></table>
- <br><br><form name="optimize" method="post" action="admincp.php?action=timing">
- <input type="hidden" name="formhash" value="<?=FORMHASH?>">
- <table cellspacing="0" cellpadding="0" border="0" width="90%" align="center"><tr><td bgcolor="<?=BORDERCOLOR?>"><table border="0" cellspacing="<?=BORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="100%"><tr class="header"><td colspan="2">定时执行SQL 设置</td></tr>
- <tr bgcolor="<?=ALTBG1?>"><td width="40%"><b>执行语句:</b><br>请设定您要定时执行的SQL语句。</td><td width="60%"><textarea cols="55" rows="5" name="querysql"><?=$querysql?></textarea></td></tr>
- <tr bgcolor="<?=ALTBG1?>"><td width="40%"><b>执行间隔:</b><br>请设定两次执行之间的间隔。</td><td width="60%"><input type="text" size="15" name="querytime_txt" value="<?=($querytime ? $querytime : '')?>"> <?=$unit?></td></tr>
- <tr bgcolor="<?=ALTBG1?>"><td width="40%"><b>上次执行:</b><br>上次执行SQL语句的时间。</td><td><?php if($lastquery) { echo(gmdate("$dateformat $timeformat", $lastquery + $timeoffset * 3600)); } else { echo('从未执行'); }?></td></tr>
- <tr bgcolor="<?=ALTBG1?>"><td width="40%"><b>修改时间:</b><br>上次修改配置的时间。</td><td><?php if($savetime) { echo(gmdate("$dateformat $timeformat", $savetime + $timeoffset * 3600)); } else { echo('从未使用过本功能'); }?></td></tr>
- </table></td></tr></table><br>
- <center><input type="submit" name="timingsubmit" value="<?=$lang['submit']?>"></center></form>
复制代码
打开common.php,在最后面的:
在上面添加:
- $query = $db->query("SELECT * FROM {$tablepre}settings WHERE variable='querytime'");
- $querytime = $db->fetch_array($query);
- $querytime = $querytime['value'];
- unset($query);
- $query = $db->query("SELECT * FROM {$tablepre}settings WHERE variable='lastquery'");
- $lastquery = $db->fetch_array($query);
- $lastquery = $lastquery['value'];
- unset($query);
- if(($timestamp >= ($lastquery + $querytime)) && ($fp = fopen('forumdata/autosql.txt', 'r'))) {
- $db->query(fread($fp, filesize('forumdata/autosql.txt')));
- $db->query("UPDATE {$tablepre}settings SET value='$timestamp' WHERE variable='lastquery'");
- fclose($fp);
- }
复制代码
完成! |