New posting API

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The current feature release of phpBB 3 is 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.3.x. If you need support for phpBB 3.3.x please visit the 3.3.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Post Reply
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

New posting API

Post by igorw »

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.

idiotnesia
Registered User
Posts: 29
Joined: Thu May 22, 2008 2:46 am

Re: New posting API

Post by idiotnesia »

Good idea. The problem I found so far is to submit post with other users.
idiotnesia wuz here

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1904
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: New posting API

Post by DavidIQ »

The entire MOD writing community would love you for this :D
Image

halindrome
Registered User
Posts: 1
Joined: Sat May 21, 2011 8:58 pm

Re: New posting API

Post by halindrome »

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?

Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: New posting API

Post by Oleg »

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.

sajaki
Registered User
Posts: 86
Joined: Mon Jun 21, 2010 8:28 pm

Re: New posting API

Post by sajaki »

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.

Post Reply