开发者

Pulling all posts in vBulletin

开发者 https://www.devze.com 2023-04-05 17:22 出处:网络
I want to create a script that counts threads words\' count in a vBulletin forum. Bascially, I want to pull that database from the mysql database, and play with it. 开发者_如何学CI don\'t have experie

I want to create a script that counts threads words' count in a vBulletin forum. Bascially, I want to pull that database from the mysql database, and play with it. 开发者_如何学CI don't have experience working with vBulettin, so I'm thinking of two ways:

  1. Does vBulletin provides API to handle database stuff. (Allow me to grab all the threads content, and URLs). I'm almost sure there is, a link where to start?

  2. Is there a solution doing this without the interferance of vBulletin. This means grab the data manually from the mysql database and do stuff the typical way.

I'll prefer the second method if the vBulettin learning curve is too steep. Thanks for the advice.


Is this for vBulletin 3 or 4?

I mostly work with vB3, and the quickest way to include all of the vBulletin resources is to create a new php file in your forums directory with the following code.

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

That $vbulletin variable is the registry object that contains just about everything you're ever going to need, including the database connection and it's read and write functions, userinfo data, cleaned _POST and _REQUEST values, and a lot more (phrases, session data, caches, etc).

There are 4 database functions you'll use the most.

  • $vbulletin->db->query_read() // fetch more than one row
  • $vbulletin->db->fetch_array() // converts the query_read returned data into an array
  • $vbulletin->db->query_first() // fetches a single row
  • $vbulletin->db->query_write() // update, insert or delete rows, tables, etc.

query_read is what you would use when you expect more than one result that you intend to loop through. For example, if you wanted to count all the words in a single thread, you would would need to query the post table with the threadid, loop through each post in that thread and count all the words in the message.

Note: "TABLE_PREFIX" is a constant set in config.php. It's important to always prepend the table name with that constant in case other forums decide to prefix their tables.

<?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));

The query_first function is what you would use when you need to fetch a single row from the database. No looping required or anything like that. If, for instances, you wanted to fetch a single user's information from the database - which you don't need a query for, but we'll do it as an example - you would use something like this.

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

Lastly, if you wanted to update something in the database, you would use "query_write". This function is pretty straight forward. This function just takes any MySQL update insert or delete query. For example, if I needed to update a user's yahoo id, you would do.

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

Hopefully this will help you get started. I would recommend using vBulletin 3 for a little while until you're comfortable with it. I think it'll be easier on you. Most of this will translate to vBulletin 4 with some tweaking, but that code base is not as friendly to new comers.

0

精彩评论

暂无评论...
验证码 换一张
取 消