first index and search test
[phorkie.git] / scripts / index.php
1 <?php
2 //index repositories in elasticsearch
3
4 namespace phorkie;
5 set_include_path(
6     __DIR__ . '/../src/'
7     . PATH_SEPARATOR . get_include_path()
8 );
9 spl_autoload_register(
10     function ($class) {
11         $file = str_replace(array('\\', '_'), '/', $class) . '.php';
12         $hdl = @fopen($file, 'r', true);
13         if ($hdl !== false) {
14             fclose($hdl);
15             require $file;
16         }
17     }
18 );
19 require_once __DIR__ . '/../data/config.default.php';
20 if (file_exists(__DIR__ . '/../data/config.php')) {
21     require_once __DIR__ . '/../data/config.php';
22 }
23 if ($GLOBALS['phorkie']['cfg']['setupcheck']) {
24     SetupCheck::run();
25 }
26
27 //delete all repos
28 $r = new \HTTP_Request2(
29     'http://localhost:9200/phorkie/repo/_query',
30     \HTTP_Request2::METHOD_DELETE
31 );
32 $r->setBody(
33     json_encode(
34         (object)array(
35             'match_all' => (object)array()
36         )
37     )
38 );
39 $r->send();
40 $r = new \HTTP_Request2(
41     'http://localhost:9200/phorkie/file/_query',
42     \HTTP_Request2::METHOD_DELETE
43 );
44 $r->setBody(
45     json_encode(
46         (object)array(
47             'match_all' => (object)array()
48         )
49     )
50 );
51 $r->send();
52
53 //create mapping
54 $r = new \HTTP_Request2(
55     'http://localhost:9200/phorkie/file/_mapping',
56     \HTTP_Request2::METHOD_PUT
57 );
58 $r->setBody(
59     json_encode(
60         (object)array(
61             'file' => (object)array(
62                 '_parent' => (object)array(
63                     'type' => 'repo'
64                 )
65             )
66         )
67     )
68 );
69 $r->send();
70
71
72
73 //FIXME: define schema
74 $rs = new Repositories();
75 foreach ($rs->getList(0, 10000) as $repo) {
76     $r = new \HTTP_Request2(
77         //FIXME: make configurable
78         'http://localhost:9200/phorkie/repo/' . $repo->id,
79         \HTTP_Request2::METHOD_PUT
80     );
81     $r->setBody(
82         json_encode(
83             (object)array(
84                 'id' => $repo->id,
85                 'description' => $repo->getDescription(),
86             )
87         )
88     );
89     $res = $r->send();
90
91     foreach ($repo->getFiles() as $file) {
92         $r = new \HTTP_Request2(
93             //FIXME: make configurable
94             'http://localhost:9200/phorkie/file/?parent=' . $repo->id,
95             \HTTP_Request2::METHOD_POST
96         );
97         $r->setBody(
98             json_encode(
99                 (object)array(
100                     'name'      => $file->getFilename(),
101                     'extension' => $file->getExt(),
102                     'content'   => $file->isText() ? $file->getContent() : '',
103                 )
104             )
105         );
106         $r->send();
107     }
108 }
109 ?>