/** * Singleton. * * This avoids instantiating multiple Text_Wiki instances where a number * of objects are required in one call, e.g. to save memory in a * CMS invironment where several parsers are required in a single page. * * $single = & singleton(); * * or * * $single = & singleton('Parser', array('Prefilter', 'Delimiter', 'Code', 'Function', * 'Html', 'Raw', 'Include', 'Embed', 'Anchor', 'Heading', 'Toc', 'Horiz', * 'Break', 'Blockquote', 'List', 'Deflist', 'Table', 'Image', 'Phplookup', * 'Center', 'Newline', 'Paragraph', 'Url', 'Freelink', 'Interwiki', 'Wikilink', * 'Colortext', 'Strong', 'Bold', 'Emphasis', 'Italic', 'Underline', 'Tt', * 'Superscript', 'Subscript', 'Revise', 'Tighten')); * * Call using a subset of this list. The order of passing rulesets in the * $rules array is important! * * After calling this, call $single->setParseConf(), setRenderConf() or setFormatConf() * as usual for a constructed object of this class. * * The internal static array of singleton objects has no index on the parser * rules, the only index is on the parser name. So if you call this multiple * times with different rules but the same parser name, you will get the same * static parser object each time. * * @access public * @static * @since Method available since Release 1.1.0 * @param string $parser The parser to be used (defaults to 'Default'). * @param array $rules The set of rules to instantiate the object. This * will only be used when the first call to singleton is made, if included * in further calls it will be effectively ignored. * @return &object a reference to the Text_Wiki unique instantiation. */ function &singleton($parser = 'Default', $rules = null) { static $only = array(); if (!isset($only[$parser])) { $ret =& Text_Wiki::factory($parser, $rules); if (PEAR::isError($ret)) { return $ret; } $only[$parser] =& $ret; } return $only[$parser]; }
/** * Create a Text_Wiki object to handle the parsing * of Mediawiki syntax and define some configuration * option */ function configureParser() { $this->parser = Text_Wiki::factory('Mediawiki'); // do not replace space by underscore in wikilinks $this->parser->setParseConf('Wikilink', 'spaceUnderscore', false); // define possible localized namespace for image and files in the wikilink syntax $namespaces = $this->dom->getElementsByTagName('namespace'); $prefix = array('Image', 'image'); if ($namespaces->length > 0) { foreach ($namespaces as $namespace) { if ($namespace->getAttribute('key') == '-2' || $namespace->getAttribute('key') == '6') { $prefix[] = $namespace->nodeValue; } } } $this->parser->setParseConf('Image', 'prefix', $prefix); }
static function &singleton($parser = 'Default', $rules = null) { static $only = array(); if (!isset($only[$parser])) { $ret =& Text_Wiki::factory($parser, $rules); /* HVZM -- */ //if (Text_Wiki::isError($ret)) { if (@Text_Wiki::isError($ret)) { /* -- HVZM */ return $ret; } $only[$parser] =& $ret; } return $only[$parser]; }
/** * Transform text using Wiki library * * @author Greg Meiste <*****@*****.**> */ function transform($wikitext) { require_once 'Text/Wiki.php'; if (!defined('WIKI_PARSER')) { define('WIKI_PARSER', 'Default'); } $wiki =& Text_Wiki::factory(WIKI_PARSER); if (PEAR::isError($wiki)) { return sprintf(dgettext('wiki', 'Error! %s parser not found.'), WIKI_PARSER); } $wikitext = str_replace("'", "'", $wikitext); $db = new PHPWS_DB('wiki_pages'); $db->addColumn('title'); $pages = $db->select('col'); if (PEAR::isError($pages)) { PHPWS_Error::log($pages); $pages = NULL; } // Add custom parser rules $wiki->addPath('parse', PHPWS_SOURCE_DIR . 'mod/wiki/class/parse/'); $wiki->insertRule('Template', ''); $wiki->setRenderConf('xhtml', 'wikilink', 'pages', $pages); $wiki->setRenderConf('xhtml', 'wikilink', 'view_url', MOD_REWRITE_ENABLED ? 'wiki/%s' : './index.php?module=wiki&page=%s'); $wiki->setRenderConf('xhtml', 'wikilink', 'new_url', MOD_REWRITE_ENABLED ? 'wiki/%s' : './index.php?module=wiki&page=%s'); $wiki->setRenderConf('xhtml', 'toc', 'title', '<strong>' . dgettext('wiki', 'Table of Contents') . '</strong>'); $wiki->setRenderConf('xhtml', 'image', 'base', 'images/wiki/'); $wiki->setRenderConf('xhtml', 'url', 'target', PHPWS_Settings::get('wiki', 'ext_page_target')); $wiki->setRenderConf('xhtml', 'interwiki', 'target', PHPWS_Settings::get('wiki', 'ext_page_target')); $sites = array(); $db = new PHPWS_DB('wiki_interwiki'); $result = $db->select(); foreach ($result as $row) { $sites[$row['label']] = $row['url']; } $wiki->setRenderConf('xhtml', 'interwiki', 'sites', $sites); if (PHPWS_Settings::get('wiki', 'ext_chars_support')) { $wiki->setParseConf('Wikilink', 'ext_chars', true); $wiki->setFormatConf('Xhtml', 'translate', HTML_SPECIALCHARS); } // Setting CSS styles for tags $wiki->setRenderConf('xhtml', 'blockquote', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'code', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'code', 'css_code', 'wiki'); $wiki->setRenderConf('xhtml', 'code', 'css_php', 'wiki'); $wiki->setRenderConf('xhtml', 'code', 'css_html', 'wiki'); $wiki->setRenderConf('xhtml', 'deflist', 'css_dl', 'wiki'); $wiki->setRenderConf('xhtml', 'deflist', 'css_dt', 'wiki'); $wiki->setRenderConf('xhtml', 'deflist', 'css_dd', 'wiki'); $wiki->setRenderConf('xhtml', 'emphasis', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'heading', 'css_h1', 'wiki'); $wiki->setRenderConf('xhtml', 'heading', 'css_h2', 'wiki'); $wiki->setRenderConf('xhtml', 'heading', 'css_h3', 'wiki'); $wiki->setRenderConf('xhtml', 'heading', 'css_h4', 'wiki'); $wiki->setRenderConf('xhtml', 'heading', 'css_h5', 'wiki'); $wiki->setRenderConf('xhtml', 'heading', 'css_h6', 'wiki'); $wiki->setRenderConf('xhtml', 'horiz', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'image', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'interwiki', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'list', 'css_ol', 'wiki'); $wiki->setRenderConf('xhtml', 'list', 'css_ol_li', 'wiki'); $wiki->setRenderConf('xhtml', 'list', 'css_ul', 'wiki'); $wiki->setRenderConf('xhtml', 'list', 'css_ul_li', 'wiki'); $wiki->setRenderConf('xhtml', 'paragraph', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'table', 'css_table', 'wiki'); $wiki->setRenderConf('xhtml', 'table', 'css_tr', 'wiki'); $wiki->setRenderConf('xhtml', 'table', 'css_th', 'wiki'); $wiki->setRenderConf('xhtml', 'table', 'css_td', 'wiki'); $wiki->setRenderConf('xhtml', 'tt', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'url', 'css_inline', 'wiki'); $wiki->setRenderConf('xhtml', 'url', 'css_footnote', 'wiki'); $wiki->setRenderConf('xhtml', 'url', 'css_descr', 'wiki'); $wiki->setRenderConf('xhtml', 'url', 'css_img', 'wiki'); $wiki->setRenderConf('xhtml', 'wikilink', 'css', 'wiki'); $wiki->setRenderConf('xhtml', 'wikilink', 'css_new', 'wiki'); if (ALLOW_PROFANITY) { $wikitext = $wiki->transform($wikitext); } else { $wikitext = $wiki->transform(PHPWS_Text::profanityFilter($wikitext)); } if (PHPWS_Settings::get('wiki', 'allow_bbcode')) { $wikitext = PHPWS_Text::bb2html($wikitext, 'wiki'); } $wikitext = PHPWS_Text::fixAnchors($wikitext); $wikitext = str_replace('%23', '#', $wikitext); return PHPWS_Text::parseTag($wikitext); }
<?php // load Zend classes require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Rest_Client'); // load wikitext converter require_once 'Text/Wiki.php'; // instantiate a Text_Wiki object from the given class // and set it to use the Mediawiki adapter $wiki =& Text_Wiki::factory('Mediawiki'); // set some rendering rules $wiki->setRenderConf('xhtml', 'wikilink', 'view_url', 'http://en.wikipedia.org/wiki/'); $wiki->setRenderConf('xhtml', 'wikilink', 'pages', false); // define page title $query = 'The A Team'; try { // initialize REST client $wikipedia = new Zend_Rest_Client('http://en.wikipedia.org/w/api.php'); // set query parameters $wikipedia->action('query'); $wikipedia->prop('revisions'); $wikipedia->rvprop('content'); $wikipedia->format('xml'); $wikipedia->redirects('1'); $wikipedia->titles($query); // perform request // get page content as XML $result = $wikipedia->get(); $content = $result->query->pages->page->revisions->rev; } catch (Exception $e) { die('ERROR: ' . $e->getMessage());