phpBB旧主题储物柜

I had this request to implement a feature on a phpBB board whereby topics older than one month to get deleted. A quick search on phpbbhacks.com revealed a result - Auto Lock hack, but it had a disturbing note saying that it's not recommended for larger boards, although I couldn't figure out why. Anyway, the hack looked too involved, with DB changes, new Admin Panel options, etc., so I decided to hack something together quickly. Turned out to be pretty easy. Here' s the result.

我有这个要求,要在phpBB板上实现一项功能,以便删除一个月以上的主题。 在phpbbhacks.com上进行的快速搜索显示了一个结果-Auto Lock hack ,但是有一个令人不安的注释,说它不建议用于较大的主板,尽管我不知道为什么。 无论如何,黑客看起来过于复杂,涉及数据库更改,新的“管理面板”选项等,因此我决定很快一起黑客入侵。 原来很简单。 这是结果。

SQL查询 (The SQL query)

UPDATE phpbb_posts AS p, phpbb_topics AS t
  SET t.topic_status = 1
WHERE p.post_id = t.topic_last_post_id
  AND FROM_UNIXTIME(p.post_time) + INTERVAL 1 MONTH < NOW()
  AND t.topic_status = 0

Basically the logic is so simple that it can be done in one query. The topics table contains the id of the last post. I join the posts table to figure out the date/time of the last post. And if it's old enough, I set the topic status to 1 (locked).

逻辑基本上很简单,可以在一个查询中完成。 主题表包含最近发布的ID。 我加入了帖子表,以找出上一篇帖子的日期/时间。 如果足够老,我将主题状态设置为1(锁定)。

I like the INTERVAL MySQL thing so I used it here, converting the Unix timestamps to human dates. It makes it much more easier to see at a glance that the period is one month and quickly change it afterwards if need be, without starting the old and tired calculation: "OK, 60 seconds times 60 minutes times 24 hours... Wait a sec! Is it seconds or milliseconds?"

我喜欢INTERVAL MySQL,所以在这里使用它,将Unix时间戳转换为人类日期。 这样一目了然,您可以一目了然地看到该期限为一个月,然后在需要时Swift更改该期限,而无需开始进行陈旧的计算:“好吧,60秒乘以60分钟乘以24小时……等待秒!是秒还是毫秒?”

common.php (The common.php)

Instead of dealing with cron jobs, I decided to execute the query in common.php, a script that is always loaded on every page. I execute the query and send myself an email of something wrong happens.

我决定不处理cron作业,而是决定在common.php中执行查询,该脚本始终加载在每个页面上。 我执行查询,并向自己发送发生错误的电子邮件。

<?php
$sql = 'UPDATE phpbb_posts AS p, phpbb_topics AS t
            SET t.topic_status = 1
        WHERE p.post_id = t.topic_last_post_id
            AND FROM_UNIXTIME(p.post_time) + INTERVAL 3 MONTH < NOW()
            AND t.topic_status = 0';
if (!$db->sql_query($sql))
{

    @mail('me@example.org',
          '[thesite] Locker',
          'Error while locking old topics',
          'From: meagain@example.org'
         );
}
?>

概率触摸 (A probabilistic touch)

Now, executing this query is not necessary on every page load. So I added a touch of probability.

现在,不必在每次页面加载时都执行此查询。 因此,我增加了一些可能性。

<?php
if (mt_rand(1, 10) == 5) { //chance one in ten to run
    // do the stuff
}
?>

That means that this code has a chance one in ten of being executed on a page load. Even this is a bit high, but can always be changed to 1 in a 100 for example.

这意味着该代码有机会在页面加载时执行十分之一的代码。 即使这个值有点高,但是例如可以始终将其更改为100中的1。

分配? (Distribution?)

This was done only for internal purposes, not designed to be made available to the larger phpBB community. Otherwise it would be better if it doesn't hard-code the "phpbb_" table prefix, as well as the table names in general and the topic status value. Instead, phpBB constants must be used - like POSTS_TABLE, TOPICS_TABLE and TOPIC_LOCKED.

这样做仅出于内部目的,并非旨在提供给更大的phpBB社区。 否则,如果不对“ phpbb_”表前缀以及常规表名和主题状态值进行硬编码,则更好。 相反,必须使用phpBB常量-像POSTS_TABLE,TOPICS_TABLE和TOPIC_LOCKED。

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/phpbb-old-topics-locker/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值