aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <silvio@devlet.com.br>2010-03-29 12:20:10 -0300
committerSilvio <silvio@devlet.com.br>2010-03-29 12:20:10 -0300
commitdc40765aa4c48b802a135ae22f5349f4a9d275de (patch)
treeb159f66debb40a19eae74611fbd3085ec8efc551
parentbac9ab414127873b058d0b2ff3902399ef5d646f (diff)
downloadcinisis-dc40765aa4c48b802a135ae22f5349f4a9d275de.tar.gz
cinisis-dc40765aa4c48b802a135ae22f5349f4a9d275de.tar.bz2
Adding db schemas and adding sample code
-rw-r--r--.gitignore4
-rw-r--r--index.php194
-rw-r--r--isis.php91
-rw-r--r--schemas/anu10.yaml42
-rw-r--r--schemas/tupi.yaml128
-rw-r--r--test.php190
6 files changed, 434 insertions, 215 deletions
diff --git a/.gitignore b/.gitignore
index 0ec7f93..ea134eb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
*.swp
-malete
localhost:*
db/*
-spyc*
+contrib/*
+tmp/*
diff --git a/index.php b/index.php
index f5a5704..423485e 100644
--- a/index.php
+++ b/index.php
@@ -1,191 +1,25 @@
<?php
/**
- * Query script.
+ * Database procedures.
*/
// Import Malete Library
-require 'malete/php/Isis.php';
+require 'contrib/malete/php/Isis.php';
// Import Spyc
-include('spyc/spyc.php');
+include('contrib/spyc/spyc.php');
-?>
-
-<html><head><title>Query</title></head><body>
-
-<?php
-
- // Get request parameters a and b
- // (as well as any plain numeric and v%d style
- $param = Isis_Http::fromReq(array(
- 'a' => 22,
- 'b' => 42,
- ));
-
- // Create a db with field list ("fdt")
- $fdt_anu10 = array(
- 'Periodico' => 1,
- 'Data' => 2,
- 'Titulo' => 3,
- 'Autor' => 4,
- 'Assuntos primarios' => 5,
- 'Assuntos secundarios' => 6,
- 'Ilustrado' => 7,
- 'Caderno' => 8,
- 'Pagina' => 9,
- 'Arquivo digital' => 10,
- 'Forma documento' => 11,
- 'Local de publicacao' => 12,
- 'Observacoes' => 13,
- 'Descritores imagem' => 14,
- 'Termo geografico' => 16,
- 'Coluna' => 17,
- 'Recorte' => 19,
- 'Alimentador' => 20,
- 'Tema Anuario' => 21,
- );
-
- $fdt_tupi = array(
- 'cod.titulo' => 1,
- 'postopo' => 2,
- 'num.entrada' => 3,
- 'num.tombo' => 4,
- 'datain' => 5,
- 'dataex' => 6,
- 'acervo (DIF ou PRE)' => 7,
- 'cadarq' => 10,
- 'caddep' => 11,
- 'cadddl' => 12,
- 'outarq' => 13,
- 'categorias' => 15,
- 'presok' => 16,
- 'roteiro de locução' => 17,
- 'mat.acervo' => 18,
- 'mat.orig' => 19,
- 'titulo' => 20,
- 'outros titulos' => 22,
- 'data/série' => 24,
- 'conteudo/sinopse' => 25,
- 'descritores' => 26,
- 'descritores secundários' => 27,
- 'identidades' => 28,
- 'ndxlib' => 29,
- 'materiais' => 40,
- 'materiais' => 41,
- 'materiais' => 42,
- 'materiais' => 43,
- 'materiais' => 44,
- 'materiais' => 45,
- 'mat' => 46,
- 'mat' => 47,
- 'mat' => 48,
- 'mat' => 49,
- 'mat' => 50,
- 'mat' => 51,
- 'mat' => 52,
- 'mat' => 53,
- 'mat' => 54,
- 'mat' => 55,
- 'evol.estado tec.' => 56,
- 'movimentacao' => 57,
- 'movimentacao' => 58,
- 'obsmat' => 59,
- 'producao*' => 60,
- 'dir.arte*' => 61,
- 'fotografia*' => 62,
- 'musica*' => 63,
- 'som*' => 64,
- 'montagem*' => 65,
- 'direcao*' => 66,
- 'arg/roteiro*' => 67,
- 'distribuicao*' => 68,
- 'colab./outros' => 69,
- 'producao1' => 70,
- 'dir.arte1' => 71,
- 'fotografia1' => 72,
- 'musica1' => 73,
- 'som1' => 74,
- 'montagem1' => 75,
- 'producao2(res.p/expandir)' => 80,
- 'dir.arte2(res.p/expandir)' => 81,
- 'fotografia2(res.p/expandir)' => 82,
- 'musica2(res.p/expandir)' => 83,
- 'som2(res.p/expandir)' => 84,
- 'loc.prod.lan' => 85,
- 'dat.prod.lan' => 86,
- 'lab/est/locacoes' => 87,
- 'premios' => 88,
- 'fontes' => 89,
- 'certificados' => 95,
- 'examinador' => 98,
- 'observações' => 99,
- 'termos geográficos' => 165,
- 'quicktime' => 900,
- 'revisão' => 901,
- 'vídeo' => 902,
- );
-
- $db = 'tupi';
- //$db = 'anu10';
- $fdt = ${'fdt_'. $db};
- $yaml = Spyc::YAMLDump($fdt);
-?>
+// Import database classes
+require 'isis.php';
-<h2>server</h2>
+// Test database connection.
+$schema = Spyc::YAMLLoad('schemas/anu10.yaml');
+$db = new MaleteDb($schema);
+if ($db) {
+ $result = $db->read(4);
+ echo '<pre>';
+ print_r($result);
+ echo '</pre>';
+}
-<?php
- $db = new Isis_Db($fdt, $db, new Isis_Server());
- if (!$db->srv->sock) {
- echo "could not contact server\n";
- }
- else {
- //print_r($db);
?>
-
-<h3>number of records...</h3>
-
-<?php
- $query = 'HORA';
- $recs = $db->num_recs($query);
- echo "got ",count($recs), " terms for '$query'</br>\n";
-?>
-
-<h3>terms beginning with...</h3>
-
-<?php
- $query = 'Hora';
- $terms = $db->terms(strtoupper($query));
- echo "got ",count($terms), " terms for '$query'</br>\n";
- foreach ($terms as $t) {
- list($cnt, $term) = explode("\t", $t);
- echo "'$term'($cnt) ";
- }
- echo "</br>\n";
-?>
-
-<h3>query reading records</h3>
-
-<?php
- $query = 'Corumbiara';
- $recs = $db->query(strtoupper($query));
- echo "got ",count($recs), " records for '$query'</br>\n";
- foreach ($recs as $r) {
- echo "<pre>---\n", $r->toString(), "---\n</pre>\n";
- }
-?>
-
-<h3>query reading a record</h3>
-
-<?php
- $r = $db->read(6);
- echo "<pre>---\n", $r->toString(), "---\n</pre><br>\n";
- echo '<pre>';
- //print_r($r);
- //print_r($r->val[8]);
- print_r(preg_split('/\t/', $r->val[7]));
-
- echo '</pre>';
- } // end else could contact server
-?>
-
-</body></html>
diff --git a/isis.php b/isis.php
index 66b3b7d..9c8bbc2 100644
--- a/isis.php
+++ b/isis.php
@@ -4,27 +4,6 @@
*/
/**
- * Schema format example.
- */
-$schema = array(
- 'db' => array(
- 'name' => 'dbname',
- ),
- 'fields' => array(
- 'field_name' => array(
- 'id' => 1,
- 'size' => 1000,
- 'format' => 'numeric',
- 'repeat' => TRUE,
- 'subfields' => array(
- 'a' => 'test',
- 'b' => 'test2',
- ),
- ),
- ),
-);
-
-/**
* Generic interface for reading Isis databases.
*/
interface IsisDb {
@@ -32,16 +11,19 @@ interface IsisDb {
public function __construct($schema);
// Return field data for a given entry.
- public function fields($id);
+ public function fields($id = NULL);
// Return subfield data for a given entry.
- public function subfields($id);
+ public function subfields($id = NULL);
// Read an entry.
public function read($id);
// Return number of rows in the database.
public function rows();
+
+ // Return a default example schema.
+ public function default_schema();
}
/**
@@ -54,33 +36,76 @@ class MaleteDb implements IsisDb {
public function __construct($schema) {
// Save db schema.
- $format = $schema;
+ $this->format = $schema;
// Setup $fdt used by malete.
foreach ($schema['fields'] as $field => $info) {
- $fdt[$field] = $info['id'];
+ $this->fdt[$field] = $info['name'];
}
// Open a database connection.
- $db = new Isis_Db($fdt, $schema['db']['name'], new Isis_Server());
+ $this->db = new Isis_Db($this->fdt, $schema['db']['name'], new Isis_Server());
+ if (!$this->db->srv->sock) {
+ return FALSE;
+ }
}
- public function fields($id == NULL) {
+ public function fields($id = NULL) {
}
- public function subfields($id == NULL) {
+ public function subfields($id = NULL) {
}
- // TODO: put result into $schema format.
public function read($id) {
- if (!is_numeric($id) {
+ if (!is_numeric($id)) {
return FALSE;
}
- return $this->$db->read($id);
+ $results = $this->db->read($id);
+ return $this->tag($results);
}
public function rows() {
}
-}
-?>
+ /**
+ * Schema format example.
+ */
+ public function default_schema() {
+ $schema = array(
+ 'db' => array(
+ 'name' => 'dbname',
+ ),
+ 'fields' => array(
+ 1 => array(
+ 'name' => 'field_name',
+ 'size' => 1000,
+ 'format' => 'numeric',
+ 'repeat' => TRUE,
+ 'subfields' => array(
+ 'a' => 'test',
+ 'b' => 'test2',
+ ),
+ ),
+ ),
+ );
+
+ return $schema;
+ }
+
+ // Tag results of a db query.
+ function tag($results) {
+ foreach ($results->val as $key => $value) {
+ $field = $results->tag[$key];
+ $name = $this->format['fields'][$field]['name'];
+
+ // Handles field repetition.
+ if ($this->format['fields'][$field]['repeat']) {
+ $data[$name][] = $value;
+ }
+ else {
+ $data[$name] = $value;
+ }
+ }
+ return $data;
+ }
+}
diff --git a/schemas/anu10.yaml b/schemas/anu10.yaml
new file mode 100644
index 0000000..77d7c61
--- /dev/null
+++ b/schemas/anu10.yaml
@@ -0,0 +1,42 @@
+---
+db:
+ name: anu10
+fields:
+ 1:
+ name: Periodico
+ 2:
+ name: Data
+ 3:
+ name: Titulo
+ 4:
+ name: Autor
+ 5:
+ name: Assuntos primarios
+ 6:
+ name: Assuntos secundarios
+ 7:
+ name: Ilustrado
+ 8:
+ name: Caderno
+ 9:
+ name: Pagina
+ 10:
+ name: Arquivo digital
+ 11:
+ name: Forma documento
+ 12:
+ name: Local de publicacao
+ 13:
+ name: Observacoes
+ 14:
+ name: Descritores imagem
+ 16:
+ name: Termo geografico
+ 17:
+ name: Coluna
+ 19:
+ name: Recorte
+ 20:
+ name: Alimentador
+ 21:
+ name: Tema Anuario
diff --git a/schemas/tupi.yaml b/schemas/tupi.yaml
new file mode 100644
index 0000000..e7904cb
--- /dev/null
+++ b/schemas/tupi.yaml
@@ -0,0 +1,128 @@
+---
+db:
+ name: tupi
+fields:
+ 1:
+ name: cod.titulo
+ 2:
+ name: postopo
+ 3:
+ name: num.entrada
+ 4:
+ name: num.tombo
+ 5:
+ name: datain
+ 6:
+ name: dataex
+ 7:
+ name: acervo (DIF ou PRE)
+ 10:
+ name: cadarq
+ 11:
+ name: caddep
+ 12:
+ name: cadddl
+ 13:
+ name: outarq
+ 15:
+ name: categorias
+ 16:
+ name: presok
+ 17:
+ name: roteiro de locução
+ 18:
+ name: mat.acervo
+ 19:
+ name: mat.orig
+ 20:
+ name: titulo
+ 22:
+ name: outros titulos
+ 24:
+ name: data/série
+ 25:
+ name: conteudo/sinopse
+ 26:
+ name: descritores
+ 27:
+ name: descritores secundários
+ 28:
+ name: identidades
+ 29:
+ name: ndxlib
+ 45:
+ name: materiais
+ 55:
+ name: mat
+ 56:
+ name: evol.estado tec.
+ 58:
+ name: movimentacao
+ 59:
+ name: obsmat
+ 60:
+ name: 'producao*'
+ 61:
+ name: 'dir.arte*'
+ 62:
+ name: 'fotografia*'
+ 63:
+ name: 'musica*'
+ 64:
+ name: 'som*'
+ 65:
+ name: 'montagem*'
+ 66:
+ name: 'direcao*'
+ 67:
+ name: 'arg/roteiro*'
+ 68:
+ name: 'distribuicao*'
+ 69:
+ name: colab./outros
+ 70:
+ name: producao1
+ 71:
+ name: dir.arte1
+ 72:
+ name: fotografia1
+ 73:
+ name: musica1
+ 74:
+ name: som1
+ 75:
+ name: montagem1
+ 80:
+ name: producao2(res.p/expandir)
+ 81:
+ name: dir.arte2(res.p/expandir)
+ 82:
+ name: fotografia2(res.p/expandir)
+ 83:
+ name: musica2(res.p/expandir)
+ 84:
+ name: som2(res.p/expandir)
+ 85:
+ name: loc.prod.lan
+ 86:
+ name: dat.prod.lan
+ 87:
+ name: lab/est/locacoes
+ 88:
+ name: premios
+ 89:
+ name: fontes
+ 95:
+ name: certificados
+ 98:
+ name: examinador
+ 99:
+ name: observações
+ 165:
+ name: termos geográficos
+ 900:
+ name: quicktime
+ 901:
+ name: revisão
+ 902:
+ name: vídeo
diff --git a/test.php b/test.php
new file mode 100644
index 0000000..8818c88
--- /dev/null
+++ b/test.php
@@ -0,0 +1,190 @@
+<?php
+/**
+ * Test query script.
+ */
+
+// Import Malete Library
+require 'contrib/malete/php/Isis.php';
+
+// Import Spyc
+include('contrib/spyc/spyc.php');
+
+?>
+
+<html><head><title>Query</title></head><body>
+
+<?php
+
+ // Get request parameters a and b
+ // (as well as any plain numeric and v%d style
+ $param = Isis_Http::fromReq(array(
+ 'a' => 22,
+ 'b' => 42,
+ ));
+
+ // Create a db with field list ("fdt")
+ $fdt_anu10 = array(
+ 'Periodico' => 1,
+ 'Data' => 2,
+ 'Titulo' => 3,
+ 'Autor' => 4,
+ 'Assuntos primarios' => 5,
+ 'Assuntos secundarios' => 6,
+ 'Ilustrado' => 7,
+ 'Caderno' => 8,
+ 'Pagina' => 9,
+ 'Arquivo digital' => 10,
+ 'Forma documento' => 11,
+ 'Local de publicacao' => 12,
+ 'Observacoes' => 13,
+ 'Descritores imagem' => 14,
+ 'Termo geografico' => 16,
+ 'Coluna' => 17,
+ 'Recorte' => 19,
+ 'Alimentador' => 20,
+ 'Tema Anuario' => 21,
+ );
+
+ $fdt_tupi = array(
+ 'cod.titulo' => 1,
+ 'postopo' => 2,
+ 'num.entrada' => 3,
+ 'num.tombo' => 4,
+ 'datain' => 5,
+ 'dataex' => 6,
+ 'acervo (DIF ou PRE)' => 7,
+ 'cadarq' => 10,
+ 'caddep' => 11,
+ 'cadddl' => 12,
+ 'outarq' => 13,
+ 'categorias' => 15,
+ 'presok' => 16,
+ 'roteiro de locução' => 17,
+ 'mat.acervo' => 18,
+ 'mat.orig' => 19,
+ 'titulo' => 20,
+ 'outros titulos' => 22,
+ 'data/série' => 24,
+ 'conteudo/sinopse' => 25,
+ 'descritores' => 26,
+ 'descritores secundários' => 27,
+ 'identidades' => 28,
+ 'ndxlib' => 29,
+ 'materiais' => 40,
+ 'materiais' => 41,
+ 'materiais' => 42,
+ 'materiais' => 43,
+ 'materiais' => 44,
+ 'materiais' => 45,
+ 'mat' => 46,
+ 'mat' => 47,
+ 'mat' => 48,
+ 'mat' => 49,
+ 'mat' => 50,
+ 'mat' => 51,
+ 'mat' => 52,
+ 'mat' => 53,
+ 'mat' => 54,
+ 'mat' => 55,
+ 'evol.estado tec.' => 56,
+ 'movimentacao' => 57,
+ 'movimentacao' => 58,
+ 'obsmat' => 59,
+ 'producao*' => 60,
+ 'dir.arte*' => 61,
+ 'fotografia*' => 62,
+ 'musica*' => 63,
+ 'som*' => 64,
+ 'montagem*' => 65,
+ 'direcao*' => 66,
+ 'arg/roteiro*' => 67,
+ 'distribuicao*' => 68,
+ 'colab./outros' => 69,
+ 'producao1' => 70,
+ 'dir.arte1' => 71,
+ 'fotografia1' => 72,
+ 'musica1' => 73,
+ 'som1' => 74,
+ 'montagem1' => 75,
+ 'producao2(res.p/expandir)' => 80,
+ 'dir.arte2(res.p/expandir)' => 81,
+ 'fotografia2(res.p/expandir)' => 82,
+ 'musica2(res.p/expandir)' => 83,
+ 'som2(res.p/expandir)' => 84,
+ 'loc.prod.lan' => 85,
+ 'dat.prod.lan' => 86,
+ 'lab/est/locacoes' => 87,
+ 'premios' => 88,
+ 'fontes' => 89,
+ 'certificados' => 95,
+ 'examinador' => 98,
+ 'observações' => 99,
+ 'termos geográficos' => 165,
+ 'quicktime' => 900,
+ 'revisão' => 901,
+ 'vídeo' => 902,
+ );
+
+ //$db = 'tupi';
+ $db = 'anu10';
+ $fdt = ${'fdt_'. $db};
+ $yaml = Spyc::YAMLDump($fdt);
+?>
+
+<h2>server</h2>
+
+<?php
+ $db = new Isis_Db($fdt, $db, new Isis_Server());
+ if (!$db->srv->sock) {
+ echo "could not contact server\n";
+ }
+ else {
+ //print_r($db);
+?>
+
+<h3>number of records...</h3>
+
+<?php
+ $query = 'HORA';
+ $recs = $db->num_recs($query);
+ echo "got ",count($recs), " terms for '$query'</br>\n";
+?>
+
+<h3>terms beginning with...</h3>
+
+<?php
+ $query = 'Hora';
+ $terms = $db->terms(strtoupper($query));
+ echo "got ",count($terms), " terms for '$query'</br>\n";
+ foreach ($terms as $t) {
+ list($cnt, $term) = explode("\t", $t);
+ echo "'$term'($cnt) ";
+ }
+ echo "</br>\n";
+?>
+
+<h3>query reading records</h3>
+
+<?php
+ $query = 'Corumbiara';
+ $recs = $db->query(strtoupper($query));
+ echo "got ",count($recs), " records for '$query'</br>\n";
+ foreach ($recs as $r) {
+ echo "<pre>---\n", $r->toString(), "---\n</pre>\n";
+ }
+?>
+
+<h3>query reading a record</h3>
+
+<?php
+ $r = $db->read(6);
+ echo "<pre>---\n", $r->toString(), "---\n</pre><br>\n";
+ echo '<pre>';
+ //print_r($r);
+ //print_r($r->val[8]);
+ //print_r(preg_split('/\t/', $r->val[7]));
+ //print_r(array_flip($fdt_anu10));
+ echo '</pre>';
+ } // end else could contact server
+?>
+</body></html>