Add some documentation for import functions.

This commit is contained in:
Klaus Weidenbach 2017-03-24 23:32:53 +01:00
parent 3b6a0d8e5c
commit 2e2f984c45
5 changed files with 464 additions and 322 deletions

View File

@ -83,6 +83,7 @@ class Import extends \Zotlabs\Web\Controller {
$api_path .= 'channel/export/basic?f=&channel=' . $channelname; $api_path .= 'channel/export/basic?f=&channel=' . $channelname;
if($import_posts) if($import_posts)
$api_path .= '&posts=1'; $api_path .= '&posts=1';
$binary = false; $binary = false;
$redirects = 0; $redirects = 0;
$opts = array('http_auth' => $email . ':' . $password); $opts = array('http_auth' => $email . ':' . $password);
@ -104,8 +105,8 @@ class Import extends \Zotlabs\Web\Controller {
$data = json_decode($data,true); $data = json_decode($data,true);
// logger('import: data: ' . print_r($data,true)); //logger('import: data: ' . print_r($data,true));
// print_r($data); //print_r($data);
if(! array_key_exists('compatibility',$data)) { if(! array_key_exists('compatibility',$data)) {
call_hooks('import_foreign_channel_data',$data); call_hooks('import_foreign_channel_data',$data);
@ -248,7 +249,7 @@ class Import extends \Zotlabs\Web\Controller {
logger('import step 6'); logger('import step 6');
// import xchans
$xchans = $data['xchan']; $xchans = $data['xchan'];
if($xchans) { if($xchans) {
foreach($xchans as $xchan) { foreach($xchans as $xchan) {
@ -292,13 +293,11 @@ class Import extends \Zotlabs\Web\Controller {
dbesc($photodate), dbesc($photodate),
dbesc($xchan['xchan_hash']) dbesc($xchan['xchan_hash'])
); );
} }
logger('import step 7'); logger('import step 7');
} }
$friends = 0; $friends = 0;
$feeds = 0; $feeds = 0;
@ -367,19 +366,20 @@ class Import extends \Zotlabs\Web\Controller {
logger('import step 8'); logger('import step 8');
} }
// import groups
$groups = $data['group']; $groups = $data['group'];
if($groups) { if($groups) {
$saved = array(); $saved = array();
foreach($groups as $group) { foreach($groups as $group) {
$saved[$group['hash']] = array('old' => $group['id']); $saved[$group['hash']] = array('old' => $group['id']);
if(array_key_exists('name',$group)) { if(array_key_exists('name', $group)) {
$group['gname'] = $group['name']; $group['gname'] = $group['name'];
unset($group['name']); unset($group['name']);
} }
unset($group['id']); unset($group['id']);
$group['uid'] = $channel['channel_id']; $group['uid'] = $channel['channel_id'];
create_table_from_array('groups',$group); create_table_from_array('groups', $group);
} }
$r = q("select * from groups where uid = %d", $r = q("select * from groups where uid = %d",
intval($channel['channel_id']) intval($channel['channel_id'])
@ -391,7 +391,7 @@ class Import extends \Zotlabs\Web\Controller {
} }
} }
// import group members
$group_members = $data['group_member']; $group_members = $data['group_member'];
if($group_members) { if($group_members) {
foreach($group_members as $group_member) { foreach($group_members as $group_member) {
@ -401,7 +401,7 @@ class Import extends \Zotlabs\Web\Controller {
if($x['old'] == $group_member['gid']) if($x['old'] == $group_member['gid'])
$group_member['gid'] = $x['new']; $group_member['gid'] = $x['new'];
} }
create_table_from_array('group_member',$group_member); create_table_from_array('group_member', $group_member);
} }
} }
@ -450,7 +450,6 @@ class Import extends \Zotlabs\Web\Controller {
notifications_on($channel['channel_id'],$saved_notification_flags); notifications_on($channel['channel_id'],$saved_notification_flags);
if(array_key_exists('item_id',$data) && $data['item_id']) if(array_key_exists('item_id',$data) && $data['item_id'])
import_item_ids($channel,$data['item_id']); import_item_ids($channel,$data['item_id']);
@ -490,7 +489,7 @@ class Import extends \Zotlabs\Web\Controller {
function get() { function get() {
if(! get_account_id()) { if(! get_account_id()) {
notice( t('You must be logged in to use this feature.')); notice( t('You must be logged in to use this feature.') . EOL);
return ''; return '';
} }

View File

@ -3,54 +3,58 @@ namespace Zotlabs\Module;
require_once('include/import.php'); require_once('include/import.php');
/**
* @brief Module for importing items.
*
* Import existing posts and content from an export file.
*/
class Import_items extends \Zotlabs\Web\Controller { class Import_items extends \Zotlabs\Web\Controller {
function post() { function post() {
if(! local_channel()) if(! local_channel())
return; return;
$data = null; $data = null;
$src = $_FILES['filename']['tmp_name']; $src = $_FILES['filename']['tmp_name'];
$filename = basename($_FILES['filename']['name']); $filename = basename($_FILES['filename']['name']);
$filesize = intval($_FILES['filename']['size']); $filesize = intval($_FILES['filename']['size']);
$filetype = $_FILES['filename']['type']; $filetype = $_FILES['filename']['type'];
if($src) { if($src) {
// This is OS specific and could also fail if your tmpdir isn't very large // This is OS specific and could also fail if your tmpdir isn't very large
// mostly used for Diaspora which exports gzipped files. // mostly used for Diaspora which exports gzipped files.
if(strpos($filename,'.gz')){ if(strpos($filename,'.gz')){
@rename($src,$src . '.gz'); @rename($src,$src . '.gz');
@system('gunzip ' . escapeshellarg($src . '.gz')); @system('gunzip ' . escapeshellarg($src . '.gz'));
} }
if($filesize) { if($filesize) {
$data = @file_get_contents($src); $data = @file_get_contents($src);
} }
unlink($src); unlink($src);
} }
if(! $src) { if(! $src) {
$old_address = ((x($_REQUEST,'old_address')) ? $_REQUEST['old_address'] : ''); $old_address = ((x($_REQUEST,'old_address')) ? $_REQUEST['old_address'] : '');
if(! $old_address) { if(! $old_address) {
logger('mod_import: nothing to import.'); logger('Nothing to import.');
notice( t('Nothing to import.') . EOL); notice( t('Nothing to import.') . EOL);
return; return;
} }
$email = ((x($_REQUEST,'email')) ? $_REQUEST['email'] : ''); $email = ((x($_REQUEST,'email')) ? $_REQUEST['email'] : '');
$password = ((x($_REQUEST,'password')) ? $_REQUEST['password'] : ''); $password = ((x($_REQUEST,'password')) ? $_REQUEST['password'] : '');
$year = ((x($_REQUEST,'year')) ? $_REQUEST['year'] : ''); $year = ((x($_REQUEST,'year')) ? $_REQUEST['year'] : '');
$channelname = substr($old_address,0,strpos($old_address,'@')); $channelname = substr($old_address,0,strpos($old_address,'@'));
$servername = substr($old_address,strpos($old_address,'@')+1); $servername = substr($old_address,strpos($old_address,'@')+1);
$scheme = 'https://'; $scheme = 'https://';
$api_path = '/api/red/channel/export/items?f=&channel=' . $channelname . '&year=' . intval($year); $api_path = '/api/red/channel/export/items?f=&channel=' . $channelname . '&year=' . intval($year);
$binary = false; $binary = false;
@ -64,68 +68,65 @@ class Import_items extends \Zotlabs\Web\Controller {
$data = $ret['body']; $data = $ret['body'];
else else
notice( t('Unable to download data from old server') . EOL); notice( t('Unable to download data from old server') . EOL);
} }
if(! $data) { if(! $data) {
logger('mod_import: empty file.'); logger('Empty file.');
notice( t('Imported file is empty.') . EOL); notice( t('Imported file is empty.') . EOL);
return; return;
} }
$data = json_decode($data,true); $data = json_decode($data, true);
// logger('import: data: ' . print_r($data,true)); //logger('import: data: ' . print_r($data,true));
// print_r($data); //print_r($data);
if(! is_array($data)) if(! is_array($data))
return; return;
if(array_key_exists('compatibility',$data) && array_key_exists('database',$data['compatibility'])) { if(array_key_exists('compatibility',$data) && array_key_exists('database',$data['compatibility'])) {
$v1 = substr($data['compatibility']['database'],-4); $v1 = substr($data['compatibility']['database'],-4);
$v2 = substr(DB_UPDATE_VERSION,-4); $v2 = substr(DB_UPDATE_VERSION,-4);
if($v2 > $v1) { if($v2 > $v1) {
$t = sprintf( t('Warning: Database versions differ by %1$d updates.'), $v2 - $v1 ); $t = sprintf( t('Warning: Database versions differ by %1$d updates.'), $v2 - $v1 );
notice($t); notice($t . EOL);
} }
} }
$channel = \App::get_channel(); $channel = \App::get_channel();
if(array_key_exists('item',$data) && $data['item']) { if(array_key_exists('item',$data) && $data['item']) {
import_items($channel,$data['item'],false,((array_key_exists('relocate',$data)) ? $data['relocate'] : null)); import_items($channel,$data['item'],false,((array_key_exists('relocate',$data)) ? $data['relocate'] : null));
} }
if(array_key_exists('item_id',$data) && $data['item_id']) { if(array_key_exists('item_id',$data) && $data['item_id']) {
import_item_ids($channel,$data['item_id']); import_item_ids($channel,$data['item_id']);
} }
info( t('Import completed') . EOL); info( t('Import completed') . EOL);
return;
} }
/**
* @brief Generate item import page.
*
* @return string with parsed HTML.
*/
function get() { function get() {
if(! local_channel()) { if(! local_channel()) {
notice( t('Permission denied') . EOL); notice( t('Permission denied') . EOL);
return login(); return login();
} }
$o = replace_macros(get_markup_template('item_import.tpl'),array( $o = replace_macros(get_markup_template('item_import.tpl'), array(
'$title' => t('Import Items'), '$title' => t('Import Items'),
'$desc' => t('Use this form to import existing posts and content from an export file.'), '$desc' => t('Use this form to import existing posts and content from an export file.'),
'$label_filename' => t('File to Upload'), '$label_filename' => t('File to Upload'),
'$submit' => t('Submit') '$submit' => t('Submit')
)); ));
return $o; return $o;
} }
} }

View File

@ -1,6 +1,17 @@
<?php /** @file */ <?php
/**
* @file include/hubloc.php
* @brief Hubloc related functions.
*/
/**
* @brief Create an array for hubloc table and insert record.
*
* Creates an assoziative array which will be inserted into the hubloc table.
*
* @param array $arr An assoziative array with hubloc values
* @return boolean|PDOStatement
*/
function hubloc_store_lowlevel($arr) { function hubloc_store_lowlevel($arr) {
$store = [ $store = [
@ -25,8 +36,7 @@ function hubloc_store_lowlevel($arr) {
'hubloc_deleted' => ((array_key_exists('hubloc_deleted',$arr)) ? $arr['hubloc_deleted'] : 0) 'hubloc_deleted' => ((array_key_exists('hubloc_deleted',$arr)) ? $arr['hubloc_deleted'] : 0)
]; ];
return create_table_from_array('hubloc',$store); return create_table_from_array('hubloc', $store);
} }
@ -45,9 +55,8 @@ function prune_hub_reinstalls() {
// see if this url has more than one sitekey, indicating it has been re-installed. // see if this url has more than one sitekey, indicating it has been re-installed.
if(count($x) > 1) { if(count($x) > 1) {
$d1 = datetime_convert('UTC', 'UTC', $x[0]['c']);
$d1 = datetime_convert('UTC','UTC',$x[0]['c']); $d2 = datetime_convert('UTC', 'UTC', 'now - 3 days');
$d2 = datetime_convert('UTC','UTC','now - 3 days');
// allow some slop period, say 3 days - just in case this is a glitch or transient occurrence // allow some slop period, say 3 days - just in case this is a glitch or transient occurrence
// Then remove any hublocs pointing to the oldest entry. // Then remove any hublocs pointing to the oldest entry.
@ -63,18 +72,22 @@ function prune_hub_reinstalls() {
} }
} }
/**
* @brief Remove obsolete hublocs.
*
* Get rid of any hublocs which are ours but aren't valid anymore -
* e.g. they point to a different and perhaps transient URL that we aren't using.
*
* I need to stress that this shouldn't happen. fix_system_urls() fixes hublocs
* when it discovers the URL has changed. So it's unclear how we could end up
* with URLs pointing to the old site name. But it happens. This may be an artifact
* of an old bug or maybe a regression in some newer code. In any event, they
* mess up communications and we have to take action if we find any.
*/
function remove_obsolete_hublocs() { function remove_obsolete_hublocs() {
logger('remove_obsolete_hublocs',LOGGER_DEBUG); logger('remove_obsolete_hublocs', LOGGER_DEBUG);
// Get rid of any hublocs which are ours but aren't valid anymore -
// e.g. they point to a different and perhaps transient URL that we aren't using.
// I need to stress that this shouldn't happen. fix_system_urls() fixes hublocs
// when it discovers the URL has changed. So it's unclear how we could end up
// with URLs pointing to the old site name. But it happens. This may be an artifact
// of an old bug or maybe a regression in some newer code. In any event, they
// mess up communications and we have to take action if we find any.
// First make sure we have any hublocs (at all) with this URL and sitekey. // First make sure we have any hublocs (at all) with this URL and sitekey.
// We don't want to perform this operation while somebody is in the process // We don't want to perform this operation while somebody is in the process
@ -82,27 +95,25 @@ function remove_obsolete_hublocs() {
$r = q("select hubloc_id from hubloc where hubloc_url = '%s' and hubloc_sitekey = '%s'", $r = q("select hubloc_id from hubloc where hubloc_url = '%s' and hubloc_sitekey = '%s'",
dbesc(z_root()), dbesc(z_root()),
dbesc(get_config('system','pubkey')) dbesc(get_config('system', 'pubkey'))
); );
if((! $r) || (! count($r))) if((! $r) || (! count($r)))
return; return;
$channels = array();
// Good. We have at least one *valid* hubloc. // Good. We have at least one *valid* hubloc.
// Do we have any invalid ones? // Do we have any invalid ones?
$r = q("select hubloc_id from hubloc where hubloc_sitekey = '%s' and hubloc_url != '%s'", $r = q("select hubloc_id from hubloc where hubloc_sitekey = '%s' and hubloc_url != '%s'",
dbesc(get_config('system','pubkey')), dbesc(get_config('system', 'pubkey')),
dbesc(z_root()) dbesc(z_root())
); );
$p = q("select hubloc_id from hubloc where hubloc_sitekey != '%s' and hubloc_url = '%s'", $p = q("select hubloc_id from hubloc where hubloc_sitekey != '%s' and hubloc_url = '%s'",
dbesc(get_config('system','pubkey')), dbesc(get_config('system', 'pubkey')),
dbesc(z_root()) dbesc(z_root())
); );
if(is_array($r) && is_array($p)) if(is_array($r) && is_array($p))
$r = array_merge($r,$p); $r = array_merge($r, $p);
if(! $r) if(! $r)
return; return;
@ -111,8 +122,8 @@ function remove_obsolete_hublocs() {
logger('remove_obsolete_hublocs: removing ' . count($r) . ' hublocs.'); logger('remove_obsolete_hublocs: removing ' . count($r) . ' hublocs.');
$interval = ((get_config('system','delivery_interval') !== false) $interval = ((get_config('system', 'delivery_interval') !== false)
? intval(get_config('system','delivery_interval')) : 2 ); ? intval(get_config('system', 'delivery_interval')) : 2 );
foreach($r as $rr) { foreach($r as $rr) {
q("update hubloc set hubloc_deleted = 1 where hubloc_id = %d", q("update hubloc set hubloc_deleted = 1 where hubloc_id = %d",
@ -120,10 +131,10 @@ function remove_obsolete_hublocs() {
); );
$x = q("select channel_id from channel where channel_hash = '%s' limit 1", $x = q("select channel_id from channel where channel_hash = '%s' limit 1",
dbesc($rr['hubloc_hash']) dbesc($rr['hubloc_hash'])
); );
if($x) { if($x) {
Zotlabs\Daemon\Master::Summon(array('Notifier','location',$x[0]['channel_id'])); Zotlabs\Daemon\Master::Summon(array('Notifier', 'location', $x[0]['channel_id']));
if($interval) if($interval)
@time_sleep_until(microtime(true) + (float) $interval); @time_sleep_until(microtime(true) + (float) $interval);
} }
@ -131,8 +142,15 @@ function remove_obsolete_hublocs() {
} }
// This actually changes other structures to match the given (presumably current) hubloc primary selection /**
* @brief Change primary hubloc.
*
* This actually changes other structures to match the given (presumably current)
* hubloc primary selection.
*
* @param array $hubloc
* @return boolean
*/
function hubloc_change_primary($hubloc) { function hubloc_change_primary($hubloc) {
if(! is_array($hubloc)) { if(! is_array($hubloc)) {
@ -170,7 +188,7 @@ function hubloc_change_primary($hubloc) {
dbesc($hubloc['hubloc_hash']) dbesc($hubloc['hubloc_hash'])
); );
if(! $r) { if(! $r) {
logger('xchan not found'); logger('xchan not found');
return false; return false;
} }
if($r[0]['xchan_addr'] === $hubloc['hubloc_addr']) { if($r[0]['xchan_addr'] === $hubloc['hubloc_addr']) {
@ -179,7 +197,7 @@ function hubloc_change_primary($hubloc) {
} }
$url = $hubloc['hubloc_url']; $url = $hubloc['hubloc_url'];
$lwebbie = substr($hubloc['hubloc_addr'],0,strpos($hubloc['hubloc_addr'],'@')); $lwebbie = substr($hubloc['hubloc_addr'], 0, strpos($hubloc['hubloc_addr'], '@'));
$r = q("update xchan set xchan_addr = '%s', xchan_url = '%s', xchan_follow = '%s', xchan_connurl = '%s' where xchan_hash = '%s'", $r = q("update xchan set xchan_addr = '%s', xchan_url = '%s', xchan_follow = '%s', xchan_connurl = '%s' where xchan_hash = '%s'",
dbesc($hubloc['hubloc_addr']), dbesc($hubloc['hubloc_addr']),
@ -191,14 +209,19 @@ function hubloc_change_primary($hubloc) {
if(! $r) if(! $r)
logger('xchan_update failed.'); logger('xchan_update failed.');
logger('primary hubloc changed.' . print_r($hubloc,true),LOGGER_DEBUG); logger('primary hubloc changed.' . print_r($hubloc, true), LOGGER_DEBUG);
return true; return true;
} }
// We use the post url to distinguish between http and https hublocs.
// The https might be alive, and the http dead.
/**
* @brief Mark a hubloc as down.
*
* We use the post url to distinguish between http and https hublocs.
* The https might be alive, and the http dead.
*
* @param string $posturl Hubloc callback url which to disable
*/
function hubloc_mark_as_down($posturl) { function hubloc_mark_as_down($posturl) {
$r = q("update hubloc set hubloc_status = ( hubloc_status | %d ) where hubloc_callback = '%s'", $r = q("update hubloc set hubloc_status = ( hubloc_status | %d ) where hubloc_callback = '%s'",
intval(HUBLOC_OFFLINE), intval(HUBLOC_OFFLINE),
@ -208,22 +231,21 @@ function hubloc_mark_as_down($posturl) {
function ping_site($url) { function ping_site($url) {
$ret = array('success' => false); $ret = array('success' => false);
$sys = get_sys_channel(); $sys = get_sys_channel();
$m = zot_build_packet($sys,'ping'); $m = zot_build_packet($sys, 'ping');
$r = zot_zot($url . '/post',$m); $r = zot_zot($url . '/post', $m);
if(! $r['success']) { if(! $r['success']) {
$ret['message'] = 'no answer from ' . $url; $ret['message'] = 'no answer from ' . $url;
return $ret; return $ret;
} }
$packet_result = json_decode($r['body'],true); $packet_result = json_decode($r['body'], true);
if(! $packet_result['success']) { if(! $packet_result['success']) {
$ret['message'] = 'packet failure from ' . $url; $ret['message'] = 'packet failure from ' . $url;
return $ret; return $ret;
} }

File diff suppressed because it is too large Load Diff

View File

@ -3050,7 +3050,15 @@ function array2XML($obj, $array) {
} }
} }
/**
* @brief Inserts an array into $table.
*
* @TODO Why is this function in include/text.php?
*
* @param string $table
* @param array $arr
* @return boolean|PDOStatement
*/
function create_table_from_array($table, $arr) { function create_table_from_array($table, $arr) {
if(! ($arr && $table)) if(! ($arr && $table))