]> git.mxchange.org Git - friendica.git/blobdiff - mod/network.php
Merge remote-tracking branch 'upstream/develop' into relayed-dfrn-dspr
[friendica.git] / mod / network.php
index b493f7770c53b249ee76e4fbdbd434e665ed4cd7..23e03d03b0b724e133359050cdfff3fee13c383b 100644 (file)
@@ -48,8 +48,6 @@ function network_init(App $a)
                return;
        }
 
-       Hook::add('head', __FILE__, 'network_infinite_scroll_head');
-
        $is_a_date_query = false;
 
        $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
@@ -287,10 +285,17 @@ function network_content(App $a, $update = 0, $parent = 0)
        $arr = ['query' => DI::args()->getQueryString()];
        Hook::callAll('network_content_init', $arr);
 
+       if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll') && ($_GET['mode'] ?? '') != 'minimal') {
+               $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
+               $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
+       } else {
+               $o = '';
+       }
+
        if (!empty($_GET['file'])) {
-               $o = networkFlatView($a, $update);
+               $o .= networkFlatView($a, $update);
        } else {
-               $o = networkThreadedView($a, $update, $parent);
+               $o .= networkThreadedView($a, $update, $parent);
        }
 
        if (!$update && ($o === '')) {
@@ -499,19 +504,24 @@ function networkThreadedView(App $a, $update, $parent)
                $o .= status_editor($a, $x);
        }
 
-       $sql_table = $update ? '`item`' : '`thread`';
+       $conditionFields = ['uid' => local_user()];
+       $conditionStrings = [];
 
-       $sql_extra = ($star ? " AND `thread`.`starred` " : '') .
-               ($conv ? " AND $sql_table.`mention`" : '') .
-               ($nets ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
+       if ($star) {
+               $conditionFields['starred'] = true;
+       }
+       if ($conv) {
+               $conditionFields['mention'] = true;
+       }
+       if ($nets) {
+               $conditionFields['network'] = $nets;
+       }
 
        if ($datequery) {
-               $sql_extra .= Strings::protectSprintf(sprintf(" AND $sql_table.received <= '%s' ",
-                               DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
+               $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get())]);
        }
        if ($datequery2) {
-               $sql_extra .= Strings::protectSprintf(sprintf(" AND $sql_table.received >= '%s' ",
-                               DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
+               $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get())]);
        }
 
        if ($gid) {
@@ -525,7 +535,7 @@ function networkThreadedView(App $a, $update, $parent)
                        // NOTREACHED
                }
 
-               $sql_extra .= sprintf(" AND `thread`.`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = %d) ", intval($gid));
+               $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", $gid]);
 
                $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
                        '$title' => DI::l10n()->t('Group: %s', $group['name'])
@@ -533,7 +543,7 @@ function networkThreadedView(App $a, $update, $parent)
        } elseif ($cid) {
                $contact = Contact::getById($cid);
                if (DBA::isResult($contact)) {
-                       $sql_extra .= " AND " . $sql_table . ".`contact-id` = " . intval($cid);
+                       $conditionFields['contact-id'] = $cid;
 
                        $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
                                'contacts' => [ModuleContact::getContactTemplateVars($contact)],
@@ -558,8 +568,6 @@ function networkThreadedView(App $a, $update, $parent)
                $order_mode = 'commented';
        }
 
-       $sql_order = "$sql_table.$ordering";
-
        $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
 
        networkPager($a, $pager);
@@ -572,22 +580,22 @@ function networkThreadedView(App $a, $update, $parent)
        switch ($order_mode) {
                case 'received':
                        if ($last_received != '') {
-                               $sql_extra .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
+                               $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", $last_received]);
                        }
                        break;
                case 'commented':
                        if ($last_commented != '') {
-                               $sql_extra .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
+                               $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", $last_commented]);
                        }
                        break;
                case 'created':
                        if ($last_created != '') {
-                               $sql_extra .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
+                               $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", $last_created]);
                        }
                        break;
                case 'uriid':
                        if ($last_uriid > 0) {
-                               $sql_extra .= sprintf(" AND $sql_table.`uri-id` < '%s'", DBA::escape($last_uriid));
+                               $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", $last_uriid]);
                        }
                        break;
        }
@@ -596,47 +604,23 @@ function networkThreadedView(App $a, $update, $parent)
        if ($update) {
                if (!empty($parent)) {
                        // Load only a single thread
-                       $sql_extra2 = "`item`.`id` = ".intval($parent);
+                       $conditionFields['parent'] = $parent;
                } elseif ($order === 'post') {
                        // Only load new toplevel posts
-                       $sql_extra2 = "`item`.`unseen` AND `item`.`gravity` = " . GRAVITY_PARENT;
+                       $conditionFields['unseen'] = true;
+                       $conditionFields['gravity'] = GRAVITY_PARENT;
                } else {
                        // Load all unseen items
-                       $sql_extra2 = "`item`.`unseen`";
+                       $conditionFields['unseen'] = true;
                }
 
-               $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
-                       FROM `item`
-                       INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`
-                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
-                               AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-                       LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
-                       WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
-                       AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
-                       AND NOT `thread`.`moderated` AND $sql_extra2
-                       $sql_extra
-                       ORDER BY `order_date` DESC LIMIT 100",
-                       intval(local_user()),
-                       intval(local_user())
-               );
+               $params = ['order' => [$order_mode => true], 'limit' => 100];
+               $table = 'network-item-view';
        } else {
-               $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
-                       FROM `thread`
-                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
-                               AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-                       STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
-                       LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
-                       WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
-                       AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
-                       AND NOT `thread`.`moderated`
-                       $sql_extra
-                       ORDER BY `order_date` DESC LIMIT %d, %d",
-                       intval(local_user()),
-                       intval(local_user()),
-                       intval($pager->getStart()),
-                       intval($pager->getItemsPerPage())
-               );
+               $params = ['order' => [$order_mode => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
+               $table = 'network-thread-view';
        }
+       $r = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
 
        return $o . network_display_post($a, $pager, (!$gid && !$cid && !$star), $update, $ordering, $r);
 }
@@ -649,16 +633,14 @@ function network_display_post($a, $pager, $mark_all, $update, $ordering, $items)
                $parents_arr = [];
 
                foreach ($items as $item) {
-                       if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
-                               $parents_arr[] = $item['item_id'];
+                       if (!in_array($item['parent'], $parents_arr) && ($item['parent'] > 0)) {
+                               $parents_arr[] = $item['parent'];
                        }
                }
                $parents_str = implode(', ', $parents_arr);
        }
 
-       $query_string = DI::args()->getQueryString();
-
-       $pager->setQueryString($query_string);
+       $pager->setQueryString(DI::args()->getQueryString());
 
        // We aren't going to try and figure out at the item, group, and page
        // level which items you've seen and which you haven't. If you're looking
@@ -755,34 +737,3 @@ function network_tabs(App $a)
 
        // --- end item filter tabs
 }
-
-/**
- * Network hook into the HTML head to enable infinite scroll.
- *
- * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
- * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
- * networkThreadedView or networkFlatView.
- *
- * @param App     $a
- * @param  string $htmlhead The head tag HTML string
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- * @global Pager  $pager
- */
-function network_infinite_scroll_head(App $a, &$htmlhead)
-{
-       /// @TODO this will have to be converted to a static property of the converted Module\Network class
-       /**
-        * @var $pager Pager
-        */
-       global $pager;
-
-       if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')
-               && ($_GET['mode'] ?? '') != 'minimal'
-       ) {
-               $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
-               $htmlhead .= Renderer::replaceMacros($tpl, [
-                       '$pageno'     => $pager->getPage(),
-                       '$reload_uri' => $pager->getBaseQueryString()
-               ]);
-       }
-}