use Friendica\Core\Config;
use Friendica\Core\System;
use Friendica\Database\DBM;
-use Friendica\Model\GlobalContact;
+use Friendica\Model\Contact;
+use Friendica\Model\GContact;
use Friendica\Network\Probe;
-use Friendica\Object\Contact;
-use Friendica\Object\Photo;
+use Friendica\Object\Image;
use Friendica\Util\Lock;
use Friendica\Util\XML;
use dba;
use DOMDocument;
-use DomXPath;
+use DOMXPath;
+require_once 'include/dba.php';
require_once 'include/threads.php';
require_once 'include/html2bbcode.php';
require_once 'include/bbcode.php';
require_once 'include/items.php';
require_once 'mod/share.php';
require_once 'include/enotify.php';
-require_once 'include/follow.php';
require_once 'include/api.php';
require_once 'mod/proxy.php';
$found = false;
if ($aliaslink != '') {
- $condition = array("`uid` = ? AND `alias` = ? AND `network` != ?",
- $importer["uid"], $aliaslink, NETWORK_STATUSNET);
- $r = dba::select('contact', array(), $condition, array('limit' => 1));
+ $condition = ["`uid` = ? AND `alias` = ? AND `network` != ?",
+ $importer["uid"], $aliaslink, NETWORK_STATUSNET];
+ $r = dba::selectFirst('contact', [], $condition);
if (DBM::is_result($r)) {
$found = true;
$aliaslink = $author["author-link"];
}
- $condition = array("`uid` = ? AND `nurl` IN (?, ?) AND `network` != ?", $importer["uid"],
- normalise_link($author["author-link"]), normalise_link($aliaslink), NETWORK_STATUSNET);
- $r = dba::select('contact', array(), $condition, array('limit' => 1));
+ $condition = ["`uid` = ? AND `nurl` IN (?, ?) AND `network` != ?", $importer["uid"],
+ normalise_link($author["author-link"]), normalise_link($aliaslink), NETWORK_STATUSNET];
+ $r = dba::selectFirst('contact', [], $condition);
if (DBM::is_result($r)) {
$found = true;
}
if (!$found && ($addr != "")) {
- $condition = array("`uid` = ? AND `addr` = ? AND `network` != ?",
- $importer["uid"], $addr, NETWORK_STATUSNET);
- $r = dba::select('contact', array(), $condition, array('limit' => 1));
+ $condition = ["`uid` = ? AND `addr` = ? AND `network` != ?",
+ $importer["uid"], $addr, NETWORK_STATUSNET];
+ $r = dba::selectFirst('contact', [], $condition);
if (DBM::is_result($r)) {
$found = true;
$cid = Contact::getIdForURL($aliaslink, 0);
if ($cid) {
- $fields = array('url', 'nurl', 'name', 'nick', 'alias', 'about', 'location');
- $old_contact = dba::select('contact', $fields, array('id' => $cid), array('limit' => 1));
+ $fields = ['url', 'nurl', 'name', 'nick', 'alias', 'about', 'location'];
+ $old_contact = dba::selectFirst('contact', $fields, ['id' => $cid]);
// Update it with the current values
$fields = array('url' => $author["author-link"], 'name' => $contact["name"],
$contact["generation"] = 2;
$contact["hide"] = false; // OStatus contacts are never hidden
$contact["photo"] = $author["author-avatar"];
- $gcid = GlobalContact::update($contact);
+ $gcid = GContact::update($contact);
- GlobalContact::link($gcid, $contact["uid"], $contact["id"]);
+ GContact::link($gcid, $contact["uid"], $contact["id"]);
}
return $author;
$doc = new DOMDocument();
@$doc->loadXML($xml);
- $xpath = new DomXPath($doc);
+ $xpath = new DOMXPath($doc);
$xpath->registerNamespace('atom', NAMESPACE_ATOM1);
$xpath->registerNamespace('thr', NAMESPACE_THREAD);
$xpath->registerNamespace('georss', NAMESPACE_GEORSS);
$doc = new DOMDocument();
@$doc->loadXML($xml);
- $xpath = new DomXPath($doc);
+ $xpath = new DOMXPath($doc);
$xpath->registerNamespace('atom', NAMESPACE_ATOM1);
$xpath->registerNamespace('thr', NAMESPACE_THREAD);
$xpath->registerNamespace('georss', NAMESPACE_GEORSS);
*/
private static function deleteNotice($item)
{
- $condition = array('uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']);
- $deleted = dba::select('item', array('id', 'parent-uri'), $condition, array('limit' => 1));
+ $condition = ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']];
+ $deleted = dba::selectFirst('item', ['id', 'parent-uri'], $condition);
if (!DBM::is_result($deleted)) {
logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it. ");
return;
if (!@$doc->loadHTML($conversation_data['body'])) {
return;
}
- $xpath = new DomXPath($doc);
+ $xpath = new DOMXPath($doc);
$links = $xpath->query('//link');
if ($links) {
$doc = new DOMDocument();
@$doc->loadXML($xml);
- $xpath = new DomXPath($doc);
+ $xpath = new DOMXPath($doc);
$xpath->registerNamespace('atom', NAMESPACE_ATOM1);
$xpath->registerNamespace('thr', NAMESPACE_THREAD);
$xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
*/
private static function fetchRelated($related, $related_uri, $importer)
{
- $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $related_uri, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
- $conversation = dba::select('conversation', array('source', 'protocol'), $condition, array('limit' => 1));
+ $condition = ['`item-uri` = ? AND `protocol` IN (?, ?)', $related_uri, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON];
+ $conversation = dba::selectFirst('conversation', ['source', 'protocol'], $condition);
if (DBM::is_result($conversation)) {
$stored = true;
$xml = $conversation['source'];
if (!@$doc->loadHTML($related_data['body'])) {
return;
}
- $xpath = new DomXPath($doc);
+ $xpath = new DOMXPath($doc);
$atom_file = '';
// Finally we take the data that we fetched from "ostatus:conversation"
if ($xml == '') {
- $condition = array('item-uri' => $related_uri, 'protocol' => PROTOCOL_SPLITTED_CONV);
- $conversation = dba::select('conversation', array('source'), $condition, array('limit' => 1));
+ $condition = ['item-uri' => $related_uri, 'protocol' => PROTOCOL_SPLITTED_CONV];
+ $conversation = dba::selectFirst('conversation', ['source'], $condition);
if (DBM::is_result($conversation)) {
$stored = true;
logger('Got cached XML from conversation for URI '.$related_uri, LOGGER_DEBUG);
$orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
$orig_edited = $xpath->query('atom:updated/text()', $activityobjects)->item(0)->nodeValue;
- $orig_contact = $contact;
- $orig_author = self::fetchAuthor($xpath, $activityobjects, $importer, $orig_contact, false);
+ $orig_author = self::fetchAuthor($xpath, $activityobjects, $importer, $dummy, false);
$item["author-name"] = $orig_author["author-name"];
$item["author-link"] = $orig_author["author-link"];
/**
* @brief Adds the header elements to the XML document
*
- * @param object $doc XML document
- * @param array $owner Contact data of the poster
+ * @param object $doc XML document
+ * @param array $owner Contact data of the poster
+ * @param string $filter The related feed filter (activity, posts or comments)
*
* @return object header root element
*/
- private static function addHeader($doc, $owner)
+ private static function addHeader($doc, $owner, $filter)
{
$a = get_app();
$root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
$root->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
- $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
+ switch ($filter) {
+ case 'activity': $title = t('%s\'s timeline', $owner['name']); break;
+ case 'posts' : $title = t('%s\'s posts' , $owner['name']); break;
+ case 'comments': $title = t('%s\'s comments', $owner['name']); break;
+ }
+
+ $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION . "-" . DB_UPDATE_VERSION);
XML::addElement($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
- XML::addElement($doc, $root, "id", System::baseUrl()."/profile/".$owner["nick"]);
- XML::addElement($doc, $root, "title", sprintf("%s timeline", $owner["name"]));
+ XML::addElement($doc, $root, "id", System::baseUrl() . "/profile/" . $owner["nick"]);
+ XML::addElement($doc, $root, "title", $title);
XML::addElement($doc, $root, "subtitle", sprintf("Updates from %s on %s", $owner["name"], $a->config["sitename"]));
XML::addElement($doc, $root, "logo", $owner["photo"]);
XML::addElement($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
self::hublinks($doc, $root, $owner["nick"]);
- $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "salmon");
+ $attributes = array("href" => System::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "salmon");
XML::addElement($doc, $root, "link", "", $attributes);
- $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies");
+ $attributes = array("href" => System::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies");
XML::addElement($doc, $root, "link", "", $attributes);
- $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention");
+ $attributes = array("href" => System::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention");
XML::addElement($doc, $root, "link", "", $attributes);
- $attributes = array("href" => System::baseUrl()."/api/statuses/user_timeline/".$owner["nick"].".atom",
- "rel" => "self", "type" => "application/atom+xml");
+ $attributes = array("href" => System::baseUrl() . "/api/statuses/user_timeline/" . $owner["nick"] . ".atom",
+ "rel" => "self", "type" => "application/atom+xml");
XML::addElement($doc, $root, "link", "", $attributes);
return $root;
switch ($siteinfo["type"]) {
case 'photo':
- $imgdata = Photo::getInfoFromURL($siteinfo["image"]);
+ $imgdata = Image::getInfoFromURL($siteinfo["image"]);
$attributes = array("rel" => "enclosure",
"href" => $siteinfo["image"],
"type" => $imgdata["mime"],
}
if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
- $imgdata = Photo::getInfoFromURL($siteinfo["image"]);
+ $imgdata = Image::getInfoFromURL($siteinfo["image"]);
$attributes = array("rel" => "enclosure",
"href" => $siteinfo["image"],
"type" => $imgdata["mime"],
}
/**
+ * Creates the XML feed for a given nickname
+ *
+ * Supported filters:
+ * - activity (default): all the public posts
+ * - posts: all the public top-level posts
+ * - comments: all the public replies
+ *
+ * Updates the provided last_update parameter if the result comes from the
+ * cache or it is empty
+ *
* @brief Creates the XML feed for a given nickname
*
- * @param object $a The application class
* @param string $owner_nick Nickname of the feed owner
* @param string $last_update Date of the last update
* @param integer $max_items Number of maximum items to fetch
+ * @param string $filter Feed items filter (activity, posts or comments)
+ * @param boolean $nocache Wether to bypass caching
*
* @return string XML feed
*/
- public static function feed(App $a, $owner_nick, &$last_update, $max_items = 300)
+ public static function feed($owner_nick, &$last_update, $max_items = 300, $filter = 'activity', $nocache = false)
{
$stamp = microtime(true);
- $cachekey = "ostatus:feed:".$owner_nick.":".$last_update;
+ $cachekey = "ostatus:feed:" . $owner_nick . ":" . $filter . ":" . $last_update;
$previous_created = $last_update;
$result = Cache::get($cachekey);
- if (!is_null($result)) {
- logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created.' (cached)', LOGGER_DEBUG);
+ if (!$nocache && !is_null($result)) {
+ logger('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created . ' (cached)', LOGGER_DEBUG);
$last_update = $result['last_update'];
return $result['feed'];
}
- $r = q(
+ $owner = dba::fetch_first(
"SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
- WHERE `contact`.`self` AND `user`.`nickname` = '%s' LIMIT 1",
- dbesc($owner_nick)
+ WHERE `contact`.`self` AND `user`.`nickname` = ? LIMIT 1",
+ $owner_nick
);
- if (!DBM::is_result($r)) {
+ if (!DBM::is_result($owner)) {
return;
}
- $owner = $r[0];
-
if (!strlen($last_update)) {
$last_update = 'now -30 days';
}
$check_date = datetime_convert('UTC', 'UTC', $last_update, 'Y-m-d H:i:s');
$authorid = Contact::getIdForURL($owner["url"], 0);
+ $sql_extra = '';
+ if ($filter === 'posts') {
+ $sql_extra .= ' AND `item`.`id` = `item`.`parent` ';
+ }
+
+ if ($filter === 'comments') {
+ $sql_extra .= sprintf(" AND `item`.`object-type` = '%s' ", dbesc(ACTIVITY_OBJ_COMMENT));
+ }
+
$items = q(
"SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
- WHERE `item`.`uid` = %d AND `item`.`contact-id` = %d AND
- `item`.`author-id` = %d AND `item`.`created` > '%s' AND
- NOT `item`.`deleted` AND NOT `item`.`private` AND
- `thread`.`network` IN ('%s', '%s')
+ WHERE `item`.`uid` = %d
+ AND `item`.`contact-id` = %d
+ AND `item`.`author-id` = %d
+ AND `item`.`created` > '%s'
+ AND NOT `item`.`deleted`
+ AND NOT `item`.`private`
+ AND `thread`.`network` IN ('%s', '%s')
+ $sql_extra
ORDER BY `item`.`created` DESC LIMIT %d",
- intval($owner["uid"]), intval($owner["id"]),
- intval($authorid), dbesc($check_date),
- dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN), intval($max_items)
+ intval($owner["uid"]),
+ intval($owner["id"]),
+ intval($authorid),
+ dbesc($check_date),
+ dbesc(NETWORK_OSTATUS),
+ dbesc(NETWORK_DFRN),
+ intval($max_items)
);
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
- $root = self::addHeader($doc, $owner);
+ $root = self::addHeader($doc, $owner, $filter);
foreach ($items as $item) {
if (Config::get('system', 'ostatus_debug')) {
$msg = array('feed' => $feeddata, 'last_update' => $last_update);
Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR);
- logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created, LOGGER_DEBUG);
+ logger('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created, LOGGER_DEBUG);
return $feeddata;
}