rocksolid-light/Rocksolid_Light/rslight/scripts/import-db3.php

153 lines
5.1 KiB
PHP
Raw Normal View History

<?php
/* This script allows importing a group .db3 file from a backup
* or another rslight site.
*
* Place the article database file group.name-articles.db3 in
* your spool directory, and change user/group to your web user.
* Run this script as your web user from your $webdir/spoolnews dir:
* php $config_dir/scripts/import.php group.name
*
* To import/upgrade all group.db3 files, do not list group.name
* after the above command.
*
* This will create the overview files necessary to import the group
* into your site.
* Next: Add the group to the groups.txt file of the section you wish
* it to appear:
* $config_dir/<section>/groups.txt
*/
include "config.inc.php";
include ("$file_newsportal");
$logfile=$logdir.'/import.log';
# END MAIN CONFIGURATION
$workpath=$spooldir."/";
$path=$workpath."articles/";
$lockfile = sys_get_temp_dir() . '/'.$config_name.'-spoolnews.lock';
$pid = file_get_contents($lockfile);
if (posix_getsid($pid) === false || !is_file($lockfile)) {
print "Starting Import...\n";
file_put_contents($lockfile, getmypid()); // create lockfile
} else {
print "Import currently running\n";
exit;
}
$group = trim($argv[1]);
2021-03-11 07:55:01 +01:00
if($group == '') {
$group_files = scandir($workpath);
foreach($group_files as $this_file) {
if(strpos($this_file, '-articles.db3') === false) {
continue;
}
$group = preg_replace('/-articles.db3/', '', $this_file);
echo 'Importing: '.$group."\n";
import_articles($group);
}
} else {
import_articles($group);
}
2021-02-21 00:55:48 +01:00
echo "\nImport Done\r\n";
function import_articles($group) {
global $spooldir, $CONFIG, $workpath, $path, $config_name, $logfile;
$overview_file = $workpath.'/'.$group."-overview";
# Prepare databases
// Overview db
2021-03-11 07:55:01 +01:00
$new_article_dbh = article_db_open($spooldir.'/'.$group.'-articles.db3-new');
$new_article_sql = 'INSERT INTO articles(newsgroup, number, msgid, date, name, subject, article, search_snippet) VALUES(?,?,?,?,?,?,?,?)';
$new_article_stmt = $new_article_dbh->prepare($new_article_sql);
$database = $spooldir.'/articles-overview.db3';
$table = 'overview';
$dbh = rslight_db_open($database, $table);
2021-02-21 00:55:48 +01:00
$clear_stmt = $dbh->prepare("DELETE FROM overview WHERE newsgroup=:group");
$clear_stmt->bindParam(':group', $group);
$clear_stmt->execute();
unlink($overview_file);
$sql = 'INSERT INTO '.$table.'(newsgroup, number, msgid, date, name, subject) VALUES(?,?,?,?,?,?)';
$stmt = $dbh->prepare($sql);
// Incoming db
$article_dbh = article_db_open($spooldir.'/'.$group.'-articles.db3');
2021-02-21 00:55:48 +01:00
$article_stmt = $article_dbh->query('SELECT DISTINCT * FROM articles');
while ($row = $article_stmt->fetch()) {
$local = $row['number'];
$this_article = preg_split("/\r\n|\n|\r/", $row['article']);
$lines=0;
$bytes=0;
$ref=0;
$banned=0;
$is_header=1;
2021-03-11 07:55:01 +01:00
$body="";
foreach($this_article as $response)
{
$bytes = $bytes + mb_strlen($response, '8bit');
if(trim($response) == "" || $lines > 0) {
$is_header=0;
$lines++;
}
if($is_header == 1) {
$response = str_replace("\t", " ", $response);
// Find article date
if(stripos($response, "Date: ") === 0) {
$finddate=explode(': ', $response, 2);
$ref=0;
}
// Get overview data
$mid[1] = $row['msgid'];
$from[1] = $row['name'];
$subject[1] = $row['subject'];
2021-02-21 00:55:48 +01:00
$article_date = $row['date'];
if(stripos($response, "Xref: ") === 0) {
if(isset($CONFIG['enable_nntp']) && $CONFIG['enable_nntp'] == true) {
$response="Xref: ".$CONFIG['pathhost']." ".$group.":".$local;
}
$xref=$response;
$ref=0;
}
2021-03-11 07:55:01 +01:00
if(stripos($response, "Content-Type: ") === 0) {
preg_match('/.*charset=.*/', $response, $te);
$content_type = explode("Content-Type: text/plain; charset=", $te[0]);
}
if(stripos($response, "References: ") === 0) {
$this_references=explode('References: ', $response);
$references = $this_references[1];
$ref=1;
}
if((stripos($response, ':') === false) && (strpos($response, '>'))) {
if($ref == 1) {
$references=$references.$response;
}
}
$response=str_replace("\n","",str_replace("\r","",$response));
2021-03-11 07:55:01 +01:00
} else {
$body.=$response."\n";
}
}
$lines=$lines-1;
$bytes = $bytes + ($lines * 2);
// add to database
2021-03-11 07:55:01 +01:00
// CREATE SEARCH SNIPPET
$this_snippet = get_search_snippet($body, $content_type[1]);
$new_article_stmt->execute([$group, $local, $mid[1], $article_date, $from[1], $subject[1], $row['article'], $this_snippet]);
2021-02-21 00:55:48 +01:00
$stmt->execute([$group, $local, $mid[1], $article_date, $from[1], $subject[1]]);
file_put_contents($overview_file, $local."\t".$subject[1]."\t".$from[1]."\t".$finddate[1]."\t".$mid[1]."\t".$references."\t".$bytes."\t".$lines."\t".$xref."\n", FILE_APPEND);
echo "\nImported: ".$group." ".$local;
file_put_contents($logfile, "\n".format_log_date()." ".$config_name." Imported: ".$group.":".$local, FILE_APPEND);
$i++;
$references="";
}
2021-03-11 07:55:01 +01:00
$new_article_dbh = null;
$article_dbh = null;
$dbh = null;
unlink($spooldir.'/'.$group.'-articles.db3');
2021-03-11 07:55:01 +01:00
rename($spooldir.'/'.$group.'-articles.db3-new', $spooldir.'/'.$group.'-articles.db3');
}
?>