TYPO3 CMS  TYPO3_6-2
MicroDataSchema.php
Go to the documentation of this file.
1 <?php
3 
18 
25 
26  // The key of the TYPO3 extension that is extending htmlArea RTE
27  protected $extensionKey = 'rtehtmlarea';
28 
29  // The name of the plugin registered by the extension
30  protected $pluginName = 'MicrodataSchema';
31 
32  // Path to this main locallang file of the extension relative to the extension dir.
33  protected $relativePathToLocallangFile = 'extensions/MicrodataSchema/locallang.xlf';
34 
35  // Path to the skin (css) file relative to the extension dir
36  protected $relativePathToSkin = 'extensions/MicrodataSchema/skin/htmlarea.css';
37 
38  protected $htmlAreaRTE;
39 
40  // Reference to the invoking object
41  protected $thisConfig;
42 
43  // Reference to RTE PageTSConfig
44  protected $toolbar;
45 
46  // Reference to RTE toolbar array
47  protected $LOCAL_LANG;
48 
49  // Frontend language array
50  protected $pluginButtons = 'showmicrodata';
51 
53  'showmicrodata' => 'ShowMicrodata'
54  );
55 
62  public function buildJavascriptConfiguration($RTEcounter) {
63  $registerRTEinJavascriptString = '';
64  $schema = array(
65  'types' => array(),
66  'properties' => array()
67  );
68  // Parse configured schemas
69  if (is_array($this->thisConfig['schema.']) && is_array($this->thisConfig['schema.']['sources.'])) {
70  foreach ($this->thisConfig['schema.']['sources.'] as $source) {
71  $fileName = trim($source);
72  $absolutePath = GeneralUtility::getFileAbsFileName($fileName);
73  // Fallback to default schema file if configured file does not exists or is of zero size
74  if (!$fileName || !file_exists($absolutePath) || !filesize($absolutePath)) {
75  $fileName = 'EXT:' . $this->ID . '/extensions/MicrodataSchema/res/schemaOrgAll.rdf';
76  }
77  $fileName = $this->htmlAreaRTE->getFullFileName($fileName);
78  $rdf = GeneralUtility::getUrl($fileName);
79  if ($rdf) {
80  $this->parseSchema($rdf, $schema);
81  }
82  }
83  }
84  uasort($schema['types'], array($this, 'compareLabels'));
85  uasort($schema['properties'], array($this, 'compareLabels'));
86  // Insert no type and no property entries
87  if ($this->htmlAreaRTE->is_FE()) {
88  $noSchema = $GLOBALS['TSFE']->getLLL('No type', $this->LOCAL_LANG);
89  $noProperty = $GLOBALS['TSFE']->getLLL('No property', $this->LOCAL_LANG);
90  } else {
91  $noSchema = $GLOBALS['LANG']->getLL('No type');
92  $noProperty = $GLOBALS['LANG']->getLL('No property');
93  }
94  array_unshift($schema['types'], array('name' => 'none', 'label' => $noSchema));
95  array_unshift($schema['properties'], array('name' => 'none', 'label' => $noProperty));
96  // Convert character set
97  if ($this->htmlAreaRTE->is_FE()) {
98  $GLOBALS['TSFE']->csConvObj->convArray($schema, $this->htmlAreaRTE->outputCharset, 'utf-8');
99  }
100  // Store json encoded array in temporary file
101  $registerRTEinJavascriptString = LF . TAB . 'RTEarea[editornumber].schemaUrl = "' . ($this->htmlAreaRTE->is_FE() && $GLOBALS['TSFE']->absRefPrefix ? $GLOBALS['TSFE']->absRefPrefix : '') . $this->htmlAreaRTE->writeTemporaryFile('', ('schema_' . $this->htmlAreaRTE->language), 'js', json_encode($schema), TRUE) . '";';
102  return $registerRTEinJavascriptString;
103  }
104 
112  protected function compareLabels($a, $b) {
113  return strcoll($a['label'], $b['label']);
114  }
115 
123  protected function parseSchema($string, &$schema) {
124  $resources = array();
125  $types = array();
126  $properties = array();
127  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
128  $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
129  // Load the document
130  $document = new \DOMDocument();
131  $document->loadXML($string);
132  libxml_disable_entity_loader($previousValueOfEntityLoader);
133  if ($document) {
134  // Scan resource descriptions
135  $items = $document->getElementsByTagName('Description');
136  $itemsCount = $items->length;
137  foreach ($items as $item) {
138  $name = $item->getAttribute('rdf:about');
139  $type = $item->getElementsByTagName('type');
140  if ($name && $type->length) {
141  $type = $type->item(0)->getAttribute('rdf:resource');
142  $resource = array();
143  $resource['name'] = $name;
144  $labels = $item->getElementsByTagName('label');
145  if ($labels->length) {
146  foreach ($labels as $label) {
147  $language = $label->getAttribute('xml:lang');
148  if ($language === $this->language) {
149  $resource['label'] = $label->nodeValue;
150  } elseif ($language === 'en') {
151  $defaultLabel = $label->nodeValue;
152  }
153  }
154  if (!$resource['label']) {
155  $resource['label'] = $defaultLabel;
156  }
157  }
158  $comments = $item->getElementsByTagName('comment');
159  if ($comments->length) {
160  foreach ($comments as $comment) {
161  $language = $comment->getAttribute('xml:lang');
162  if ($language === $this->language) {
163  $resource['comment'] = $comment->nodeValue;
164  } elseif ($language === 'en') {
165  $defaultComment = $comment->nodeValue;
166  }
167  }
168  if (!$resource['comment']) {
169  $resource['comment'] = $defaultComment;
170  }
171  }
172  switch ($type) {
173  case 'http://www.w3.org/2000/01/rdf-schema#Class':
174  $subClassOfs = $item->getElementsByTagName('subClassOf');
175  if ($subClassOfs->length) {
176  foreach ($subClassOfs as $subClassOf) {
177  $resource['subClassOf'] = $subClassOf->getAttribute('rdf:resource');
178  }
179  }
180  // schema.rdfs.org/all.rdf may contain duplicates!!
181  if (!in_array($resource['name'], $types)) {
182  $schema['types'][] = $resource;
183  $types[] = $resource['name'];
184  }
185  break;
186  case 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property':
187  // Keep only the last level of the name
188  // This is the value we want in the itemprop attribute
189  $pos = strrpos($resource['name'], '/');
190  if ($pos) {
191  $resource['name'] = substr($resource['name'], $pos + 1);
192  }
193  $domains = $item->getElementsByTagName('domain');
194  if ($domains->length) {
195  foreach ($domains as $domain) {
196  $resource['domain'] = $domain->getAttribute('rdf:resource');
197  }
198  }
199  $ranges = $item->getElementsByTagName('range');
200  if ($ranges->length) {
201  foreach ($ranges as $range) {
202  $resource['range'] = $range->getAttribute('rdf:resource');
203  }
204  }
205  // schema.rdfs.org/all.rdf may contain duplicates!!
206  if (!in_array($resource['name'], $properties)) {
207  $schema['properties'][] = $resource;
208  $properties[] = $resource['name'];
209  }
210  break;
211  default:
212  // Do nothing
213  }
214  }
215  }
216  }
217  }
218 
219 }
static getUrl($url, $includeHeader=0, $requestHeaders=FALSE, &$report=NULL)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)