From 2ff7bcfc5d05fc0bd01201af33f80dae2f9cd688 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 13 Jan 2009 10:56:50 -0500 Subject: Move low-level xml outputting code to a class Made a class for outputting XML code --- lib/xmloutputter.php | 223 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 lib/xmloutputter.php (limited to 'lib') diff --git a/lib/xmloutputter.php b/lib/xmloutputter.php new file mode 100644 index 000000000..9a7d12e4c --- /dev/null +++ b/lib/xmloutputter.php @@ -0,0 +1,223 @@ +. + * + * @category Output + * @package Laconica + * @author Evan Prodromou + * @author Sarven Capadisli + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Low-level generator for XML + * + * This is a thin wrapper around PHP's XMLWriter. The main + * advantage is the element() method, which simplifies outputting + * an element. + * + * @category Output + * @package Laconica + * @author Evan Prodromou + * @author Sarven Capadisli + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + * @see Action + * @see HTMLOutputter + */ + +class XMLOutputter +{ + /** + * Wrapped XMLWriter object, which does most of the heavy lifting + * for output. + */ + + var $xw = null; + + /** + * Constructor + * + * Initializes the wrapped XMLWriter. + * + * @param $output URL for outputting, defaults to stdout + * @param $indent Whether to indent output, default true + */ + + function __construct($output='php://output', $indent=true) + { + $this->xw = new XMLWriter(); + $this->xw->openURI($output); + $this->xw->setIndent($indent); + $this->xw->startDocument('1.0', 'UTF-8'); + } + + /** + * Start a new XML document + * + * @param string $doc document element + * @param string $public public identifier + * @param string $system system identifier + * + * @return void + */ + + function startXML($doc=null, $public=null, $system=null) + { + if ($doc) { + $this->xw->writeDTD($doc, $public, $system); + } + } + + /** + * finish an XML document + * + * It's probably a bad idea to continue to use this object + * after calling endXML(). + * + * @return void + */ + + function endXML() + { + $this->xw->endDocument(); + $this->xw->flush(); + } + + /** + * output an XML element + * + * Utility for outputting an XML element. A convenient wrapper + * for a bunch of longer XMLWriter calls. This is best for + * when an element doesn't have any sub-elements; if that's the + * case, use elementStart() and elementEnd() instead. + * + * The $content element will be escaped for XML. If you need + * raw output, use elementStart() and elementEnd() with a call + * to raw() in the middle. + * + * @param string $tag Element type or tagname + * @param array $attrs Array of element attributes, as + * key-value pairs + * @param string $content string content of the element + * + * @return void + */ + + function element($tag, $attrs=null, $content=null) + { + $this->elementStart($tag, $attrs); + if (!is_null($content)) { + $this->xw->text($content); + } + $this->elementEnd($tag); + } + + /** + * output a start tag for an element + * + * Mostly used for when an element has content that's + * not a simple string. + * + * @param string $tag Element type or tagname + * @param array $attrs Array of element attributes + * + * @return void + */ + + function elementStart($tag, $attrs=null) + { + $this->xw->startElement($tag); + if (is_array($attrs)) { + foreach ($attrs as $name => $value) { + $this->xw->writeAttribute($name, $value); + } + } else if (is_string($attrs)) { + $this->xw->writeAttribute('class', $attrs); + } + } + + /** + * output an end tag for an element + * + * Used in conjunction with elementStart(). $tag param + * should match the elementStart() param. + * + * For HTML 4 compatibility, this method will force + * a full end element () even if the element is + * empty, except for a handful of exception tagnames. + * This is a hack. + * + * @param string $tag Element type or tagname. + * + * @return void + */ + + function elementEnd($tag) + { + static $empty_tag = array('base', 'meta', 'link', 'hr', + 'br', 'param', 'img', 'area', + 'input', 'col'); + // XXX: check namespace + if (in_array($tag, $empty_tag)) { + $this->xw->endElement(); + } else { + $this->xw->fullEndElement(); + } + } + + /** + * output plain text + * + * Text will be escaped. If you need it not to be, + * use raw() instead. + * + * @param string $txt Text to output. + * + * @return void + */ + + function text($txt) + { + $this->xw->text($txt); + } + + /** + * output raw xml + * + * This will spit out its argument verbatim -- no escaping is + * done. + * + * @param string $xml XML to output. + * + * @return void + */ + + function raw($xml) + { + $this->xw->writeRaw($xml); + } +} -- cgit v1.2.3-54-g00ecf From 81745625aa820b8a826412a3cc6ec11dff027c6d Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 13 Jan 2009 11:02:34 -0500 Subject: Remove XML-generating function from lib/util.php --- lib/util.php | 71 ------------------------------------------------------------ 1 file changed, 71 deletions(-) (limited to 'lib') diff --git a/lib/util.php b/lib/util.php index fbe04c6c4..dc26a705b 100644 --- a/lib/util.php +++ b/lib/util.php @@ -79,65 +79,6 @@ function common_user_error($msg, $code=400) common_show_footer(); } -$xw = null; - -// Start an HTML element -function common_element_start($tag, $attrs=null) -{ - global $xw; - $xw->startElement($tag); - if (is_array($attrs)) { - foreach ($attrs as $name => $value) { - $xw->writeAttribute($name, $value); - } - } else if (is_string($attrs)) { - $xw->writeAttribute('class', $attrs); - } -} - -function common_element_end($tag) -{ - static $empty_tag = array('base', 'meta', 'link', 'hr', - 'br', 'param', 'img', 'area', - 'input', 'col'); - global $xw; - // XXX: check namespace - if (in_array($tag, $empty_tag)) { - $xw->endElement(); - } else { - $xw->fullEndElement(); - } -} - -function common_element($tag, $attrs=null, $content=null) -{ - common_element_start($tag, $attrs); - global $xw; - if (!is_null($content)) { - $xw->text($content); - } - common_element_end($tag); -} - -function common_start_xml($doc=null, $public=null, $system=null, $indent=true) -{ - global $xw; - $xw = new XMLWriter(); - $xw->openURI('php://output'); - $xw->setIndent($indent); - $xw->startDocument('1.0', 'UTF-8'); - if ($doc) { - $xw->writeDTD($doc, $public, $system); - } -} - -function common_end_xml() -{ - global $xw; - $xw->endDocument(); - $xw->flush(); -} - function common_init_locale($language=null) { if(!$language) { @@ -319,18 +260,6 @@ function common_show_footer() common_end_xml(); } -function common_text($txt) -{ - global $xw; - $xw->text($txt); -} - -function common_raw($xml) -{ - global $xw; - $xw->writeRaw($xml); -} - function common_nav_menu() { $user = common_current_user(); -- cgit v1.2.3-54-g00ecf From bbb32dd2f6567bbbfc16db542f631dcc688020fe Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 13 Jan 2009 11:44:09 -0500 Subject: Extract HTML outputting code to a class HTMLOutputter Moved the common_* methods for low-level HTML output to its own class, HTMLOutputter in lib/htmloutputter.php. --- lib/htmloutputter.php | 353 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/util.php | 151 +-------------------- 2 files changed, 354 insertions(+), 150 deletions(-) create mode 100644 lib/htmloutputter.php (limited to 'lib') diff --git a/lib/htmloutputter.php b/lib/htmloutputter.php new file mode 100644 index 000000000..b3b2a5ae4 --- /dev/null +++ b/lib/htmloutputter.php @@ -0,0 +1,353 @@ +. + * + * @category Output + * @package Laconica + * @author Evan Prodromou + * @author Sarven Capadisli + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/lib/xmloutputter.php'; + +define('PAGE_TYPE_PREFS', + 'text/html,application/xhtml+xml,'. + 'application/xml;q=0.3,text/xml;q=0.2'); + +/** + * Low-level generator for HTML + * + * Abstracts some of the code necessary for HTML generation. Especially + * has methods for generating HTML form elements. Note that these have + * been created kind of haphazardly, not with an eye to making a general + * HTML-creation class. + * + * @category Output + * @package Laconica + * @author Evan Prodromou + * @author Sarven Capadisli + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + * @see Action + * @see HTMLOutputter + */ + +class HTMLOutputter extends XMLOutputter +{ + /** + * Constructor + * + * Just wraps the XMLOutputter constructor. + * + * @param string $output URI to output to, default = stdout + * @param boolean $indent Whether to indent output, default true + */ + + function __construct($output='php://output', $indent=true) + { + parent::__construct($output, $indent); + } + + /** + * Start an HTML document + * + * If $type isn't specified, will attempt to do content negotiation. + * + * Attempts to do content negotiation for language, also. + * + * @param string $type MIME type to use; default is to do negotation. + * + * @todo extract content negotiation code to an HTTP module or class. + * + * @return void + */ + + function startHTML($type=null) + { + if (!$type) { + $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ? + $_SERVER['HTTP_ACCEPT'] : null; + + // XXX: allow content negotiation for RDF, RSS, or XRDS + + $cp = common_accept_to_prefs($httpaccept); + $sp = common_accept_to_prefs(PAGE_TYPE_PREFS); + + $type = common_negotiate_type($cp, $sp); + + if (!$type) { + common_user_error(_('This page is not available in a '. + 'media type you accept'), 406); + exit(0); + } + } + + header('Content-Type: '.$type); + + $this->startXML('html', + '-//W3C//DTD XHTML 1.0 Strict//EN', + 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'); + + // FIXME: correct language for interface + + $language = common_language(); + + $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml', + 'xml:lang' => $language, + 'lang' => $language)); + } + + /** + * Output an HTML text input element + * + * Despite the name, it is specifically for outputting a + * text input element, not other elements. It outputs + * a cluster of elements, including a