[TASK] RTE: Remove unused css files
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Extension / MicroDataSchema.php
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Rtehtmlarea\RteHtmlAreaApi;
19
20 /**
21 * Microdata Schema extension for htmlArea RTE
22 *
23 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
24 */
25 class MicroDataSchema extends RteHtmlAreaApi {
26
27 /**
28 * The name of the plugin registered by the extension
29 *
30 * @var string
31 */
32 protected $pluginName = 'MicrodataSchema';
33
34 /**
35 * Path to this main locallang file of the extension relative to the extension directory
36 *
37 * @var string
38 */
39 protected $relativePathToLocallangFile = 'extensions/MicrodataSchema/locallang.xlf';
40
41 /**
42 * The comma-separated list of button names that the registered plugin is adding to the htmlArea RTE toolbar
43 *
44 * @var string
45 */
46 protected $pluginButtons = 'showmicrodata';
47
48 /**
49 * The name-converting array, converting the button names used in the RTE PageTSConfing to the button id's used by the JS scripts
50 *
51 * @var array
52 */
53 protected $convertToolbarForHtmlAreaArray = array(
54 'showmicrodata' => 'ShowMicrodata'
55 );
56
57 /**
58 * Return JS configuration of the htmlArea plugins registered by the extension
59 *
60 * @param string $rteNumberPlaceholder A dummy string for JS arrays
61 * @return string JS configuration for registered plugins
62 */
63 public function buildJavascriptConfiguration($rteNumberPlaceholder) {
64 $registerRTEinJavascriptString = '';
65 $schema = array(
66 'types' => array(),
67 'properties' => array()
68 );
69 // Parse configured schemas
70 if (is_array($this->thisConfig['schema.']) && is_array($this->thisConfig['schema.']['sources.'])) {
71 foreach ($this->thisConfig['schema.']['sources.'] as $source) {
72 $fileName = trim($source);
73 $absolutePath = GeneralUtility::getFileAbsFileName($fileName);
74 // Fallback to default schema file if configured file does not exists or is of zero size
75 if (!$fileName || !file_exists($absolutePath) || !filesize($absolutePath)) {
76 $fileName = 'EXT:' . $this->extensionKey . '/Resources/Public/Rdf/MicrodataSchema/SchemaOrgAll.rdf';
77 }
78 $fileName = $this->htmlAreaRTE->getFullFileName($fileName);
79 $rdf = GeneralUtility::getUrl($fileName);
80 if ($rdf) {
81 $this->parseSchema($rdf, $schema);
82 }
83 }
84 }
85 uasort($schema['types'], array($this, 'compareLabels'));
86 uasort($schema['properties'], array($this, 'compareLabels'));
87 // Insert no type and no property entries
88 if ($this->htmlAreaRTE->is_FE()) {
89 $noSchema = $GLOBALS['TSFE']->getLLL('No type', $this->LOCAL_LANG);
90 $noProperty = $GLOBALS['TSFE']->getLLL('No property', $this->LOCAL_LANG);
91 } else {
92 $noSchema = $GLOBALS['LANG']->getLL('No type');
93 $noProperty = $GLOBALS['LANG']->getLL('No property');
94 }
95 array_unshift($schema['types'], array('name' => 'none', 'label' => $noSchema));
96 array_unshift($schema['properties'], array('name' => 'none', 'label' => $noProperty));
97 // Convert character set
98 if ($this->htmlAreaRTE->is_FE()) {
99 $GLOBALS['TSFE']->csConvObj->convArray($schema, $this->htmlAreaRTE->outputCharset, 'utf-8');
100 }
101 // Store json encoded array in temporary file
102 $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) . '";';
103 return $registerRTEinJavascriptString;
104 }
105
106 /**
107 * Compare the labels of two schema types or properties for localized sort purposes
108 *
109 * @param array $a: first type/property definition array
110 * @param array $b: second type/property definition array
111 * @return int
112 */
113 protected function compareLabels($a, $b) {
114 return strcoll($a['label'], $b['label']);
115 }
116
117 /**
118 * Convert the xml rdf schema into an array
119 *
120 * @param string $string XML rdf schema to convert into an array
121 * @param array $schema: reference to the array to be filled
122 * @return void
123 */
124 protected function parseSchema($string, &$schema) {
125 $resources = array();
126 $types = array();
127 $properties = array();
128 // Load the document
129 $document = new \DOMDocument();
130 $document->loadXML($string);
131 if ($document) {
132 // Scan resource descriptions
133 $items = $document->getElementsByTagName('Description');
134 $itemsCount = $items->length;
135 foreach ($items as $item) {
136 $name = $item->getAttribute('rdf:about');
137 $type = $item->getElementsByTagName('type');
138 if ($name && $type->length) {
139 $type = $type->item(0)->getAttribute('rdf:resource');
140 $resource = array();
141 $resource['name'] = $name;
142 $labels = $item->getElementsByTagName('label');
143 if ($labels->length) {
144 foreach ($labels as $label) {
145 $language = $label->getAttribute('xml:lang');
146 if ($language === $this->language) {
147 $resource['label'] = $label->nodeValue;
148 } elseif ($language === 'en') {
149 $defaultLabel = $label->nodeValue;
150 }
151 }
152 if (!$resource['label']) {
153 $resource['label'] = $defaultLabel;
154 }
155 }
156 $comments = $item->getElementsByTagName('comment');
157 if ($comments->length) {
158 foreach ($comments as $comment) {
159 $language = $comment->getAttribute('xml:lang');
160 if ($language === $this->language) {
161 $resource['comment'] = $comment->nodeValue;
162 } elseif ($language === 'en') {
163 $defaultComment = $comment->nodeValue;
164 }
165 }
166 if (!$resource['comment']) {
167 $resource['comment'] = $defaultComment;
168 }
169 }
170 switch ($type) {
171 case 'http://www.w3.org/2000/01/rdf-schema#Class':
172 $subClassOfs = $item->getElementsByTagName('subClassOf');
173 if ($subClassOfs->length) {
174 foreach ($subClassOfs as $subClassOf) {
175 $resource['subClassOf'] = $subClassOf->getAttribute('rdf:resource');
176 }
177 }
178 // schema.rdfs.org/all.rdf may contain duplicates!!
179 if (!in_array($resource['name'], $types)) {
180 $schema['types'][] = $resource;
181 $types[] = $resource['name'];
182 }
183 break;
184 case 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property':
185 // Keep only the last level of the name
186 // This is the value we want in the itemprop attribute
187 $pos = strrpos($resource['name'], '/');
188 if ($pos) {
189 $resource['name'] = substr($resource['name'], $pos + 1);
190 }
191 $domains = $item->getElementsByTagName('domain');
192 if ($domains->length) {
193 foreach ($domains as $domain) {
194 $resource['domain'] = $domain->getAttribute('rdf:resource');
195 }
196 }
197 $ranges = $item->getElementsByTagName('range');
198 if ($ranges->length) {
199 foreach ($ranges as $range) {
200 $resource['range'] = $range->getAttribute('rdf:resource');
201 }
202 }
203 // schema.rdfs.org/all.rdf may contain duplicates!!
204 if (!in_array($resource['name'], $properties)) {
205 $schema['properties'][] = $resource;
206 $properties[] = $resource['name'];
207 }
208 break;
209 default:
210 // Do nothing
211 }
212 }
213 }
214 }
215 }
216
217 }