Inheritance: use trait Profilable
Example #1
0
 /**
  * Gets a configuration value
  *
  * Plugin authors should use elgg_get_config().
  *
  * @note Internal: These settings are stored in the dbprefix_config table and read
  * during system boot into $CONFIG.
  *
  * @param string $name The name of the config value
  *
  * @return mixed|null
  */
 function get($name)
 {
     $name = trim($name);
     // check for deprecated values.
     // @todo might be a better spot to define this?
     $new_name = false;
     switch ($name) {
         case 'pluginspath':
             $new_name = 'plugins_path';
             break;
         case 'sitename':
             $new_name = 'site_name';
             break;
     }
     // @todo these haven't really been implemented in Elgg 1.8. Complete in 1.9.
     // show dep message
     if ($new_name) {
         //	$msg = "Config value $name has been renamed as $new_name";
         $name = $new_name;
         //	elgg_deprecated_notice($msg, $dep_version);
     }
     // decide from where to return the value
     if (isset($this->CONFIG->{$name})) {
         return $this->CONFIG->{$name};
     }
     $sql = "\n\t\t\tSELECT value\n\t\t\tFROM {$this->CONFIG->dbprefix}config\n\t\t\tWHERE name = :name\n\t\t";
     $params[':name'] = $name;
     $result = $this->db->getDataRow($sql, null, $params);
     if ($result) {
         $result = unserialize($result->value);
         $this->CONFIG->{$name} = $result;
         return $result;
     }
     return null;
 }
Example #2
0
 /**
  * Returns SQL appropriate for relationship joins and wheres
  *
  * @todo add support for multiple relationships and guids.
  *
  * @param string $column               Column name the GUID should be checked against.
  *                                     Provide in table.column format.
  * @param string $relationship         Type of the relationship
  * @param int    $relationship_guid    Entity GUID to check
  * @param bool   $inverse_relationship Is $relationship_guid the target of the relationship?
  *
  * @return mixed
  * @access private
  */
 public function getEntityRelationshipWhereSql($column, $relationship = null, $relationship_guid = null, $inverse_relationship = false)
 {
     if ($relationship == null && $relationship_guid == null) {
         return '';
     }
     $wheres = array();
     $joins = array();
     $group_by = '';
     if ($inverse_relationship) {
         $joins[] = "JOIN {$this->db->getTablePrefix()}entity_relationships r on r.guid_one = {$column}";
     } else {
         $joins[] = "JOIN {$this->db->getTablePrefix()}entity_relationships r on r.guid_two = {$column}";
     }
     if ($relationship) {
         $wheres[] = "r.relationship = '" . $this->db->sanitizeString($relationship) . "'";
     }
     if ($relationship_guid) {
         if ($inverse_relationship) {
             $wheres[] = "r.guid_two = '{$relationship_guid}'";
         } else {
             $wheres[] = "r.guid_one = '{$relationship_guid}'";
         }
     } else {
         // See #5775. Queries that do not include a relationship_guid must be grouped by entity table alias,
         // otherwise the result set is not unique
         $group_by = $column;
     }
     if ($where_str = implode(' AND ', $wheres)) {
         return array('wheres' => array("({$where_str})"), 'joins' => $joins, 'group_by' => $group_by);
     }
     return '';
 }
Example #3
0
 /**
  * Deletes all private settings for an entity
  *
  * @param int $entity_guid The Entity GUID
  * @return bool
  */
 public function removeAllForEntity($entity_guid)
 {
     $this->cache->clear($entity_guid);
     _elgg_services()->boot->invalidateCache();
     $query = "\n\t\t\tDELETE FROM {$this->table}\n\t\t\tWHERE entity_guid = :entity_guid\n\t\t";
     $params = [':entity_guid' => (int) $entity_guid];
     return $this->db->deleteData($query, $params);
 }
Example #4
0
 /**
  * Deletes all private settings for an entity
  *
  * @param int $entity_guid The Entity GUID
  * @return bool
  */
 function removeAllForEntity($entity_guid)
 {
     $entity_guid = (int) $entity_guid;
     $entity = $this->entities->get($entity_guid);
     if (!$entity instanceof \ElggEntity) {
         return false;
     }
     return $this->db->deleteData("DELETE FROM {$this->table}\n\t\t\tWHERE entity_guid = {$entity_guid}");
 }
Example #5
0
 /**
  * Check to see if a user has already created an annotation on an object
  *
  * @param int    $entity_guid     Entity guid
  * @param string $annotation_type Type of annotation
  * @param int    $owner_guid      Defaults to logged in user.
  *
  * @return bool
  */
 function exists($entity_guid, $annotation_type, $owner_guid = null)
 {
     if (!$owner_guid && !($owner_guid = $this->session->getLoggedInUserGuid())) {
         return false;
     }
     $sql = "SELECT id FROM {$this->db->prefix}annotations\n\t\t\t\tWHERE owner_guid = :owner_guid\n\t\t\t\tAND entity_guid = :entity_guid\n\t\t\t\tAND name = :annotation_type";
     $result = $this->db->getDataRow($sql, null, [':owner_guid' => (int) $owner_guid, ':entity_guid' => (int) $entity_guid, ':annotation_type' => $annotation_type]);
     return (bool) $result;
 }
Example #6
0
 /**
  * Get a populated cache object
  *
  * @return array
  */
 protected function getPopulatedCache()
 {
     if ($this->cache === null) {
         $rows = $this->db->getData("\n\t\t\t\tSELECT *\n\t\t\t\tFROM {$this->db->prefix}entity_subtypes\n\t\t\t");
         $this->cache = [];
         foreach ($rows as $row) {
             $this->cache[$row->id] = $row;
         }
     }
     return $this->cache;
 }
Example #7
0
 /**
  * Load entire datalist in memory.
  * 
  * This could cause OOM problems if the datalists table is large.
  * 
  * @todo make a list of datalists that we want to get in one grab
  * 
  * @return array
  * @access private
  */
 function loadAll()
 {
     $result = $this->db->getData("SELECT * FROM {$this->table}");
     $map = array();
     if (is_array($result)) {
         foreach ($result as $row) {
             $map[$row->name] = $row->value;
             $this->cache->put($row->name, $row->value);
         }
     }
     return $map;
 }
Example #8
0
 /**
  * When an entity is updated, resets the access ID on all of its child metadata
  *
  * @param string      $event       The name of the event
  * @param string      $object_type The type of object
  * @param \ElggEntity $object      The entity itself
  *
  * @return true
  * @access private Set as private in 1.9.0
  */
 function handleUpdate($event, $object_type, $object)
 {
     if ($object instanceof \ElggEntity) {
         if (!$this->isMetadataIndependent($object->getType(), $object->getSubtype())) {
             $access_id = (int) $object->access_id;
             $guid = (int) $object->getGUID();
             $query = "update {$this->table} set access_id = {$access_id} where entity_guid = {$guid}";
             $this->db->updateData($query);
         }
     }
     return true;
 }
Example #9
0
 /**
  * Get subscription records from the database
  *
  * Records are an object with two vars: guid and methods with the latter
  * being a comma-separated list of subscription relationship names.
  *
  * @param int $container_guid The GUID of the subscription target
  * @return array
  */
 protected function getSubscriptionRecords($container_guid)
 {
     $container_guid = $this->db->sanitizeInt($container_guid);
     // create IN clause
     $rels = $this->getMethodRelationships();
     if (!$rels) {
         return array();
     }
     array_walk($rels, array($this->db, 'sanitizeString'));
     $methods_string = "'" . implode("','", $rels) . "'";
     $db_prefix = $this->db->prefix;
     $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods\n\t\t\tFROM {$db_prefix}entity_relationships\n\t\t\tWHERE guid_two = {$container_guid} AND\n\t\t\t\t\trelationship IN ({$methods_string}) GROUP BY guid_one";
     return $this->db->getData($query);
 }
Example #10
0
 /**
  * Disables all entities owned and contained by a user (or another entity)
  *
  * @param int $owner_guid The owner GUID
  * @return bool
  */
 public function disableEntities($owner_guid)
 {
     $entity = get_entity($owner_guid);
     if (!$entity || !$entity->canEdit()) {
         return false;
     }
     if (!$this->events->trigger('disable', $entity->type, $entity)) {
         return false;
     }
     $query = "\n\t\t\tUPDATE {$this->table}entities\n\t\t\tSET enabled='no'\n\t\t\tWHERE owner_guid = :owner_guid\n\t\t\tOR container_guid = :owner_guid";
     $params = [':owner_guid' => (int) $owner_guid];
     _elgg_invalidate_cache_for_entity($entity->guid);
     _elgg_invalidate_memcache_for_entity($entity->guid);
     if ($this->db->updateData($query, true, $params)) {
         return true;
     }
     return false;
 }
Example #11
0
 /**
  * Removes user $guid's admin flag.
  *
  * @param int $user_guid User GUID
  * @return bool
  */
 public function removeAdmin($user_guid)
 {
     $user = get_entity($user_guid);
     if (!$user instanceof ElggUser || !$user->canEdit()) {
         return false;
     }
     if (!$this->events->trigger('remove_admin', 'user', $user)) {
         return false;
     }
     $query = "\n\t\t\tUPDATE {$this->table}\n\t\t\tSET admin = 'no'\n\t\t\tWHERE guid = :guid\n\t\t";
     $params = [':guid' => (int) $user_guid];
     _elgg_invalidate_cache_for_entity($user_guid);
     _elgg_invalidate_memcache_for_entity($user_guid);
     if ($this->db->updateData($query, true, $params)) {
         return true;
     }
     return false;
 }
Example #12
0
 /**
  * {@inheritDoc}
  */
 public function gc($max_lifetime)
 {
     $life = time() - $max_lifetime;
     $query = "DELETE FROM {$this->db->getTablePrefix()}users_sessions WHERE ts < '{$life}'";
     return (bool) $this->db->deleteData($query);
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function sanitizeString($value)
 {
     return $this->db->sanitizeString($value);
 }
Example #14
0
 /**
  * The full name of the metastrings table, including prefix.
  * 
  * @return string
  */
 public function getTableName()
 {
     return $this->db->getTablePrefix() . "metastrings";
 }
Example #15
0
 /**
  * Get the number of queries performed since the object was constructed
  *
  * @return int # of queries
  */
 public function getDelta()
 {
     return $this->db->getQueryCount() - $this->initial;
 }
Example #16
0
 /**
  * {@inheritdoc}
  */
 public function __construct(Config $config, Logger $logger = null)
 {
     parent::__construct($config, $logger);
     $this->test = TestCase::getInstance();
 }
Example #17
0
 /**
  * Populate the boot data
  *
  * @param \stdClass      $config   Elgg CONFIG object
  * @param \Elgg\Database $db       Elgg database
  * @param EntityTable    $entities Entities service
  * @param Plugins        $plugins  Plugins service
  *
  * @return void
  * @throws \InstallationException
  */
 public function populate(\stdClass $config, Database $db, EntityTable $entities, Plugins $plugins)
 {
     // get subtypes
     $rows = $db->getData("\n\t\t\tSELECT *\n\t\t\tFROM {$db->prefix}entity_subtypes\n\t\t");
     foreach ($rows as $row) {
         $this->subtype_data[$row->id] = $row;
     }
     // get config
     $rows = $db->getData("\n\t\t\tSELECT *\n\t\t\tFROM {$db->prefix}config\n\t\t");
     foreach ($rows as $row) {
         $this->config_values[$row->name] = unserialize($row->value);
     }
     if (!array_key_exists('installed', $this->config_values)) {
         // try to fetch from old pre 3.0 datalists table
         // need to do this to be able to perform an upgrade from 2.x to 3.0
         try {
             $rows = $db->getData("\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM {$db->prefix}datalists\n\t\t\t\t");
             foreach ($rows as $row) {
                 $value = $row->value;
                 if ($row->name == 'processed_upgrades') {
                     // config table already serializes data so no need to double serialize
                     $value = unserialize($value);
                 }
                 $this->config_values[$row->name] = $value;
             }
         } catch (\Exception $e) {
         }
     }
     // get site entity
     $this->site = $entities->get($this->config_values['default_site'], 'site');
     if (!$this->site) {
         throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down.");
     }
     // get plugins
     $this->active_plugins = $plugins->find('active');
     // get plugin settings
     if (!$this->active_plugins) {
         return;
     }
     // find GUIDs with not too many private settings
     $guids = array_map(function (\ElggPlugin $plugin) {
         return $plugin->guid;
     }, $this->active_plugins);
     // find plugin GUIDs with not too many settings
     $limit = 40;
     $set = implode(',', $guids);
     $sql = "\n\t\t\tSELECT entity_guid\n\t\t\tFROM {$db->prefix}private_settings\n\t\t\tWHERE entity_guid IN ({$set})\n\t\t\t  AND name NOT LIKE 'plugin:user_setting:%'\n\t\t\t  AND name NOT LIKE 'elgg:internal:%'\n\t\t\tGROUP BY entity_guid\n\t\t\tHAVING COUNT(*) > {$limit}\n\t\t";
     $unsuitable_guids = $db->getData($sql, function ($row) {
         return (int) $row->entity_guid;
     });
     $guids = array_values($guids);
     $guids = array_diff($guids, $unsuitable_guids);
     if ($guids) {
         // get the settings
         $set = implode(',', $guids);
         $rows = $db->getData("\n\t\t\t\tSELECT entity_guid, `name`, `value`\n\t\t\t\tFROM {$db->prefix}private_settings\n\t\t\t\tWHERE entity_guid IN ({$set})\n\t\t\t\t  AND name NOT LIKE 'plugin:user_setting:%'\n\t\t\t\t  AND name NOT LIKE 'elgg:internal:%'\n\t\t\t\tORDER BY entity_guid\n\t\t\t");
         // make sure we show all entities as loaded
         $this->plugin_settings = array_fill_keys($guids, []);
         foreach ($rows as $i => $row) {
             $this->plugin_settings[$row->entity_guid][$row->name] = $row->value;
         }
     }
 }
Example #18
0
 /**
  * Checks if mutex is locked
  *
  * @param string $namespace Namespace to use for the database table
  * @return bool
  */
 public function isLocked($namespace)
 {
     $this->assertNamespace($namespace);
     return (bool) count($this->db->getData("SHOW TABLES LIKE '{$this->db->getTablePrefix()}{$namespace}_lock'"));
 }