PDA

View Full Version : How to create a article via API / auto-create of articles


Adan0s
06-16-2010, 07:09 PM
Hey there,

is there any reference or tutorial on how to create articles with the API? Or even better, is there something like a mod or a function to automatically create articles out of threads which are posted in a specific forum?

greeting

fzone
06-18-2010, 10:06 AM
hi, i would also like to know if there is anyway this can be done,
I want to have an rss feed post straight to an article (primary content)
I would really appreciate it if someone could help!

Adan0s
06-20-2010, 06:57 PM
*bump* any news on this? do you think the vb-team will reply with a solution if I ask them via a support ticket?

Adan0s
06-28-2010, 05:26 PM
I contacted the official support and got this answer, in case someone also wants to know how to do it:

Hello,

Thank you for contacting vBulletin Support. I am sorry but we do not support creating articles via API at this time. Such an API is scheduled for inclusion in a future version though we don't have a specific version number or date available at this time. The API is currently being written to specification and we'll make an announcement when we have more information.

leitel
11-12-2010, 11:26 AM
Has anyone come up with an interim hack/mod/solution? Although the API will be very useful, I need something to import a batch of articles NOW. I'll accept any scraps / code snippets. Thank you :)

Adan0s
11-12-2010, 06:26 PM
Well I got a direct response from a vB developer with some information for creating articles via own code. Also i got a code example by an user of vbulletin-germany.org.

Code Example (provided by Andreas http://www.vbulletin-germany.org/showpost.php?p=67396&postcount=4, modified by me):


<?php
define('VB_AREA', 'API');
require('./includes/init.php');
require_once(DIR . '/includes/class_bootstrap_framework.php');
vB_Bootstrap_Framework::init();

function create_article($sectionid, $title, $pagetext, $userid, $categoryid = '', $datestamp = '', $description = '', $htmltitle = '')
{
global $db;

if (empty($htmltitle)) $htmltitle = $title;
if (empty($categoryid)) $categoryid = 41;

$nodedm = new vBCMS_DM_Article();
$nodedm->info['skip_verify_pagetext'] = true;
$nodedm->set('contenttypeid', vB_Types::instance()->getContentTypeID('vBCms_Article'));
$nodedm->set('parentnode', $sectionid);
$nodedm->set('publicpreview', 1);
$nodedm->set('comments_enabled', 1);
$nodedm->set('pagetext', $pagetext);
$nodedm->set('title', $title);
$nodedm->set('html_title', $htmltitle);
$nodedm->set('description', $description);
$nodedm->set('userid', $userid);
$nodedm->set('url', vB_Friendly_Url::clean_entities($title));
$nodeid = $nodedm->save();
if (empty($nodeid))
{
return false;
}
else
{
$db->query_write("INSERT INTO ". TABLE_PREFIX . "cms_nodecategory (nodeid, categoryid) values (" . $nodeid . ", $categoryid) ");
$db->query_write("UPDATE " . TABLE_PREFIX . "cms_node SET new = 0 WHERE nodeid = $nodeid");
if(empty($datestamp) OR $datestamp < 946681200) // if datestamp is empty or before 01.01.2000
{
$datestamp = TIMENOW - vBCms_ContentManager::getTimeOffset(vB::$vbulletin->userinfo, false); // use current datestamp
}
$db->query_write("UPDATE " . TABLE_PREFIX . "cms_node set setpublish = 1, publishdate = $datestamp WHERE nodeid = $nodeid");
}

return $nodeid;
}

echo create_article(113, 'Headline', 'article text', 1);
?>

And the additional information by Ed Brown:


We don't have an API. You'll have to work directly with the database insert. You'll need to put data in the following tables:

1) cms_article- the most important fields are userid (the author), pagetext, setpublish (whether the page is published) and publishdate (unix timestamp when is was/will be published. If there are images, they are referenced as <attachmentid>. The content needs to be rendered in BBCode if it is anything but plain text.
2) cms_node. This is a modified preorder traversal table. You can ignore the nodeleft and noderight values and when you are done run a "verify and repair node table" in the admincp, which will save a lot of trouble. The most important fields are nodeid, parentnode (the nodeid of the section into which the article will go), contenttypeid, contentid (from the article table), and url
3) cms_nodeinfo. There is one record here for every record in cms_node, with the same nodeid. The most important field here is the title.
4) (if you have attachments) filedata . There must be a record here for every image you need. One image file can be referenced by multiple articles.
5) (if you have attachments) attachment. This relates the image data to the articles, and is where the permission comes from.


I built my whole e107 importer around those two bits. If you need more help just ask.

leitel
11-12-2010, 06:40 PM
Thank you. It works!!

I noticed however that the new node/article is not found when searching content contained in a newly added article.

Is there a method for forcing an update of the index?

Your post made my day!!!!

--------------- Added 1289599712 at 1289599712 ---------------

I tried this to index the new article:

vB_Search_Indexcontroller_QueueProcessor::indexNow ('vBCms', 'vBCms_Article','index', array_slice(func_get_args(), 3));

Still no joy.

The last parameter is looking for an Associative Array of values. I suppose I can manually populate an array.

--------------- Added 1289600978 at 1289600978 ---------------

Here is the revised code to set publish ON and to index the article immediately. If you adding a batch, it would be better to find the way to index them as a batch.



<?php
define('VB_AREA', 'API');
define('VB_ENTRY', 1);

require('./includes/init.php');
require_once(DIR . '/includes/class_bootstrap_framework.php');
require_once (DIR . "/vb/search/core.php");
require_once (DIR . '/vb/search/indexcontroller/queueprocessor.php');

vB_Bootstrap_Framework::init();

function create_article($sectionid, $title, $pagetext, $userid, $description = '', $htmltitle = '')
{
global $db;

if (!$htmltitle)
{
$htmltitle = $title;
}

$nodedm = new vBCMS_DM_Article();
$nodedm->info['skip_verify_pagetext'] = true;
$nodedm->set('contenttypeid', vB_Types::instance()->getContentTypeID('vBCms_Article'));
$nodedm->set('parentnode', $sectionid);
$nodedm->set('publicpreview', 1);
$nodedm->set('comments_enabled', 1);
$nodedm->set('pagetext', $pagetext);
$nodedm->set('title', $title);
$nodedm->set('html_title', $htmltitle);
$nodedm->set('description', $description);
$nodedm->set('userid', $userid);
$nodedm->set('publishdate', TIMENOW);
$nodedm->set('url', vB_Friendly_Url::clean_entities($title));
if (!$nodeid = $nodedm->save())
{
return false;
}
else
{
$db->query_write("UPDATE " . TABLE_PREFIX . "cms_node SET new = 0, setpublish = 1 WHERE nodeid = $nodeid");
}
$data['nodeid'] = $nodeid;
$data['pagetext'] = $pagetext;
vB_Search_Indexcontroller_QueueProcessor::indexNow ('vbcms', 'article','index', $data);
return $nodeid;
}
create_article(1, 'Now is the time for all good martians...', 'Dies ist ein Test', 1, 'foo');
:

Please feel free to share any other code snippets such as adding tags, etc. to an article.

Thanks again for the useful suggestions.

Adan0s
11-13-2010, 07:31 AM
2) cms_node. This is a modified preorder traversal table. You can ignore the nodeleft and noderight values and when you are done run a "verify and repair node table" in the admincp, which will save a lot of trouble. The most important fields are nodeid, parentnode (the nodeid of the section into which the article will go), contenttypeid, contentid (from the article table), and url

this should normaly update the index.

to add tags, just add the following line into the method:


$nodedm->set('keywords', $keywords);

and append an additional variable to the top

function create_article($sectionid, $title, $pagetext, $userid, $description = '', $htmltitle = '', $keywords = '')

leitel
11-13-2010, 10:59 AM
I did a test to see if item is added to the index after it is added. I removed my code:
//$data['nodeid'] = $nodeid;
//$data['pagetext'] = $pagetext;
//vB_Search_Indexcontroller_QueueProcessor::indexNow ('vbcms', 'article','index', $data);
I added an article and it was not found using search.

I added the param for keywords and that works great.

However, I don't see a method for associating tags vs keywords. I am referring to the tags that form the basis for tag cloud.

--------------- Added 1289654370 at 1289654370 ---------------

I was mistaken. My code to immediately update index with new article is wrong. It isn't working. I also tried Verify and Repair Node Table.

Adan0s
11-13-2010, 11:50 AM
Sorry, thought the keywords were interpreted as tags.

To add tags to the article look in the following class:

includes/class_taggablecontent.php

The following function is propably the thing you are searching for. Unfortunately I also don't know how exactly to use it. You propably need to create an entity of vB_Taggable_Content_Item, add the appropriate information (public static function create($registry, $contenttypeid, $contentid, $contentinfo = null)) and then use the following method:

public function add_tags_to_content($taglist, $limits)

leitel
11-13-2010, 12:30 PM
Gotta run now but this is what I have to start the ball rolling:

$taglist = 'tag1, tag2, tag3';
if ($taglist)
{
require_once DIR . '/includes/class_taggablecontent.php';
$taggable = vB_Taggable_Content_Item::create(vB::$vbulletin,
vB_Types::instance()->getContentTypeID("vBCms_Article"),
$nodedm->getField('contentid'));
$taggable->add_tags_to_content($taglist, array('content_limit' => 25));
}

Later I will hammer on it further.

leitel
11-21-2010, 06:08 PM
Just an update. I am making great progress on this project. I'm finding it increasingly productive to add the articles directly to the database. I would prefer a clean API however with great care, pushing new articles DIRECTLY to vB is sweat!
I wonder if other sites would have interest in bulk importing of articles in to vBCMS?

Adan0s
11-21-2010, 06:32 PM
AFAIK there isn't even an API planned. vB4 is just a joke. :/ The only API they are currently implementing is the Mobile-API. Which they demand money for.

Are there additional features you've added in? I'm searching for methods/functions to add attachments from the filesystem right into the article and replace the old links. At least that's some kind of wish. :)

leitel
11-22-2010, 01:18 AM
Adan0s, feel free to Skype me at larryeitel. I would be happy to discuss. Sorry I don't use ICQ, etc.