source: trunk/prototype/Sync.php @ 5399

Revision 5399, 4.4 KB checked in by cristiano, 12 years ago (diff)

Ticket #2434 - Alteração da estrutura de diretórios da nova API

Line 
1<?php
2
3
4$accept = $_SERVER["HTTP_ACCEPT"];
5
6if( !isset( $args ) )
7    parse_str( file_get_contents('php://input'), $args );
8
9if(!function_exists('parseURI'))
10{   
11    function parseURI( $URI )
12    {
13    //     $regex = "#^([a-zA-Z0-9-_]+)\(([a-zA-Z0-9-_]+)\)://(.*)|([a-zA-Z0-9-_]+)://(.*)$#";//TODO: checar essa RegExp
14        $regex = "#^([a-zA-Z0-9-_]+)://(.*)$#";
15
16        preg_match( $regex, $URI, $matches );
17
18        if( !$matches || empty($matches) )
19            return( array(false, $URI, false) );
20
21        return( $matches );
22    }
23}
24
25if(!function_exists('formatURI'))
26
27    function formatURI( $concept = false, $id = false, $service = false )
28    {
29        return $concept ? $id ? $service ?
30
31               $concept.'://'.$id.'('.$service.')':
32
33               $concept.'://'.$id:
34
35               $concept:
36
37               false;
38    }
39}
40
41require_once 'api/controller.php';
42
43$mounted = array(); $synced = array();
44
45if(!function_exists('prepare'))
46
47    function prepare( $concept, $id, $dt, &$data, &$oldIds, &$mounted, &$synced )
48    {
49        $oldIds[] = $id;
50
51        if( $dt === 'false' ||
52            $dt ===  false )
53            return( false );
54
55        if( !preg_match( '#^([a-zA-Z0-9-_]+)\(.*\)$#', $id ) )
56            $dt['id'] = $id;
57        elseif( isset($dt['id']) && $dt['id'] === $id )
58            unset($dt['id']);
59
60        $links = Controller::links( $concept );
61
62        foreach( $links as $linkName => $linkTarget )
63        {
64                    if( isset( $dt[$linkName] ) )
65                    {
66                            if( $notArray = Controller::isConcept( $linkName ) )
67                                    $dt[$linkName] = array( $dt[$linkName] );
68
69                            foreach( $dt[$linkName] as $i => $d )
70                            {
71                                    $currentURI = formatURI($links[$linkName], $d);
72
73                                    if( isset( $mounted[ $currentURI ] ) )
74                                    {
75                                            unset( $dt[$linkName][$i] );
76                                    }
77                                    elseif( isset( $synced[ $d ] ) )
78                                    {
79                                            $dt[$linkName][$i] = $synced[ $d ];
80                                    }
81                                    elseif( isset($data[ $currentURI ]) )
82                                    {
83                                            $value = $data[$currentURI];
84                                            unset( $data[ $currentURI ] );
85
86                                            $mounted[ $currentURI ] = true;
87
88                                            $dt[$linkName][$i] = prepare( $links[$linkName], $d, $value, $data, $oldIds, $mounted, $synced );
89                                    }
90                            }
91
92                            if( empty( $dt[$linkName] ) )
93                                    unset( $dt[$linkName] );
94                            elseif( $notArray )
95                                    $dt[$linkName] = $dt[$linkName][0];
96                    }
97        }
98
99        return( $dt );
100    }
101}
102
103$return = array();
104
105if( !isset( $args[0] ) )
106    $args = array( $args );
107
108foreach( $args as $i => $data )
109{
110    foreach( $data as $uri => $dt )
111    {
112          if( !isset($data[$uri]) )
113                  continue;
114
115          list( , $concept, $id ) = parseURI( $uri );
116
117          unset( $data[$uri] );
118          $mounted[$uri] = true;
119
120          $oldIds = array();
121
122          $dt = prepare( $concept, $id, $dt, $data, $oldIds, $mounted, $synced );
123
124          $result = Controller::put( array( 'concept' => $concept, 'id' => $id ), $dt );
125
126          if( !$result )
127          {
128              $return[ $uri ] = 'ROLLBACK';
129              unset( $data[$uri] );
130              continue;
131          }
132
133          foreach( $result as $ii => $tx )
134          {
135                  $oldId = $oldIds[ $tx['order'] ];
136
137                  if( isset( $oldId ) && $oldId )
138                  {
139                      $synced[ $oldId ] = $tx['id'];
140                      unset( $oldIds[ $tx['order'] ] );
141                  }
142
143                  $oldURI = formatURI( $tx['concept'], $oldId );
144                  unset( $data[$oldURI] );
145
146                  $return[ $oldURI ] = !$tx['rollback'] ? $dt ?
147                                       array( 'id' => $tx['id'] ) : false : 'ROLLBACK';;
148          }
149    }
150
151    foreach( $data as $oldURI => $oldVal )
152      $return[ $oldURI ] = 'ROLLBACK';
153}
154
155echo json_encode( $return );
156
157Controller::closeAll();
158
159//              ob_start();
160//              print "\n";
161//              print $data[$linkName] . ": ";
162//              print_r( $synced );
163//              $output = ob_get_clean();
164//              file_put_contents( "/tmp/prototype.log", $output , FILE_APPEND );
Note: See TracBrowser for help on using the repository browser.