function elgg_solr_delete_entity($event, $type, $entity) { if (!elgg_instanceof($entity)) { return true; } if (!is_registered_entity_type($entity->type, $entity->getSubtype())) { return true; } elgg_solr_defer_index_delete($entity->guid); return true; }
function elgg_solr_add_update_user($entity) { if (!elgg_instanceof($entity, 'user')) { return false; } if (!is_registered_entity_type($entity->type, $entity->getSubtype())) { return false; } // lump public profile fields in with description $profile_fields = elgg_get_config('profile_fields'); $desc = ''; if (is_array($profile_fields) && sizeof($profile_fields) > 0) { $walled = elgg_get_config('walled_garden'); foreach ($profile_fields as $shortname => $valtype) { $md = elgg_get_metadata(array('guid' => $entity->guid, 'metadata_names' => array($shortname))); foreach ($md as $m) { if ($m->access_id == ACCESS_PUBLIC || $walled && $m->access_id == ACCESS_LOGGED_IN) { $desc .= $m->value . ' '; } } } } $client = elgg_solr_get_client(); $commit = elgg_get_config('elgg_solr_nocommit') ? false : true; $query = $client->createUpdate(); $subtype = $entity->getSubtype() ? $entity->getSubtype() : ''; // add document $doc = $query->createDocument(); $doc->id = $entity->guid; $doc->type = $entity->type; $doc->subtype = $subtype; $doc->owner_guid = $entity->owner_guid; $doc->container_guid = $entity->container_guid; $doc->access_id = $entity->access_id; $doc->title = elgg_strip_tags($entity->title); $doc->name = elgg_strip_tags($entity->name); $doc->username = $entity->username; $doc->description = elgg_strip_tags($desc); $doc->time_created = $entity->time_created; $doc = elgg_solr_add_tags($doc, $entity); $doc->enabled = $entity->enabled; $params = array('entity' => $entity); $doc = elgg_trigger_plugin_hook('elgg_solr:index', $entity->type, $params, $doc); if (!$doc) { return true; // a plugin has stopped the index } $query->addDocument($doc, true); if ($commit) { $query->addCommit(); } // this executes the query and returns the result try { $client->update($query); } catch (Exception $exc) { error_log($exc->getMessage()); } return true; }
/** * Determine if entity type/subtype is registered for search, * or is registered solr entity * * @see elgg_register_entity_type * @see elgg_solr_register_solr_entity_type * * @param string $type Entity type * @param string $subtype Entity subtype * @return bool */ function elgg_solr_is_registered_entity_type($type, $subtype = null) { if (is_registered_entity_type($type, $subtype)) { return true; } $solr_entities = elgg_get_config('solr_entities'); if ($subtype && isset($solr_entities[$type][$subtype])) { return true; } return false; }
$owner_guid = (int) elgg_get_page_owner_guid(); $site = elgg_get_site_entity(); $all_query = "SELECT distinct e.type, e.subtype, count(*) as count from " . $dbprefix . "entities e\n\twhere e.owner_guid = " . $owner_guid . " group by e.type, e.subtype"; $site_query = "SELECT distinct e.type, e.subtype, count(*) as count from " . $dbprefix . "entities e\n\twhere e.owner_guid = " . $owner_guid . " and e.site_guid = " . $site->guid . " group by e.type, e.subtype"; // Get a list of major types $all_results = get_data($all_query); $site_results = get_data($site_query); $rows = "<tr>"; $rows .= "<th> </th>"; if (elgg_is_admin_logged_in()) { $rows .= "<th>" . elgg_echo("all") . "</th>"; } $rows .= "<th>" . $site->name . "</th>"; $rows .= "</tr>"; foreach ($all_results as $row) { if (!elgg_is_admin_logged_in() && !is_registered_entity_type($row->type, get_subtype_from_id($row->subtype))) { // skip unsearchable entities for regular users continue; } if ($row->subtype) { $label = elgg_echo("item:" . $row->type . ":" . get_subtype_from_id($row->subtype)); } else { $label = elgg_echo("item:" . $row->type); } if (strpos($label, "item:") === 0) { if ($row->subtype) { $label = elgg_echo(get_subtype_from_id($row->subtype)); } else { $label = elgg_echo($row->type); } }
/** * check if a user is over the threshold for content creation * * @param string $event * @param string $object_type * @param ElggObject $object * @return boolean */ function create_check($event, $object_type, $object) { if (!$object instanceof \ElggEntity) { return true; } $user = elgg_get_logged_in_user_entity(); if (is_exempt($user)) { return true; } // only want to track content they are creating // some automated scripts may be triggered on their session // also allow messages if (is_registered_entity_type($object->type, $object->getSubtype()) && !elgg_instanceof($object, 'object', 'messages') && $object->owner_guid != $user->guid) { return true; } // reported content doesn't count (also this prevents an infinite loop...) if (elgg_instanceof($object, 'object', 'reported_content')) { return true; } // delete the content and warn them if they are on a suspension if ($user->spam_throttle_suspension > time()) { $timeleft = $user->spam_throttle_suspension - time(); $hours = ($timeleft - $timeleft % 3600) / 3600; $minutes = round($timeleft % 3600 / 60); register_error(elgg_echo('spam_throttle:suspended', array($hours, $minutes))); return false; } $typesubtype = $object->type; if ($object->getSubtype()) { $typesubtype .= ':' . $object->getSubtype(); } // They've made it this far, time to check if they've exceeded limits or not // first check for global setting $globallimit = (int) elgg_get_plugin_setting('global_limit', PLUGIN_ID); $globaltime = (int) elgg_get_plugin_setting('global_time', PLUGIN_ID); if ($globallimit && $globaltime) { // because 2 are created initially if (elgg_instanceof($object, 'object', 'messages')) { $globallimit++; } // we have globals set, lets give it a test $default_lowertime = time() - $globaltime * 60; $time_lower = max(array($default_lowertime, (int) $user->spam_throttle_unsuspended)); $params = array('type' => 'object', 'created_time_lower' => $time_lower, 'owner_guids' => array($user->guid), 'count' => true); $entitycount = elgg_get_entities($params); // note that some entity types may need to be counted slightly differently // eg. core messages plugin creates a entities on behalf of a recipient, so a direct count of objects is incorrect // let plugins chime in to make corrections for their entities $entitycount = elgg_trigger_plugin_hook('spam_throttle', 'entity_count:global', $params, $entitycount); if ($entitycount > $globallimit) { elgg_unregister_event_handler('shutdown', 'system', __NAMESPACE__ . '\\limit_exceeded'); elgg_register_event_handler('shutdown', 'system', __NAMESPACE__ . '\\limit_exceeded'); elgg_set_config('spam_throttle_reasons', array('type' => $typesubtype, 'created' => $entitycount)); return false; } } // if we're still going now we haven't exceeded globals, check for individual types $limit = (int) elgg_get_plugin_setting($typesubtype . '_limit', PLUGIN_ID); $time = (int) elgg_get_plugin_setting($typesubtype . '_time', PLUGIN_ID); if ($limit && $time) { // we have globals set, lets give it a test $default_lowertime = time() - $time * 60; $time_lower = max(array($default_lowertime, (int) $user->spam_throttle_unsuspended)); $params = array('type' => $object->type, 'created_time_lower' => $time_lower, 'owner_guids' => array($user->guid), 'count' => true); if ($object->getSubtype()) { $params['subtypes'] = array($object->getSubtype()); } $entitycount = elgg_get_entities($params); // note that some entity types may need to be counted slightly differently // eg. core messages plugin creates a entities on behalf of a recipient, so a direct count of objects is incorrect // let plugins chime in to make corrections for their entities $entitycount = elgg_trigger_plugin_hook('spam_throttle', 'entity_count', $params, $entitycount); if ($entitycount > $limit) { elgg_unregister_event_handler('shutdown', 'system', __NAMESPACE__ . '\\limit_exceeded'); elgg_register_event_handler('shutdown', 'system', __NAMESPACE__ . '\\limit_exceeded'); elgg_set_config('spam_throttle_reasons', array('type' => $typesubtype, 'created' => $entitycount, 'since' => $time_lower)); return false; } } return true; }