News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Batch posting?

Started by BigMike, September 30, 2014, 12:11:12 PM

Previous topic - Next topic

BigMike

Hello SMF,

I've searched a bit and can't find what I need so here goes- This is for SMF 2.0.8

I share trip reports on our forum that consist of upwards to 200 pictures at a time. What I do is I prepare all my pictures locally, then upload them to our server manually via FTP, then I have a PHP script that spits out all the BBC code to display my pictures broken up into chunks of 5 pictures/groups. I then copy each chunk of BBC code to my clipboard, start a new thread on our forum with the first chunk of 5 images, then reply to the thread with the next chunk copied/pasted from my clipboard and repeat over and over again until I've created enough replies for all my pictures.

I'm doing 5 pics at a time (per post) so that each page doesn't contain a bazillion images, which is nice to someone on a slow internet connection, but of course the fewer pics I configure my PHP script to bunch together per post, the more posts I'll end up having to make. This process as you can imagine is very time consuming and cumbersome.

My Question: Is there a method to batch post to a thread? Is there a mod or script where I can paste a large amount of text into a textarea using markers as post separators, and then click one button to have 40-something replies suddenly appear to an existing thread?

One thing I've tried is I created a thread and then wrote a PHP script that queried my SMF database to insert new messages (replies, linked to the already existing thread), but the replies never showed up and of course all the board totals never changed. I wonder if this method may work if I then use the 'Recalculate all forum totals' option in the admin section? Would the manually inserted DB messages magically appear in their parent thread? I have not tested this but this is certainly an option as doing a DB query is a piece-o-cake.

Please let me know if anyone knows of a mod for this or some other method I've not thought of, and as always, thank you very much for the help!

Regards,
BigMike

Arantor

Sure, the script that gets you your bbc could load SSI.php then load Subs-Post.php and call createPost() for every post in question. You could just feed it the details as to which board/poster/topic etc. and it will create the posts - and also do the updating of stats for you (and also handle notifications if you want that)

It's always better to use SMF's own code to do this than to try to do it manually since SMF's code knows about all the related things that need doing ;)

BigMike

OH man OH man OH man OH man OH man OH man

* BigMike goes to get this working right meow

SSI never came to mind. THANK YOU SIR!



Quote from: Arantor on September 30, 2014, 12:21:16 PM
Sure, the script that gets you your bbc could load SSI.php then load Subs-Post.php and call createPost() for every post in question. You could just feed it the details as to which board/poster/topic etc. and it will create the posts - and also do the updating of stats for you (and also handle notifications if you want that)

It's always better to use SMF's own code to do this than to try to do it manually since SMF's code knows about all the related things that need doing ;)

vbgamer45

#3
I wrote one long ago using tab separators i believe SMF 1.1.x
Data 1 = Poster
Data 2 = Subject
Data 3 =
Body 4 = Member Id

<?php
/*
Import Script for SMF 
by:vbgamer45
http://www.smfhacks.com


  Instructions:
  Copy this file to the root directory of the forum then run it.
  You need to login as admin account first!
*/

// Fixes conflict ad rev sharing mod
//define('SMF',1);
if (!file_exists($boarddir) && file_exists(dirname(__FILE__) . '/agreement.txt'))
$boarddir dirname(__FILE__);
if (!
file_exists($sourcedir) && file_exists($boarddir '/Sources'))
$sourcedir $boarddir '/Sources';


require_once 
'SSI.php';

// Check that they are logged in as admin
isAllowedTo('admin_forum');

// Store the resulting message;
$message '';

// Process the file
if (isset($_REQUEST['action']))
{

$boardid = (int) $_REQUEST['boardid'];

$filepath $_REQUEST['filepath'];

if ($boardid == 0)
{
$message 'Please select a board!';
}
else 
{
//define('SMF',1);
if (!empty($filepath))
ImportFileTab($filepath,$boardid);
else 
ImportFileTab($_FILES['fileupload']['tmp_name'],$boardid);
}

}


$context['page_title'] = 'Import Topics Script';

$request db_query("
SELECT b.ID_BOARD, b.name AS bName, c.name AS cName
FROM 
{$db_prefix}boards AS b, {$db_prefix}categories AS c
WHERE b.ID_CAT = c.ID_CAT
ORDER BY c.catOrder, b.boardOrder"
__FILE____LINE__);
while ($row mysql_fetch_assoc($request))
$importBoards[$row['ID_BOARD']] = $row['cName'] . ' - ' $row['bName'];
mysql_free_result($request);

template_header();



echo 
'<div class="tborder">
<h1 align="center">Import Topics Script</h1>
<form  method="post" enctype="multipart/form-data" action="?action=import">

<div align="center"><b>'
$message .'</b></div>
<table align="center">
<tr>
<td>Board:</td>
<td>'
;


echo '<select name="boardid">
<option value="">Select Board</option>
'
;

foreach ($importBoards as $key => $option)
if (is_array($option))
echo '<option value="' $option[0] . '"' . ($option[0] == $board_news ' selected="selected"' '') . '>' $option[1] . '</option>';
else
echo  '<option value="' $key '"' . ($key == $modSettings['smfportal_board_news'] ? ' selected="selected"' '') . '>' $option '</option>';

echo  '</select>';


echo '</td>
</tr>
<tr>
<td>File Upload:</td>
<td><input type="file" size="48" name="fileupload" /></td>
</tr>
<tr>
<td valign="top">File Path:</td>
<td><input type="text" name="filepath" /><br />If the file is located on the webserver</td>
</tr>
<tr>
<td colspan="2" align="center">

<input type="submit" value="Import Topics" />
</td>
</tr>
</table>
</form>
</div>'
;

template_footer();



function 
ImportFileTab($filename$boardid)
{
global $message$sourcedir$func$modSettings;

require_once($sourcedir '/Subs-Post.php');

$modSettings['disableQueryCheck'] = 1;

$row 1;
$handle fopen($filename"r");
while ((
$data fgetcsv($handle1000" ")) !== FALSE) {
//while (($data = fgetcsv_ex($handle, " ","","")) !== FALSE) {
    //$num = count($data);
    //echo "<p> $num fields in line $row: <br /></p>\n";
    //$row++;
    /*(for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
    */
    
// Prepare the message a bit for some additional testing.
$data[2]= $func['htmlspecialchars']($data[2], ENT_QUOTES);


preparsecode($data[2]);


$data[1] = strtr($func['htmlspecialchars']($data[1], ENT_QUOTES), array("\r" => ''"\n" => ''"\t" => ''));
$data[0] = htmlspecialchars($data[0], ENT_QUOTES);


// At this point, we want to make sure the subject isn't too long.
/*
if ($func['strlen']($data[1]) > 100)
$data[1] = addslashes($func['substr'](stripslashes($data[1]), 0, 100));
*/

    
    // Data 1 = Poster Data 2 = Subject Data 3 = Body 4 = Member Id
 
$msgOptions = array(
'id' => 0,
'subject' => $data[1],
'body' =>$data[2],
'icon' => 'xx',
'smileys_enabled' => 1,
'attachments' => array(),
);
$topicOptions = array(
'id' => 0,
'board' => $boardid,
'poll' => null,
'lock_mode' => 0,
'sticky_mode' => null,
'mark_as_read' => true,
);
$posterOptions = array(
'id' => (int) $data[3],
'name' => $data[0],
'update_post_count' => true,
);

//preparsecode($msgOptions['body']);


$newtopicid = createPost($msgOptions$topicOptions$posterOptions);
    
    
}
fclose($handle);

$message 'Topics Imported!';

}

function 
ImportFileCSV($filename,$boardid)
{
global $message;
}

define('BUFFER_READ_LEN'4096);
function 
fgetcsv_ex($file_handle$delim ','$enclosure '"'$escape '"') {
    
$fields null;
    
$fldCount 0;
    
$inQuotes false;
   
    
$complete false;
    
$search_chars_list = array('\r\n''\n''\r');
    if ( 
$delim && ($delim != '') )
      
$search_chars_list[] = $delim;
    if ( 
$enclosure && ($enclosure != '') )
    {
      
$search_chars_list[] = $enclosure;
      
$enclosure_len strlen($enclosure);
    }
    else
      
$enclosure_len 0;

    if ( 
$escape && ($escape != '') )
    {
      
$search_chars_list[] = $escape;
      
$escape_len strlen($escape);
    }
    else
      
$escape_len 0;
    
$search_regex '/' implode('|'$search_chars_list) . '/';

    
$cur_pos 0;
    
$line '';
    
$cur_value '';
    
$in_value false;
    
$last_value 0;
    while ( ! 
$complete )
    {
      
$read_result fread($file_handleBUFFER_READ_LEN);
      if ( 
$read_result )
        
$line .= $read_result;
      else if ( 
strlen($line) == )
        return 
null;
      else
        
$line .= "\n";

      
$line_len strlen($line);
     
      while ( 
true )
      {
        if (! 
preg_match($search_regex$line$matchesPREG_OFFSET_CAPTURE$cur_pos))
        {
          if (
$read_result//need more chars
            
break;
          else
            return 
null//Incomplete file
        
}
        else
        {
          
$non_escape false;
          
$cur_char $matches[0][0];
          
$cur_len strlen($cur_char);
          
$new_pos $matches[0][1];
          if ((
$enclosure == $escape) && $in_value && ($cur_char == $escape))  //Escape char = enclosure char special handling
          
{
            if ((
$new_pos $cur_len $enclosure_len) >= $line_len)  //We need the next char
              
break;
           
            
$next_char substr($line$new_pos $cur_len$enclosure_len);
            if ( (! 
$enclosure) || ($next_char != $enclosure) )
              
$non_escape true;
          }
           
          
$cur_pos $new_pos;
          if ( 
$in_value && (! $non_escape) )
          {
            
$cur_value .= mb_substr($line$last_value$cur_pos $last_value);
            if (
$cur_char == $escape)  //Skip escape char
              
$cur_pos += $escape_len;
            
$last_value $cur_pos;
          }
          else if ( ( 
$cur_char == "\n" ) || ( $cur_char == "\r" ) || ( $cur_char == "\r\n" ) )
          {
              
$blank_start_lines = ( $cur_pos == );
              ++
$cur_pos;
              
$cur_pos $cur_pos strspn($line"\n\r"$cur_pos);
              if ( ! 
$blank_start_lines )
              {
                
$complete true;
                break;
              }
              else
              {
                
$last_value $cur_pos;
                continue;
              }
          }
          else if ( 
$cur_char == $delim )
          {
            if ( 
is_null($fields) )
              
$fields = array();
            
$fields[] = $cur_value trim(mb_substr($line$last_value$cur_pos $last_value));
            
$last_value $cur_pos $cur_len;
            
$cur_value '';
          }
          else if ( 
$cur_char == $enclosure )
          {
            if ( 
$in_value )
              
$cur_value .= mb_substr($line$last_value$cur_pos $last_value);
            
$last_value $cur_pos $cur_len;
            
$in_value = ! $in_value;
          }
          
$cur_pos += $cur_len;
        }
      }
  }
 
  
fseek($file_handle$cur_pos strlen($line), SEEK_CUR);
  return 
$fields;
}
?>

Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

BigMike

#4
Quote from: BigMike on September 30, 2014, 02:24:30 PM
* BigMike goes to get this working right meow
Just got it working and it's PERFECT. Thanks again Aranton!

Here is a very basic and simple example of how to reply to an existing thread using only a single PHP file:

<?php
  
require("SSI.php");
  require(
"Sources/Subs-Post.php");
  
// Bounce to log-in page if not logged in as Admin
  
isAllowedTo('admin_forum');

  echo 
'<html><body>';

  
// Set some defaults -- modify these as desired
  
$topicOptions['id'] = '123'// Topic ID
  
$topicOptions['board'] = '123'// Board ID
  
$posterOptions['id'] = '123';  // Poster ID
  
$msgOptions['subject'] = 'This is a test'// Subject of post
  
$msgOptions['body'] = 'Hello from php file!'// The content of the post

  // Don't touch these
  
$posterOptions['name'] = $user_info['name'];
  
$posterOptions['email'] = $user_info['email'];
  
$posterOptions['ip'] = $user_info['ip'];

  
// Create the reply!
  
createPost($msgOptions$topicOptions$posterOptions);

  echo 
'<a href="' $boardurl '/index.php?topic=' $topicOptions['id'] . '">Click here to go view the new reply!</a>';

  echo 
'</body></html>';
?>



Log into your forum as admin, save this as a new blank .PHP file, modify the topic ID, board ID, & poster ID variables to point to an existing thread in some board and with your own user ID (or anyone else's ID ;)), upload it to your forum's root directory, then load the file in your browser.

vbgamer45: Your script is many times more powerful and practical than what I did so I am going to look through it and see what I can use, thank you very much as well!

Regards,
BigMike

BigMike

Update, it's working great! Just did my first live test by posting 201 images across 50 replies with one mouse click!! http://board.marlincrawler.com/index.php?topic=97633 (this example is a mirror from our Facebook Album (link) using JSON. One mouse click Facebook Album to Forum Integration FTW!).

SMF never ceases to amaze!

Regards,
BigMike

Advertisement: