TYPO3 API  SVNRelease
class.t3lib_xml.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 /**
00028  * Contains class for creating XML output from records
00029  *
00030  * $Id: class.t3lib_xml.php 10121 2011-01-18 20:15:30Z ohader $
00031  * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
00032  *
00033  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00034  */
00035 /**
00036  * [CLASS/FUNCTION INDEX of SCRIPT]
00037  *
00038  *
00039  *
00040  *   86: class t3lib_xml
00041  *  102:     function t3lib_xml($topLevelName)
00042  *  113:     function setRecFields($table,$list)
00043  *  122:     function getResult()
00044  *  132:     function WAPHeader()
00045  *  144:     function renderHeader()
00046  *  155:     function renderFooter()
00047  *  167:     function newLevel($name,$beginEndFlag=0,$params=array())
00048  *  192:     function output($content)
00049  *  208:     function indent($b)
00050  *  224:     function renderRecords($table,$res)
00051  *  237:     function addRecord($table,$row)
00052  *  255:     function getRowInXML($table,$row)
00053  *  271:     function utf8($content)
00054  *  281:     function substNewline($string)
00055  *  292:     function fieldWrap($field,$value)
00056  *  301:     function WAPback()
00057  *  315:     function addLine($str)
00058  *
00059  * TOTAL FUNCTIONS: 17
00060  * (This index is automatically created/updated by the extension "extdeveval")
00061  *
00062  */
00063 
00064 
00065 /**
00066  * XML class, Used to create XML output from input rows.
00067  * Doesn't contain a lot of advanced features - pretty straight forward, practical stuff
00068  * You are encouraged to use this class in your own applications.
00069  *
00070  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00071  * @package TYPO3
00072  * @subpackage t3lib
00073  * @see user_xmlversion, user_wapversion
00074  */
00075 class t3lib_xml {
00076     var $topLevelName = 'typo3_test'; // Top element name
00077     var $XML_recFields = array(); // Contains a list of fields for each table which should be presented in the XML output
00078 
00079     var $XMLIndent = 0;
00080     var $Icode = '';
00081     var $XMLdebug = 0;
00082     var $includeNonEmptyValues = 0; // 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.
00083     var $lines = array();
00084 
00085     /**
00086      * Constructor, setting topLevelName to the input var
00087      *
00088      * @param   string      Top Level Name
00089      * @return  void
00090      */
00091     function t3lib_xml($topLevelName) {
00092         $this->topLevelName = $topLevelName;
00093     }
00094 
00095     /**
00096      * When outputting a input record in XML only fields listed in $this->XML_recFields for the current table will be rendered.
00097      *
00098      * @param   string      Table name
00099      * @param   string      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.
00100      * @return  void
00101      */
00102     function setRecFields($table, $list) {
00103         $this->XML_recFields[$table] = $list;
00104     }
00105 
00106     /**
00107      * Returns the result of the XML rendering, basically this is imploding the internal ->lines array with linebreaks.
00108      *
00109      * @return  string
00110      */
00111     function getResult() {
00112         $content = implode(LF, $this->lines);
00113         return $this->output($content);
00114     }
00115 
00116     /**
00117      * Initialize WML (WAP) document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
00118      *
00119      * @return  void
00120      */
00121     function WAPHeader() {
00122         $this->lines[] = '<?xml version="1.0"?>';
00123         $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . ' PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
00124         $this->newLevel($this->topLevelName, 1);
00125     }
00126 
00127     /**
00128      * Initialize "anonymous" XML document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
00129      * Encoding is set to UTF-8!
00130      *
00131      * @return  void
00132      */
00133     function renderHeader() {
00134         $this->lines[] = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
00135         $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . '>';
00136         $this->newLevel($this->topLevelName, 1);
00137     }
00138 
00139     /**
00140      * Sets the footer (of ->topLevelName)
00141      *
00142      * @return  void
00143      */
00144     function renderFooter() {
00145         $this->newLevel($this->topLevelName, 0);
00146     }
00147 
00148     /**
00149      * Indents/Outdents a new level named, $name
00150      *
00151      * @param   string      The name of the new element for this level
00152      * @param   boolean     If false, then this function call will *end* the level, otherwise create it.
00153      * @param   array       Array of attributes in key/value pairs which will be added to the element (tag), $name
00154      * @return  void
00155      */
00156     function newLevel($name, $beginEndFlag = 0, $params = array()) {
00157         if ($beginEndFlag) {
00158             $pList = '';
00159             if (count($params)) {
00160                 $par = array();
00161                 foreach ($params as $key => $val) {
00162                     $par[] = $key . '="' . htmlspecialchars($val) . '"';
00163                 }
00164                 $pList = ' ' . implode(' ', $par);
00165             }
00166             $this->lines[] = $this->Icode . '<' . $name . $pList . '>';
00167             $this->indent(1);
00168         } else {
00169             $this->indent(0);
00170             $this->lines[] = $this->Icode . '</' . $name . '>';
00171         }
00172     }
00173 
00174     /**
00175      * 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
00176      *
00177      * @param   string      The XML content to output
00178      * @return  string      Output
00179      */
00180     function output($content) {
00181         if ($this->XMLdebug) {
00182             return '<pre>' . htmlspecialchars($content) . '</pre>
00183             <hr /><font color="red">Size: ' . strlen($content) . '</font>';
00184         } else {
00185             return $content;
00186         }
00187     }
00188 
00189     /**
00190      * Increments/Decrements Indentation counter, ->XMLIndent
00191      * 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)
00192      *
00193      * @param   boolean     If true the XMLIndent var is increased, otherwise decreased
00194      * @return  string      ->Icode - the prefix string with TAB-chars.
00195      */
00196     function indent($b) {
00197         if ($b) {
00198             $this->XMLIndent++;
00199         } else {
00200             $this->XMLIndent--;
00201         }
00202         $this->Icode = '';
00203         for ($a = 0; $a < $this->XMLIndent; $a++) {
00204             $this->Icode .= TAB;
00205         }
00206         return $this->Icode;
00207     }
00208 
00209     /**
00210      * Takes a SQL result for $table and traverses it, adding rows
00211      *
00212      * @param   string      Tablename
00213      * @param   pointer     SQL resource pointer, should be reset
00214      * @return  void
00215      */
00216     function renderRecords($table, $res) {
00217         while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00218             $this->addRecord($table, $row);
00219         }
00220     }
00221 
00222     /**
00223      * Adds record, $row, from table, $table, to the internal array of XML-lines
00224      *
00225      * @param   string      Table name
00226      * @param   array       The row to add to XML structure from the table name
00227      * @return  void
00228      */
00229     function addRecord($table, $row) {
00230         $this->lines[] = $this->Icode . '<' . $table . ' uid="' . $row["uid"] . '">';
00231         $this->indent(1);
00232         $this->getRowInXML($table, $row);
00233         $this->indent(0);
00234         $this->lines[] = $this->Icode . '</' . $table . '>';
00235     }
00236 
00237     /**
00238      * Internal function for adding the actual content of the $row from $table to the internal structure.
00239      * 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!)
00240      * 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.
00241      *
00242      * @param   string      Table name
00243      * @param   array       Row from table to add.
00244      * @return  void
00245      * @access private
00246      */
00247     function getRowInXML($table, $row) {
00248         $fields = t3lib_div::trimExplode(',', $this->XML_recFields[$table], 1);
00249         foreach ($fields as $field) {
00250             if ($row[$field] || $this->includeNonEmptyValues) {
00251                 $this->lines[] = $this->Icode . $this->fieldWrap($field, $this->substNewline($this->utf8(htmlspecialchars($row[$field]))));
00252             }
00253         }
00254     }
00255 
00256     /**
00257      * UTF-8 encodes the input content (from ISO-8859-1!)
00258      *
00259      * @param   string      String content to UTF-8 encode
00260      * @return  string      Encoded content.
00261      */
00262     function utf8($content) {
00263         return utf8_encode($content);
00264     }
00265 
00266     /**
00267      * Substitutes LF characters with a '<newline/>' tag.
00268      *
00269      * @param   string      Input value
00270      * @return  string      Processed input value
00271      */
00272     function substNewline($string) {
00273         return str_replace(LF, '<newline/>', $string);
00274     }
00275 
00276     /**
00277      * Wraps the value in tags with element name, $field.
00278      *
00279      * @param   string      Fieldname from a record - will be the element name
00280      * @param   string      Value from the field - will be wrapped in the elements.
00281      * @return  string      The wrapped string.
00282      */
00283     function fieldWrap($field, $value) {
00284         return '<' . $field . '>' . $value . '</' . $field . '>';
00285     }
00286 
00287     /**
00288      * Creates the BACK button for WAP documents
00289      *
00290      * @return  void
00291      */
00292     function WAPback() {
00293         $this->newLevel('template', 1);
00294         $this->newLevel('do', 1, array('type' => 'accept', 'label' => 'Back'));
00295         $this->addLine('<prev/>');
00296         $this->newLevel('do');
00297         $this->newLevel('template');
00298     }
00299 
00300     /**
00301      * Add a line to the internal XML structure (automatically prefixed with ->Icode.
00302      *
00303      * @param   string      Line to add to the $this->lines array
00304      * @return  void
00305      */
00306     function addLine($str) {
00307         $this->lines[] = $this->Icode . $str;
00308     }
00309 }
00310 
00311 
00312 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_xml.php'])) {
00313     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_xml.php']);
00314 }
00315 ?>