]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/ItemContent.php
Merge pull request #9411 from tobiasd/20201011-de
[friendica.git] / src / Model / ItemContent.php
index 21dbd34d0a4feb7a7e62d5631c51992123933056..5268c49e953a6fa7b52bc4f7218bc2ddf27aea79 100644 (file)
@@ -1,23 +1,79 @@
 <?php
-
 /**
- * @file src/Model/ItemContent.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\Model;
 
-use Friendica\BaseObject;
 use Friendica\Content\Text;
-use Friendica\Core\PConfig;
-
-require_once 'boot.php';
-require_once 'include/items.php';
-require_once 'include/text.php';
+use Friendica\Content\Text\BBCode;
+use Friendica\Core\Protocol;
+use Friendica\Database\DBA;
+use Friendica\DI;
 
-class ItemContent extends BaseObject
+class ItemContent
 {
        /**
-        * @brief Convert a message into plaintext for connectors to other networks
+        * Search posts for given content
+        *
+        * @param string $search
+        * @param integer $uid
+        * @param integer $start
+        * @param integer $limit
+        * @param integer $last_uriid
+        * @return array
+        */
+       public static function getURIIdListBySearch(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
+       {
+               $condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
+                       AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
+
+               if (!empty($last_uriid)) {
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
+               }
+
+               $params = [
+                       'order' => ['uri-id' => true],
+                       'group_by' => ['uri-id'],
+                       'limit' => [$start, $limit]
+               ];
+
+               $tags = DBA::select('item', ['uri-id'], $condition, $params);
+
+               $uriids = [];
+               while ($tag = DBA::fetch($tags)) {
+                       $uriids[] = $tag['uri-id'];
+               }
+               DBA::close($tags);
+
+               return $uriids;
+       }
+
+       public static function countBySearch(string $search, int $uid = 0)
+       {
+               $condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
+                       AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
+               return DBA::count('item', $condition);
+       }
+
+       /**
+        * Convert a message into plaintext for connectors to other networks
         *
         * @param array  $item           The message array that is about to be posted
         * @param int    $limit          The maximum number of characters when posting to that network
@@ -25,11 +81,12 @@ class ItemContent extends BaseObject
         * @param int    $htmlmode       This controls the behavior of the BBCode conversion
         * @param string $target_network Name of the network where the post should go to.
         *
-        * @see \Friendica\Content\Text\BBCode::getAttachedData
-        *
         * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @see   \Friendica\Content\Text\BBCode::getAttachedData
+        *
         */
-       public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = '')
+       public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::API, $target_network = '')
        {
                // Remove hashtags
                $URLSearchString = '^\[\]';
@@ -67,15 +124,14 @@ class ItemContent extends BaseObject
                        }
                } else {// Try to guess the correct target network
                        switch ($htmlmode) {
-                               case 8:
-                                       $abstract = Text\BBCode::getAbstract($item['body'], NETWORK_TWITTER);
-                                       break;
-                               case 7:
-                                       $abstract = Text\BBCode::getAbstract($item['body'], NETWORK_STATUSNET);
+                               case BBCode::TWITTER:
+                                       $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
                                        break;
-                               case 6:
-                                       $abstract = Text\BBCode::getAbstract($item['body'], NETWORK_APPNET);
+
+                               case BBCode::OSTATUS:
+                                       $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
                                        break;
+
                                default: // We don't know the exact target.
                                        // We fetch an abstract since there is a posting limit.
                                        if ($limit > 0) {
@@ -93,7 +149,7 @@ class ItemContent extends BaseObject
                        }
                }
 
-               $html = Text\BBCode::convert($post['text'] . $post['after'], false, $htmlmode);
+               $html = Text\BBCode::convert($post['text'] . ($post['after'] ?? ''), false, $htmlmode);
                $msg = Text\HTML::toPlaintext($html, 0, true);
                $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
 
@@ -102,7 +158,7 @@ class ItemContent extends BaseObject
                        if ($post['type'] == 'link') {
                                $link = $post['url'];
                        } elseif ($post['type'] == 'text') {
-                               $link = $post['url'];
+                               $link = $post['url'] ?? '';
                        } elseif ($post['type'] == 'video') {
                                $link = $post['url'];
                        } elseif ($post['type'] == 'photo') {
@@ -128,8 +184,8 @@ class ItemContent extends BaseObject
                                        $msg = trim(str_replace($link, '', $msg));
                                } elseif (($limit == 0) || ($pos < $limit)) {
                                        // The limit has to be increased since it will be shortened - but not now
-                                       // Only do it with Twitter (htmlmode = 8)
-                                       if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
+                                       // Only do it with Twitter
+                                       if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == BBCode::TWITTER)) {
                                                $limit = $limit - 23 + strlen($link);
                                        }
 
@@ -162,7 +218,7 @@ class ItemContent extends BaseObject
                                        $post['url'] = $item['plink'];
                                } elseif (strpos($item['body'], '[share') !== false) {
                                        $post['url'] = $item['plink'];
-                               } elseif (PConfig::get($item['uid'], 'system', 'no_intelligent_shortening')) {
+                               } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
                                        $post['url'] = $item['plink'];
                                }
                                $msg = Text\Plaintext::shorten($msg, $limit);