phpBB

Development Discussion Board

phpBB's testing ground of bleeding edge code
Advanced search

New posting API

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The next feature release of phpBB 3 will be 3.1/Ascreaus followed by 3.2/Arsia.

New posting API

Postby igorw » Sat Jul 10, 2010 12:42 pm

Code: Select all
/**
* Submit Post
* @todo Split up and create lightweight, simple API for this.
*/


Let's face it. submit_post is not only a monster function. It's a pain to use. Same holds true for submit_pm. It would be great to have a posting API that is sane.

Just to prove my point, take a look at this:
Code: Select all
// Many thanks to paul999 for helping me with this!
include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
include_once($phpbb_root_path . 'includes/message_parser.' . $phpEx);

$sql = 'SELECT forum_name
   FROM ' . FORUMS_TABLE . '
   WHERE forum_id = ' . (int) $config['contact_bot_forum'];
$result = $db->sql_query($sql);
$forum_name = $db->sql_fetchfield('forum_name');
$db->sql_freeresult($result);

$post_data = array(
   'topic_title'         => $data['subject'],
   'topic_first_post_id'   => 0,
   'topic_last_post_id'   => 0,
   'topic_time_limit'      => 0,
   'topic_attachment'      => 0,
   'post_id'            => 0,
   'topic_id'            => 0,
   'forum_id'            => (int) $config['contact_bot_forum'],
   'icon_id'            => 0,
   'poster_id'            => 0,
   'enable_sig'         => true,
   'enable_bbcode'         => (bool) $allow_bbcode,
   'enable_smilies'      => (bool) $allow_smilies,
   'enable_urls'         => (bool) $allow_urls,
   'enable_indexing'      => false,
   'message_md5'         => (string) md5($text),
   'post_time'            => time(),
   'post_checksum'         => '',
   'post_edit_reason'      => '',
   'post_edit_user'      => 0,
   'forum_name'         => $forum_name,
   'notify'            => false,
   'notify_set'         => false,
   'poster_ip'            => $user->ip,
   'post_edit_locked'      => 0,
   'bbcode_bitfield'      => $bitfield,
   'bbcode_uid'         => $uid,
   'message'            => $text,
   'attachment_data'      => 0,
   'filename_data'         => 0,

   'topic_approved'      => 1,
   'post_approved'         => 1,
);

$poll = array();

// Submit the post!
submit_post('post', $data['subject'], $user->data['username'], POST_NORMAL, $poll, $post_data);


Required information

So let's see what's actually going in there.

  • post/topic subject
  • post text
  • forum_id
  • topic_id (if it's a reply)
  • bbcode allow options
  • index post?
  • time of post
  • ip of poster
  • post locked?
  • attachments
  • poll

All the other stuff could be computed from within the function. And many of these can also have default values, submit_post relies on all of those to be present. And it gets even worse. submit_post relies heavily on $user (which represents the current user). For example:
Code: Select all
$poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];

Code: Select all
'poster_id'                     => (int) $user->data['user_id'],

Code: Select all
poster_ip'                     => $user->ip

Code: Select all
'post_username'         => (!$user->data['is_registered']) ? $username : '',

And the list goes on. What does this mean? It means that if I want to submit a post as an other user (a bot maybe), I have to replace $user! And up to 3.0.6 you even had to replace $auth and give that user posting permissions, otherwise it created a non-approved post. (I wrote a helper for that).

There were some efforts by DavidMJ to clean this up. You can take a look at that here.

Design

Here's what a new posting API should be like imo:

  • Not depend on globals (dependency injection, pass things like $db into the constructor), this makes testing easier
  • provide defaults for required info
  • separate topic and reply into separate methods
  • not depend on $user, except maybe for defaults
  • simple to use API for setting required options
  • (use exceptions?)

Backward compatibility

submit_post should be converted to a legacy wrapper around the new API.
User avatar
igorw
Registered User
 
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: New posting API

Postby idiotnesia » Sun Jul 11, 2010 6:37 am

Good idea. The problem I found so far is to submit post with other users.
idiotnesia wuz here
idiotnesia
Registered User
 
Posts: 29
Joined: Thu May 22, 2008 2:46 am

Re: New posting API

Postby DavidIQ » Sun Jul 11, 2010 12:19 pm

The entire MOD writing community would love you for this :D
Image
User avatar
DavidIQ
MOD Team Leader
MOD Team Leader
 
Posts: 753
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth

Re: New posting API

Postby halindrome » Sat May 21, 2011 9:00 pm

I agree - it would be great if this API were simplified. And while we are at it... I noticed today that the post_time parameter of the $data structure is ignored. If I choose to set a time of the post, the system should really honor it! The change is pretty simple. Is there a sensible way to propose patches to the existing function?
halindrome
Registered User
 
Posts: 1
Joined: Sat May 21, 2011 8:58 pm

Re: New posting API

Postby Oleg » Sun May 22, 2011 12:49 am

halindrome wrote:Is there a sensible way to propose patches to the existing function?

Yes there is - please see http://wiki.phpbb.com/Get_Involved#Creating_Patches.
Oleg
3.1 Release Manager
3.1 Release Manager
 
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am

Re: New posting API

Postby sajaki » Sun May 22, 2011 6:49 am

it would be great if you could encapsulate the posting function in a neat class. the way it is now i always forget bits and it makes it harder to add posting forms to my custom pages.

edit: oh i didn't realise it was an older post.
sajaki
Registered User
 
Posts: 46
Joined: Mon Jun 21, 2010 8:28 pm


Return to [3.x] Discussion

Who is online

Users browsing this forum: No registered users and 13 guests