* @file include/items.php
*/
-use \Friendica\ParseUrl;
+use Friendica\App;
+use Friendica\ParseUrl;
require_once 'include/bbcode.php';
require_once 'include/oembed.php';
$body = substr($body, 0, strlen($title));
}
- if (($title != $body) and (substr($title, -3) == "...")) {
+ if (($title != $body) && (substr($title, -3) == "...")) {
$pos = strrpos($title, "...");
if ($pos > 0) {
$title = substr($title, 0, $pos);
// It maybe is a rich content, but if it does have everything that a link has,
// then treat it that way
- if (($data["type"] == "rich") AND is_string($data["title"]) AND
- is_string($data["text"]) AND (sizeof($data["images"]) > 0)) {
+ if (($data["type"] == "rich") && is_string($data["title"]) &&
+ is_string($data["text"]) && (sizeof($data["images"]) > 0)) {
$data["type"] = "link";
}
- if ((($data["type"] != "link") AND ($data["type"] != "video") AND ($data["type"] != "photo")) OR ($data["title"] == $data["url"])) {
+ if ((($data["type"] != "link") && ($data["type"] != "video") && ($data["type"] != "photo")) || ($data["title"] == $data["url"])) {
return "";
}
- if ($no_photos AND ($data["type"] == "photo")) {
+ if ($no_photos && ($data["type"] == "photo")) {
return "";
}
$preview = str_replace(array("[", "]"), array("[", "]"), htmlentities($data["images"][0]["src"], ENT_QUOTES, 'UTF-8', false));
// if the preview picture is larger than 500 pixels then show it in a larger mode
// But only, if the picture isn't higher than large (To prevent huge posts)
- if (($data["images"][0]["width"] >= 500) AND ($data["images"][0]["width"] >= $data["images"][0]["height"])) {
+ if (($data["images"][0]["width"] >= 500) && ($data["images"][0]["width"] >= $data["images"][0]["height"])) {
$text .= " image='".$preview."'";
} else {
$text .= " preview='".$preview."'";
$text .= "]".$data["text"]."[/attachment]";
$hashtags = "";
- if (isset($data["keywords"]) AND count($data["keywords"])) {
+ if (isset($data["keywords"]) && count($data["keywords"])) {
$hashtags = "\n";
foreach ($data["keywords"] AS $keyword) {
/// @todo make a positive list of allowed characters
logger('fetch page info for ' . $url . ' ' . print_r($data, true), LOGGER_DEBUG);
- if (!$keywords AND isset($data["keywords"])) {
+ if (!$keywords && isset($data["keywords"])) {
unset($data["keywords"]);
}
- if (($keyword_blacklist != "") AND isset($data["keywords"])) {
+ if (($keyword_blacklist != "") && isset($data["keywords"])) {
$list = explode(", ", $keyword_blacklist);
foreach ($list AS $keyword) {
$keyword = trim($keyword);
$data = query_page_info($url, $no_photos, $photo, $keywords, $keyword_blacklist);
$tags = "";
- if (isset($data["keywords"]) AND count($data["keywords"])) {
+ if (isset($data["keywords"]) && count($data["keywords"])) {
foreach ($data["keywords"] AS $keyword) {
$hashtag = str_replace(array(" ", "+", "/", ".", "#", "'"),
array("", "", "", "", "", ""), $keyword);
}
// Convert urls without bbcode elements
- if (!$matches AND $texturl) {
+ if (!$matches && $texturl) {
preg_match("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", " ".$body, $matches);
// Yeah, a hack. I really hate regular expressions :)
}
// Remove the link from the body if the link is attached at the end of the post
- if (isset($footer) AND (trim($footer) != "") AND (strpos($footer, $matches[1]))) {
+ if (isset($footer) && (trim($footer) != "") && (strpos($footer, $matches[1]))) {
$removedlink = trim(str_replace($matches[1], "", $body));
- if (($removedlink == "") OR strstr($body, $removedlink)) {
+ if (($removedlink == "") || strstr($body, $removedlink)) {
$body = $removedlink;
}
$url = str_replace(array('/', '.'), array('\/', '\.'), $matches[1]);
$removedlink = preg_replace("/\[url\=" . $url . "\](.*?)\[\/url\]/ism", '', $body);
- if (($removedlink == "") OR strstr($body, $removedlink)) {
+ if (($removedlink == "") || strstr($body, $removedlink)) {
$body = $removedlink;
}
}
// Add the page information to the bottom
- if (isset($footer) AND (trim($footer) != "")) {
+ if (isset($footer) && (trim($footer) != "")) {
$body .= $footer;
}
* Adds a "lang" specification in a "postopts" element of given $arr,
* if possible and not already present.
* Expects "body" element to exist in $arr.
- *
+ *
* @todo Add a parameter to request forcing override
*/
function item_add_language_opt(&$arr) {
return $guid_prefix.$host_hash;
}
-/// @TODO Maybe $arr must be called-by-reference? This function modifies it
+/**
+ * @brief Store the conversation data
+ *
+ * @param array $arr Item array with conversation data
+ * @return array Item array with removed conversation data
+ */
+function store_conversation($arr) {
+ if (in_array($arr['network'], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
+ $conversation = array('item-uri' => $arr['uri'], 'received' => dbm::date());
+
+ if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
+ $conversation['reply-to-uri'] = $arr['parent-uri'];
+ }
+ if (isset($arr['thr-parent']) && ($arr['thr-parent'] != $arr['uri'])) {
+ $conversation['reply-to-uri'] = $arr['thr-parent'];
+ }
+
+ if (isset($arr['conversation-uri'])) {
+ $conversation['conversation-uri'] = $arr['conversation-uri'];
+ }
+
+ if (isset($arr['conversation-href'])) {
+ $conversation['conversation-href'] = $arr['conversation-href'];
+ }
+
+ if (isset($arr['protocol'])) {
+ $conversation['protocol'] = $arr['protocol'];
+ }
+
+ if (isset($arr['source'])) {
+ $conversation['source'] = $arr['source'];
+ }
+
+ $old_conv = dba::fetch_first("SELECT `item-uri`, `reply-to-uri`, `conversation-uri`, `conversation-href`, `protocol`, `source`
+ FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']);
+ if (dbm::is_result($old_conv)) {
+ // Don't update when only the source has changed.
+ // Only do this when there had been no source before.
+ if ($old_conv['source'] != '') {
+ unset($old_conv['source']);
+ }
+ // Update structure data all the time but the source only when its from a better protocol.
+ if (($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
+ unset($conversation['protocol']);
+ unset($conversation['source']);
+ }
+ if (!dba::update('conversation', $conversation, array('item-uri' => $conversation['item-uri']), $old_conv)) {
+ logger('Conversation: update for '.$conversation['item-uri'].' from '.$conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
+ }
+ } else {
+ if (!dba::insert('conversation', $conversation)) {
+ logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
+ }
+ }
+ }
+
+ unset($arr['conversation-uri']);
+ unset($arr['conversation-href']);
+ unset($arr['protocol']);
+ unset($arr['source']);
+
+ return $arr;
+}
+
/// @TODO add type-hint array
function item_store($arr, $force_parent = false, $notify = false, $dontcache = false) {
$arr['origin'] = 1;
$arr['last-child'] = 1;
$arr['network'] = NETWORK_DFRN;
+ $arr['protocol'] = PROTOCOL_DFRN;
// We have to avoid duplicates. So we create the GUID in form of a hash of the plink or uri.
// In difference to the call to "uri_to_guid" several lines below we add the hash of our own host.
}
}
+ if ($notify) {
+ $guid_prefix = "";
+ } elseif ((trim($arr['guid']) == "") && (trim($arr['plink']) != "")) {
+ $arr['guid'] = uri_to_guid($arr['plink']);
+ } elseif ((trim($arr['guid']) == "") && (trim($arr['uri']) != "")) {
+ $arr['guid'] = uri_to_guid($arr['uri']);
+ } else {
+ $parsed = parse_url($arr["author-link"]);
+ $guid_prefix = hash("crc32", $parsed["host"]);
+ }
+
+ $arr['guid'] = ((x($arr, 'guid')) ? notags(trim($arr['guid'])) : get_guid(32, $guid_prefix));
+ $arr['uri'] = ((x($arr, 'uri')) ? notags(trim($arr['uri'])) : item_new_uri($a->get_hostname(), $uid, $arr['guid']));
+
+ // Store conversation data
+ $arr = store_conversation($arr);
+
/*
* If a Diaspora signature structure was passed in, pull it out of the
* item array and set it aside for later storage.
item_add_language_opt($arr);
- if ($notify) {
- $guid_prefix = "";
- } elseif ((trim($arr['guid']) == "") AND (trim($arr['plink']) != "")) {
- $arr['guid'] = uri_to_guid($arr['plink']);
- } elseif ((trim($arr['guid']) == "") AND (trim($arr['uri']) != "")) {
- $arr['guid'] = uri_to_guid($arr['uri']);
- } else {
- $parsed = parse_url($arr["author-link"]);
- $guid_prefix = hash("crc32", $parsed["host"]);
- }
-
$arr['wall'] = ((x($arr, 'wall')) ? intval($arr['wall']) : 0);
- $arr['guid'] = ((x($arr, 'guid')) ? notags(trim($arr['guid'])) : get_guid(32, $guid_prefix));
- $arr['uri'] = ((x($arr, 'uri')) ? notags(trim($arr['uri'])) : item_new_uri($a->get_hostname(), $uid, $arr['guid']));
$arr['extid'] = ((x($arr, 'extid')) ? notags(trim($arr['extid'])) : '');
$arr['author-name'] = ((x($arr, 'author-name')) ? trim($arr['author-name']) : '');
$arr['author-link'] = ((x($arr, 'author-link')) ? notags(trim($arr['author-link'])) : '');
$arr['edited'] = datetime_convert();
}
- if (($arr['author-link'] == "") AND ($arr['owner-link'] == "")) {
+ if (($arr['author-link'] == "") && ($arr['owner-link'] == "")) {
logger("Both author-link and owner-link are empty. Called by: " . App::callstack(), LOGGER_DEBUG);
}
item_body_set_hashtags($arr);
$arr['thr-parent'] = $arr['parent-uri'];
+
if ($arr['parent-uri'] === $arr['uri']) {
$parent_id = 0;
$parent_deleted = 0;
$a = get_app();
$self = normalise_link(App::get_baseurl() . '/profile/' . $u[0]['nickname']);
logger("item_store: 'myself' is ".$self." for parent ".$parent_id." checking against ".$arr['author-link']." and ".$arr['owner-link'], LOGGER_DEBUG);
- if ((normalise_link($arr['author-link']) == $self) OR (normalise_link($arr['owner-link']) == $self)) {
+ if ((normalise_link($arr['author-link']) == $self) || (normalise_link($arr['owner-link']) == $self)) {
q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($parent_id));
logger("item_store: tagged thread ".$parent_id." as mention for user ".$self, LOGGER_DEBUG);
}
logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
- q("COMMIT");
- q("START TRANSACTION;");
+ dba::transaction();
$r = dbq("INSERT INTO `item` (`"
. implode("`, `", array_keys($arr))
}
} else {
// This can happen - for example - if there are locking timeouts.
- q("ROLLBACK");
+ dba::rollback();
// Store the data into a spool file so that we can try again later.
if ($current_post == 0) {
// This is one of these error messages that never should occur.
logger("couldn't find created item - we better quit now.");
- q("ROLLBACK");
+ dba::rollback();
return 0;
}
if (!dbm::is_result($r)) {
// This shouldn't happen, since COUNT always works when the database connection is there.
logger("We couldn't count the stored entries. Very strange ...");
- q("ROLLBACK");
+ dba::rollback();
return 0;
}
logger('Duplicated post occurred. uri = ' . $arr['uri'] . ' uid = ' . $arr['uid']);
// Yes, we could do a rollback here - but we are having many users with MyISAM.
- q("DELETE FROM `item` WHERE `id` = %d", intval($current_post));
- q("COMMIT");
+ dba::delete('item', array('id' => $current_post));
+ dba::commit();
return 0;
} elseif ($r[0]["entries"] == 0) {
// This really should never happen since we quit earlier if there were problems.
logger("Something is terribly wrong. We haven't found our created entry.");
- q("ROLLBACK");
+ dba::rollback();
return 0;
}
// update the commented timestamp on the parent
// Only update "commented" if it is really a comment
- if (($arr['verb'] == ACTIVITY_POST) OR !get_config("system", "like_no_comment")) {
+ if (($arr['verb'] == ACTIVITY_POST) || !get_config("system", "like_no_comment")) {
q("UPDATE `item` SET `commented` = '%s', `changed` = '%s' WHERE `id` = %d",
dbesc(datetime_convert()),
dbesc(datetime_convert()),
* current post can be deleted if is for a community page and no mention are
* in it.
*/
- if (!$deleted AND !$dontcache) {
+ if (!$deleted && !$dontcache) {
$r = q('SELECT * FROM `item` WHERE `id` = %d', intval($current_post));
if ((dbm::is_result($r)) && (count($r) == 1)) {
update_thread($parent_id);
}
- q("COMMIT");
+ dba::commit();
/*
* Due to deadlock issues with the "term" table we are doing these steps after the commit.
check_item_notification($current_post, $uid);
if ($notify) {
- proc_run(PRIORITY_HIGH, "include/notifier.php", $notify_type, $current_post);
+ proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "include/notifier.php", $notify_type, $current_post);
}
return $current_post;
*/
function item_set_last_item($arr) {
- $update = (!$arr['private'] AND (($arr["author-link"] === $arr["owner-link"]) OR ($arr["parent-uri"] === $arr["uri"])));
+ $update = (!$arr['private'] && (($arr["author-link"] === $arr["owner-link"]) || ($arr["parent-uri"] === $arr["uri"])));
// Is it a forum? Then we don't care about the rules from above
- if (!$update AND ($arr["network"] == NETWORK_DFRN) AND ($arr["parent-uri"] === $arr["uri"])) {
+ if (!$update && ($arr["network"] == NETWORK_DFRN) && ($arr["parent-uri"] === $arr["uri"])) {
$isforum = q("SELECT `forum` FROM `contact` WHERE `id` = %d AND `forum`",
intval($arr['contact-id']));
if (dbm::is_result($isforum)) {
// Does the given user have this item?
if ($uid) {
$r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
- WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+ WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 AND `item`.`moderated` = 0
AND `item`.`guid` = '%s' AND `item`.`uid` = %d", dbesc($guid), intval($uid));
if (dbm::is_result($r)) {
$id = $r[0]["id"];
// Or is it anywhere on the server?
if ($nick == "") {
$r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
- WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+ WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 AND `item`.`moderated` = 0
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
AND `item`.`private` = 0 AND `item`.`wall` = 1
*/
$dlink = normalise_link(App::get_baseurl() . '/u/' . $u[0]['nickname']);
- $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches,PREG_SET_ORDER);
+ $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
if (link_compare($link, $mtch[1]) || link_compare($dlink, $mtch[1])) {
// mmh.. no mention.. community page or private group... no wall.. no origin.. top-post (not a comment)
// delete it!
logger("tag_deliver: no-mention top-level post to communuty or private group. delete.");
- q("DELETE FROM item WHERE id = %d and uid = %d",
- intval($item_id),
- intval($uid)
- );
+ dba::delete('item', array('id' => $item_id));
return true;
}
return;
);
update_thread($item_id);
- proc_run(PRIORITY_HIGH,'include/notifier.php', 'tgroup', $item_id);
+ proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'include/notifier.php', 'tgroup', $item_id);
}
*/
$dlink = normalise_link(App::get_baseurl() . '/u/' . $u[0]['nickname']);
- $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches,PREG_SET_ORDER);
+ $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
if (link_compare($link, $mtch[1]) || link_compare($dlink, $mtch[1])) {
return false;
}
- if (($contact['network'] != NETWORK_FEED) AND $datarray['private']) {
+ if (($contact['network'] != NETWORK_FEED) && $datarray['private']) {
return false;
}
intval($importer['uid'])
);
- if (dbm::is_result($r) AND !in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE))) {
+ if (dbm::is_result($r) && !in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE))) {
// create notification
$hash = random_string();
));
}
- } elseif (dbm::is_result($r) AND in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE))) {
+ } elseif (dbm::is_result($r) && in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE))) {
$r = q("UPDATE `contact` SET `pending` = 0 WHERE `uid` = %d AND `url` = '%s' AND `pending` LIMIT 1",
intval($importer['uid']),
dbesc($url)
logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: ' . $push_url . ' with verifier ' . $verify_token);
- if (!strlen($contact['hub-verify']) OR ($contact['hub-verify'] != $verify_token)) {
+ if (!strlen($contact['hub-verify']) || ($contact['hub-verify'] != $verify_token)) {
$r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d",
dbesc($verify_token),
intval($contact['id'])
$x = strpos($i, '-');
if ($x) {
- $res = substr($i, $x+1);
- $i = substr($i,0, $x);
+ $res = substr($i, $x + 1);
+ $i = substr($i, 0, $x);
$r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
dbesc($i),
intval($res),
drop_item($item['id'], false);
}
- proc_run(PRIORITY_HIGH, "include/notifier.php", "expire", $uid);
+ proc_run(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "include/notifier.php", "expire", $uid);
}
// multiple threads may have been deleted, send an expire notification
if ($uid) {
- proc_run(PRIORITY_HIGH, "include/notifier.php", "expire", $uid);
+ proc_run(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "include/notifier.php", "expire", $uid);
}
}
$item = $r[0];
+ if ($item['deleted']) {
+ return 0;
+ }
+
$owner = $item['uid'];
$contact_id = 0;
// clean up categories and tags so they don't end up as orphans
$matches = false;
- $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches,PREG_SET_ORDER);
+ $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],true);
$matches = false;
- $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches,PREG_SET_ORDER);
+ $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],false);
// ignore the result
}
-
- // clean up item_id and sign meta-data tables
-
- /*
- /// @TODO Old code - caused very long queries and warning entries in the mysql logfiles:
-
- $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
- intval($item['id']),
- intval($item['uid'])
- );
-
- $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
- intval($item['id']),
- intval($item['uid'])
- );
- */
-
// The new code splits the queries since the mysql optimizer really has bad problems with subqueries
// Creating list of parents
}
}
+ // send the notification upstream/downstream
+ // The priority depends on how the deletion is done.
$drop_id = intval($item['id']);
+ $priority = ($interactive ? PRIORITY_HIGH : PRIORITY_LOW);
- // send the notification upstream/downstream as the case may be
-
- proc_run(PRIORITY_HIGH, "include/notifier.php", "drop", $drop_id);
+ proc_run(array('priority' => $priority, 'dont_fork' => true), "include/notifier.php", "drop", $drop_id);
if (! $interactive) {
return $owner;
}
// Set the start and end date to the beginning of the month
- $dnow = substr($dnow, 0, 8).'01';
- $dthen = substr($dthen, 0, 8).'01';
+ $dnow = substr($dnow, 0, 8) . '01';
+ $dthen = substr($dthen, 0, 8) . '01';
$ret = array();
/*