]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Search/Index.php
Merge pull request #10116 from mexon/mat/addon-console-command
[friendica.git] / src / Module / Search / Index.php
index 1f8bfe4bc2459a3275bbafcc385ebacfb4904fa0..e0050f405cd4fb873bedf3949c6a6cc509972552 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Module\Search;
 
@@ -9,12 +28,15 @@ use Friendica\Content\Widget;
 use Friendica\Core\Cache\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
+use Friendica\Core\Search;
 use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
-use Friendica\Model\Term;
+use Friendica\Model\ItemContent;
+use Friendica\Model\Post;
+use Friendica\Model\Tag;
 use Friendica\Module\BaseSearch;
 use Friendica\Network\HTTPException;
 use Friendica\Util\Strings;
@@ -61,7 +83,7 @@ class Index extends BaseSearch
                }
 
                if (local_user()) {
-                       DI::page()['aside'] .= Widget\SavedSearches::getHTML('search?q=' . urlencode($search), $search);
+                       DI::page()['aside'] .= Widget\SavedSearches::getHTML(Search::getSearchPath($search), $search);
                }
 
                Nav::setSelected('search');
@@ -111,6 +133,14 @@ class Index extends BaseSearch
                        }
                }
 
+               // Don't perform a fulltext or tag search on search results that look like an URL
+               // Tags don't look like an URL and the fulltext search does only work with natural words
+               if (parse_url($search, PHP_URL_SCHEME) && parse_url($search, PHP_URL_HOST)) {
+                       Logger::info('Skipping tag and fulltext search since the search looks like a URL.', ['q' => $search]);
+                       notice(DI::l10n()->t('No results.'));
+                       return $o;
+               }
+
                $tag = $tag || DI::config()->get('system', 'only_tag_search');
 
                // Here is the way permissions work in the search module...
@@ -118,57 +148,45 @@ class Index extends BaseSearch
                // OR your own posts if you are a logged in member
                // No items will be shown if the member has a blocked profile wall.
 
-               $pager = new Pager(DI::args()->getQueryString());
-
-               if ($tag) {
-                       Logger::info('Start tag search.', ['q' => $search]);
+               if (DI::mode()->isMobile()) {
+                       $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
+                               DI::config()->get('system', 'itemspage_network_mobile'));
+               } else {
+                       $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
+                               DI::config()->get('system', 'itemspage_network'));
+               }
 
-                       $condition = [
-                               "(`uid` = 0 OR (`uid` = ? AND NOT `global`))
-                               AND `otype` = ? AND `type` = ? AND `term` = ?",
-                               local_user(), Term::OBJECT_TYPE_POST, Term::HASHTAG, $search
-                       ];
-                       $params = [
-                               'order' => ['received' => true],
-                               'limit' => [$pager->getStart(), $pager->getItemsPerPage()]
-                       ];
-                       $terms = DBA::select('term', ['oid'], $condition, $params);
-
-                       $itemids = [];
-                       while ($term = DBA::fetch($terms)) {
-                               $itemids[] = $term['oid'];
-                       }
+               $last_uriid = isset($_GET['last_uriid']) ? intval($_GET['last_uriid']) : 0;
 
-                       DBA::close($terms);
+               $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
 
-                       if (!empty($itemids)) {
-                               $params = ['order' => ['id' => true]];
-                               $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
-                               $r = Item::inArray($items);
-                       } else {
-                               $r = [];
-                       }
+               if ($tag) {
+                       Logger::info('Start tag search.', ['q' => $search]);
+                       $uriids = Tag::getURIIdListByTag($search, local_user(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
+                       $count = Tag::countByTag($search, local_user());
                } else {
                        Logger::info('Start fulltext search.', ['q' => $search]);
+                       $uriids = Post\Content::getURIIdListBySearch($search, local_user(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
+                       $count = Post\Content::countBySearch($search, local_user());
+               }
 
-                       $condition = [
-                               "(`uid` = 0 OR (`uid` = ? AND NOT `global`))
-                               AND `body` LIKE CONCAT('%',?,'%')",
-                               local_user(), $search
-                       ];
-                       $params = [
-                               'order' => ['id' => true],
-                               'limit' => [$pager->getStart(), $pager->getItemsPerPage()]
-                       ];
-                       $items = Item::selectForUser(local_user(), [], $condition, $params);
-                       $r = Item::inArray($items);
+               if (!empty($uriids)) {
+                       $params = ['order' => ['id' => true], 'group_by' => ['uri-id']];
+                       $items = Post::toArray(Post::selectForUser(local_user(), Item::DISPLAY_FIELDLIST, ['uri-id' => $uriids], $params));
                }
 
-               if (!DBA::isResult($r)) {
-                       info(DI::l10n()->t('No results.'));
+               if (empty($items)) {
+                       if (empty($last_uriid)) {
+                               notice(DI::l10n()->t('No results.'));
+                       }
                        return $o;
                }
 
+               if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
+                       $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
+                       $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
+               }
+
                if ($tag) {
                        $title = DI::l10n()->t('Items tagged with: %s', $search);
                } else {
@@ -181,9 +199,14 @@ class Index extends BaseSearch
 
                Logger::info('Start Conversation.', ['q' => $search]);
 
-               $o .= conversation(DI::app(), $r, $pager, 'search', false, false, 'commented', local_user());
+               $o .= conversation(DI::app(), $items, 'search', false, false, 'commented', local_user());
+
+               if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
+                       $o .= HTML::scrollLoader();
+               } else {
+                       $o .= $pager->renderMinimal($count);
+               }
 
-               $o .= $pager->renderMinimal(count($r));
 
                return $o;
        }
@@ -226,13 +249,13 @@ class Index extends BaseSearch
                } else {
                        // Cheaper local lookup for anonymous users, no probe
                        if ($isAddr) {
-                               $contact = Contact::selectFirst(['id' => 'cid'], ['addr' => $search, 'uid' => 0]);
+                               $contact = Contact::selectFirst(['id'], ['addr' => $search, 'uid' => 0]);
                        } else {
-                               $contact = Contact::getDetailsByURL($search, 0, ['cid' => 0]);
+                               $contact = Contact::getByURL($search, null, ['id']) ?: ['id' => 0];
                        }
 
                        if (DBA::isResult($contact)) {
-                               $contact_id = $contact['cid'];
+                               $contact_id = $contact['id'];
                        }
                }
 
@@ -266,7 +289,7 @@ class Index extends BaseSearch
                }
 
                if (!empty($item_id)) {
-                       $item = Item::selectFirst(['guid'], ['id' => $item_id]);
+                       $item = Post::selectFirst(['guid'], ['id' => $item_id]);
                        if (DBA::isResult($item)) {
                                DI::baseUrl()->redirect('display/' . $item['guid']);
                        }