[TASK] Move XLIFF handling to t3lib
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_tsparser_tsconfig.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * (c) 2007-2011 Kraft Bernhard (kraftb@kraftb.at)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * A TS-Config parsing class which performs condition evaluation
30 *
31 * @author Kraft Bernhard <kraftb@kraftb.at>
32 */
33
34 class t3lib_TSparser_TSconfig extends t3lib_TSparser {
35 /**
36 * @var array
37 */
38 protected $rootLine = array();
39
40 /**
41 * Parses the passed TS-Config using conditions and caching
42 *
43 * @param string $TStext: The TSConfig being parsed
44 * @param string $type: The type of TSConfig (either "userTS" or "PAGES")
45 * @param integer $id: The uid of the page being handled
46 * @param array $rootLine: The rootline of the page being handled
47 * @return array Array containing the parsed TSConfig and a flag wheter the content was retrieved from cache
48 * @see t3lib_TSparser
49 */
50 public function parseTSconfig($TStext, $type, $id = 0, array $rootLine = array()) {
51 $this->type = $type;
52 $this->id = $id;
53 $this->rootLine = $rootLine;
54 $hash = md5($type . ':' . $TStext);
55 $cachedContent = t3lib_BEfunc::getHash($hash, 0);
56
57 if ($cachedContent) {
58 $storedData = unserialize($cachedContent);
59 $storedMD5 = substr($cachedContent, -strlen($hash));
60 $storedData['match'] = array();
61 $storedData = $this->matching($storedData);
62 $checkMD5 = md5(serialize($storedData));
63
64 if ($checkMD5 == $storedMD5) {
65 $res = array(
66 'TSconfig' => $storedData['TSconfig'],
67 'cached' => 1,
68 );
69 } else {
70 $shash = md5($checkMD5 . $hash);
71 $cachedSpec = t3lib_BEfunc::getHash($shash, 0);
72 if ($cachedSpec) {
73 $storedData = unserialize($cachedSpec);
74 $res = array(
75 'TSconfig' => $storedData['TSconfig'],
76 'cached' => 1,
77 );
78 } else {
79 $storeData = $this->parseWithConditions($TStext);
80 $serData = serialize($storeData);
81 t3lib_BEfunc::storeHash($shash, $serData, $type . '_TSconfig');
82 $res = array(
83 'TSconfig' => $storeData['TSconfig'],
84 'cached' => 0,
85 );
86 }
87 }
88 } else {
89 $storeData = $this->parseWithConditions($TStext);
90 $serData = serialize($storeData);
91 $md5 = md5($serData);
92 t3lib_BEfunc::storeHash($hash, $serData . $md5, $type . '_TSconfig');
93 $res = array(
94 'TSconfig' => $storeData['TSconfig'],
95 'cached' => 0,
96 );
97 }
98
99 return $res;
100 }
101
102 /**
103 * Does the actual parsing using the parent objects "parse" method. Creates the match-Object
104 *
105 * @param string $TSconfig: The TSConfig being parsed
106 * @return array Array containing the parsed TSConfig, the encountered sectiosn, the matched sections
107 */
108 protected function parseWithConditions($TSconfig) {
109 /* @var $matchObj t3lib_matchCondition_backend */
110 $matchObj = t3lib_div::makeInstance('t3lib_matchCondition_backend');
111 $matchObj->setRootline($this->rootLine);
112 $matchObj->setPageId($this->id);
113
114 $this->parse($TSconfig, $matchObj);
115
116 $storeData = array(
117 'TSconfig' => $this->setup,
118 'sections' => $this->sections,
119 'match' => $this->sectionsMatch,
120 );
121
122 return $storeData;
123 }
124
125
126 /**
127 * Is just going through an array of conditions to determine which are matching (for getting correct cache entry)
128 *
129 * @param array $cc: An array containing the sections to match
130 * @return array The input array with matching sections filled into the "match" key
131 */
132 protected function matching(array $cc) {
133 if (is_array($cc['sections'])) {
134 /* @var $matchObj t3lib_matchCondition_backend */
135 $matchObj = t3lib_div::makeInstance('t3lib_matchCondition_backend');
136 $matchObj->setRootline($this->rootLine);
137 $matchObj->setPageId($this->id);
138
139 foreach ($cc['sections'] as $key => $pre) {
140 if ($matchObj->match($pre)) {
141 $cc['match'][$key] = $pre;
142 }
143 }
144 }
145
146 return $cc;
147 }
148
149 }
150
151
152 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser_tsconfig.php'])) {
153 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser_tsconfig.php']);
154 }
155 ?>