TYPO3  7.6
MicroDataSchema.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Extension;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
30  protected $pluginName = 'MicrodataSchema';
31 
37  protected $pluginButtons = 'showmicrodata';
38 
45  'showmicrodata' => 'ShowMicrodata'
46  );
47 
53  public function buildJavascriptConfiguration()
54  {
55  $schema = array(
56  'types' => array(),
57  'properties' => array()
58  );
59  // Parse configured schemas
60  if (is_array($this->configuration['thisConfig']['schema.']) && is_array($this->configuration['thisConfig']['schema.']['sources.'])) {
61  foreach ($this->configuration['thisConfig']['schema.']['sources.'] as $source) {
62  $fileName = trim($source);
63  $absolutePath = GeneralUtility::getFileAbsFileName($fileName);
64  // Fallback to default schema file if configured file does not exists or is of zero size
65  if (!$fileName || !file_exists($absolutePath) || !filesize($absolutePath)) {
66  $fileName = 'EXT:' . $this->extensionKey . '/Resources/Public/Rdf/MicrodataSchema/SchemaOrgAll.rdf';
67  }
68  $fileName = $this->getFullFileName($fileName);
69  $rdf = GeneralUtility::getUrl($fileName);
70  if ($rdf) {
71  $this->parseSchema($rdf, $schema);
72  }
73  }
74  }
75  uasort($schema['types'], array($this, 'compareLabels'));
76  uasort($schema['properties'], array($this, 'compareLabels'));
77  // Insert no type and no property entries
78  $languageService = $this->getLanguageService();
79  $noSchema = $languageService->sL('LLL:EXT:rtehtmlarea/Resources/Private/Language/Plugins/MicrodataSchema/locallang.xlf:No type');
80  $noProperty = $languageService->sL('LLL:EXT:rtehtmlarea/Resources/Private/Language/Plugins/MicrodataSchema/locallang.xlf:No property');
81  array_unshift($schema['types'], array('name' => 'none', 'label' => $noSchema));
82  array_unshift($schema['properties'], array('name' => 'none', 'label' => $noProperty));
83  // Store json encoded array in temporary file
84  return 'RTEarea[editornumber].schemaUrl = "' . $this->writeTemporaryFile('schema_' . $this->configuration['language'], 'js', json_encode($schema)) . '";';
85  }
86 
94  protected function compareLabels($a, $b)
95  {
96  return strcoll($a['label'], $b['label']);
97  }
98 
106  protected function parseSchema($string, &$schema)
107  {
108  $types = array();
109  $properties = array();
110  // Load the document
111  $document = new \DOMDocument();
112  $document->loadXML($string);
113  if ($document) {
114  // Scan resource descriptions
115  $items = $document->getElementsByTagName('Description');
116  foreach ($items as $item) {
117  $name = $item->getAttribute('rdf:about');
118  $type = $item->getElementsByTagName('type');
119  if ($name && $type->length) {
120  $type = $type->item(0)->getAttribute('rdf:resource');
121  $resource = array();
122  $resource['name'] = $name;
123  $labels = $item->getElementsByTagName('label');
124  if ($labels->length) {
125  foreach ($labels as $label) {
126  $language = $label->getAttribute('xml:lang');
127  if ($language === $this->language) {
128  $resource['label'] = $label->nodeValue;
129  } elseif ($language === 'en') {
130  $defaultLabel = $label->nodeValue;
131  }
132  }
133  if (!$resource['label']) {
134  $resource['label'] = $defaultLabel;
135  }
136  }
137  $comments = $item->getElementsByTagName('comment');
138  if ($comments->length) {
139  foreach ($comments as $comment) {
140  $language = $comment->getAttribute('xml:lang');
141  if ($language === $this->language) {
142  $resource['comment'] = $comment->nodeValue;
143  } elseif ($language === 'en') {
144  $defaultComment = $comment->nodeValue;
145  }
146  }
147  if (!$resource['comment']) {
148  $resource['comment'] = $defaultComment;
149  }
150  }
151  switch ($type) {
152  case 'http://www.w3.org/2000/01/rdf-schema#Class':
153  $subClassOfs = $item->getElementsByTagName('subClassOf');
154  if ($subClassOfs->length) {
155  foreach ($subClassOfs as $subClassOf) {
156  $resource['subClassOf'] = $subClassOf->getAttribute('rdf:resource');
157  }
158  }
159  // schema.rdfs.org/all.rdf may contain duplicates!!
160  if (!in_array($resource['name'], $types)) {
161  $schema['types'][] = $resource;
162  $types[] = $resource['name'];
163  }
164  break;
165  case 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property':
166  // Keep only the last level of the name
167  // This is the value we want in the itemprop attribute
168  $pos = strrpos($resource['name'], '/');
169  if ($pos) {
170  $resource['name'] = substr($resource['name'], $pos + 1);
171  }
172  $domains = $item->getElementsByTagName('domain');
173  if ($domains->length) {
174  foreach ($domains as $domain) {
175  $resource['domain'] = $domain->getAttribute('rdf:resource');
176  }
177  }
178  $ranges = $item->getElementsByTagName('range');
179  if ($ranges->length) {
180  foreach ($ranges as $range) {
181  $resource['range'] = $range->getAttribute('rdf:resource');
182  }
183  }
184  // schema.rdfs.org/all.rdf may contain duplicates!!
185  if (!in_array($resource['name'], $properties)) {
186  $schema['properties'][] = $resource;
187  $properties[] = $resource['name'];
188  }
189  break;
190  default:
191  // Do nothing
192  }
193  }
194  }
195  }
196  }
197 }