我想创建一个脚本来计算vBulletin论坛中线程词的计数.基本上,我想从mysql数据库中提取该数据库,并使用它.我没有使用vBulettin的经验,所以我想到两种方法:
vBulletin是否提供API来处理数据库内容.(允许我获取所有线程内容和URL).我几乎可以肯定,有一个链接从哪里开始?
有没有一个解决方案这样做没有vBulletin的干扰.这意味着从mysql数据库手动获取数据并以典型的方式填充.
如果vBulettin学习曲线太陡,我会更喜欢第二种方法.感谢您的建议.
Ken*_*Ken 10
这是vBulletin 3还是4?
我主要使用vB3,包含所有vBulletin资源的最快方法是使用以下代码在论坛目录中创建一个新的php文件.
<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');
var_dump($vbulletin);
Run Code Online (Sandbox Code Playgroud)
$ vbulletin变量是注册表对象,它包含您将要使用的所有内容,包括数据库连接及其读写函数,userinfo数据,清理的_POST和_REQUEST值以及更多(短语,会话数据) ,缓存等).
您将使用最多的4个数据库函数.
query_read是您希望循环使用多个结果时使用的内容.例如,如果要计算单个线程中的所有单词,则需要使用threadid查询post表,遍历该线程中的每个帖子并计算消息中的所有单词.
注意:"TABLE_PREFIX"是config.php中设置的常量.在其他论坛决定为其表添加前缀的情况下,始终在表名前加上该常量非常重要.
<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');
$threadid = 1;
// fetch all post from a specfic thread
$posts = $vbulletin->db->query_read("
SELECT pagetext
FROM " . TABLE_PREFIX . "post
WHERE threadid = $threadid
");
/**
* Loop through each post.
*
* Here we use the "fetch_array" method to convert the MySQL data into
* a useable array. 99% of the time you'll use "fetch_array" when you
* use "query_read".
*
* $post will contains the post data for each loop. In our case, we only
* have "pagetext" avaliable to use since that's all we told MySQL we needed
* in our query. You can do SELECT * if you want ALL the table data.
*/
while ($post = $vbulletin->db->fetch_array($posts)) {
$totalWords = $totalWords + str_word_count($post['pagetext']);
}
/**
* Print the total number of words this thread contains.
*
* The "vb_number_format" is basically wrapper of php's "number_format", but
* with some vBulletin extras. You can visit the /includes/functions.php file
* for all the functions available to you. Many of them are just convenient
* functions so you don't have to repeat a lot of code. Like vBDate(), or
* is_valid_email().
*/
echo sprintf("Thread ID %i contains %s words", $threadid, vb_number_format($totalWords));
Run Code Online (Sandbox Code Playgroud)
该query_first功能是当你需要从数据库中提取单个行,你会用什么.不需要循环或类似的东西.例如,如果您想从数据库中获取单个用户的信息 - 您不需要查询,但我们将其作为示例 - 您将使用类似的东西.
<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');
$userid = 1;
$user = $vbulletin->db->query_first("
SELECT *
FROM " . TABLE_PREFIX . "user
WHERE userid = $userid
");
echo sprintf("Hello, %s. Your email address is %s and you have %s posts",
$user['username'],
$user['email'],
vb_number_format($user['posts'])
);
Run Code Online (Sandbox Code Playgroud)
最后,如果您想更新数据库中的某些内容,可以使用" query_write ".这个功能很简单.此函数只接受任何MySQL更新插入或删除查询.例如,如果我需要更新用户的yahoo id,你会这样做.
<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');
$userid = 1;
$update = $vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET yahoo = 'myYahooID@yahoo.com'
WHERE userid = $userid
");
if ($update) {
$userinfo = fetch_userinfo($userid);
echo sprintf("Updated %s yahoo ID to %s", $userinfo['username'], $userinfo['yahoo']);
}
Run Code Online (Sandbox Code Playgroud)
希望这将有助于您入门.我会建议使用vBulletin 3一段时间,直到你对它感到满意为止.我觉得你会更容易.其中大部分将通过一些调整转换为vBulletin 4,但该代码库对新来者并不友好.