/**
     * Move old permalinks
     */
    public function move_old_permalinks($edition_id)
    {
        /*
         * Get the current edition.
         */
        $edition_obj = new Edition(array('db' => $this->db));
        $current_edition = $edition_obj->current();
        /*
         * First, delete anything that's not a real permalink for any edition
         * that's not current.
         */
        $sql = 'DELETE FROM permalinks
			WHERE permalink = 0 AND edition_id <> :edition_id';
        $sql_args = array(':edition_id' => $current_edition->id);
        $statement = $this->db->prepare($sql);
        $statement->execute($sql_args);
        /*
         * Then make all remaining permalinks preferred for any edition that's
         * not current.
         */
        $sql = 'UPDATE permalinks
			SET preferred = 1
			WHERE permalink = 1 AND preferred = 0 AND
			edition_id <> :edition_id';
        $sql_args = array(':edition_id' => $edition_id);
        $statement = $this->db->prepare($sql);
        $statement->execute($sql_args);
    }
if (!isset($args['relational_id']) || empty($args['relational_id'])) {
    $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();
if (isset($args['edition_id'])) {
    $struct->edition_id = $args['edition_id'];
} else {
    $edition_data = $edition->current();
    $struct->edition_id = $edition_data->id;
}
/*
 * 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) {
    public function get_url($law_id, $edition_id = null, $permalink = false)
    {
        /*
         * If a section number hasn't been passed to this function, then there's nothing to do.
         */
        if (empty($law_id)) {
            return FALSE;
        }
        /*
         * Set the default edition.
         */
        if (empty($edition_id)) {
            $edition_obj = new Edition(array('db' => $this->db));
            $edition = $edition_obj->current();
            $edition_id = $edition->id;
        }
        $sql = 'SELECT *
				FROM permalinks
				WHERE object_type="law"
				AND relational_id = :law_id';
        $sql_args = array(':law_id' => $law_id);
        if ($permalink === true) {
            $sql .= ' AND permalink = 1';
        } else {
            $sql .= ' AND preferred = 1';
        }
        $statement = $this->db->prepare($sql);
        $result = $statement->execute($sql_args);
        if ($result === FALSE || $statement->rowCount() == 0) {
            return FALSE;
        }
        $permalink = $statement->fetch(PDO::FETCH_OBJ);
        return $permalink;
    }
 public function index_laws($args)
 {
     if (!isset($this->edition)) {
         $edition_obj = new Edition(array('db' => $this->db));
         $this->edition = $edition_obj->current();
     }
     if (!isset($this->edition)) {
         throw new Exception('No edition, cannot index laws.');
     }
     if ($this->edition->current != '1') {
         $this->logger->message('The edition is not current, skipping the update of the search ' . ' index', 9);
         return;
     }
     if (!defined('SEARCH_CONFIG')) {
         $this->logger->message('Solr is not in use, skipping index', 9);
         return;
     } else {
         /*
          * Index the laws.
          */
         $this->logger->message('Updating search index', 5);
         $this->logger->message('Indexing laws', 6);
         $search_index = new SearchIndex(array('config' => json_decode(SEARCH_CONFIG, TRUE)));
         $law_obj = new Law(array('db' => $this->db));
         $result = $law_obj->get_all_laws($this->edition->id, true);
         $search_index->start_update();
         while ($law = $result->fetch()) {
             // Get the full data of the actual law.
             $document = new Law(array('db' => $this->db));
             $document->law_id = $law['id'];
             $document->config->get_all = TRUE;
             $document->get_law();
             // Bring over our edition info.
             $document->edition = $this->edition;
             try {
                 $search_index->add_document($document);
             } catch (Exception $error) {
                 $this->logger->message('Search index error "' . $error->getStatusMessage() . '"', 10);
                 return FALSE;
             }
         }
         $search_index->commit();
         // $this->logger->message('Indexing structures', 6);
         ### TODO: Index structures
         $this->logger->message('Laws were indexed', 5);
         return TRUE;
     }
 }
 function handle($args)
 {
     /*
      * If we have received neither a term nor a section, we can't do anything.
      */
     if (empty($args['term']) && empty($_GET['section'])) {
         json_error('Neither a dictionary term nor a section number have been provided.');
         die;
     }
     /*
      * Clean up the term.
      */
     $term = filter_var($args['term'], FILTER_SANITIZE_STRING);
     /*
      * If a section has been specified, then clean that up.
      */
     if (isset($_GET['section'])) {
         $section = filter_input(INPUT_GET, 'section', FILTER_SANITIZE_STRING);
     }
     if (isset($_GET['law_id'])) {
         $law_id = filter_input(INPUT_GET, 'law_id', FILTER_SANITIZE_STRING);
     }
     if (isset($_GET['edition_id'])) {
         $edition_id = filter_input(INPUT_GET, 'edition_id', FILTER_SANITIZE_STRING);
     } else {
         $edition = new Edition();
         $current_edition = $edition->current();
         $edition_id = $current_edition->id;
     }
     $dict = new Dictionary();
     /*
      * Get the definitions for the requested term, if a term has been requested.
      */
     if (!empty($args['term'])) {
         if (isset($section)) {
             $dict->section_number = $section;
         }
         if (isset($law_id)) {
             $dict->law_id = $law_id;
         }
         if (isset($edition_id)) {
             $dict->edition_id = $edition_id;
         }
         $dict->term = $term;
         $dictionary = $dict->define_term();
         /*
          * If, for whatever reason, this term is not found, return an error.
          */
         if ($dictionary === FALSE) {
             $response = array('definition' => 'Definition not available.');
         } else {
             /*
              * Uppercase the first letter of the first (quoted) word. We perform this twice because
              * some egal codes begin the definition with a quotation mark and some do not. (That is,
              * some write '"Whale" is a large sea-going mammal' and some write 'Whale is a large
              * sea-going mammal.")
              */
             if (preg_match('/[A-Za-z]/', $dictionary->definition[0]) === 1) {
                 $dictionary->definition[0] = strtoupper($dictionary->definition[0]);
             } elseif (preg_match('/[A-Za-z]/', $dictionary->definition[1]) === 1) {
                 $dictionary->definition[1] = strtoupper($dictionary->definition[1]);
             }
             /*
              * 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);
                 foreach ($dictionary as &$term) {
                     /*
                      * Step through our response fields and eliminate those that aren't in the
                      * requested list.
                      */
                     foreach ($term as $field => &$value) {
                         if (in_array($field, $returned_fields) === FALSE) {
                             unset($term->{$field});
                         }
                     }
                 }
             }
             /*
              * If a section has been specified, then simplify this response by returning just a
              * single definition.
              */
             if (isset($section) || isset($law_id)) {
                 $dictionary = $dictionary->{0};
             }
             /*
              * Rename this variable to use the expected name.
              */
             $response = $dictionary;
         }
         // end else if term is found
     } elseif (!empty($_GET['section'])) {
         /*
          * Get the structural ID of the container for this section.
          */
         $law = new Law();
         $law->section_number = $section;
         $law->config = FALSE;
         $result = $law->get_law();
         if ($result == FALSE) {
             $response = array('terms' => 'Term list not available.');
         } else {
             /*
              * Now get the term list.
              */
             $dict->section_id = $law->section_id;
             $dict->structure_id = $law->structure_id;
             $response = $dict->term_list();
             if ($response == FALSE) {
                 $response = array('terms' => 'Term list not available.');
             }
         }
     }
     // end elseif (!empty($args['section']))
     $this->render($response, 'OK', $_REQUEST['callback']);
 }