2011-09-19 105 views
0

我想創建一個腳本來計算vBulletin論壇中的線程字數。基本上,我想從MySQL數據庫中提取數據庫,並使用它。我沒有經驗vBulettin工作,所以我想有兩種方法:vBulletin中的所有帖子

  1. 是否vBulletin提供的API來處理數據庫的東西。 (允許我抓取所有線程內容和URL)。我幾乎可以肯定,有一個鏈接從哪裏開始?

  2. 有沒有解決方案這樣做,而不會干擾vBulletin。這意味着從mysql數據庫中手動獲取數據並按照典型的方式進行操作。

如果vBulettin學習曲線太陡峭,我更喜歡第二種方法。感謝您的建議。

回答

10

這是用vBulletin 3還是4?

我主要使用vB3,並且包含所有vBulletin資源的最快方法是使用以下代碼在您的論壇目錄中創建一個新的php文件。

<?php 
error_reporting(E_ALL & ~E_NOTICE & ~8192); 
require_once('./global.php'); 
var_dump($vbulletin); 

這$ vBulletin變量是包含只是你以往任何時候都需要,包括數據庫連接一切註冊表對象,它的讀寫功能,用戶信息數據,清理POST和_REQUEST值,和很多(短語,會話數據,緩存等)。

有4個數據庫函數可供您使用最多。

  • $ vbulletin->分貝 - > query_read()//取多行
  • $ vbulletin->分貝 - > fetch_array()//轉換query_read返回的數據到一個數組
  • $ vbulletin->分貝 - > query_first()//取出的單個行
  • $ vbulletin->分貝 - > query_write()//更新,插入或刪除行,表格等

query_read是您希望在您打算循環使用多個結果時使用的。例如,如果您想要統計單個線程中的所有單詞,則需要使用threadid查詢發佈表,循環該線程中的每個帖子並計算消息中的所有單詞。

注意:「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)); 

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']) 
); 

最後,如果你想在數據庫中更新的東西,你就可以用「query_write」。這個功能非常簡單。這個函數只需要任何MySQL更新插入或刪除查詢。例如,如果我需要更新用戶的雅虎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 = '[email protected]' 
    WHERE userid = $userid 
"); 

if ($update) { 
    $userinfo = fetch_userinfo($userid); 
    echo sprintf("Updated %s yahoo ID to %s", $userinfo['username'], $userinfo['yahoo']); 
} 

希望這會幫助你開始。我會建議使用vBulletin 3一段時間,直到你對它感到滿意爲止。我認爲這對你更容易。其中大部分將轉化爲vBulletin 4,並進行一些調整,但該代碼基礎並不像新手一樣友善。

+0

vB4代碼庫對有經驗的編碼器更友好嗎?我試圖決定保持3和升級。 – zylstra

相關問題