[-FEATURE] Remove xml export in fullsearch, deprecate t3lib_xml
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_xml.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * XML class, Used to create XML output from input rows.
30 * Doesn't contain a lot of advanced features - pretty straight forward, practical stuff
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 * @package TYPO3
34 * @subpackage t3lib
35 * @deprecated since TYPO3 6.0. It will be removed with TYPO3 6.2, please use other xml helper solutions instead.
36 */
37 class t3lib_xml {
38 // Top element name
39 public $topLevelName = 'typo3_test';
40 // Contains a list of fields for each table which should be presented in the XML output
41 public $XML_recFields = array();
42
43 public $XMLIndent = 0;
44 public $Icode = '';
45 public $XMLdebug = 0;
46 // If set, all fields from records are rendered no matter their content. If not set, only 'true' (that is '' or zero) fields make it to the document.
47 public $includeNonEmptyValues = 0;
48 public $lines = array();
49
50 /**
51 * Constructor, setting topLevelName to the input var
52 *
53 * @param string $topLevelName Top Level Name
54 * @return void
55 */
56 public function __construct($topLevelName) {
57 t3lib_div::deprecationLog(
58 'Class t3lib_div is deprecated since TYPO3 6.0. ' .
59 'It will be removed with TYPO3 6.2, please use other xml helper solutions instead.'
60 );
61 $this->topLevelName = $topLevelName;
62 }
63
64 /**
65 * When outputting a input record in XML only fields listed in $this->XML_recFields for the current table will be rendered.
66 *
67 * @param string $table Table name
68 * @param string $list Commalist of fields names from the table, $table, which is supposed to be rendered in the XML output. If a field is not in this list, it is not rendered.
69 * @return void
70 */
71 public function setRecFields($table, $list) {
72 $this->XML_recFields[$table] = $list;
73 }
74
75 /**
76 * Returns the result of the XML rendering, basically this is imploding the internal ->lines array with linebreaks.
77 *
78 * @return string
79 */
80 public function getResult() {
81 $content = implode(LF, $this->lines);
82 return $this->output($content);
83 }
84
85 /**
86 * Initialize WML (WAP) document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
87 *
88 * @return void
89 */
90 public function WAPHeader() {
91 $this->lines[] = '<?xml version="1.0"?>';
92 $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . ' PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
93 $this->newLevel($this->topLevelName, 1);
94 }
95
96 /**
97 * Initialize "anonymous" XML document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
98 * Encoding is set to UTF-8!
99 *
100 * @return void
101 */
102 public function renderHeader() {
103 $this->lines[] = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
104 $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . '>';
105 $this->newLevel($this->topLevelName, 1);
106 }
107
108 /**
109 * Sets the footer (of ->topLevelName)
110 *
111 * @return void
112 */
113 public function renderFooter() {
114 $this->newLevel($this->topLevelName, 0);
115 }
116
117 /**
118 * Indents/Outdents a new level named, $name
119 *
120 * @param string $name The name of the new element for this level
121 * @param boolean $beginEndFlag If FALSE, then this function call will *end* the level, otherwise create it.
122 * @param array $params Array of attributes in key/value pairs which will be added to the element (tag), $name
123 * @return void
124 */
125 public function newLevel($name, $beginEndFlag = 0, $params = array()) {
126 if ($beginEndFlag) {
127 $pList = '';
128 if (count($params)) {
129 $par = array();
130 foreach ($params as $key => $val) {
131 $par[] = $key . '="' . htmlspecialchars($val) . '"';
132 }
133 $pList = ' ' . implode(' ', $par);
134 }
135 $this->lines[] = $this->Icode . '<' . $name . $pList . '>';
136 $this->indent(1);
137 } else {
138 $this->indent(0);
139 $this->lines[] = $this->Icode . '</' . $name . '>';
140 }
141 }
142
143 /**
144 * Function that will return the content from string $content. If the internal ->XMLdebug flag is set the content returned will be formatted in <pre>-tags
145 *
146 * @param string The XML content to output
147 * @return string Output
148 */
149 public function output($content) {
150 if ($this->XMLdebug) {
151 return '<pre>' . htmlspecialchars($content) . '</pre>
152 <hr /><font color="red">Size: ' . strlen($content) . '</font>';
153 } else {
154 return $content;
155 }
156 }
157
158 /**
159 * Increments/Decrements Indentation counter, ->XMLIndent
160 * Sets and returns ->Icode variable which is a line prefix consisting of a number of tab-chars corresponding to the indent-levels of the current posision (->XMLindent)
161 *
162 * @param boolean $b If TRUE the XMLIndent var is increased, otherwise decreased
163 * @return string ->Icode - the prefix string with TAB-chars.
164 */
165 public function indent($b) {
166 if ($b) {
167 $this->XMLIndent++;
168 } else {
169 $this->XMLIndent--;
170 }
171 $this->Icode = '';
172 for ($a = 0; $a < $this->XMLIndent; $a++) {
173 $this->Icode .= TAB;
174 }
175 return $this->Icode;
176 }
177
178 /**
179 * Takes a SQL result for $table and traverses it, adding rows
180 *
181 * @param string $table Tablename
182 * @param pointer $res SQL resource pointer, should be reset
183 * @return void
184 */
185 public function renderRecords($table, $res) {
186 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
187 $this->addRecord($table, $row);
188 }
189 $GLOBALS['TYPO3_DB']->sql_free_result($res);
190 }
191
192 /**
193 * Adds record, $row, from table, $table, to the internal array of XML-lines
194 *
195 * @param string $table Table name
196 * @param array $row The row to add to XML structure from the table name
197 * @return void
198 */
199 public function addRecord($table, $row) {
200 $this->lines[] = $this->Icode . '<' . $table . ' uid="' . $row['uid'] . '">';
201 $this->indent(1);
202 $this->getRowInXML($table, $row);
203 $this->indent(0);
204 $this->lines[] = $this->Icode . '</' . $table . '>';
205 }
206
207 /**
208 * Internal function for adding the actual content of the $row from $table to the internal structure.
209 * Notice that only fields from $table that are listed in $this->XML_recFields[$table] (set by setRecFields()) will be rendered (and in the order found in that array!)
210 * Content from the row will be htmlspecialchar()'ed, UTF-8 encoded and have LF (newlines) exchanged for '<newline/>' tags. The element name for a value equals the fieldname from the record.
211 *
212 * @param string $table Table name
213 * @param array $row Row from table to add.
214 * @return void
215 * @access private
216 */
217 public function getRowInXML($table, $row) {
218 $fields = t3lib_div::trimExplode(',', $this->XML_recFields[$table], 1);
219 foreach ($fields as $field) {
220 if ($row[$field] || $this->includeNonEmptyValues) {
221 $this->lines[] = $this->Icode . $this->fieldWrap($field, $this->substNewline($this->utf8(htmlspecialchars($row[$field]))));
222 }
223 }
224 }
225
226 /**
227 * UTF-8 encodes the input content (from ISO-8859-1!)
228 *
229 * @param string $content String content to UTF-8 encode
230 * @return string Encoded content.
231 */
232 public function utf8($content) {
233 return utf8_encode($content);
234 }
235
236 /**
237 * Substitutes LF characters with a '<newline/>' tag.
238 *
239 * @param string $string Input value
240 * @return string Processed input value
241 */
242 public function substNewline($string) {
243 return str_replace(LF, '<newline/>', $string);
244 }
245
246 /**
247 * Wraps the value in tags with element name, $field.
248 *
249 * @param string $field Fieldname from a record - will be the element name
250 * @param string $value Value from the field - will be wrapped in the elements.
251 * @return string The wrapped string.
252 */
253 public function fieldWrap($field, $value) {
254 return '<' . $field . '>' . $value . '</' . $field . '>';
255 }
256
257 /**
258 * Creates the BACK button for WAP documents
259 *
260 * @return void
261 */
262 public function WAPback() {
263 $this->newLevel('template', 1);
264 $this->newLevel('do', 1, array('type' => 'accept', 'label' => 'Back'));
265 $this->addLine('<prev/>');
266 $this->newLevel('do');
267 $this->newLevel('template');
268 }
269
270 /**
271 * Add a line to the internal XML structure (automatically prefixed with ->Icode.
272 *
273 * @param string Line to add to the $this->lines array
274 * @return void
275 */
276 public function addLine($str) {
277 $this->lines[] = $this->Icode . $str;
278 }
279 }
280
281 ?>