893e3fb7c3aee3d322e41e57fb2c8967268212ba
[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     $GLOBALS['phorkie']['cfg']['elasticsearch'] . '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     $GLOBALS['phorkie']['cfg']['elasticsearch'] . '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     $GLOBALS['phorkie']['cfg']['elasticsearch'] . '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         $GLOBALS['phorkie']['cfg']['elasticsearch'] . 'repo/' . $repo->id,
78         \HTTP_Request2::METHOD_PUT
79     );
80     $r->setBody(
81         json_encode(
82             (object)array(
83                 'id' => $repo->id,
84                 'description' => $repo->getDescription(),
85             )
86         )
87     );
88     $res = $r->send();
89
90     foreach ($repo->getFiles() as $file) {
91         $r = new \HTTP_Request2(
92             $GLOBALS['phorkie']['cfg']['elasticsearch'] . 'file/?parent=' . $repo->id,
93             \HTTP_Request2::METHOD_POST
94         );
95         $r->setBody(
96             json_encode(
97                 (object)array(
98                     'name'      => $file->getFilename(),
99                     'extension' => $file->getExt(),
100                     'content'   => $file->isText() ? $file->getContent() : '',
101                 )
102             )
103         );
104         $r->send();
105     }
106 }
107 ?>