Hook "injection" and data capture
#1
Hello,
I think about use hooks to make "different" think - capture MyBB data, for example resource to DB query.

Why use that?
For example, we must do something for all posts in showthread page and get data from DB (for each post). Standard method is to use postbit hook and make queries in loop. Sorry, I don't like queries in loops - it isn't a good solution.

So i try this (it's only example):

$plugins->add_hook("postbit", "reverse_inject");
$plugins->add_hook("showthread_linear", "reverse_return");

(...)

function reverse_inject(&$post) 
{
	global $db, $mybb, $lang, $query, $posts_tpl, $posts_ids;
    static $injected = false;
    
    // Reverse flag - UP!
    if ($injected)
    {
        return;
    }
    $injected = true;

    // Collect posts ids
    $posts_ids = $posts_data = array(); 
    $posts_ids[] = $post['pid'];
    
    // Capture global DB resource
    while ($row = $db->fetch_array($query))
    {
        $posts_ids[] = $row['pid'];
        $posts_data[$row['pid']] = $row;
    }
    
    // Get all required data for all posts (1 query)
    reverse_get_required_data($posts_ids);
    
    // We must modify first post manualy
    reverse_modify_post($post);
    
    $posts_tpl = '';
    foreach ($posts_data as &$post_single)
    {
        reverse_modify_post($post_single);
        $posts_tpl .= build_postbit($post_single);   
    }	
}

(...)

function reverse_return()
{
    global $posts, $posts_tpl;

    $posts .= $posts_tpl;
}

And it works.
How do you think about that? Is this a good idea?
On what problems in your opinion it may encounter?
Priority - it's very important, what else?
Reply
#2
This user has been denied support. This user has been denied support.
And here I thought the query hijacking I'm doing in Google SEO was ugly...

Main problem with your code is that it would break if two plugins tried to use it at the same time.

It would be easier if MyBB itself fetched all data first before processing it, but alas...
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)