-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL-US-TaxonomyPackage.php
383 lines (338 loc) · 11.7 KB
/
XBRL-US-TaxonomyPackage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/**
* XBRL US Taxonomy Package handler
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2018 Lyquidity Solutions Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Implements functions to read a US taxonomy package. The US SEC taxonomy zip files
* have a distinctive structure. The root folder is of the form 'us-gaap-yyyy-mm-dd'
* Beneath this folder are four folders: 'dis', 'elts', 'entire' and 'stm'.
*
*/
class XBRL_US_TaxonomyPackage extends XBRL_SimplePackage
{
/**
* This will need to be updated when a new taxonomy is released
* @var array Indexed by publication date
*/
private static $defaultEntryPoints = array(
'2016-01-31' => 'http://xbrl.fasb.org/us-gaap/2016/entire/us-gaap-entryPoint-all-2016-01-31.xsd',
'2017-01-31' => 'http://xbrl.fasb.org/us-gaap/2017/entire/us-gaap-entryPoint-all-2017-01-31.xsd',
'2018-01-31' => 'http://xbrl.fasb.org/us-gaap/2018/entire/us-gaap-entryPoint-all-2018-01-31.xsd',
'2019-01-31' => 'http://xbrl.fasb.org/us-gaap/2019/entire/us-gaap-entryPoint-all-2019-01-31.xsd',
'2020-01-31' => 'http://xbrl.fasb.org/us-gaap/2020/entire/us-gaap-entryPoint-all-2020-01-31.xsd'
);
/**
* Notes about using this package instance
* @var string
*/
const notes = <<<EOT
A US taxonomy package implementation will determine then namespace and schema file to use based on the name of the
root folder which includes the date. These will be used to create a path to use in the cache. The packages will
not include an instance document.\n\n
EOT;
/**
* The actual path to the schema file
* @var string
*/
private $actualSchemaFilename;
/**
* The year of the taxonomy
* @var string
*/
private $taxonomyYear;
/**
* The prefix used for filenames
* @var string
*/
const filePrefix = "http://xbrl.fasb.org/us-gaap/";
/**
* The prefix used for namespaces
* @var string
*/
const namespacePrefix = "http://fasb.org/us-gaap/";
/**
* Provides a URL for the entity publishing the taxonomy. This element SHOULD be used to provide
* the primary website of the publishing entity. The URL used SHOULD be the same as that used in
* other Taxonomy Packages published by the same entity.
* @var string
*/
public $publisherURL;
/**
* Provides a date on which the taxonomy was published.
* @var string
*/
public $publicationDate = "";
/**
* Default constructor
* @param ZipArchive $zipArchive
*/
public function __construct( ZipArchive $zipArchive )
{
parent::__construct( $zipArchive );
$this->publisherURL = str_replace( "us-gaap/", "", XBRL_US_TaxonomyPackage::namespacePrefix );
}
/**
* Returns true if the zip file represents an SEC package
* {@inheritDoc}
* @see XBRL_Package::isPackage()
*/
public function isPackage()
{
// A simple package does not have a manifest file
try
{
// There must be just one root folder with the format 'us-gaap-yyyy-mm-dd'
if ( count( $this->contents ) != 1 ) return false;
$root = $this->getFirstFolderName();
$matches = false;
if ( ! preg_match( "/^us-gaap-(?<date>(?<year>\d{4,4})-\d{2,2}-\d{2,2})$/", $root, $matches ) )
{
return false;
}
$this->publicationDate = $matches['date'];
$found = false;
$this->traverseContents( function( $path, $name, $type ) use( &$found )
{
if ( $type == PATHINFO_DIRNAME ) return true;
$found = $name == XBRL_SEC_XML_Package::manifestFilename || XBRL::endsWith( $name, '.json' );
return ! $found;
} );
if ( $found )
{
throw XBRL_TaxonomyPackageException::withError( "tpe:metadataFileFound", "The package contains a manifest.xml file or JSON manifest file." );
}
if ( isset( XBRL_US_TaxonomyPackage::$defaultEntryPoints[ $this->publicationDate ] ) )
{
$this->schemaFile = XBRL_US_TaxonomyPackage::$defaultEntryPoints[ $this->publicationDate ];
$this->actualSchemaFilename = $this->getActualUri( $this->schemaFile );
// $this->schemaNamespace = XBRL_US_TaxonomyPackage::namespacePrefix . "{$matches['date']}/";
}
else
{
$this->schemaFile = XBRL_US_TaxonomyPackage::filePrefix . "{$this->taxonomyYear}/elts/us-gaap-{$matches['date']}.xsd";
$this->actualSchemaFilename = "{$root}/elts/us-gaap-{$matches['date']}.xsd";
}
$this->taxonomyYear = $matches['year'];
$contents = $this->getFile( $this->actualSchemaFilename );
if ( ! $contents )
{
throw XBRL_TaxonomyPackageException::withError( "tpe:noSchemaFileFound", "The package must contain a schema file (.xsd)." );
}
$this->schemaNamespace = $this->getTargetNamespace( $this->actualSchemaFilename, $contents );
// $this->schemaNamespace = XBRL_US_TaxonomyPackage::namespacePrefix . "{$matches['date']}/";
return true;
}
catch ( Exception $ex )
{
$code = $ex instanceof XBRL_TaxonomyPackageException ? $ex->error : $ex->getCode();
$this->errors[ $code ] = $ex->getMessage();
}
return false;
}
/**
* Workout which file is the schema file
* @return void
* @throws "tpe:schemaFileNotFound"
*/
protected function determineSchemaFile()
{
// This must do nothing because the schema file and namespace are set by the isPackage function
}
/**
* Returns a localized version of the schema file path
* @param string $uri
* @return string
*/
protected function getActualUri( $uri )
{
// Break the Uri into its parts
$path = parse_url( $uri, PHP_URL_PATH );
// Skip the scheme/domain/us-gaap/year/
$parts = explode( '/', $path );
array_splice( $parts, 0, 3, $this->getFirstFolderName() );
return join( '/', $parts );
return $this->actualSchemaFilename;
}
/**
* Returns XBRL_US_GAAP_2015
*/
public function getXBRLClassname()
{
return "XBRL_US_GAAP_2015";
}
/**
* Returns an array containing the root folder for the actual uri and the url
* @param string $actualUri
* @param string $uri
* @return string[]
*/
public function getCommonRootFolder( $actualUri, $uri )
{
// The uri will be like: http://xbrl.fasb.org/us-gaap/2016/elts/my.xsd
// The actualUri will be like: us-gaap-2016-01-31/dis/us-gaap-dis-acec-def-2016-01-31.xml
// The equivalents are http://xbrl.fasb.org/us-gaap/2016/ and us-gaap-2016-01-31
// $actualSuffix = str_replace( $this->getFirstFolderName(), "", $actualUri );
$pos = strpos( $actualUri, $this->getFirstFolderName() );
$actualSuffix = $pos !== false && $pos == 0
? substr_replace( $actualUri, '', $pos, strlen( $this->getFirstFolderName() ) )
: $actualUri;
$uri = XBRL_US_TaxonomyPackage::filePrefix . "{$this->taxonomyYear}$actualSuffix";
return array(
'actual' => $actualUri,
'uri' => $uri
);
}
/**
* Cache variable for entry points
*/
private $entryPoints = array();
/**
* Returns an array of schema file names defined as entry points
*/
public function getSchemaEntryPoints()
{
if ( $this->entryPoints ) return $this->entryPoints;
$entryPoints = array();
$this->traverseContents( function( $path, $name, $type ) use( &$entryPoints )
{
if ( $type == PATHINFO_DIRNAME ) return true;
if ( ! \XBRL::endsWith( $name, '.xsd' ) ) return true;
if ( ! XBRL::compiled_taxonomy_for_xsd( $name ) ) return true;
$common = $this->getCommonRootFolder( "$path$name", $this->schemaFile );
$entryPoints[] = $common['uri'];
return true;
} );
$this->entryPoints = $entryPoints;
return $entryPoints;
}
/**
* Return the details for an entry point identified by index or document name
* @param int|string $entryPointId
* @return array
*/
public function getDetailForEntryPoint( $entryPointId )
{
$entryPoints = $this->getSchemaEntryPoints();
if ( is_numeric( $entryPointId ) )
{
if ( isset( $entryPoints[ $entryPointId ] ) )
$entryPointId = $entryPoints[ $entryPointId ];
}
if ( is_string( $entryPointId ) )
{
if ( in_array( $entryPointId, $entryPoints ) )
{
$pos = strpos( $entryPointId, 'us-gaap/' . substr( $this->publicationDate, 0, 4 ) );
if ( $pos !== false )
{
$path = $this->getFirstFolderName() . "/" . substr( $entryPointId, $pos + 13 );
$xml = $this->getFileAsXML( $path );
$appInfo = $xml->children( XBRL_Constants::$standardPrefixes[ STANDARD_PREFIX_SCHEMA ] );
try
{
$documentation = $appInfo->annotation->documentation;
$attributes = $xml->attributes();
$namespace = property_exists( $attributes, 'targetNamespace' ) ? (string)$attributes->targetNamespace : '';
return array(
'name' => array(),
'description' => array( 'en_US' => trim( (string)$documentation ) ),
'version' => $this->publicationDate,
'entryPointDocument' => array( $entryPointId ),
'namespace' => $namespace
);
}
catch( \Exception $ex )
{
// Do nothing
}
}
}
}
return array();
}
/**
* Save the taxonomy for all entry points
*/
public function saveTaxonomy( $cacheLocation )
{
$entryPoints = $this->getSchemaEntryPoints();
$originalSchemaFile = $this->schemaFile;
$originalNamespace = $this->schemaNamespace;
$context = XBRL_Global::getInstance();
if ( ! $context->useCache )
{
$context->useCache = true;
$context->cacheLocation = $cacheLocation;
$context->initializeCache();
}
try
{
foreach ( $entryPoints as $index => $entryPoint )
{
if ( $context->findCachedFile( $entryPoint ) )
{
$this->errors[] = "The schema file '{$this->schemaFile}' already exists in the cache.";
continue;
}
// Get the namespace
$this->schemaFile = $entryPoint;
$this->schemaNamespace = null;
// Find the schema document
$content = trim( $this->getFile( $this->getActualUri( $this->schemaFile ) ) );
$result = $this->processSchemaDocument( $context, $content, false );
$this->setUrlMap();
if ( ! $result )
{
$this->errors[] = "Unable to process the schema document";
return false;
}
// Look for the non-schema file
$firstFolder = $this->getFirstFolderName();
$this->traverseContents( function( $path, $name, $type ) use( &$context, $firstFolder )
{
if ( $type == PATHINFO_DIRNAME ) return true;
$extension = pathinfo( $name, PATHINFO_EXTENSION );
if ( ! in_array( $extension, array( 'xml', 'xbrl', 'xsd' ) ) ) return true;
$path = $path ? "$path$name" : "$name";
$content = $this->getFile( $path );
if ( $firstFolder )
{
$path = preg_replace( "|^$firstFolder/|", '/us-gaap/' . $this->taxonomyYear . "/", $path );
}
$uri = XBRL::resolve_path( $this->schemaFile, $path ); // $common = $this->getCommonRootFolder( $path, $this->schemaFile );
if ( $context->findCachedFile( $uri ) ) return true;
$context->saveCacheFile( $uri, $content );
return true;
} );
}
return true;
}
catch( \Exception $ex )
{
$this->errors[] = $ex->getMessage();
}
finally
{
$this->schemaFile = $originalSchemaFile;
$this->schemaNamespace = $originalNamespace;
}
}
}