1. provide automatic relocation of important links in items that are imported or synced to clones

2. provide framework for custom curl request bodies using custom/non-standard request methods. This was a real nightmare because curl doesn't actually let you specify a string to send as the request body (except when doing POST). You have to treat it as a file upload using a custom file handler function which provides the actual content in chunks as if it were buffered I/O.
3. item_store and item_store_update now return the item that was stored
This commit is contained in:
redmatrix 2016-06-21 18:18:06 -07:00
parent 63423c8ee1
commit b593c3a9b9
9 changed files with 69 additions and 53 deletions

View File

@ -131,6 +131,8 @@ class Import extends \Zotlabs\Web\Controller {
// import channel
$relocate = ((array_key_exists('relocate',$data)) ? $data['relocate'] : null);
if(array_key_exists('channel',$data)) {
if($completed < 1) {
@ -475,7 +477,7 @@ class Import extends \Zotlabs\Web\Controller {
import_events($channel,$data['event']);
if(is_array($data['event_item']))
import_items($channel,$data['event_item']);
import_items($channel,$data['event_item'],false,$relocate);
if(is_array($data['menu']))
import_menus($channel,$data['menu']);
@ -486,7 +488,7 @@ class Import extends \Zotlabs\Web\Controller {
$saved_notification_flags = notifications_off($channel['channel_id']);
if($import_posts && array_key_exists('item',$data) && $data['item'])
import_items($channel,$data['item']);
import_items($channel,$data['item'],false,$relocate);
notifications_on($channel['channel_id'],$saved_notification_flags);

View File

@ -92,7 +92,7 @@ class Import_items extends \Zotlabs\Web\Controller {
if(array_key_exists('item',$data) && $data['item']) {
import_items($channel,$data['item']);
import_items($channel,$data['item'],false,((array_key_exists('relocate',$data)) ? $data['relocate'] : null));
}
if(array_key_exists('item_id',$data) && $data['item_id']) {
@ -106,7 +106,7 @@ class Import_items extends \Zotlabs\Web\Controller {
function get() {
function get() {
if(! local_channel()) {
notice( t('Permission denied') . EOL);

View File

@ -894,7 +894,7 @@ class Item extends \Zotlabs\Web\Controller {
if($orig_post) {
$datarray['id'] = $post_id;
item_store_update($datarray,$execflag);
$x = item_store_update($datarray,$execflag);
if(! $parent) {
$r = q("select * from item where id = %d",

View File

@ -44,7 +44,7 @@ class Uexport extends \Zotlabs\Web\Controller {
}
}
function get() {
function get() {
$y = datetime_convert('UTC',date_default_timezone_get(),'now','Y');

View File

@ -496,8 +496,10 @@ function identity_basic_export($channel_id, $items = false) {
$r = q("select * from channel where channel_id = %d limit 1",
intval($channel_id)
);
if($r)
if($r) {
$ret['channel'] = $r[0];
$ret['relocate'] = [ 'channel_address' => $r[0]['channel_address'], 'url' => z_root()];
}
$r = q("select * from profile where uid = %d",
intval($channel_id)
@ -717,6 +719,10 @@ function identity_export_year($channel_id,$year,$month = 0) {
$ret = array();
$ch = channelx_by_n($channel_id);
if($ch) {
$ret['relocate'] = [ 'channel_address' => $ch['channel_address'], 'url' => z_root()];
}
$mindate = datetime_convert('UTC','UTC',$year . '-' . $target_month . '-01 00:00:00');
if($month && $month < 12)
$maxdate = datetime_convert('UTC','UTC',$year . '-' . $target_month_plus . '-01 00:00:00');

View File

@ -553,7 +553,7 @@ function sync_chatrooms($channel,$chatrooms) {
function import_items($channel,$items,$sync = false) {
function import_items($channel,$items,$sync = false,$relocate = null) {
if($channel && $items) {
$allow_code = false;
@ -584,13 +584,6 @@ function import_items($channel,$items,$sync = false) {
$item['id'] = $r[0]['id'];
$item['uid'] = $channel['channel_id'];
$item_result = item_store_update($item,$allow_code,$deliver);
if($sync && $item['item_wall']) {
// deliver singletons if we have any
if($item_result && $item_result['success']) {
Zotlabs\Daemon\Master::Summon(array('Notifier','single_activity',$item_result['item_id']));
}
}
continue;
}
}
else {
@ -598,19 +591,37 @@ function import_items($channel,$items,$sync = false) {
$item['uid'] = $channel['channel_id'];
$item_result = item_store($item,$allow_code,$deliver);
}
if($sync && $item['item_wall']) {
// deliver singletons if we have any
if($item_result && $item_result['success']) {
Zotlabs\Daemon\Master::Summon(array('Notifier','single_activity',$item_result['item_id']));
}
}
if($relocate && $item_result['item_id']) {
$item = $item_result['item'];
if($item['mid'] === $item['parent_mid']) {
item_url_replace($channel,$item,$relocate['url'],z_root(),$relocate['channel_address']);
dbesc_array($item);
$item_id = $item_result['item_id'];
unset($item['id']);
$str = '';
foreach($item as $k => $v) {
if($str)
$str .= ",";
$str .= " `" . $k . "` = '" . $v . "' ";
}
$r = dbq("update `item` set " . $str . " where id = " . $item_id );
}
}
}
}
}
function sync_items($channel,$items) {
import_items($channel,$items,true);
function sync_items($channel,$items,$relocate = null) {
import_items($channel,$items,true,$relocate);
}
@ -1200,31 +1211,9 @@ function sync_files($channel,$files) {
}
}
if($f['item']) {
sync_items($channel,$f['item']);
foreach($f['item'] as $i) {
if($i['message_id'] !== $i['message_parent'])
continue;
$r = q("select * from item where mid = '%s' and uid = %d limit 1",
dbesc($i['message_id']),
intval($channel['channel_id'])
);
if($r) {
$item = $r[0];
item_url_replace($channel,$item,$oldbase,z_root(),$original_channel);
dbesc_array($item);
$item_id = $item['id'];
unset($item['id']);
$str = '';
foreach($item as $k => $v) {
if($str)
$str .= ",";
$str .= " `" . $k . "` = '" . $v . "' ";
}
$r = dbq("update `item` set " . $str . " where id = " . $item_id );
}
}
sync_items($channel,$f['item'],
['channel_address' => $original_channel,'url' => $oldbase]
);
}
}
}

View File

@ -1889,6 +1889,7 @@ function item_store($arr, $allow_exec = false, $deliver = true) {
}
$ret['item'] = $arr;
call_hooks('post_remote_end',$arr);
@ -2139,6 +2140,15 @@ function item_store_update($arr,$allow_exec = false, $deliver = true) {
return $ret;
}
// fetch an unescaped complete copy of the stored item
$r = q("select * from item where id = %d",
intval($orig_post_id)
);
if($r)
$arr = $r[0];
$r = q("delete from term where oid = %d and otype = %d",
intval($orig_post_id),
intval(TERM_OBJ_POST)
@ -2170,6 +2180,8 @@ function item_store_update($arr,$allow_exec = false, $deliver = true) {
$arr['iconfig'] = $meta;
}
$ret['item'] = $arr;
call_hooks('post_remote_update_end',$arr);
if($deliver) {

View File

@ -62,7 +62,17 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) {
@curl_setopt($ch, CURLOPT_HEADER, $false);
}
if(x($opts,'upload'))
@curl_setopt($ch, CURLOPT_UPLOAD, $opts['upload']);
if(x($opts,'infile'))
@curl_setopt($ch, CURLOPT_INFILE, $opts['infile']);
if(x($opts,'infilesize'))
@curl_setopt($ch, CURLOPT_INFILESIZE, $opts['infilesize']);
if(x($opts,'readfunc'))
@curl_setopt($ch, CURLOPT_READFUNCTION, $opts['readfunc']);
if(x($opts,'headers'))
@curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);

View File

@ -3031,7 +3031,8 @@ function build_sync_packet($uid = 0, $packet = null, $groups_changed = false) {
$info = (($packet) ? $packet : array());
$info['type'] = 'channel_sync';
$info['encoding'] = 'red'; // note: not zot, this packet is very red specific
$info['encoding'] = 'red'; // note: not zot, this packet is very platform specific
$info['relocate'] = ['channel_address' => $channel['channel_address'], 'url' => z_root() ];
if(array_key_exists($uid,App::$config) && array_key_exists('transient',App::$config[$uid])) {
$settings = App::$config[$uid]['transient'];
@ -3169,10 +3170,13 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) {
sync_events($channel,$arr['event']);
if(array_key_exists('event_item',$arr) && $arr['event_item'])
sync_items($channel,$arr['event_item']);
sync_items($channel,$arr['event_item'],((array_key_exists('relocate',$arr)) ? $arr['relocate'] : null));
if(array_key_exists('item',$arr) && $arr['item'])
sync_items($channel,$arr['item']);
sync_items($channel,$arr['item'],((array_key_exists('relocate',$arr)) ? $arr['relocate'] : null));
// deprecated, maintaining for a few months for upward compatibility
// this should sync webpages, but the logic is a bit subtle
if(array_key_exists('item_id',$arr) && $arr['item_id'])
sync_items($channel,$arr['item_id']);
@ -3529,13 +3533,6 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) {
}
}
if(array_key_exists('item',$arr) && $arr['item'])
sync_items($channel,$arr['item']);
if(array_key_exists('item_id',$arr) && $arr['item_id'])
sync_items($channel,$arr['item_id']);
$addon = array('channel' => $channel,'data' => $arr);
call_hooks('process_channel_sync_delivery',$addon);