friendica-github/src/Model/Post/Content.php

135 lines
3.6 KiB
PHP
Raw Normal View History

2021-01-30 22:03:53 +00:00
<?php
2024-08-24 15:27:00 +02:00
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2021-01-30 22:03:53 +00:00
namespace Friendica\Model\Post;
use \BadMethodCallException;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
2024-01-17 19:46:22 +00:00
use Friendica\Model\Item;
2021-01-30 22:03:53 +00:00
use Friendica\Model\Post;
class Content
{
/**
* Insert a new post-content entry
*
* @param integer $uri_id
* @param array $fields
* @return bool success
* @throws \Exception
*/
public static function insert(int $uri_id, array $data = [])
{
if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id');
}
2022-07-13 00:23:12 +02:00
$fields = DI::dbaDefinition()->truncateFieldsForTable('post-content', $data);
2021-01-30 22:03:53 +00:00
// Additionally assign the key fields
$fields['uri-id'] = $uri_id;
return DBA::insert('post-content', $fields, Database::INSERT_IGNORE);
}
/**
* Update a post content entry
*
* @param integer $uri_id
* @param array $data
* @param bool $insert_if_missing
* @return bool
* @throws \Exception
*/
public static function update(int $uri_id, array $data = [], bool $insert_if_missing = false)
{
if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id');
}
2022-07-13 00:23:12 +02:00
$fields = DI::dbaDefinition()->truncateFieldsForTable('post-content', $data);
2021-01-30 22:03:53 +00:00
// Remove the key fields
unset($fields['uri-id']);
if (empty($fields)) {
return true;
}
return DBA::update('post-content', $fields, ['uri-id' => $uri_id], $insert_if_missing ? true : []);
}
/**
* Delete a row from the post-content table
*
* @param array $conditions Field condition(s)
* @param array $options
* - cascade: If true we delete records in other tables that depend on the one we're deleting through
* relations (default: true)
*
* @return boolean was the delete successful?
* @throws \Exception
*/
public static function delete(array $conditions, array $options = [])
{
return DBA::delete('post-content', $conditions, $options);
}
/**
* 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)
{
2024-01-17 19:46:22 +00:00
$search = Post\Engagement::escapeKeywords($search);
if ($uid != 0) {
$condition = ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE) AND (NOT `restricted` OR `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `uid` = ?))", $search, $uid];
2024-01-17 19:46:22 +00:00
} else {
$condition = ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE) AND NOT `restricted`", $search];
2024-01-17 19:46:22 +00:00
}
2021-01-30 22:03:53 +00:00
if (!empty($last_uriid)) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
}
$params = [
'order' => ['uri-id' => true],
'limit' => [$start, $limit]
];
$tags = DBA::select(SearchIndex::getSearchTable(), ['uri-id'], $condition, $params);
2021-01-30 22:03:53 +00:00
$uriids = [];
while ($tag = DBA::fetch($tags)) {
$uriids[] = $tag['uri-id'];
}
DBA::close($tags);
return $uriids;
}
public static function countBySearch(string $search, int $uid = 0)
{
2024-01-17 19:46:22 +00:00
$search = Post\Engagement::escapeKeywords($search);
if ($uid != 0) {
$condition = ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE) AND (NOT `restricted` OR `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `uid` = ?))", $search, $uid];
2024-01-17 19:46:22 +00:00
} else {
$condition = ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE) AND NOT `restricted`", $search];
2024-01-17 19:46:22 +00:00
}
return DBA::count(SearchIndex::getSearchTable(), $condition);
2021-01-30 22:03:53 +00:00
}
}