private function get_stat($stat, $limit = null, midgard_datetime $since = null) { $qb = new midgard_query_builder('fi_openkeidas_diary_stat'); $qb->add_constraint('person', '=', midgardmvc_core::get_instance()->authentication->get_person()->id); $qb->add_constraint('stat', '=', $stat); $qb->add_order('date', 'DESC'); if (!is_null($limit)) { $qb->set_limit($limit); } if (!is_null($since)) { $qb->add_constraint('date', '>', $since); } $stats = $qb->execute(); if (empty($stats)) { if ($limit == 1) { return 0; } return array(date('d.m.Y') => 0); } if ($limit == 1) { return round($stats[0]->value, 1); } $values = array(); foreach ($stats as $stat) { $values[$stat->date->format('d.m.Y')] = round($stat->value, 1); } return $values; }
public function get_active(array $args) { $qb = new midgard_query_builder('fi_openkeidas_groups_group'); $qb->set_limit(5); $qb->add_order('metadata.score', 'DESC'); $this->data['groups'] = array_map('fi_openkeidas_groups_controllers_groups::add_members_for_group', $qb->execute()); }
public function get_top(array $args) { $qb = new midgard_query_builder('fi_openkeidas_diary_activity'); $qb->set_limit(5); $qb->add_order('metadata.score', 'DESC'); $this->data['sports'] = $qb->execute(); }
private function prepare_qb(midgardmvc_core_node $node, $limit, $offset = 0) { $qb = new midgard_query_builder('fi_openkeidas_articles_article'); $qb->add_constraint('node', 'INTREE', $node->id); if (!midgardmvc_ui_create_injector::can_use()) { // Regular user, hide unapproved articles // TODO: This check should be moved to authentication service when QB has signals $qb->add_constraint('metadata.isapproved', '=', true); } $qb->add_order('metadata.created', 'DESC'); $qb->set_limit($limit); $qb->set_offset($offset); return $qb; }
public function _on_execute() { debug_add('called!'); midcom::get('dbclassloader')->load_classes('midcom', 'core_classes.inc', null, true); $qb = new midgard_query_builder('midcom_core_login_session_db'); $qb->add_constraint('timestamp', '<', time() - $GLOBALS['midcom_config']['auth_login_session_timeout']); $qb->set_limit(500); $result = $qb->execute(); foreach ($result as $tmp) { if (!$tmp->delete()) { // Print and log error $msg = "Failed to delete login session {$tmp->id}, last Midgard error was: " . midcom_connection::get_error_string(); $this->print_error($msg); debug_add($msg, MIDCOM_LOG_ERROR); debug_print_r('Tried to delete this object:', $tmp); } else { $tmp->purge(); debug_add("Deleted login session {$tmp->id}."); } } }
/** * Get document list for requested sequences */ public function get_docs_by_seq(array $args) { //$this->authenticate(); // List the requested sequence data $qb = new midgard_query_builder('midgard_sequence'); if (isset($_MIDCOM->dispatcher->get['startkey'])) { $qb->add_constraint('id', '>', (int) $_MIDCOM->dispatcher->get['startkey']); } if (isset($_MIDCOM->dispatcher->get['limit'])) { $qb->set_limit((int) $_MIDCOM->dispatcher->get['limit']); } $qb->add_order('id', 'ASC'); $transactions = $qb->execute(); // Prepare the data list $this->data['total_rows'] = count($transactions); $this->data['offset'] = 0; $this->data['rows'] = array(); // Map the transaction data to what CouchDb protocol wants foreach ($transactions as $transaction) { $this->data['rows'][] = array('id' => $transaction->objectguid, 'key' => $transaction->id, 'value' => array('rev' => $transaction->revision . '-' . $transaction->metadata->revised->format('U'))); } }
public function _on_execute() { debug_add('called!'); $cut_off = mktime(23, 59, 59, date('n'), date('j') - $GLOBALS['midcom_config']['cron_purge_deleted_after'], date('Y')); foreach (midcom_connection::get_schema_types() as $mgdschema) { if (substr($mgdschema, 0, 2) == '__') { continue; } debug_add("Processing class {$mgdschema}"); $qb = new midgard_query_builder($mgdschema); $qb->add_constraint('metadata.deleted', '<>', 0); $qb->add_constraint('metadata.revised', '<', gmdate('Y-m-d H:i:s', $cut_off)); $qb->include_deleted(); $qb->set_limit(500); $objects = $qb->execute(); unset($qb); if (!is_array($objects)) { debug_add("QB failed fatally on class {$mgdschema}, errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR); continue; } $found = count($objects); $purged = 0; foreach ($objects as $obj) { if (!$obj->purge()) { debug_add("Failed to purge {$mgdschema} {$obj->guid}, deleted: {$obj->metadata->deleted}, revised: {$obj->metadata->revised}. errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR); debug_print_r('Failed object', $obj); continue; } $purged++; } if ($found > 0) { debug_add("Found {$found} {$mgdschema} objects deleted before " . date('Y-m-d H:i:s', $cut_off) . ", purged {$purged}", MIDCOM_LOG_INFO); } else { debug_add("No {$mgdschema} objects deleted before " . date('Y-m-d H:i:s', $cut_off) . " found"); } } }
/** * Checks if there are any objects (for the mode given in constructor) that need to be processed * * Note: does not check user's privileges or that the objects actually exist (the cleanup cronjob * handles dangling references) * @return boolean indicating need for processing (merge/not duplicate) */ function merge_needed() { switch ($this->_object_mode) { case 'person': break; case 'group': break; default: // object mode not set properly $this->_errstr = 'invalid object mode'; return false; break; } $qb = new midgard_query_builder('midgard_parameter'); $qb->add_constraint('domain', '=', 'org.openpsa.contacts.duplicates:possible_duplicate'); $qb->add_order('name', 'ASC'); $qb->set_limit(1); $results = @$qb->execute(); if (!empty($results)) { return true; } return false; }
public static function get_location_for_person(midgard_person $person, midgard_datetime $when = null) { $qb = new midgard_query_builder('midgardmvc_helper_location_log'); $qb->add_constraint('person', '=', $person->id); if (!is_null($when)) { $qb->add_constraint('metadata.published', '<=', $when); } $qb->set_limit(1); $qb->add_order('metadata.published', 'DESC'); $logs = $qb->execute(); foreach ($logs as $log) { $spot = new midgardmvc_helper_location_spot($log); $spot->source = $log->importer; $spot->text = $log->text; return $spot; } return null; }
public function get_node_by_component($component) { if (isset(midgardmvc_core_providers_hierarchy_node_midgard2::$nodes_by_component[$component])) { return midgardmvc_core_providers_hierarchy_node_midgard2::$nodes_by_component[$component]; } $qb = new midgard_query_builder('midgardmvc_core_node'); $qb->add_constraint('component', '=', $component); $qb->begin_group('OR'); $qb->add_constraint('up', 'INTREE', self::$root_node_id); $qb->add_constraint('id', '=', self::$root_node_id); $qb->end_group(); $qb->set_limit(1); $nodes = $qb->execute(); if (count($nodes) == 0) { return null; } return midgardmvc_core_providers_hierarchy_node_midgard2::get_instance($nodes[0]); }
<?php $midgard = new midgard_connection(); $midgard->open('midgard'); @ini_set('max_execution_time', 0); // Figure out when sequence was updated last time $qb = new midgard_query_builder('midgard_sequence'); $qb->set_limit(1); $qb->add_order('id', 'DESC'); $transactions = $qb->execute(); if (count($transactions) == 0) { $latest_sequence = new midgard_datetime('0000-00-00'); } else { foreach ($transactions as $transaction) { $latest_sequence = $transaction->metadata->created; } } $sequence = array(); foreach ($_MIDGARD['schema']['types'] as $type => $val) { if (substr($type, 0, 2) == '__') { continue; } // Updated objects $qb = new midgard_query_builder($type); $qb->add_constraint('metadata_revised', '>', $latest_sequence); $qb->add_order('metadata_revised', 'ASC'); $objects = $qb->execute(); foreach ($objects as $object) { $revised = $object->metadata->revised->format(DATE_ATOM); $sequence["{$revised}_{$object->guid}"] = array('objectguid' => $object->guid, 'type' => $type, 'revision' => $object->metadata->revision); }
/** * Generates an URL for given route_id with given arguments * * @param string $route_id the id of the route to generate a link for * @param array $args associative arguments array * @return string url */ public function generate_url($route_id, array $args, midgardmvc_core_node $page = null, $component = null) { static $pages_for_component = array(); if (is_null($page) && !is_null($component)) { if (!isset($pages_for_component[$component])) { // Find a page matching the requested component $qb = new midgard_query_builder('midgardmvc_core_node'); $qb->add_constraint('component', '=', $component); $qb->begin_group('OR'); $qb->add_constraint('up', 'INTREE', $this->midgardmvc->context->root_page->id); $qb->add_constraint('id', '=', $this->midgardmvc->context->root_page->id); $qb->end_group(); $qb->set_limit(1); $pages = $qb->execute(); if (empty($pages)) { throw new OutOfBoundsException("No page matching component {$component} found"); } $pages_for_component[$component] = $pages[0]; } $page = $pages_for_component[$component]; } if (!is_null($page)) { $this->midgardmvc->context->create(); $request = new midgardmvc_core_helpers_request(); $request->set_page($page); $request->populate_context(); $this->initialize($request); } $route_definitions = $this->get_routes(); if (!isset($route_definitions[$route_id])) { throw new OutOfBoundsException("route_id '{$route_id}' not found in routes configuration in context " . $this->midgardmvc->context->get_current_context()); } $route = $route_definitions[$route_id]['route']; $link = $route; foreach ($args as $key => $value) { if (is_array($value)) { $value_array = array(); foreach ($value as $part) { if (empty($part)) { continue; } $value_array[] = $part; } $value = implode('.', $value_array); // This is a token replacement, add the type hint $key = "token:{$key}"; } $link = str_replace("{\${$key}}", $value, $link); } if (preg_match_all('%\\{$(.+?)\\}%', $link, $link_matches)) { throw new UnexpectedValueException("Missing arguments matching route '{$route_id}' of {$this->midgardmvc->core->component}: " . implode(', ', $link_remaining_args)); } if (!is_null($page)) { $url = preg_replace('%/{2,}%', '/', $this->midgardmvc->context->uri . $link); $this->midgardmvc->context->delete(); return $url; } return preg_replace('%/{2,}%', '/', $this->midgardmvc->context->uri . $link); }
/** * Get an object, deleted or not * * @param string $guid GUID of the object * @param string $type MgdSchema type * @return mixed MgdSchema object */ public static function get_object($guid, $type) { static $objects = array(); if (!isset($objects[$guid])) { $qb = new midgard_query_builder($type); $qb->add_constraint('guid', '=', $guid); // We know we want/need only one result $qb->set_limit(1); $qb->include_deleted(); $results = $qb->execute(); if (count($results) == 0) { $objects[$guid] = null; } else { $objects[$guid] = $results[0]; } } return $objects[$guid]; }
public function action_stream_page($route_id, &$data, $args) { $qb = new midgard_query_builder('net_nemein_avaudu_message'); $qb->set_limit((int) $this->configuration->get('messages_per_page')); $qb->set_offset((int) $args['page']); $qb->add_order('metadata.published', 'DESC'); $messages = $qb->execute(); $data['statuses'] = array(); foreach ($messages as $message) { $data['statuses'][] = net_nemein_avaudu_controllers_timeline::message2status($message); } // TODO: New MidCOM does this way more gracefully header('Content-type: application/json'); echo json_encode($data); die; }
/** * @param mixed $handler_id The ID of the handler. * @param Array $args The argument list. * @param Array &$data The local request data. */ public function _handler_sidebyside($handler_id, array $args, array &$data) { $this->set_active_leaf('persons_merge'); midcom::get('auth')->require_valid_user(); // Process the selection if present. if (!empty($_POST['org_openpsa_contacts_handler_duplicates_person_keep']) && !empty($_POST['org_openpsa_contacts_handler_duplicates_person_options']) && count($_POST['org_openpsa_contacts_handler_duplicates_person_options']) == 2) { $option1 = new org_openpsa_contacts_person_dba($_POST['org_openpsa_contacts_handler_duplicates_person_options'][1]); $option2 = new org_openpsa_contacts_person_dba($_POST['org_openpsa_contacts_handler_duplicates_person_options'][2]); foreach ($_POST['org_openpsa_contacts_handler_duplicates_person_keep'] as $keep => $dummy) { switch (true) { case $keep == 'both': $option1->require_do('midgard:update'); $option2->require_do('midgard:update'); if (!$option1->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option2->guid, time()) || !$option2->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option1->guid, time())) { $errstr = midcom_connection::get_error_string(); // Failed to set as not duplicate, clear parameters that might have been set $option1->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option2->guid, ''); $option2->parameter('org.openpsa.contacts.duplicates:not_duplicate', $option1->guid, ''); // TODO: Localize midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.contacts'), "Failed to mark #{$option1->id} and # {$option2->id} as not duplicates, errstr: {$errstr}", 'error'); // Switch is a "loop" so we continue 2 levels to get out of the foreach as well continue 2; } // Clear the possible duplicate parameters $option1->parameter('org.openpsa.contacts.duplicates:possible_duplicate', $option2->guid, ''); $option2->parameter('org.openpsa.contacts.duplicates:possible_duplicate', $option1->guid, ''); // TODO: Localize midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.contacts'), "Keeping both \"{$option1->name}\" and \"{$option2->name}\", they will not be marked as duplicates in the future", 'ok'); // Switch is a "loop" so we continue 2 levels to get out of the foreach as well continue 2; // Safety break break; case $keep == $option1->guid: $person1 =& $option1; $person2 =& $option2; break; case $keep == $option2->guid: $person1 =& $option2; $person2 =& $option1; break; default: throw new midcom_error('Something weird happened (basically we got bogus data)'); } $person1->require_do('midgard:update'); $person2->require_do('midgard:delete'); // TODO: Merge person2 data to person1 and then delete person2 $merger = new org_openpsa_contacts_duplicates_merge('person'); if (!$merger->merge_delete($person1, $person2)) { // TODO: Localize midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.contacts'), 'Merge failed, errstr: ' . $merger->errstr(), 'error'); } } //PONDER: redirect to avoid reloading the POST in case user presses reload ?? } // Then find us next pair we have sufficient rights for... $this->_request_data['notfound'] = false; $this->_request_data['person1'] = false; $this->_request_data['person2'] = false; $this->_request_data['loop_i'] = 0; $i =& $this->_request_data['loop_i']; if (isset($_REQUEST['org_openpsa_contacts_handler_duplicates_person_decide_later']) && isset($_REQUEST['org_openpsa_contacts_handler_duplicates_person_loop_i'])) { $i = $_REQUEST['org_openpsa_contacts_handler_duplicates_person_loop_i'] + 1; } while ($i < 100) { debug_add("Loop iteration {$i}"); $qb = new midgard_query_builder('midgard_parameter'); $qb->add_constraint('domain', '=', 'org.openpsa.contacts.duplicates:possible_duplicate'); $qb->add_order('name', 'ASC'); $qb->set_limit(1); if ($i > 0) { $qb->set_offset($i); } $ret = @$qb->execute(); if (empty($ret)) { debug_add("No more results to be had, setting notfound and breaking out of loop"); $this->_request_data['notfound'] = true; break; } $param =& $ret[0]; debug_add("Found duplicate mark on person #{$param->parentguid} for person {$param->name}"); try { $person1 = new org_openpsa_contacts_person_dba($param->parentguid); $person2 = new org_openpsa_contacts_person_dba($param->name); } catch (midcom_error $e) { $i++; continue; } // Make sure we actually have enough rights to do this if (!$person1->can_do('midgard:update') || !$person1->can_do('midgard:delete') || !$person2->can_do('midgard:update') || !$person2->can_do('midgard:delete')) { debug_add("Insufficient rights to merge these two, continuing to see if we have more"); $i++; continue; } // Extra sanity check (in case of semi-successful not-duplicate mark) if ($person1->parameter('org.openpsa.contacts.duplicates:not_duplicate', $person2->guid) || $person2->parameter('org.openpsa.contacts.duplicates:not_duplicate', $person1->guid)) { debug_add("It seems these two (#{$person1->id} and #{$person2->id}) have also marked as not duplicates, some cleanup might be a good thing", MIDCOM_LOG_WARN); $i++; continue; } $this->_request_data['probability'] = (double) $param->value; $this->_request_data['person1'] = $person1; $this->_request_data['person2'] = $person2; break; } }
continue; } echo "<h2>Processing class {$mgdschema}</h2>"; flush(); $total = 0; $purged = 0; $failed_guids = array(); do { $qb = new midgard_query_builder($mgdschema); $qb->add_constraint('metadata.deleted', '<>', 0); if (!empty($failed_guids)) { $qb->add_constraint('guid', 'NOT IN', $failed_guids); } $qb->add_constraint('metadata.revised', '<', gmdate('Y-m-d H:i:s', $cut_off)); $qb->include_deleted(); $qb->set_limit($chunk_size); $objects = $qb->execute(); unset($qb); if (!is_array($objects)) { echo "FATAL QB ERROR\n"; continue; } $total += count($objects); foreach ($objects as $obj) { if (!$obj->purge()) { echo "ERROR: Failed to purge <tt>{$obj->guid}</tt>, deleted: {$obj->metadata->deleted}, revised: {$obj->metadata->revised}. errstr: " . midcom_connection::get_error_string() . "\n"; $failed_guids[] = $obj->guid; continue 1; } $purged++; }