Issue with rewriting links in external file

Support for the phpBB3 SEO mods released in the phpBB3 SEO Toolikt forum.

Moderator: Moderators


Issue with rewriting links in external file

Postby tbxn » Tue Sep 02, 2008 2:46 pm

Hello, first of all I would like to thank you for the mods that you have created. I've found them very useful.

I have a small problem, hopefully you can give me some insight on this matter. I am integrating my website with the phpbb forum. I have the latest advanced seo mod installed on my forum, as well as the other seo toolkit mods. On my news page I fetch posts from the database and display them. A few of the URLs are rewritten but some of them are not and I am hoping you can help me with this. Below is a portion of my news script which fetches posts from the forum for display. Perhaps I am just missing an included file? My post-author/member links are rewritten, as well as the forum url but the viewtopic.php?t=1 etc. links are not rewritten, and I would like them to be rewritten to the topic url such as mysite.com/this-topic/. Thank you for your help.
Code: Select all
<?php

define('IN_PHPBB', true);
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
   
// Start session management
$user->session_begin();
$auth->acl($user->data);

//news script variables
$charlimit = '0';
$timestamp = 'D M j, Y g:i a';
$forum_id = array('2');
$showavatars = 'false';
$newsdisplay = '10';

$allow_bbcode = true;
$allow_smilies = false;
$allow_urls = true;

   // Auth
   $can_read_forum = $auth->acl_getf('f_read');                   //Get the forums the user can read from
   $forums_auth_ary = array_keys($can_read_forum);                   //Re-work the array some
   $authed_news_ary = array_intersect($forum_id, $forums_auth_ary);   //Of the desired forums, pull out the authed ones
   unset($can_read_forum);
   unset($forums_auth_ary);

   $sql = 'SELECT t.forum_id, t.topic_id, t.topic_title, t.topic_replies, p.post_id, p.post_subject, p.post_text, p.post_time, p.bbcode_bitfield, p.bbcode_uid, u.username, u.user_id, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height, u.user_colour
         FROM ' . TOPICS_TABLE . ' t
            INNER JOIN ' . POSTS_TABLE . ' p ON (t.topic_id = p.topic_id)
               INNER JOIN ' . USERS_TABLE . ' u ON (p.poster_id = u.user_id)
                  WHERE t.topic_approved = 1
                     AND p.post_approved = 1
                     AND ' . $db->sql_in_set('p.forum_id', $authed_news_ary) . '
                     AND p.post_id = t.topic_first_post_id
                  ORDER BY p.post_time DESC';

   $result = $db->sql_query_limit($sql, $newsdisplay);
   $row = $db->sql_fetchrowset($result);
   $db->sql_freeresult($result);
   
   $flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);

   // Check that we have enough entries to display
   if(sizeof($row) < $newsdisplay)
   {
      $newsdisplay = sizeof($row);
   }

   //
   // Organize the posts into an array
   // Trim and work some display magic if necessary
   //
   $posts = array();
   for($i = 0; $i < $newsdisplay; $i++)
   {
      $posts[$i]['topic_id'] = $row[$i]['topic_id'];
      $posts[$i]['forum_id'] = $row[$i]['forum_id'];
      $posts[$i]['topic_replies'] = $row[$i]['topic_replies'];
      $posts[$i]['post_time'] = date($timestamp, $row[$i]['post_time']);
      $posts[$i]['topic_title'] = $row[$i]['topic_title'];
      $posts[$i]['post_subject'] = $row[$i]['post_subject'];
      $posts[$i]['user_id'] = $row[$i]['user_id'];
      $posts[$i]['user_link'] = get_username_string('full', $row[$i]['user_id'], $row[$i]['username'], $row[$i]['user_colour']);
      $posts[$i]['user_avatar'] = get_user_avatar($row[$i]['user_avatar'], $row[$i]['user_avatar_type'], $row[$i]['user_avatar_width'], $row[$i]['user_avatar_height']);
      $posts[$i]['topic_link'] = append_sid("{$forum_root_path}viewtopic.$phpEx", 'f=' . $posts[$i]['forum_id'] . '&amp;t=' . $posts[$i]['topic_id']);
      $posts[$i]['reply_link'] = append_sid("{$forum_root_path}posting.$phpEx", 'mode=reply&amp;f=' . $posts[$i]['forum_id'] . '&amp;t=' . $posts[$i]['topic_id']);

      censor_text($posts[$i]['topic_title']);
      censor_text($posts[$i]['post_subject']);
   
      $post_text = '';
      //If the text is short enough, don't trim it, and fully display it with BBCdoes, Smilies, and URLs
      if(($charlimit == 0) || (strlen($row[$i]['post_text'] <= $charlimit)))
      {
         $posts[$i]['message'] = generate_text_for_display($row[$i]['post_text'], $row[$i]['bbcode_uid'], $row[$i]['bbcode_bitfield'], $flags);
      }
      else
      {
         strip_bbcode($row[$i]['post_text']);
         censor_text($row[$i]['post_text']);
         $posts[$i]['message'] = substr($row[$i]['post_text'], 0, $charlimit) . '...' . '<br /><br /><a href="' . $posts[$i]['topic_link'] . '">[Read More]</a>';
      }
     
      $sql = 'SELECT forum_name
            FROM ' . FORUMS_TABLE . '
               WHERE forum_id = ' . intval($posts[$i]['forum_id']);
      $result = $db->sql_query($sql);
      $posts[$i]['forum_name'] = $db->sql_fetchfield('forum_name');
      $db->sql_freeresult($result);

      if($posts[$i]['topic_title'] == '')
      {
         continue;
      }

      $template->assign_block_vars('fetchpost_row', array(
         'AVATAR'      => $posts[$i]['user_avatar'],
         'FORUM_NAME'   => $posts[$i]['forum_name'],
         'POSTER'      => $posts[$i]['user_link'],
         'POST_SUBJECT'   => $posts[$i]['post_subject'],
         'POST_TIME'      => $posts[$i]['post_time'],
         'REPLIES'      => $posts[$i]['topic_replies'],
         'TEXT'         => $posts[$i]['message'],
         'TOPIC_TITLE'   => $posts[$i]['topic_title'],
         'U_FORUMLINK'   => append_sid("{$forum_root_path}viewforum.$phpEx", 'f=' . $posts[$i]['forum_id']),
         'U_REPLYLINK'   => $posts[$i]['reply_link'],
         'U_VIEWLINK'   => $posts[$i]['topic_link'],
         ));
   }

    $template->set_filenames(array(
        'body' => 'home.html',
    ));

    page_footer();

?>
tbxn
 
Posts: 11
Joined: Sun Aug 03, 2008 1:38 pm

Advertisement

Postby dcz » Sat Sep 06, 2008 8:38 am

You'd probably should do better to get forum names :

Code: Select all
      $sql = 'SELECT forum_name
            FROM ' . FORUMS_TABLE . '
               WHERE forum_id = ' . intval($posts[$i]['forum_id']);
      $result = $db->sql_query($sql);
      $posts[$i]['forum_name'] = $db->sql_fetchfield('forum_name');
      $db->sql_freeresult($result);


Means one SQL per topic listed where you could do it all at once if you'd first collect all forum IDs from the topic data, and then use WHERE forum_id IN (id1,id2 ...).
This would mean to loop twice, once to parse topic data and collect forum ids and then once to assemble the forum data to the topic data and send it to the template, but it should be more efficient this way.

Anyway, for the rewriting, you just need to fill the seo_url arrays, as is, you'd need to add :
Code: Select all
t.topic_type,

after :
Code: Select all
t.topic_replies,


and :

Code: Select all
// www.phpBB-SEO.com SEO TOOLKIT BEGIN
if ( empty($phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']]) ) {
   if ($posts[$i]['topic_type'] == POST_GLOBAL) {
      $phpbb_seo->seo_opt['topic_type'][$posts[$i]['topic_id']] = POST_GLOBAL;
   }
   $phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']] = $phpbb_seo->format_url(censor_text($posts[$i]['topic_title']));
}
if ( empty($phpbb_seo->seo_url['forum'][$posts[$i]['forum_id']]) ) {
   $phpbb_seo->seo_url['forum'][$posts[$i]['forum_id']] = $phpbb_seo->set_url($posts[$i]['forum_name'], $posts[$i]['forum_id'], $phpbb_seo->seo_static['forum']);
}
// www.phpBB-SEO.com SEO TOOLKIT END


after :

Code: Select all
      if($posts[$i]['topic_title'] == '')
      {
         continue;
      }


Should do it ;)

++
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 19930
Joined: Fri Apr 28, 2006 9:03 pm

Postby tbxn » Sat Sep 06, 2008 1:48 pm

I made the adjustments but the viewtopic urls are still not rewritten. Also I was wondering if you could elaborate on what you were saying about forum names... how would I implement that change? Thanks again for your help.

Anyway, here is what my file looks like now with the SEO mod additions:

Code: Select all
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/site');

//news script variables
$charlimit = '0';
$timestamp = 'D M j, Y g:i a';
$forum_id = array('2', '16', '27', '28', '29', '30', '31', '32', '33', '34', '36', '38', '39');
$newsdisplay = '10';

// BBCodes, Smilies, etc... for fully displayed posts.  Trimmed posts will lose effects
$allow_bbcode = true;
$allow_smilies = false;
$allow_urls = true;

   // Auth
   $can_read_forum = $auth->acl_getf('f_read');                   //Get the forums the user can read from
   $forums_auth_ary = array_keys($can_read_forum);                   //Re-work the array some
   $authed_news_ary = array_intersect($forum_id, $forums_auth_ary);   //Of the desired forums, pull out the authed ones
   unset($can_read_forum);
   unset($forums_auth_ary);

   $sql = 'SELECT t.forum_id, t.topic_id, t.topic_title, t.topic_replies, t.topic_type, p.post_id, p.post_subject, p.post_text, p.post_time, p.bbcode_bitfield, p.bbcode_uid, u.username, u.user_id, u.user_colour
         FROM ' . TOPICS_TABLE . ' t
            INNER JOIN ' . POSTS_TABLE . ' p ON (t.topic_id = p.topic_id)
               INNER JOIN ' . USERS_TABLE . ' u ON (p.poster_id = u.user_id)
                  WHERE t.topic_approved = 1
                     AND p.post_approved = 1
                     AND ' . $db->sql_in_set('p.forum_id', $authed_news_ary) . '
                     AND p.post_id = t.topic_first_post_id
                  ORDER BY p.post_time DESC';

   $result = $db->sql_query_limit($sql, $newsdisplay);
   $row = $db->sql_fetchrowset($result);
   $db->sql_freeresult($result);
   
   $flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);

   // Check that we have enough entries to display
   if(sizeof($row) < $newsdisplay)
   {
      $newsdisplay = sizeof($row);
   }

   //
   // Organize the posts into an array
   // Trim and work some display magic if necessary
   //
   $posts = array();
   for($i = 0; $i < $newsdisplay; $i++)
   {
      $posts[$i]['topic_id'] = $row[$i]['topic_id'];
     $posts[$i]['topic_type'] = $row[$i]['topic_type'];
      $posts[$i]['forum_id'] = $row[$i]['forum_id'];
      $posts[$i]['topic_replies'] = $row[$i]['topic_replies'];
      $posts[$i]['post_time'] = date($timestamp, $row[$i]['post_time']);
      $posts[$i]['topic_title'] = $row[$i]['topic_title'];
      $posts[$i]['post_subject'] = $row[$i]['post_subject'];
      $posts[$i]['user_id'] = $row[$i]['user_id'];
      $posts[$i]['user_link'] = get_username_string('full', $row[$i]['user_id'], $row[$i]['username'], $row[$i]['user_colour']);
      $posts[$i]['topic_link'] = append_sid("{$forum_root_path}viewtopic.$phpEx", 'f=' . $posts[$i]['forum_id'] . '&amp;t=' . $posts[$i]['topic_id']);
      $posts[$i]['reply_link'] = append_sid("{$forum_root_path}posting.$phpEx", 'mode=reply&amp;f=' . $posts[$i]['forum_id'] . '&amp;t=' . $posts[$i]['topic_id']);

      censor_text($posts[$i]['topic_title']);
      censor_text($posts[$i]['post_subject']);
   
      $post_text = '';
      //If the text is short enough, don't trim it, and fully display it with BBCdoes, Smilies, and URLs
      if(($charlimit == 0) || (strlen($row[$i]['post_text'] <= $charlimit)))
      {
         $posts[$i]['message'] = generate_text_for_display($row[$i]['post_text'], $row[$i]['bbcode_uid'], $row[$i]['bbcode_bitfield'], $flags);
      }
      //If the text is too long, trim it.  Do not show BBCodes, Smilies, or URLs, as trimming will wreck havoc on the page if we splice an HTML code
      else
      {
         strip_bbcode($row[$i]['post_text']);
         censor_text($row[$i]['post_text']);
         $posts[$i]['message'] = substr($row[$i]['post_text'], 0, $charlimit) . '...' . '<br /><br /><a href="' . $posts[$i]['topic_link'] . '">[Read More]</a>';
      }
     
      $sql = 'SELECT forum_name
            FROM ' . FORUMS_TABLE . '
               WHERE forum_id = ' . intval($posts[$i]['forum_id']);
      $result = $db->sql_query($sql);
      $posts[$i]['forum_name'] = $db->sql_fetchfield('forum_name');
      $db->sql_freeresult($result);

      if($posts[$i]['topic_title'] == '')
      {
         continue;
      }
    
     // www.phpBB-SEO.com SEO TOOLKIT BEGIN
if ( empty($phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']]) ) {
   if ($posts[$i]['topic_type'] == POST_GLOBAL) {
      $phpbb_seo->seo_opt['topic_type'][$posts[$i]['topic_id']] = POST_GLOBAL;
   }
   $phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']] = $phpbb_seo->format_url(censor_text($posts[$i]['topic_title']));
}
if ( empty($phpbb_seo->seo_url['forum'][$posts[$i]['forum_id']]) ) {
   $phpbb_seo->seo_url['forum'][$posts[$i]['forum_id']] = $phpbb_seo->set_url($posts[$i]['forum_name'], $posts[$i]['forum_id'], $phpbb_seo->seo_static['forum']);
}
// www.phpBB-SEO.com SEO TOOLKIT END

      $template->assign_block_vars('fetchpost_row', array(
         'FORUM_NAME'   => $posts[$i]['forum_name'],
         'POSTER'      => $posts[$i]['user_link'],
         'POST_SUBJECT'   => $posts[$i]['post_subject'],
         'POST_TIME'      => $posts[$i]['post_time'],
         'REPLIES'      => $posts[$i]['topic_replies'],
         'TEXT'         => $posts[$i]['message'],
         'TOPIC_TITLE'   => $posts[$i]['topic_title'],
         'U_FORUMLINK'   => append_sid("{$forum_root_path}viewforum.$phpEx", 'f=' . $posts[$i]['forum_id']),
         'U_REPLYLINK'   => $posts[$i]['reply_link'],
         'U_VIEWLINK'   => $posts[$i]['topic_link'],
         ));
   }

    $template->set_filenames(array(
        'body' => 'home.html',
    ));

    page_footer();

?>
tbxn
 
Posts: 11
Joined: Sun Aug 03, 2008 1:38 pm

Postby dcz » Sat Sep 06, 2008 2:57 pm

oh yes, add :
Code: Select all
     // www.phpBB-SEO.com SEO TOOLKIT BEGIN
if ( empty($phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']]) ) {
   if ($posts[$i]['topic_type'] == POST_GLOBAL) {
      $phpbb_seo->seo_opt['topic_type'][$posts[$i]['topic_id']] = POST_GLOBAL;
   }
   $phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']] = $phpbb_seo->format_url(censor_text($posts[$i]['topic_title']));
}
     // www.phpBB-SEO.com SEO TOOLKIT END

after :

Code: Select all
   for($i = 0; $i < $newsdisplay; $i++)
   {



and then :
Code: Select all
// www.phpBB-SEO.com SEO TOOLKIT BEGIN
if ( empty($phpbb_seo->seo_url['forum'][$posts[$i]['forum_id']]) ) {
   $phpbb_seo->seo_url['forum'][$posts[$i]['forum_id']] = $phpbb_seo->set_url($posts[$i]['forum_name'], $posts[$i]['forum_id'], $phpbb_seo->seo_static['forum']);
}
// www.phpBB-SEO.com SEO TOOLKIT END


after :
Code: Select all
      if($posts[$i]['topic_title'] == '')
      {
         continue;
      }


should do it, the topic link was built before ;)

++
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 19930
Joined: Fri Apr 28, 2006 9:03 pm

Postby tbxn » Sat Sep 06, 2008 3:51 pm

At first it came up with undefined offset errors. I realized that the problem was the first bit of code:

Code: Select all
// www.phpBB-SEO.com SEO TOOLKIT BEGIN
if ( empty($phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']]) ) {
   if ($posts[$i]['topic_type'] == POST_GLOBAL) {
      $phpbb_seo->seo_opt['topic_type'][$posts[$i]['topic_id']] = POST_GLOBAL;
   }
   $phpbb_seo->seo_url['topic'][$posts[$i]['topic_id']] = $phpbb_seo->format_url(censor_text($posts[$i]['topic_title']));
}
     // www.phpBB-SEO.com SEO TOOLKIT END


This used the $posts[$i]['topic_id'] variables which had not yet been defined so I just changed that bit of code to:

Code: Select all
// www.phpBB-SEO.com SEO TOOLKIT BEGIN
   if ( empty($phpbb_seo->seo_url['topic'][$row[$i]['topic_id']]) ) {
    if ($row[$i]['topic_type'] == POST_GLOBAL) {
         $phpbb_seo->seo_opt['topic_type'][$row[$i]['topic_id']] = POST_GLOBAL;
      }
         $phpbb_seo->seo_url['topic'][$row[$i]['topic_id']] = $phpbb_seo->format_url(censor_text($row[$i]['topic_title']));
      }
     // www.phpBB-SEO.com SEO TOOLKIT END


and now it works great. Thanks a lot for all your help. By the way, I'm curious about what you were saying about optimizing that sql query.

Means one SQL per topic listed where you could do it all at once if you'd first collect all forum IDs from the topic data, and then use WHERE forum_id IN (id1,id2 ...).
This would mean to loop twice, once to parse topic data and collect forum ids and then once to assemble the forum data to the topic data and send it to the template, but it should be more efficient this way.


Could you please explain a bit more about how I might go about implementing this? I'm already using that bit of code that you posted towards the bottom of my script.

Thanks again.
tbxn
 
Posts: 11
Joined: Sun Aug 03, 2008 1:38 pm

Postby dcz » Sat Sep 13, 2008 7:58 am

You could do something like :

Code: Select all
// ...
   $sql = 'SELECT t.forum_id, t.topic_id, t.topic_title, t.topic_replies, t.topic_type, p.post_id, p.post_subject, p.post_text, p.post_time, p.bbcode_bitfield, p.bbcode_uid, u.username, u.user_id, u.user_colour
         FROM ' . TOPICS_TABLE . ' t
            INNER JOIN ' . POSTS_TABLE . ' p ON (t.topic_id = p.topic_id)
               INNER JOIN ' . USERS_TABLE . ' u ON (p.poster_id = u.user_id)
                  WHERE t.topic_approved = 1
                     AND p.post_approved = 1
                     AND ' . $db->sql_in_set('p.forum_id', $authed_news_ary) . '
                     AND p.post_id = t.topic_first_post_id
                  ORDER BY p.post_time DESC';

   $result = $db->sql_query_limit($sql, $newsdisplay);
   $posts = array();
   $topic_forum_ids = array();
   while ( $row = $db->sql_fetchrow($result)) {
      $posts[] = $row;
      $topic_forum_ids[] = (int) $row['forum_id'];
   }
   $db->sql_freeresult($result);
   $sql = 'SELECT forum_id, forum_name
            FROM ' . FORUMS_TABLE . '
            WHERE ' . $db->sql_in_set('forum_id', $topic_forum_ids, false);
   $result = $db->sql_query($sql);
   $forums = array();
   while ( $row = $db->sql_fetchrow($result)) {
      $forums[$row['forum_id']] = $row['forum_name'];
   }
   $db->sql_freeresult($result);
   $flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);
   if (!empty($posts)) {
      foreach ($posts as $thistopic) {
         // format the var prior to output
         $topic_id = (int) $thistopic['topic_id'];
         $forum_id= (int) $thistopic['forum_id'];
         // Var needing some formatting
         $thistopic['post_time'] = date($timestamp, thistopic['post_time']);
         // Retrieve the forum name from the previously filled forums array
         $thistopic['forum_name'] = $forums[$forum_id];
// Etc .... using $thistopic where $posts[$i] was used, and without the need for lines such as
// $posts[$i]['topic_title'] = $row[$i]['topic_title'];
// Since $row[$i] is pretty much the same as $thistopic too here (except for forum names)
// the template vars should be parsed in this loop too
      }
   }


This way, you'd only use 2 SQL queries in all cases, not up to one + one per forum (eg up to on + number of topics).
Lines starting with // are comments to help a bit to understand the general principle.

++
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 19930
Joined: Fri Apr 28, 2006 9:03 pm

Postby tbxn » Fri Oct 03, 2008 6:28 pm

Thanks a lot for taking the time to show me that bit of code. I'm having problems implementing it however. When I try to follow your suggestions I just end up with a blank white screen, which I'm assuming is because my syntax or structure is wrong. Do you think you could show me a more precise example using the script I posted above? Thank you for your time.
tbxn
 
Posts: 11
Joined: Sun Aug 03, 2008 1:38 pm


Return to phpBB SEO MODS

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 4 guests


 
cron