/** * Generate statistics about structural units * * Iterate through every structure to determine how many ancestors that it has (either ancestor * structural units or laws), and then store that data as serialized objects in the "structure" * database table. */ function structural_stats_generate() { $this->logger->message('Generating structural statistics', 3); /* * List all of the top-level structural units. */ $struct = new Structure(); $structures = $struct->list_children(); /* * Create an object to store structural statistics. */ $this->stats = new stdClass(); /* * Iterate through each of those units. */ foreach ($structures as $structure) { $this->depth = 0; $this->structure_id = $structure->id; $this->structural_stats_recurse(); } /* * Iterate through every primary structural unit. */ foreach ($this->stats as $structure_id => $structure) { /* * If this is more than 1 level deep. */ if (count($structure->ancestry) > 1) { /* * Iterate through all but the last ancestry element. */ for ($i = 0; $i < count($structure->ancestry) - 1; $i++) { $ancestor_id = $structure->ancestry[$i]; if (!isset($this->stats->{$ancestor_id}->child_laws)) { $this->stats->{$ancestor_id}->child_laws = 0; } if (!isset($this->stats->{$ancestor_id}->child_structures)) { $this->stats->{$ancestor_id}->child_structures = 0; } if (isset($structure->child_laws)) { $this->stats->{$ancestor_id}->child_laws = $this->stats->{$ancestor_id}->child_laws + $structure->child_laws; } if (isset($structure->child_structures)) { $this->stats->{$ancestor_id}->child_structures = $this->stats->{$ancestor_id}->child_structures + $structure->child_structures; } } } } $sql = 'UPDATE structure SET metadata = :metadata WHERE id = :structure_id'; $statement = $this->db->prepare($sql); foreach ($this->stats as $structure_id => $structure) { if (!isset($structure->child_laws)) { $structure->child_laws = 0; } if (!isset($structure->child_structures)) { $structure->child_structures = 0; } $structure_temp = new Structure(); $structure_temp->structure_id = $structure_id; $structure_temp->get_current(); if (!isset($structure_temp->metadata)) { $structure_temp->metadata = new stdClass(); } $structure_temp->metadata->child_laws = $structure->child_laws; $structure_temp->metadata->child_structures = $structure->child_structures; $sql_args = array(':metadata' => serialize($structure_temp->metadata), ':structure_id' => $structure_id); $result = $statement->execute($sql_args); } }
$structure_id = ''; } else { /* * Localize the identifier, filtering out unsafe characters. */ $structure_id = filter_var($args['relational_id'], FILTER_SANITIZE_STRING); } /* * Create a new instance of the class that handles information about individual laws. */ $struct = new Structure(); /* * Get the structure based on our identifier */ $struct->structure_id = $structure_id; $struct->get_current(); /* * If are at the top level, struct->structure is null. */ $response = isset($struct->structure) ? $struct->structure : ''; /* * If the URL doesn't represent a valid structural portion of the code, then bail. */ if ($response === FALSE) { send_404(); } /* * Set aside the ancestry for this structural unit, to be accessed separately. */ // Again, if we at the top level, this will return null $structure = isset($struct->structure) ? $struct->structure : '';
function handle($args) { /* * If the request is for the structural units sorted by a specific criteria. */ if (isset($_GET['sort'])) { /* * Explicitly reassign the external value to an internal one, for safety's sake. */ if ($_GET['sort'] == 'views') { $order_by = filter_var($_GET['sort'], FILTER_SANITIZE_STRING); } } /* * Create a new instance of the class that handles information about individual laws. */ $struct = new Structure(); /* * Get the structure based on our identifier. */ if ($args['relational_id']) { $struct->structure_id = $args['relational_id']; } else { $struct->structure_id = ''; } $struct->get_current(); $response = $struct->structure; /* * If this structural element does not exist. */ if ($response === FALSE) { $this->handleNotFound(); } /* * List all child structural units. */ $struct->order_by = $order_by; $response->children = $struct->list_children(); /* * List all laws. */ $response->laws = $struct->list_laws(); /* * If the request contains a specific list of fields to be returned. */ if (isset($_GET['fields'])) { /* * Turn that list into an array. */ $returned_fields = explode(',', urldecode($_GET['fields'])); foreach ($returned_fields as &$field) { $field = trim($field); } /* * It's essential to unset $field at the conclusion of the prior loop. */ unset($field); /* * Step through our response fields and eliminate those that aren't in the requested list. */ foreach ($response as $field => &$value) { if (in_array($field, $returned_fields) === false) { unset($response->{$field}); } } } $this->render($response, 'OK'); }