Index: trunk/extensions/DataTransclusion/tests/DataTransclusionTest.php |
— | — | @@ -340,6 +340,32 @@ |
341 | 341 | |
342 | 342 | $this->assertEquals( $err, 'test error' ); |
343 | 343 | $this->assertEquals( $rec['id'], 3 ); |
| 344 | + |
| 345 | + //////////////////////// |
| 346 | + $spec['url'] = 'file://' . dirname( realpath( __FILE__ ) ) . '/test-data-{field}-{value}.pser'; |
| 347 | + $spec['dataFormat'] = 'php'; |
| 348 | + $source = new WebDataTransclusionSource( $spec ); |
| 349 | + |
| 350 | + $rec = $source->fetchRecord( 'name', 'foo' ); |
| 351 | + $this->assertEquals( $rec['id'], 3 ); |
| 352 | + |
| 353 | + //////////////////////// |
| 354 | + $spec['url'] = 'file://' . dirname( realpath( __FILE__ ) ) . '/test-data-{field}-{value}.json'; |
| 355 | + $spec['dataFormat'] = 'json'; |
| 356 | + $source = new WebDataTransclusionSource( $spec ); |
| 357 | + |
| 358 | + $rec = $source->fetchRecord( 'name', 'foo' ); |
| 359 | + $this->assertEquals( $rec['id'], 3 ); |
| 360 | + |
| 361 | + //////////////////////// |
| 362 | + if ( function_exists( 'wddx_unserialize' ) ) { |
| 363 | + $spec['url'] = 'file://' . dirname( realpath( __FILE__ ) ) . '/test-data-{field}-{value}.wddx'; |
| 364 | + $spec['dataFormat'] = 'wddx'; |
| 365 | + $source = new WebDataTransclusionSource( $spec ); |
| 366 | + |
| 367 | + $rec = $source->fetchRecord( 'name', 'foo' ); |
| 368 | + $this->assertEquals( $rec['id'], 3 ); |
| 369 | + } |
344 | 370 | } |
345 | 371 | } |
346 | 372 | |
Index: trunk/extensions/DataTransclusion/tests/test-data-name-foo.wddx |
— | — | @@ -0,0 +1 @@ |
| 2 | +<wddxPacket version='1.0'><header/><data><struct><var name='response'><struct><var name='content'><struct><var name='foo'><struct><var name='name'><string>foo</string></var><var name='id'><number>3</number></var><var name='info'><string>test 1</string></var></struct></var></struct></var></struct></var></struct></data></wddxPacket> |
\ No newline at end of file |
Index: trunk/extensions/DataTransclusion/tests/makeTestData.php |
— | — | @@ -0,0 +1,15 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +$dir = dirname( __FILE__ ); |
| 5 | + |
| 6 | +$rec = array( "name" => "foo", "id" => 3, "info" => 'test 1' ); |
| 7 | + |
| 8 | +$data = array( 'response' => array( |
| 9 | + 'content' => array( |
| 10 | + 'foo' => $rec |
| 11 | + ) |
| 12 | + ) ); |
| 13 | + |
| 14 | +file_put_contents( "$dir/test-data-name-foo.pser", serialize( $data ) ); |
| 15 | +file_put_contents( "$dir/test-data-name-foo.json", json_encode( $data ) ); |
| 16 | +file_put_contents( "$dir/test-data-name-foo.wddx", wddx_serialize_value( $data ) ); |
\ No newline at end of file |
Index: trunk/extensions/DataTransclusion/tests/test-data-name-foo.json |
— | — | @@ -0,0 +1 @@ |
| 2 | +{"response":{"content":{"foo":{"name":"foo","id":3,"info":"test 1"}}}} |
\ No newline at end of file |
Index: trunk/extensions/DataTransclusion/tests/test-data-name-foo.pser |
— | — | @@ -0,0 +1 @@ |
| 2 | +a:1:{s:8:"response";a:1:{s:7:"content";a:1:{s:3:"foo";a:3:{s:4:"name";s:3:"foo";s:2:"id";i:3;s:4:"info";s:6:"test 1";}}}} |
\ No newline at end of file |
Index: trunk/extensions/DataTransclusion/WebDataTransclusionSource.php |
— | — | @@ -22,7 +22,9 @@ |
23 | 23 | * WebDataTransclusionSource accepts some additional options |
24 | 24 | * |
25 | 25 | * * $spec['url']: base URL for building urls for retrieving individual records. |
26 | | - * The key/value pair is appended to the URL as a regular URL |
| 26 | + * If the URL contains the placeholder {field} and/or {value}, these |
| 27 | + * get replaced by the field name resp. the field value. |
| 28 | + * Otherwise, the key/value pair is appended to the URL as a regular URL |
27 | 29 | * parameter (preceeded by ? or &, as appropriate). For more |
28 | 30 | * complex rules for building the url, override getRecordURL(). REQUIRED. |
29 | 31 | * * $spec['dataFormat']: Serialization format returned from the web service. |
— | — | @@ -74,7 +76,7 @@ |
75 | 77 | } |
76 | 78 | |
77 | 79 | public function fetchRecord( $field, $value ) { |
78 | | - $raw = $this->loadRecordData( $field, $value ); // TESTME |
| 80 | + $raw = $this->loadRecordData( $field, $value ); |
79 | 81 | if ( !$raw ) { |
80 | 82 | wfDebugLog( 'DataTransclusion', "failed to fetch data for $field=$value\n" ); |
81 | 83 | return false; |
— | — | @@ -105,16 +107,21 @@ |
106 | 108 | public function getRecordURL( $field, $value ) { |
107 | 109 | $u = $this->url; |
108 | 110 | |
109 | | - if ( strpos( $u, '?' ) === false ) { |
110 | | - $u .= '?'; |
| 111 | + if ( strpos( $u, '{field}' ) !== false || strpos( $u, '{value}' ) !== false ) { |
| 112 | + $u = str_replace( '{field}', urlencode( $field ), $u ); |
| 113 | + $u = str_replace( '{value}', urlencode( $value ), $u ); |
111 | 114 | } else { |
112 | | - $u .= '&'; |
| 115 | + if ( strpos( $u, '?' ) === false ) { |
| 116 | + $u .= '?'; |
| 117 | + } else { |
| 118 | + $u .= '&'; |
| 119 | + } |
| 120 | + |
| 121 | + $u .= urlencode( $field ); |
| 122 | + $u .= '='; |
| 123 | + $u .= urlencode( $value ); |
113 | 124 | } |
114 | 125 | |
115 | | - $u .= $field; |
116 | | - $u .= '='; |
117 | | - $u .= urlencode( $value ); |
118 | | - |
119 | 126 | return $u; |
120 | 127 | } |
121 | 128 | |
— | — | @@ -124,7 +131,7 @@ |
125 | 132 | if ( preg_match( '!^https?://!', $u ) ) { |
126 | 133 | $raw = Http::get( $u, $this->timeout, $this->httpOptions ); |
127 | 134 | } else { |
128 | | - $raw = file_get_contents( $u ); // TESTME |
| 135 | + $raw = file_get_contents( $u ); |
129 | 136 | } |
130 | 137 | |
131 | 138 | if ( $raw ) { |
— | — | @@ -137,15 +144,15 @@ |
138 | 145 | } |
139 | 146 | |
140 | 147 | public function decodeData( $raw, $format = 'php' ) { |
141 | | - if ( $format == 'json' ) { |
142 | | - return FormatJson::decode( $raw, true ); // TESTME |
| 148 | + if ( $format == 'json' || $format == 'js' ) { |
| 149 | + return FormatJson::decode( $raw, true ); |
143 | 150 | } |
144 | 151 | |
145 | 152 | if ( $format == 'wddx' ) { |
146 | | - return wddx_unserialize( $raw ); // TESTME |
| 153 | + return wddx_unserialize( $raw ); |
147 | 154 | } |
148 | 155 | |
149 | | - if ( $format == 'php' ) { |
| 156 | + if ( $format == 'php' || $format == 'pser' ) { |
150 | 157 | return unserialize( $raw ); |
151 | 158 | } |
152 | 159 | |