Page 1 of 3

[MODDB] Front page news syndication

Posted: Fri Jun 08, 2007 1:13 pm
by chAos
MOD Title: Front page news syndication
MOD Description: Allows you to easily pull threads from a forum to display on another page as news articles. An example would be like at phpBB.com except this also pulls the text of the post (however you can use whatever part of it as you wish). This comes with no support and is intended for use by people who already know PHP but otherwise don't want to have to construct a query and parse posts yourself.
MOD Version: Final

Important Notes:
  • Attachments haven't been handled properly and so I'm not sure what you should expect if you use them.
  • Starts a forum session as Guest user taking all the default values for time, bbcode style (from theme), etc. OR if a session exists from the forum they will be logged in as themselves.
  • While viewing this page, users will appear to be viewing the Forum Index on viewonline.php.
  • There are a few important settings at the top of the file including forum_id, number of posts, path to your phpBB forum, and how to handle the posts - display on screen in a basic format or create an array with info that you can use to format posts yourself.
  • For full details on the basic layout (so you can format using CSS) see near the bottom of the file, and for array keys see the end of the settings section.

Code: Select all

<?php
/******************************************************************************
* POST SYNDICATION SCRIPT by chAos
*
* A very basic script that pulls threads with the first post from the database
* and puts them into an array form so you can use them as you like.
*
* For use with phpBB3, freely distributable
*
******************************************************************************/

/** Notes:
*
* - Attachments haven't been handled properly.
* - Starts a forum session as Guest user, taking all the default values for time, bbcode style (from theme), etc
* - While viewing this page, users will appear to be viewing the Forum Index on viewonline.php.  
*   This can't be helped without modifying other code which is beyond this
*
*/


//////////////////////////////////////
//

define('FORUM_ID', 5);                  // Forum ID to get data from
define('POST_LIMIT', 7);               // How many to get
define('PHPBB_ROOT_PATH', './forums/');      // Path to phpBB (including trailing /)

define('PRINT_TO_SCREEN', true);         

         // If set to true, it will print the posts out
         // If set to false it will create an array $news[] with all the following info
         //
         //   'topic_id'         eg. 119
         //   
         //   'topic_time'      eg. 06 June, 07 (uses board default)
         //   'topic_replies'      eg. 26
         //   
         //   'username'         eg. chAos
         //   'topic_title'      eg. "News Post"
         //   
         //   'post_text'         eg. just the text (formatted w/ smilies, bbcode, etc)

//
//////////////////////////////////////

define('IN_PHPBB', true);
$phpbb_root_path = PHPBB_ROOT_PATH;
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);

// Start session management
$user->session_begin(false);
$auth->acl($user->data);

// Grab user preferences
$user->setup();

$query = 
"SELECT u.user_id, u.username, t.topic_title, t.topic_poster, t.forum_id, t.topic_id, t.topic_time, t.topic_replies, t.topic_first_post_id, p.poster_id, p.topic_id, p.post_id, p.post_text, p.bbcode_bitfield, p.bbcode_uid 
FROM ".USERS_TABLE." u, ".TOPICS_TABLE." t, ".POSTS_TABLE." p 
WHERE u.user_id = t.topic_poster 
AND u.user_id = p.poster_id 
AND t.topic_id = p.topic_id 
AND p.post_id = t.topic_first_post_id 
AND t.forum_id = ".FORUM_ID." 
ORDER BY t.topic_time DESC";

$result = $db->sql_query_limit($query, POST_LIMIT);
$posts = array();
$news = array();
$bbcode_bitfield = '';
$message = '';
$poster_id = 0;

while ($r = $db->sql_fetchrow($result))
{
   $posts[] = array(
         'topic_id' => $r['topic_id'],
         'topic_time' => $r['topic_time'], 
         'username' => $r['username'], 
         'topic_title' => $r['topic_title'], 
         'post_text' => $r['post_text'],
         'bbcode_uid' => $r['bbcode_uid'],
         'bbcode_bitfield' => $r['bbcode_bitfield'],
         'topic_replies' => $r['topic_replies'],
         );
   $bbcode_bitfield = $bbcode_bitfield | base64_decode($r['bbcode_bitfield']);
}


// Instantiate BBCode
if ($bbcode_bitfield !== '')
{
   $bbcode = new bbcode(base64_encode($bbcode_bitfield));
}

// Output the posts
foreach($posts as $m)
{
   $poster_id = $m['user_id'];
   
   $message = $m['post_text'];
   if($m['bbcode_bitfield'])
   {
      $bbcode->bbcode_second_pass($message, $m['bbcode_uid'], $m['bbcode_bitfield']);
   }

   $message = str_replace("\n", '<br />', $message);
   $message = smiley_text($message);

   $comment = ($m['topic_replies']==1) ? 'comment' : 'comments';
   
   if( PRINT_TO_SCREEN )
   {
      /* Output is in the following format
       *
       * <h3><span class="postinfo">date // 5 comments // poster</span>Thread Title</h3>
       * <p>First post test</p>
       * 
       */
      echo "\n\n<h3>{$m['topic_title']}</h3>";
      echo "\n<h4 class=\"postinfo\">".$user->format_date($m['topic_time'])." // <a href=\"".PHPBB_ROOT_PATH."viewtopic.php?f=".FORUM_ID."&t={$m['topic_id']}\">{$m['topic_replies']} {$comment}</a> // {$m['username']}</h4>";
      echo "\n<p>{$message}</p>";
   }
   else
   {
      $news[] = array(
            'topic_id' => $m['topic_id'], // eg: 119
            
            'topic_time' => $user->format_date($m['topic_time']), // eg: 06 June, 07 (uses board default)
            'topic_replies' => $m['topic_replies'], // eg: 26
            
            'username' => $m['username'], // eg: chAos
            'topic_title' => $m['topic_title'], // eg: "News Post"
            
            'post_text' => $message, // just the text         
            );
   }
   
   unset($message,$poster_id);
}

?>
Updates:

2008-12-16: Changed the default way posts are outputted to not require CSS to move the post information below the topic title. Also, lets call this final.
2007-06-16: Applied these changes to avoid an error when no BBcode is used.
2007-06-18: Changed "&" to "&" in default output.

Re: [RC1] Front page news syndication

Posted: Fri Jun 15, 2007 6:07 am
by TigerWolf
Bug Report: If there is no bbcode in any of the news posts - this error is chucked.

Fatal error: Call to a member function on a non-object in /home/.../forum/news.php on line 106

This is line 106:
$bbcode->bbcode_second_pass($message, $m['bbcode_uid'], $m['bbcode_bitfield']);

If its commented out it works. If there is bbcode it works.

Re: [RC1] Front page news syndication

Posted: Sat Jun 16, 2007 6:41 am
by chAos
Ah okay, should be simple to fix, leme take a look.

Replace

Code: Select all

	$bbcode->bbcode_second_pass($message, $m['bbcode_uid'], $m['bbcode_bitfield']);
with:

Code: Select all

	if($m['bbcode_bitfield'])
	{
		$bbcode->bbcode_second_pass($message, $m['bbcode_uid'], $m['bbcode_bitfield']);
	}
Let me know of any other probs. :)

Re: [RC1] Front page news syndication

Posted: Sun Jun 17, 2007 5:06 am
by daza
Would love to see a demo if possible!

Otherwise, this sound just like what I'm looking for. Great work, love the idea.

Re: [RC1] Front page news syndication

Posted: Sun Jun 17, 2007 6:31 am
by TIM3PORT
Tue Jun 12, 2007 12:47 am // 23 comments // TIM3PORTmani_admin will die so
there is a space missing there it's an easy fix.

also maybe you should instead of using f=##&t=## you should just use t=## because when you add & wihtout it's proper code

Code: Select all

 Cause:

This is usually a cascading error caused by a an undefined entity reference or use of an unencoded ampersand (&) in an URL or body text. See the previous message for further details.
Example:
bad      <a href="http://www.domain.com/cgi?x=123&y=456">
Good	<a href="http://www.domain.com/cgi?x=123&y=456">
Solution:

Replace "&" by "&". 
or as I said just skip the forum number and take them directly to the topic number

for those who need a sample

this is [ALPHA] Topic Extraction (news, latest topics etc) - 0.1.5
http://www.pandatales.org/topic.php

and this is [RC1] Front page news syndication
http://www.pandatales.org/topic2.php

both outputs have my style edits to match my home page which is where it is placed.

Re: [RC1] Front page news syndication

Posted: Mon Jun 18, 2007 3:06 am
by chAos
TIM3PORT wrote:
Tue Jun 12, 2007 12:47 am // 23 comments // TIM3PORTmani_admin will die so
there is a space missing there it's an easy fix.
That's just an issue with the style which you can change in the code itself but it's much easier to handle it through css. What I have it do is add float: right to the .postinfo class so that the title is on the left, and the rest is on the right (see below).
TIM3PORT wrote:also maybe you should instead of using f=##&t=## you should just use t=## because when you add & wihtout it's proper code

Code: Select all

 Cause:

This is usually a cascading error caused by a an undefined entity reference or use of an unencoded ampersand (&) in an URL or body text. See the previous message for further details.
Example:
bad      <a href="http://www.domain.com/cgi?x=123&y=456">
Good	<a href="http://www.domain.com/cgi?x=123&y=456">
Solution:

Replace "&" by "&". 
or as I said just skip the forum number and take them directly to the topic number
Yep, you can do that if you want. Just as easy to add &

Ultimately, the best way to do things is really to create the array of posts and then adapt it to the format of your site. I just included the way I outputted it as an example of how I do it on my site, with the combination of css to format it correctly.

Image

Re: [RC1] Front page news syndication

Posted: Wed Jun 20, 2007 2:05 am
by Survival Bill
Hello I tried this out I put it in a file and called it news.php I got a blank screen I am sure this means something what I dont know any help you can give me I would like it to have a page with my forum news...

Re: [RC1] Front page news syndication

Posted: Wed Jun 20, 2007 5:24 am
by TIM3PORT
for the record all of a sudden...

Code: Select all

[phpBB Debug] PHP Notice: in file /includes/session.php on line 814: Cannot modify header information - headers already sent by (output started at /home/carlos/public_html/topic2.php:9)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 814: Cannot modify header information - headers already sent by (output started at /home/carlos/public_html/topic2.php:9)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 814: Cannot modify header information - headers already sent by (output started at /home/carlos/public_html/topic2.php:9)
Tue Jun 12, 2007 12:47 am // 21 comments // TIM3PORTmani_admin will die soon...

Re: [RC1] Front page news syndication

Posted: Wed Jun 20, 2007 5:38 am
by TigerWolf
Make sure there is no spaces in front of the first <? or after the last ?> tags. Had the same problem and this fixed it.

Re: [RC1] Front page news syndication

Posted: Wed Jun 20, 2007 6:02 am
by TIM3PORT
the first day it was added it worked fine LoL

Re: [RC1] Front page news syndication

Posted: Wed Jun 20, 2007 6:14 am
by TigerWolf
If this it not the same problem that your having, please post your topic2.php code - need to know what line 9 is.

Re: [RC1] Front page news syndication

Posted: Wed Jun 20, 2007 2:26 pm
by TIM3PORT
I'm sorry for some reason there was a space on my start file I fucking hate how firefox copies the select all feature I have to open the links on ie to get the code with no spaces....

Re: [RC1] Front page news syndication

Posted: Thu Jun 21, 2007 6:20 pm
by Theberge43
Nice little MOD man, and exactly what I'm looking for.
I have 2 questions :

1. Could it be possible to fetch the user's avatar as well !? It would be really nice :D
2. I'd like to put the result into an array, keep the file somewhere hidden on my site and call the results (variables) inside another page ... how do I do that ?

Edit : 3 questions !

3. I'd like to duplicate the script :
A. Post the last 10 topics on the front page with the message text (like news)
B. Post only the title of the next 10 topics to put in a sidebar on my front page ...

Is there a way to select topics 11 - 20 ? And how can I duplicate the script ?

Re: [RC1] Front page news syndication

Posted: Thu Jun 21, 2007 9:32 pm
by ecwpa
great mod!

if you want to make the topic message shorter, find:

Code: Select all

       $message = str_replace("\n", '<br />', $message);
       $message = smiley_text($message);
and add this after:

Code: Select all

$varlength = strlen($message);

$limit = 600; // characters limit

if ($varlength > $limit) { 
$message = substr($message,0,$limit) . "..."; 
}
you should change $limit to any char limit you want

Re: [RC1] Front page news syndication

Posted: Fri Jun 22, 2007 7:52 am
by chAos
Theberge43 wrote:Nice little MOD man, and exactly what I'm looking for.
I have 2 questions :
Hey,

Glad you like it.
Theberge43 wrote:1. Could it be possible to fetch the user's avatar as well !? It would be really nice :D
I haven't looked as to how the avatar is stored, but I assume it'd be the path to the image in which case it should be very easy to implement just by adding it to the query and then the subsequent array.
Theberge43 wrote:2. I'd like to put the result into an array, keep the file somewhere hidden on my site and call the results (variables) inside another page ... how do I do that ?
Not sure sure I understand your question here. You can set the script to put the data into an array, and then call it from any page to get access to that array with the news.
Theberge43 wrote:Edit : 3 questions !

3. I'd like to duplicate the script :
A. Post the last 10 topics on the front page with the message text (like news)
B. Post only the title of the next 10 topics to put in a sidebar on my front page ...

Is there a way to select topics 11 - 20 ? And how can I duplicate the script ?
This is a bit more editing but wouldn't be difficult at all. The way I would go about it would be to set the limit to 20, then setup a for loop for the first 10 posts and put them in the $news array. Then, setup another loop for the last 10 posts and put them into a (for example) $news2 array which would contain that information.

I haven't got any time to look at it now, but sometime this weekend probably.
ecwpa wrote:great mod!

if you want to make the topic message shorter, find:

Code: Select all

       $message = str_replace("\n", '<br />', $message);
       $message = smiley_text($message);
and add this after:

Code: Select all

$varlength = strlen($message);

$limit = 600; // characters limit

if ($varlength > $limit) { 
$message = substr($message,0,$limit) . "..."; 
}
you should change $limit to any char limit you want
Cheers, nice edit. :D