Browse Source

Restructured "dba::exists" function

pull/3630/head
Michael 3 years ago
parent
commit
1d8c91c478
9 changed files with 33 additions and 35 deletions
  1. +2
    -2
      boot.php
  2. +18
    -10
      include/dba.php
  3. +1
    -4
      include/identity.php
  4. +1
    -2
      include/plugin.php
  5. +2
    -2
      include/remove_contact.php
  6. +1
    -6
      include/text.php
  7. +4
    -4
      mod/display.php
  8. +2
    -2
      mod/network.php
  9. +2
    -3
      mod/profile.php

+ 2
- 2
boot.php View File

@ -1090,9 +1090,9 @@ function proc_run($cmd) {
array_shift($argv);
$parameters = json_encode($argv);
$found = dba::select('workerqueue', array('id'), array('parameter' => $parameters, 'done' => false), array('limit' => 1));
$found = dba::exists('workerqueue', array('parameter' => $parameters, 'done' => false));
if (!dbm::is_result($found)) {
if (!$found) {
dba::insert('workerqueue', array('parameter' => $parameters, 'created' => $created, 'priority' => $priority));
}


+ 18
- 10
include/dba.php View File

@ -767,13 +767,19 @@ class dba {
/**
* @brief Check if data exists
*
* @param string $sql SQL statement
* @return boolean Are there rows for that query?
* @param string $table Table name
* @param array $condition array of fields for condition
*
* @return boolean Are there rows for that condition?
*/
static public function exists($sql) {
$params = self::getParam(func_get_args());
static public function exists($table, $condition) {
if (empty($table)) {
return false;
}
$stmt = self::p($sql, $params);
$fields = array_keys($condition);
$stmt = self::select($table, array($fields[0]), $condition, array('limit' => 1, 'only_query' => true));
if (is_bool($stmt)) {
$retval = $stmt;
@ -1270,11 +1276,13 @@ class dba {
$param_string = substr($param_string, 0, -2);
}
if (isset($params['limit'])) {
if (is_int($params['limit'])) {
$param_string .= " LIMIT ".$params['limit'];
$single_row =($params['limit'] == 1);
}
if (isset($params['limit']) && is_int($params['limit'])) {
$param_string .= " LIMIT ".$params['limit'];
$single_row = ($params['limit'] == 1);
}
if (isset($params['only_query']) && $params['only_query']) {
$single_row = !$params['only_query'];
}
$sql = "SELECT ".$select_fields." FROM `".$table."`".$condition_string.$param_string;


+ 1
- 4
include/identity.php View File

@ -245,10 +245,7 @@ function profile_sidebar($profile, $block = 0) {
$profile_url = normalise_link(App::get_baseurl()."/profile/".$profile["nickname"]);
}
$r = dba::select('contact', array('id'),
array('pending' => false, 'uid' => local_user(), 'nurl' => $profile_url), array('limit' => 1));
if (dbm::is_result($r))
$connect = false;
$connect = !dba::exists('contact', array('pending' => false, 'uid' => local_user(), 'nurl' => $profile_url));
}
if ($connect && ($profile['network'] != NETWORK_DFRN) && !isset($profile['remoteconnect']))


+ 1
- 2
include/plugin.php View File

@ -121,8 +121,7 @@ function reload_plugins() {
* @return boolean
*/
function plugin_enabled($plugin) {
$r = dba::select('addon', array('id'), array('installed' => true, 'name' => $plugin), array('limit' => 1));
return ((dbm::is_result($r)) && (count($r) > 0));
return dba::exists('addon', array('installed' => true, 'name' => $plugin));
}


+ 2
- 2
include/remove_contact.php View File

@ -14,8 +14,8 @@ function remove_contact_run($argv, $argc) {
$id = intval($argv[1]);
// Only delete if the contact doesn't exist (anymore)
$r = dba::select('contact', array('id'), array('id' => $id), array('limit' => 1));
if (dbm::is_result($r)) {
$r = dba::exists('contact', array('id' => $id));
if ($r) {
return;
}


+ 1
- 6
include/text.php View File

@ -488,8 +488,6 @@ if (! function_exists('item_new_uri')) {
function item_new_uri($hostname, $uid, $guid = "") {
do {
$dups = false;
if ($guid == "") {
$hash = get_guid(32);
} else {
@ -499,10 +497,7 @@ function item_new_uri($hostname, $uid, $guid = "") {
$uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
$r = dba::select('item', array('id'), array('uri' => $uri), array('limit' => 1));
if (dbm::is_result($r)) {
$dups = true;
}
$dups = dba::exists('item', array('uri' => $uri));
} while ($dups == true);
return $uri;


+ 4
- 4
mod/display.php View File

@ -292,8 +292,8 @@ function display_content(App $a, $update = 0) {
}
// We are displaying an "alternate" link if that post was public. See issue 2864
$items = dba::select('item', array('id'), array('id' => $item_id, 'private' => false, 'wall' => true));
if (dbm::is_result($items)) {
$is_public = dba::exists('item', array('id' => $item_id, 'private' => false, 'wall' => true));
if ($is_public) {
$alternate = App::get_baseurl().'/display/'.$nick.'/'.$item_id.'.atom';
} else {
$alternate = '';
@ -369,14 +369,14 @@ function display_content(App $a, $update = 0) {
$sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
if ($update) {
$r = dba::exists("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
$r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
$sql_extra AND `unseen`",
$a->profile['uid'],
$item_id
);
if (!$r) {
if (dba::num_rows($r) == 0) {
return '';
}
}


+ 2
- 2
mod/network.php View File

@ -793,9 +793,9 @@ function network_content(App $a, $update = 0) {
if (!$group && !$cid && !$star) {
$unseen = dba::select('item', array('id'), array('unseen' => true, 'uid' => local_user()), array('limit' => 1));
$unseen = dba::exists('item', array('unseen' => true, 'uid' => local_user()));
if (dbm::is_result($unseen)) {
if ($unseen) {
$r = dba::update('item', array('unseen' => false), array('uid' => local_user(), 'unseen' => true));
}
} elseif ($update_unseen) {


+ 2
- 3
mod/profile.php View File

@ -334,9 +334,8 @@ function profile_content(App $a, $update = 0) {
if ($is_owner) {
$unseen = dba::select('item', array('id'), array('wall' => true, 'unseen' => true, 'uid' => local_user()),
array('limit' => 1));
if (dbm::is_result($unseen)) {
$unseen = dba::exists('item', array('wall' => true, 'unseen' => true, 'uid' => local_user()));
if ($unseen) {
$r = dba::update('item', array('unseen' => false),
array('wall' => true, 'unseen' => true, 'uid' => local_user()));
}


Loading…
Cancel
Save