root/trunk/midcom/midcom.core/support/json_to_structure.php

Revision 13852, 2.5 kB (checked in by bergie, 3 years ago)

WriteMaps?.com JSON sitemap to Site Wizard structure converter

Line 
1 <?php
2 if (count($argv) != 2)
3 {
4     die("Usage: php json_to_structure sitemap.json\n");
5 }
6
7 $json_file = $argv[1];
8 $structure_name = basename($json_file);
9 if (!file_exists($json_file))
10 {
11     die("Error: File {$json_file} not found\n");
12 }
13
14 $json = file_get_contents($json_file);
15 $data = json_decode($json);
16
17 if (   !is_object($data)
18     || !isset($data->pages))
19 {
20     die("Error: faulty JSON data\n");
21 }
22
23 $structure = array();
24 $structure[$structure_name] = array();
25 $structure[$structure_name]['name'] = $structure_name;
26 $structure[$structure_name]['title'] = $structure_name;
27
28 $nodes = array();
29
30 foreach ($data->pages as $page)
31 {
32     $node = array();
33     $node['title'] = $page->name;
34     
35     // Users can define the components in page notes   
36     if ($page->notes)
37     {
38         // If notes doesn't contain a valid component name fall back to static
39         if (!preg_match('/[a-z][a-z0-9]\.[a-z][a-z0-9].[a-z][a-z0-9]/', $page->notes))
40         {
41             $page->notes = 'net.nehmer.static';
42         }
43
44         $node['component'] = $page->notes;
45     }
46     
47     $node_id = substr($page->id, 5);
48     $parent_id = substr($page->id, 5, -4);
49     $nodes[$node_id] = $node;
50
51     $node['acl'] = array();
52     $node['parameters'] = array();
53     $node['nodes'] = array();
54     if (empty($parent_id))
55     {
56         // Root node, note the reference here
57         $structure[$structure_name]['root'] =& $nodes[$node_id];
58     }
59     
60     if (isset($nodes[$parent_id]))
61     {
62         // Subnode
63         $nodes[$parent_id]['nodes'][$node_id] = $node;
64     }
65 }
66
67 function draw_array($array, $prefix = '')
68 {
69     $data = '';
70     foreach ($array as $key => $val)
71     {
72         $data .= $prefix;
73         if (!is_numeric($key))
74         {
75             $data .= "'{$key}' => ";
76         }
77         
78         switch(gettype($val))
79         {
80             case 'boolean':
81                 $data .= ($val)?'true':'false';
82                 break;
83             case 'array':
84                 if (empty($val))
85                 {
86                     $data .= 'array()';
87                 }
88                 else
89                 {
90                     $data .= "array\n{$prefix}(\n" . draw_array($val, "{$prefix}    ") . "{$prefix})";
91                 }
92                 break;
93
94             default:
95                 if (is_numeric($val))
96                 {
97                     $data .= $val;
98                 }
99                 else
100                 {
101                     $data .= "'{$val}'";
102                 }
103         }
104         
105         $data .= ",\n";
106
107     }
108     return $data;
109 }
110
111 echo draw_array($structure);
112 ?>
Note: See TracBrowser for help on using the browser.