streams/mod/network.php

493 lines
15 KiB
PHP
Raw Normal View History

2012-06-15 00:46:58 +00:00
<?php
require_once('include/items.php');
2013-12-06 04:34:39 +00:00
require_once('include/group.php');
require_once('include/contact_widgets.php');
require_once('include/conversation.php');
require_once('include/acl_selectors.php');
2012-06-15 00:46:58 +00:00
function network_init(&$a) {
2015-01-29 04:56:04 +00:00
if(! local_channel()) {
2012-06-15 00:46:58 +00:00
notice( t('Permission denied.') . EOL);
return;
}
2015-03-12 23:20:29 +00:00
if((count($_GET) < 2) || (count($_GET) < 3 && $_GET['JS'])) {
$network_options = get_pconfig(local_channel(),'system','network_page_default');
if($network_options)
goaway('network' . '?f=&' . $network_options);
}
$channel = $a->get_channel();
2015-01-29 04:56:04 +00:00
$a->profile_uid = local_channel();
head_set_icon($channel['xchan_photo_s']);
}
function network_content(&$a, $update = 0, $load = false) {
2015-01-29 04:56:04 +00:00
if(! local_channel()) {
$_SESSION['return_url'] = $a->query_string;
2014-11-17 03:08:10 +00:00
return login(false);
}
$arr = array('query' => $a->query_string);
call_hooks('network_content_init', $arr);
$channel = $a->get_channel();
2014-12-15 22:37:31 +00:00
$datequery = $datequery2 = '';
$group = 0;
$nouveau = false;
$datequery = ((x($_GET,'dend') && is_a_date_arg($_GET['dend'])) ? notags($_GET['dend']) : '');
$datequery2 = ((x($_GET,'dbegin') && is_a_date_arg($_GET['dbegin'])) ? notags($_GET['dbegin']) : '');
$nouveau = ((x($_GET,'new')) ? intval($_GET['new']) : 0);
$gid = ((x($_GET,'gid')) ? intval($_GET['gid']) : 0);
$category = ((x($_REQUEST,'cat')) ? $_REQUEST['cat'] : '');
$hashtags = ((x($_REQUEST,'tag')) ? $_REQUEST['tag'] : '');
2015-01-14 02:41:11 +00:00
$verb = ((x($_REQUEST,'verb')) ? $_REQUEST['verb'] : '');
2014-12-15 22:37:31 +00:00
$search = (($_GET['search']) ? $_GET['search'] : '');
if($search) {
if(strpos($search,'@') === 0) {
$r = q("select abook_id from abook left join xchan on abook_xchan = xchan_hash where xchan_name = '%s' and abook_channel = %d limit 1",
dbesc(substr($search,1)),
2015-01-29 04:56:04 +00:00
intval(local_channel())
);
if($r) {
$_GET['cid'] = $r[0]['abook_id'];
$search = $_GET['search'] = '';
}
}
elseif(strpos($search,'#') === 0) {
2014-12-15 22:37:31 +00:00
$hashtags = substr($search,1);
$search = $_GET['search'] = '';
}
}
if($datequery)
$_GET['order'] = 'post';
2014-11-17 03:08:10 +00:00
// filter by collection (e.g. group)
if($gid) {
2014-11-17 03:08:10 +00:00
$r = q("SELECT * FROM groups WHERE id = %d AND uid = %d LIMIT 1",
intval($gid),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2014-11-17 03:08:10 +00:00
);
if(! $r) {
if($update)
killme();
notice( t('No such group') . EOL );
goaway($a->get_baseurl(true) . '/network');
// NOTREACHED
}
2014-11-17 03:08:10 +00:00
$group = $gid;
$group_hash = $r[0]['hash'];
2014-11-17 03:08:10 +00:00
$def_acl = array('allow_gid' => '<' . $r[0]['hash'] . '>');
}
2012-06-15 00:46:58 +00:00
$o = '';
2012-11-09 07:04:34 +00:00
// if no tabs are selected, defaults to comments
2012-06-15 00:46:58 +00:00
2014-11-17 03:08:10 +00:00
$cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
$star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
$order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
$liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
$conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
$spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
$cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
$cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
$firehose = ((x($_GET,'fh')) ? intval($_GET['fh']) : 0);
$file = ((x($_GET,'file')) ? $_GET['file'] : '');
2012-06-15 00:46:58 +00:00
if(x($_GET,'search') || x($_GET,'file'))
$nouveau = true;
if($cid)
$def_acl = array('allow_cid' => '<' . intval($cid) . '>');
if(! $update) {
$o .= network_tabs();
// search terms header
if($search)
$o .= '<h2>' . t('Search Results For:') . ' ' . htmlspecialchars($search, ENT_COMPAT,'UTF-8') . '</h2>';
2012-06-15 00:46:58 +00:00
nav_set_selected('network');
$channel_acl = array(
'allow_cid' => $channel['channel_allow_cid'],
'allow_gid' => $channel['channel_allow_gid'],
2014-11-17 03:08:10 +00:00
'deny_cid' => $channel['channel_deny_cid'],
'deny_gid' => $channel['channel_deny_gid']
);
2012-06-15 00:46:58 +00:00
$x = array(
2014-11-17 03:08:10 +00:00
'is_owner' => true,
'allow_location' => ((intval(get_pconfig($channel['channel_id'],'system','use_browser_location'))) ? '1' : ''),
'default_location' => $channel['channel_location'],
2014-11-17 03:08:10 +00:00
'nickname' => $channel['channel_address'],
'lockstate' => (($group || $cid || $channel['channel_allow_cid'] || $channel['channel_allow_gid'] || $channel['channel_deny_cid'] || $channel['channel_deny_gid']) ? 'lock' : 'unlock'),
'acl' => populate_acl((($group || $cid) ? $def_acl : $channel_acl)),
'bang' => (($group || $cid) ? '!' : ''),
'visitor' => true,
2015-01-29 04:56:04 +00:00
'profile_uid' => local_channel()
2012-06-15 00:46:58 +00:00
);
$o .= status_editor($a,$x);
}
// We don't have to deal with ACL's on this page. You're looking at everything
// that belongs to you, hence you can see all of it. We will filter by group if
2014-11-17 03:08:10 +00:00
// desired.
2012-06-15 00:46:58 +00:00
2014-11-17 03:08:10 +00:00
$sql_options = (($star)
? " and (item_flags & " . intval(ITEM_STARRED) . ") > 0"
: '');
2012-06-15 00:46:58 +00:00
2012-07-21 10:48:59 +00:00
$sql_nets = '';
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE (item_flags & " . intval(ITEM_THREAD_TOP) . ")>0 $sql_options ) ";
2012-06-15 00:46:58 +00:00
if($group) {
2013-09-18 11:06:44 +00:00
$contact_str = '';
2014-11-17 03:08:10 +00:00
$contacts = group_get_members($group);
if($contacts) {
2013-09-18 11:06:44 +00:00
foreach($contacts as $c) {
if($contact_str)
$contact_str .= ',';
2014-11-17 03:08:10 +00:00
$contact_str .= "'" . $c['xchan'] . "'";
2013-09-18 11:06:44 +00:00
}
2014-11-17 03:08:10 +00:00
}
else {
$contact_str = ' 0 ';
info( t('Collection is empty'));
2014-11-17 03:08:10 +00:00
}
2014-11-17 03:08:10 +00:00
$sql_extra = " AND item.parent IN ( SELECT DISTINCT parent FROM item WHERE true $sql_options AND (( author_xchan IN ( $contact_str ) OR owner_xchan in ( $contact_str )) or allow_gid like '" . protect_sprintf('%<' . dbesc($group_hash) . '>%') . "' ) and id = parent and item_restrict = 0 ) ";
2015-01-29 04:56:04 +00:00
$x = group_rec_byhash(local_channel(), $group_hash);
2013-12-06 03:07:59 +00:00
if($x)
$o = '<h2>' . t('Collection: ') . $x['name'] . '</h2>' . $o;
2014-11-17 03:08:10 +00:00
}
2012-06-15 00:46:58 +00:00
elseif($cid) {
2014-11-17 03:08:10 +00:00
$r = q("SELECT abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash where abook_id = %d and abook_channel = %d and not ( abook_flags & " . intval(ABOOK_FLAG_BLOCKED) . ") > 0 limit 1",
intval($cid),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2014-11-17 03:08:10 +00:00
);
if($r) {
2015-01-29 04:56:04 +00:00
$sql_extra = " AND item.parent IN ( SELECT DISTINCT parent FROM item WHERE true $sql_options AND uid = " . intval(local_channel()) . " AND ( author_xchan = '" . dbesc($r[0]['abook_xchan']) . "' or owner_xchan = '" . dbesc($r[0]['abook_xchan']) . "' ) and item_restrict = 0 ) ";
2013-09-29 09:47:36 +00:00
$o = '<h2>' . t('Connection: ') . $r[0]['xchan_name'] . '</h2>' . $o;
2014-11-17 03:08:10 +00:00
}
else {
2013-09-29 09:47:36 +00:00
notice( t('Invalid connection.') . EOL);
2012-06-15 00:46:58 +00:00
goaway($a->get_baseurl(true) . '/network');
2014-11-17 03:08:10 +00:00
}
2012-06-15 00:46:58 +00:00
}
2014-12-15 22:37:31 +00:00
if(x($category)) {
$sql_extra .= protect_sprintf(term_query('item', $category, TERM_CATEGORY));
}
if(x($hashtags)) {
$sql_extra .= protect_sprintf(term_query('item', $hashtags, TERM_HASHTAG));
}
2012-06-15 00:46:58 +00:00
if(! $update) {
// The special div is needed for liveUpdate to kick in for this page.
2014-11-17 03:08:10 +00:00
// We only launch liveUpdate if you aren't filtering in some incompatible
2012-06-15 00:46:58 +00:00
// way and also you aren't writing a comment (discovered in javascript).
2014-06-18 04:51:20 +00:00
if($gid || $cid || $cmin || ($cmax != 99) || $star || $liked || $conv || $spam || $nouveau || $list)
2014-11-17 03:08:10 +00:00
$firehose = 0;
2015-01-29 04:56:04 +00:00
$maxheight = get_pconfig(local_channel(),'system','network_divmore_height');
if(! $maxheight)
$maxheight = 400;
2014-06-18 04:51:20 +00:00
$o .= '<div id="live-network"></div>' . "\r\n";
2015-01-29 04:56:04 +00:00
$o .= "<script> var profile_uid = " . local_channel()
. "; var profile_page = " . $a->pager['page']
. "; divmore_height = " . intval($maxheight) . "; </script>\r\n";
$a->page['htmlhead'] .= replace_macros(get_markup_template("build_query.tpl"),array(
'$baseurl' => z_root(),
2014-11-17 03:08:10 +00:00
'$pgtype' => 'network',
2015-01-29 04:56:04 +00:00
'$uid' => ((local_channel()) ? local_channel() : '0'),
2014-11-17 03:08:10 +00:00
'$gid' => (($gid) ? $gid : '0'),
'$cid' => (($cid) ? $cid : '0'),
'$cmin' => (($cmin) ? $cmin : '0'),
'$cmax' => (($cmax) ? $cmax : '0'),
'$star' => (($star) ? $star : '0'),
'$liked' => (($liked) ? $liked : '0'),
'$conv' => (($conv) ? $conv : '0'),
'$spam' => (($spam) ? $spam : '0'),
'$fh' => (($firehose) ? $firehose : '0'),
'$nouveau' => (($nouveau) ? $nouveau : '0'),
2014-11-17 03:08:10 +00:00
'$wall' => '0',
'$list' => ((x($_REQUEST,'list')) ? intval($_REQUEST['list']) : 0),
'$page' => (($a->pager['page'] != 1) ? $a->pager['page'] : 1),
'$search' => (($search) ? $search : ''),
'$order' => $order,
'$file' => $file,
2014-12-15 22:37:31 +00:00
'$cats' => $category,
'$tags' => $hashtags,
2014-11-17 03:08:10 +00:00
'$dend' => $datequery,
'$mid' => '',
2015-01-14 02:41:11 +00:00
'$verb' => $verb,
2014-11-17 03:08:10 +00:00
'$dbegin' => $datequery2
));
2012-06-15 00:46:58 +00:00
}
$sql_extra3 = '';
if($datequery) {
$sql_extra3 .= protect_sprintf(sprintf(" AND item.created <= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery))));
}
if($datequery2) {
$sql_extra3 .= protect_sprintf(sprintf(" AND item.created >= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery2))));
}
2014-11-17 03:08:10 +00:00
$sql_extra2 = (($nouveau) ? '' : " AND item.parent = item.id ");
2012-06-15 00:46:58 +00:00
$sql_extra3 = (($nouveau) ? '' : $sql_extra3);
if(x($_GET,'search')) {
$search = escape_tags($_GET['search']);
2014-11-17 03:08:10 +00:00
if(strpos($search,'#') === 0) {
$sql_extra .= term_query('item',substr($search,1),TERM_HASHTAG);
2014-11-17 03:08:10 +00:00
}
else {
$sql_extra .= sprintf(" AND item.body like '%s' ",
dbesc(protect_sprintf('%' . $search . '%'))
2012-06-15 00:46:58 +00:00
);
2014-11-17 03:08:10 +00:00
}
2012-06-15 00:46:58 +00:00
}
2015-01-14 02:41:11 +00:00
if($verb) {
$sql_extra .= sprintf(" AND item.verb like '%s' ",
dbesc(protect_sprintf('%' . $verb . '%'))
);
}
2012-06-15 00:46:58 +00:00
if(strlen($file)) {
$sql_extra .= term_query('item',$file,TERM_FILE);
2012-06-15 00:46:58 +00:00
}
if($conv) {
2014-11-17 03:08:10 +00:00
$sql_extra .= sprintf(" AND parent IN (SELECT distinct(parent) from item where ( author_xchan like '%s' or ( item_flags & %d ) > 0)) ",
dbesc(protect_sprintf($channel['channel_hash'])),
2013-01-13 03:05:14 +00:00
intval(ITEM_MENTIONSME)
);
2012-06-15 00:46:58 +00:00
}
2012-07-25 05:06:21 +00:00
if($update && ! $load) {
2012-06-15 00:46:58 +00:00
// only setup pagination on initial page view
$pager_sql = '';
}
else {
2015-01-29 04:56:04 +00:00
$itemspage = get_pconfig(local_channel(),'system','itemspage');
$a->set_pager_itemspage(((intval($itemspage)) ? $itemspage : 20));
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$pager_sql = sprintf(" LIMIT %d OFFSET %d ", intval($a->pager['itemspage']), intval($a->pager['start']));
2012-06-15 00:46:58 +00:00
}
if(($cmin != 0) || ($cmax != 99)) {
// Not everybody who shows up in the network stream will be in your address book.
// By default those that aren't are assumed to have closeness = 99; but this isn't
2014-11-17 03:08:10 +00:00
// recorded anywhere. So if cmax is 99, we'll open the search up to anybody in
// the stream with a NULL address book entry.
$sql_nets .= " AND ";
if($cmax == 99)
$sql_nets .= " ( ";
$sql_nets .= "( abook.abook_closeness >= " . intval($cmin) . " ";
$sql_nets .= " AND abook.abook_closeness <= " . intval($cmax) . " ) ";
if($cmax == 99)
$sql_nets .= " OR abook.abook_closeness IS NULL ) ";
}
2014-03-30 22:01:31 +00:00
if($firehose && (! get_config('system','disable_discover_tab'))) {
require_once('include/identity.php');
$sys = get_sys_channel();
$uids = " and item.uid = " . intval($sys['channel_id']) . " ";
$a->data['firehose'] = intval($sys['channel_id']);
$abook_uids = "";
}
else {
2015-01-29 04:56:04 +00:00
$uids = " and item.uid = " . local_channel() . " ";
$abook_uids = " and abook.abook_channel = " . local_channel() . " ";
}
2015-01-29 04:56:04 +00:00
if(get_pconfig(local_channel(),'system','network_list_mode'))
$page_mode = 'list';
else
$page_mode = 'client';
2015-02-13 02:49:42 +00:00
$simple_update = (($update) ? " and item_unseen = 1 " : '');
// This fixes a very subtle bug so I'd better explain it. You wake up in the morning or return after a day
2014-11-17 03:08:10 +00:00
// or three and look at your matrix page - after opening up your browser. The first page loads just as it
// should. All of a sudden a few seconds later, page 2 will get inserted at the beginning of the page
// (before the page 1 content). The update code is actually doing just what it's supposed
// to, it's fetching posts that have the ITEM_UNSEEN bit set. But the reason that page 2 content is being
2014-11-17 03:08:10 +00:00
// returned in an UPDATE is because you hadn't gotten that far yet - you're still on page 1 and everything
// that we loaded for page 1 is now marked as seen. But the stuff on page 2 hasn't been. So... it's being
2014-11-17 03:08:10 +00:00
// treated as "new fresh" content because it is unseen. We need to distinguish it somehow from content
// which "arrived as you were reading page 1". We're going to do this
// by storing in your session the current UTC time whenever you LOAD a network page, and only UPDATE items
// which are both ITEM_UNSEEN and have "changed" since that time. Cross fingers...
2014-11-17 03:08:10 +00:00
if($update && $_SESSION['loadtime'])
$simple_update .= " and item.changed > '" . datetime_convert('UTC','UTC',$_SESSION['loadtime']) . "' ";
2012-07-25 05:06:21 +00:00
if($load)
$simple_update = '';
2012-06-15 00:46:58 +00:00
2012-07-25 05:06:21 +00:00
if($nouveau && $load) {
2012-06-15 00:46:58 +00:00
// "New Item View" - show all items unthreaded in reverse created date order
2012-10-09 01:40:30 +00:00
2014-11-17 03:08:10 +00:00
$items = q("SELECT item.*, item.id AS item_id, received FROM item
left join abook on item.author_xchan = abook.abook_xchan
WHERE true $uids $abook_uids AND item_restrict = 0
and ((abook.abook_flags & %d) = 0 or abook.abook_flags is null)
2012-06-15 00:46:58 +00:00
$simple_update
$sql_extra $sql_nets
ORDER BY item.received DESC $pager_sql ",
intval(ABOOK_FLAG_BLOCKED)
2012-06-15 00:46:58 +00:00
);
2012-10-09 01:40:30 +00:00
2012-07-17 12:30:32 +00:00
require_once('include/items.php');
2012-10-09 01:40:30 +00:00
xchan_query($items);
2013-02-11 08:20:14 +00:00
$items = fetch_post_tags($items,true);
2012-06-15 00:46:58 +00:00
}
elseif($update) {
2012-07-26 05:55:43 +00:00
2012-06-15 00:46:58 +00:00
// Normal conversation view
if($order === 'post')
2014-11-17 03:08:10 +00:00
$ordering = "created";
2012-06-15 00:46:58 +00:00
else
2014-11-17 03:08:10 +00:00
$ordering = "commented";
2012-06-15 00:46:58 +00:00
if($load) {
$_SESSION['loadtime'] = datetime_convert();
// Fetch a page full of parent items for this page
2014-11-17 03:08:10 +00:00
$r = q("SELECT distinct item.id AS item_id, $ordering FROM item
left join abook on item.author_xchan = abook.abook_xchan
WHERE true $uids $abook_uids AND item.item_restrict = 0
AND item.parent = item.id
and ((abook.abook_flags & %d) = 0 or abook.abook_flags is null)
$sql_extra3 $sql_extra $sql_nets
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
ORDER BY $ordering DESC $pager_sql ",
intval(ABOOK_FLAG_BLOCKED)
2012-06-15 00:46:58 +00:00
);
2012-10-09 01:40:30 +00:00
}
2012-06-15 00:46:58 +00:00
else {
2014-11-17 03:08:10 +00:00
if(! $firehose) {
// update
$r = q("SELECT item.parent AS item_id FROM item
left join abook on item.author_xchan = abook.abook_xchan
WHERE true $uids $abook_uids AND item.item_restrict = 0 $simple_update
and ((abook.abook_flags & %d) = 0 or abook.abook_flags is null)
$sql_extra3 $sql_extra $sql_nets ",
intval(ABOOK_FLAG_BLOCKED)
);
}
2012-06-15 00:46:58 +00:00
}
2013-01-05 03:26:53 +00:00
2012-06-15 00:46:58 +00:00
// Then fetch all the children of the parents that are on this page
$parents_str = '';
$update_unseen = '';
2012-06-15 00:46:58 +00:00
if($r) {
2012-08-30 06:03:03 +00:00
$parents_str = ids_to_querystr($r,'item_id');
2012-06-15 00:46:58 +00:00
2014-11-17 03:08:10 +00:00
$items = q("SELECT item.*, item.id AS item_id FROM item
WHERE true $uids AND item.item_restrict = 0
AND item.parent IN ( %s )
$sql_extra ",
2012-06-15 00:46:58 +00:00
dbesc($parents_str)
);
2015-01-29 04:56:04 +00:00
xchan_query($items,true,(($firehose) ? local_channel() : 0));
2013-02-11 08:20:14 +00:00
$items = fetch_post_tags($items,true);
2014-09-23 05:03:19 +00:00
$items = conv_sort($items,$ordering);
2014-11-17 03:08:10 +00:00
}
else {
2012-06-15 00:46:58 +00:00
$items = array();
}
if($page_mode === 'list') {
/**
* in "list mode", only mark the parent item and any like activities as "seen".
* We won't distinguish between comment likes and post likes. The important thing
* is that the number of unseen comments will be accurate. The SQL to separate the
* comment likes could also get somewhat hairy.
*/
if($parents_str) {
$update_unseen = " AND ( id IN ( " . dbesc($parents_str) . " )";
$update_unseen .= " OR ( parent IN ( " . dbesc($parents_str) . " ) AND verb in ( '" . dbesc(ACTIVITY_LIKE) . "','" . dbesc(ACTIVITY_DISLIKE) . "' ))) ";
}
}
else {
if($parents_str) {
$update_unseen = " AND parent IN ( " . dbesc($parents_str) . " )";
}
}
2012-06-15 00:46:58 +00:00
}
if(($update_unseen) && (! $firehose))
$r = q("UPDATE item SET item_unseen = 0 where item_unseen = 1 AND uid = %d $update_unseen ",
2015-01-29 04:56:04 +00:00
intval(local_channel())
2012-06-15 00:46:58 +00:00
);
$mode = (($nouveau) ? 'network-new' : 'network');
$o .= conversation($a,$items,$mode,$update,$page_mode);
2012-06-15 00:46:58 +00:00
2014-11-17 03:08:10 +00:00
if(($items) && (! $update))
$o .= alt_pager($a,count($items));
2012-06-15 00:46:58 +00:00
return $o;
}