gpt4 book ai didi

php - 如何为某些帖子创建有效的内容过滤器?

转载 作者:行者123 更新时间:2023-12-01 22:50:07 25 4
gpt4 key购买 nike

我已将这篇文章标记为 WordPress,但我不完全确定它是特定于 WordPress 的,因此我将其发布在 StackOverflow 而不是 WPSE 上。该解决方案不必特定于 WordPress,只需 PHP。

场景
我运行一个 fishkeeping website带一些热带鱼Species ProfilesGlossary条目。

我们的网站以我们的个人资料为导向。正如您所说,它们是网站的基本要素。

我希望实现的是,在每个提到另一个物种或词汇表条目的物种概况中,我可以用链接替换这些词 - 例如你会看到 here .理想情况下,我也希望这也出现在新闻、文章和博客文章中。

我们有近 1400 species profiles1700 glossary entries .我们的物种概况通常很长,最后只计算我们的物种概况 numbered more than 1.7 million words的信息。

我目前正在尝试什么
目前,我有一个 filter.php有一个功能 - 我相信 - 做我需要它做的事情。代码很长,可以在完整的 here 中找到。 .

另外,在我的 WordPress 主题的 functions.php 中,我有以下几点:

# ==============================================================================================
# [Filter]
#
# Every hour, using WP_Cron, `my_updated_posts` is checked. If there are new Post IDs in there,
# it will run a filter on all of the post's content. The filter will search for Glossary terms
# and scientific species names. If found, it will replace those names with links including a
# pop-up.

include "filter.php";

# ==============================================================================================
# When saving a post (new or edited), check to make sure it isn't a revision then add its ID
# to `my_updated_posts`.

add_action( 'save_post', 'my_set_content_filter' );
function my_set_content_filter( $post_id ) {
if ( !wp_is_post_revision( $post_id ) ) {

$post_type = get_post_type( $post_id );

if ( $post_type == "species" || ( $post_type == "post" && in_category( "articles", $post_id ) ) || ( $post_type == "post" && in_category( "blogs", $post_id ) ) ) {
//get the previous value
$ids = get_option( 'my_updated_posts' );

//add new value if necessary
if( !in_array( $post_id, $ids ) ) {
$ids[] = $post_id;
update_option( 'my_updated_posts', $ids );
}
}
}
}

# ==============================================================================================
# Add the filter to WP_Cron.

add_action( 'my_filter_posts_content', 'my_filter_content' );
if( !wp_next_scheduled( 'my_filter_posts_content' ) ) {
wp_schedule_event( time(), 'hourly', 'my_filter_posts_content' );
}

# ==============================================================================================
# Run the filter.

function my_filter_content() {
//check to see if posts need to be parsed
if ( !get_option( 'my_updated_posts' ) )
return false;

//parse posts
$ids = get_option( 'my_updated_posts' );

update_option( 'error_check', $ids );

foreach( $ids as $v ) {
if ( get_post_status( $v ) == 'publish' )
run_filter( $v );

update_option( 'error_check', "filter has run at least once" );
}

//make sure no values have been added while loop was running
$id_recheck = get_option( 'my_updated_posts' );
my_close_out_filter( $ids, $id_recheck );

//once all options, including any added during the running of what could be a long cronjob are done, remove the value and close out
delete_option( 'my_updated_posts' );
update_option( 'error_check', 'working m8' );
return true;
}

# ==============================================================================================
# A "difference" function to make sure no new posts have been added to `my_updated_posts` whilst
# the potentially time-consuming filter was running.

function my_close_out_filter( $beginning_array, $end_array ) {
$diff = array_diff( $beginning_array, $end_array );
if( !empty ( $diff ) ) {
foreach( $diff as $v ) {
run_filter( $v );
}
}
my_close_out_filter( $end_array, get_option( 'my_updated_posts' ) );
}

正如(希望)代码注释所描述的那样,它的工作方式是 WordPress 每小时运行一个 cron 作业(这就像一个虚假的 cron - 适用于用户点击,但这并不重要,因为时间不是重要)运行上面找到的过滤器。

按小时运行它的基本原理是,如果我们试图在保存每个帖子时运行它,这将对作者不利。一旦我们让客座作者参与进来,这显然不是一种可以接受的处理方式。

问题...
几个月来,我一直在让这个过滤器可靠运行时遇到问题。我认为问题不在于过滤器本身,而在于启用过滤器的功能之一 - 即 cron 作业,或选择过滤哪些帖子的功能,或准备词表等的功能过滤器。

不幸的是,诊断问题非常困难(我可以看到),这要归功于它在后台运行并且仅每小时运行一次。我一直在尝试使用 WordPress' update_option函数(它基本上写入一个简单的数据库值)进行错误检查,但我运气不佳 - 老实说,我对问题出在哪里感到很困惑。

我们最终在没有此过滤器正常工作的情况下将网站上线。有时它似乎有效,有时却不起作用。结果,我们现在有很多没有正确过滤的物种概况。

我想要什么...
我基本上是在寻求有关运行此过滤器的最佳方法的建议。

Cron Job 是答案吗?我可以设置一个 .php每天运行的文件,那不会有问题。它如何确定哪些帖子需要过滤?它在运行时会对服务器产生什么影响?

或者,WordPress 管理页面是答案吗?如果我知道怎么做,那么使用 AJAX 来选择页面来运行过滤器就完美了。有一个插件叫 AJAX Regenerate Thumbnails哪个像这样,也许那会是最有效的?

注意事项
  • 数据库大小/受影响/读取/写入的信息
  • 哪些帖子被过滤了
  • 过滤器对服务器的影响;特别是考虑到我似乎无法将 WordPress 内存限制增加到 32Mb 以上。
  • 实际的过滤器本身是否高效、有效和可靠?


  • 这是一个相当复杂的问题,我不可避免地(因为我在这个过程中被同事分心了大约 18 次)遗漏了一些细节。请随时询问我以获取更多信息。

    提前致谢,

    最佳答案

    创建配置文件时执行此操作。

    尝试反转整个过程。与其检查单词的内容,不如检查内容的单词。

  • 将条目中的内容帖子分解为单词(在空格上)
  • 消除重复项、数据库中单词最小大小的单词、最大单词大小的单词以及您保留的“常用单词”列表中的重复项。
  • 检查每个表,如果您的某些表包含带空格的短语,请进行 %text% 搜索,否则进行直接匹配(更快),或者如果真的有那么大的问题,甚至构建哈希表。 (我会把它作为一个 PHP 数组来做并以某种方式缓存结果,没有意义重新发明轮子)
  • 使用现在显着缩小的列表创建您的链接。

  • 即使您要检查 100,000 个单词,您也应该能够轻松地将其保持在 1 秒以内。对于贝叶斯过滤器,我已经完全做到了这一点,没有缓存单词列表。

    使用较小的列表,即使它贪婪并且收集与“ clown ”不匹配的单词也会捕获“ clown 泥鳅”,结果较小的列表应该只有几个到几十个带有链接的单词。这根本不需要时间来查找和替换一大块文本。

    以上并没有真正解决您对旧配置文件的担忧。你没有说确切有多少,只是有很多文本,并且它在 1400 到 3100(两个项目)放在一起。如果您有信息,您可以根据受欢迎程度来制作这些较旧的内容。或按输入日期,最新的在前。不管怎样,最好的方法是编写一个脚本来暂停 PHP 的时间限制,并在所有帖子上批量运行加载/处理/保存。如果每个都需要大约 1 秒(可能要少得多,但在最坏的情况下),您说的是 3100 秒,也就是不到一个小时。

    关于php - 如何为某些帖子创建有效的内容过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11053409/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com