Difference between revisions of "Xmlwiki.php"
| Line 215: | Line 215: | ||
fwrite($FH, $transformedArticle); | fwrite($FH, $transformedArticle); | ||
fclose($FH); | fclose($FH); | ||
| − | xwMessage("Article published to $ | + | xwMessage("Article published to $url"); |
| − | } else xwMessage("Failed to publish to $ | + | } else xwMessage("Failed to publish to $url", 'red'); |
} | } | ||
} | } | ||
| Line 248: | Line 248: | ||
$docType = $docType->name; | $docType = $docType->name; | ||
$docRoot = $article->document_element(); | $docRoot = $article->document_element(); | ||
| − | xwMessage("Transforming doctype \"$docType\"" | + | xwMessage("Transforming doctype \"$docType\""); |
# If it's one of ours, | # If it's one of ours, | ||
if (ereg('^xmlwiki:', $docType)) { | if (ereg('^xmlwiki:', $docType)) { | ||
| − | xwMessage('It\'s one of ours, getting transform list... | + | xwMessage('It\'s one of ours, getting transform list...'); |
# Get the documents transforms | # Get the documents transforms | ||
# - the first transforms adjust the document while its still a DOM object | # - the first transforms adjust the document while its still a DOM object | ||
| Line 265: | Line 265: | ||
# If object hasn't been transformed into a string yet (or it isn't one of our kind)... | # If object hasn't been transformed into a string yet (or it isn't one of our kind)... | ||
if (!is_string($article)) { | if (!is_string($article)) { | ||
| − | xwMessage('Dumping article-object to string | + | xwMessage('Dumping article-object to string'); |
# validate, report errors | # validate, report errors | ||
| Line 275: | Line 275: | ||
} | } | ||
| − | # Apply syntax highlighting if string | + | # Apply syntax highlighting if string |
| − | + | # - even if the article was an object, it will be a string by now | |
if (is_string($article)) { | if (is_string($article)) { | ||
xwGeshi(); # geshi should be an env-post-transform | xwGeshi(); # geshi should be an env-post-transform | ||
| Line 377: | Line 377: | ||
$article = $article->getContent(false); | $article = $article->getContent(false); | ||
} else $article = $xwArticle; | } else $article = $xwArticle; | ||
| − | # If article is XML, try to convert it to a DOM object (trap output for | + | # If article is XML, try to convert it to a DOM object (trap output for parser warnings) |
if (ereg('^<\\?xml', $article)) { | if (ereg('^<\\?xml', $article)) { | ||
ob_start(); | ob_start(); | ||
| Line 388: | Line 388: | ||
$err = preg_replace('/Warning.+?\\(\\): /', '', $err); | $err = preg_replace('/Warning.+?\\(\\): /', '', $err); | ||
foreach (split("\n", $err) as $msg) if ($msg) xwMessage($msg, 'red'); | foreach (split("\n", $err) as $msg) if ($msg) xwMessage($msg, 'red'); | ||
| − | |||
} | } | ||
ob_end_clean(); | ob_end_clean(); | ||
| Line 404: | Line 403: | ||
# Add message to queue | # Add message to queue | ||
| − | function xwMessage($msg, $col='blue') { | + | function xwMessage($msg, $col = 'blue') { |
global $xwMessages; | global $xwMessages; | ||
return $xwMessages[] = "<div class=\"xwMessage\"><font color=\"$col\">$msg</font></div>\n"; | return $xwMessages[] = "<div class=\"xwMessage\"><font color=\"$col\">$msg</font></div>\n"; | ||
Revision as of 06:50, 3 June 2005
<php> <?php
- xmlWiki - MediaWiki XML Hack
- Nad - 2005-05-18
- DTD's
- - using xmlwiki:document, xmlwiki:image etc is for validation purposes
- - xwTransformArticle is generic and cares only for xmlwiki:*
- CSS STYLES
- the are accessed via http, so their article url can be referred to locally in the html
- - maybe make the default request 'raw' and must specifically request 'view'
- - the default CSS's should deal with geshi colour/tab etc
- LATER:
- allow XIncludes to build large docs from others
- allow docBook and DSSSL content
- ---------------------------------------------------------------------------------------------------------------------- #
- SCRIPT CONTENTS
- INIT
- - do admin and exit if running directly
- - include geshi
- - get wiki & request environment
- - get article (as an object if possible)
- - get user-info
- - get object-specific info (perms, doc-root, doc-type)
- - build env (if action=raw, env is empty)
- INPUT
- - deny access if !writable
- - currently not doing anything else
- OUTPUT
- - trap MediaWiki output
- - extract links (not used yet)
- - apply security
- - transform for raw, view, preview
- - if saving, do publishing
- - render output & messages
- TRANSFORM
- - if article is an object,
- - process transforms if doctype is one of ours,
- - validate and apply XSLT if not
- - apply geshi on article
- TRANSFORMS
- - document transforms
- - image transforms
- - layout transforms
- - database table transforms
- - transform object to php-nav-tree
- FUNCTIONS
- - get article
- - etxract hrefs from text
- - message/log
- - geshi syntax highlighting
- ADMIN
- - enable/disable patch
- ---------------------------------------------------------------------------------------------------------------------- #
- INIT
- If xmlwiki.php requested directly, return admin page
if (eregi("xmlwiki\\.php", $_SERVER['SCRIPT_NAME'])) xwAdminPage(); else {
# Geshi syntax highlighting is called from xmlWiki and should be removed from LocalSettings.php require_once("extensions/GeshiSyntaxHighlight.php");
# Otherwise set up xmlWiki global environment ready for input and output processing $xwMessages = array(); $xwArticleCache = array(); $xwEnv = array();
$xwArticleTitle = $wgTitle->mTextform; $xwArticle = xwGetArticle($xwArticleTitle); $xwDocType = null; $xwDocRoot = null;
$xwEdit = isset($_REQUEST['action'])&&($_REQUEST['action'] == 'edit'); $xwView = (!isset($_REQUEST['action'])||(isset($_REQUEST['action'])&&($_REQUEST['action'] == 'view'))); $xwRaw = isset($_REQUEST['action'])&&($_REQUEST['action'] == 'raw'); $xwPreview = isset($_REQUEST['wpPreview']); $xwSave = isset($_REQUEST['wpSave']);
$xwUserName = $wgUser->mName; $xwUserArticle = xwGetArticle("User:$xwUserName"); $xwReadable = true; $xwWritable = true;
# If article is an object... if (is_object($xwArticle)) { # Get doc info $xwDocType = $xwArticle->doctype(); $xwDocType = $xwDocType->name; $xwDocRoot = $xwArticle->document_element(); # Get perms $xwUserGroups[] = $xwUserName; $xwReadable = null; $xwWritable = null; }
# Build default transform environment (later from prefs, site etc) if ($xwRaw) { # Raw article is requested, so env is empty } else { # Create default environment (the name of another article) # - a list of default object transforms # - and a list of default string transforms (env-post-transforms) $xwEnv[] = ; }
}
- ---------------------------------------------------------------------------------------------------------------------- #
- INPUT
- No input processing for now
- - used later for custom edit forms (database table article will need it)
- Parse and process input from forms
function xwProcessInput() {
global $xwArticle; global $xwArticleTitle; global $xwDocRoot; global $xwDocType;
global $xwSave;
global $xwReadable; global $xwWritable; global $xwMessages;
# If article is being updated, if ($xwSave) { #global $_POST; #if (!$xwReadable || !$xwWritable) die("I'm sorry Dave, I'm afraid I can't do that."); }
}
- ---------------------------------------------------------------------------------------------------------------------- #
- OUTPUT
- Generate new output to send back to browser
function xwProcessOutput() {
global $wgOut;
global $xwArticle; global $xwArticleTitle; global $xwDocRoot; global $xwDocType;
global $xwView; global $xwPreview; global $xwSave; global $xwRaw;
global $xwUserName; global $xwUserArticle; global $xwReadable; global $xwWritable; global $xwMessages;
# First trap Wiki output and extract content and links (should use $ver like Admin Page) ob_start(); $wgOut->output(); $out = ob_get_clean(); $userLinks = preg_match('/id="p-personal"(.+?)<\/div>/s', $out, $m) ? xwExtractHrefs($m[1]) : array(); $articleLinks = preg_match('/id="p-cactions"(.+?)<\/div>/s', $out, $m) ? xwExtractHrefs($m[1]) : array();
# Security if (!$xwReadable) { # change output to access denied message if !readable } if (!$xwWritable) { # remove edit link }
# If raw, output consists only of transformed article if ($xwRaw) $out = xwTransformArticle();
# If normal view, article must appear within wiki content elseif ($xwView) { if (preg_match('/^(.+)(.+)(.+)$/ms', $out, $m)) { xwMessage("Viewing doctype: $xwDocType"); $out = $m[1].xwTransformArticle().$m[3]; } else xwMessage('xmlWiki couldn\'t match view-output!', 'red'); }
# If preview, article must appear in wiki content preserving wiki editing content elseif ($xwPreview) {
if (preg_match('/^(.+.+Remember.+saved!.+?<\\/p>.+?
)(.+?)(<\\/p>.+.+)$/s', $out, $m)) { xwMessage("Previewing doctype: $xwDocType"); # We have to transform the content in wikiOutput not the article itself $xwArticle = html_entity_decode($m[2]); # It must be converted to DOM if its XML $xwArticle = xwGetArticle(); $out = $m[1].xwTransformArticle().$m[3]; } else xwMessage('xmlWiki couldn\'t match preview-output!', 'red'); } # If saving, check the publishing list elseif (0&& $xwSave && is_object($xwArticle)) { $transformedArticle = xwTransformArticle(); # Get publish list $publist = $xwDocRoot->get_elements_by_tagname('publish'); if (count($publist)) $publist = $publist[0]->dump_mem(true); $publist = split(',', $publist); # Publish each item foreach ($publist as $url) { if ($FH = fopen($url, 'a')) { fwrite($FH, $transformedArticle); fclose($FH); xwMessage("Article published to $url"); } else xwMessage("Failed to publish to $url", 'red'); } } # Add messages to output unless 'raw' requested if (!$xwRaw) $out = preg_replace('//', .join("\n",$xwMessages), $out); # Render final output and messages xwMessage('Rendering output'); print $out; }
- ---------------------------------------------------------------------------------------------------------------------- #
- TRANSFORM
- Transform an article object
- - non objects (strings) can't be transformed because they can't store any transform properties
- - but the environment post-transforms (incl. geshi) will be applied
function xwTransformArticle($article = false) { # Use global current article if none passed if ($article === false) { global $xwArticle; $article = $xwArticle; } if (is_object($article)) { # Get doc info $docType = $article->doctype(); $docType = $docType->name; $docRoot = $article->document_element(); xwMessage("Transforming doctype \"$docType\""); # If it's one of ours, if (ereg('^xmlwiki:', $docType)) { xwMessage('It\'s one of ours, getting transform list...'); # Get the documents transforms # - the first transforms adjust the document while its still a DOM object # - but at some point it would normally transform to text, # - it may then have some further text-based transforms $xwTransforms = $docRoot->get_elements_by_tagname('transform'); # - add php-transform-articles,CSS's & XSLT's to $xwEnv # - $xwEnv transforms first! } # If object hasn't been transformed into a string yet (or it isn't one of our kind)... if (!is_string($article)) { xwMessage('Dumping article-object to string'); # validate, report errors # apply stylesheet/XSLT if xml asks for one, # Convert DOM back to XML $article = $article->dump_mem(true); } } # Apply syntax highlighting if string # - even if the article was an object, it will be a string by now if (is_string($article)) { xwGeshi(); # geshi should be an env-post-transform # env post-processing } return $article; }
- ---------------------------------------------------------------------------------------------------------------------- #
- TRANSFORMS
- - these transforms are only temporarily here - they should be articles
- - we should use DTD's for each kind: document, image, layout, table
function xwTransform_document_xxx() { # documents are built from parts, # the parts are a tree in its body # includes 'edit' link for each (like section, and also like table) }
- A document object is finalised by transforming it to its body's text
function xwTransform_document_render() { global $xwArticle; $root = $xwArticle->document_element(); $body = $root->get_elements_by_tagname('body'); if (count($body)) $xwArticle = $body[0]->dump_mem(true); } function xwTransform_layout_xxx() { # for layout container style article } function xwTransform_image_xxx() { # object contains properties for loading and transforming an image # image needs no body # Get the global image on which imageTransforms work global $xwArticleImage; }
- Converts the in-memory-working image into article content
function xwTransform_image_render() { global $xwArticle; global $xwArticleTitle; global $xwArticleImage; global $xwRaw; if ($xwRaw) { # render article directly if 'raw' request imagePng($xwArticleImage); # Clear article or it will render as xml $xwArticle = ; # set header to image header("Content-type: image/png"); } else { # the raw image is not being requested, render image in-page with info instead $xwArticle = "<img src=\"$xwArticleTitle?action=raw\" alt=\"$xwArticleTitle\"/>"; } }
- A table object is an interface to a database table
- - this can't be implimented fully until the 'edit' hook is done
- - the query result is a list in document body, so any normal document transforms can follow it
function xwTransform_table_xxx() { # - view lists the table/result with links to edit each (like a normal composition-doc) # - also form for create/edit/edlete # - result rows are in document body (just like a content-tree in a document would be) # NODE-LIKE-DB # - if all tables have GUID for pri-key, # - and all have time/duration (or cycle?), # - and we have master list of cols-to-table # - then we could create a meta-query (resulting in pri-key-list of all matches) }
- Transform the body content to a xtml/javascript tree
function xwTransform_tree() { # import php-tree-component }
- ---------------------------------------------------------------------------------------------------------------------- #
- FUNCTIONS
- Retreive wiki-article
- - A DOM object is returned if possible,
- - or otherwise string content is returned (which can be parsed for highlighting)
- - if nothing/false passed, then the global $xwArticle is used
function xwGetArticle($articleTitle = false) { global $xwArticleCache; global $xwArticle; # Return with results if already cached if ($articleTitle && isset($xwArticleCache[$articleTitle])) return $xwArticleCache[$articleTitle]; # Get wiki article content (or use global if no article passed) if ($articleTitle) { $article = new Article(Title::newFromText($articleTitle)); $article = $article->getContent(false); } else $article = $xwArticle; # If article is XML, try to convert it to a DOM object (trap output for parser warnings) if (ereg('^<\\?xml', $article)) { ob_start(); if ($doc = domxml_open_mem($article)) $article = $doc; else { # Could not convert, extract error messages from output xwMessage("\"$articleTitle\" is not well-formed XML:", 'red'); $err = preg_replace("/<.+?>/", "", ob_get_contents()); $err = preg_replace('/ in .+? on line.+?[0-9]+/', , $err); $err = preg_replace('/Warning.+?\\(\\): /', , $err); foreach (split("\n", $err) as $msg) if ($msg) xwMessage($msg, 'red'); } ob_end_clean(); } # Cache contents if ($articleTitle) $xwArticleCache[$articleTitle] = $article; return $article; }
- Return a list of the hrefs found in the passed content
function xwExtractHrefs($content) { preg_match_all('/(<a.+?<\/a>)/', $content, $match); return $match[1]; }
- Add message to queue
function xwMessage($msg, $col = 'blue') { global $xwMessages; return $xwMessages[] = "
\n";
}
- Include Geshi syntax highlighting
- - Geshi should be removed from LocalSettings.php
- - TODO - geshi should be an env-post-transform
function xwGeshi() { xwMessage("Hi, Geshi here, sorry I'm not currently functional."); return false;
global $xwArticle; global $xwArticleTitle;
# First check for entire article highlighting if (ereg('\\.as$', $xwArticleTitle)) $xwLang = 'actionscript'; elseif (ereg('\\.py$', $xwArticleTitle)) $xwLang = 'python'; elseif (ereg('\\.java$', $xwArticleTitle)) $xwLang = 'java'; elseif (ereg('\\.(cpp)|(h)$', $xwArticleTitle)) $xwLang = 'cpp'; elseif (ereg('\\.js$', $xwArticleTitle)) $xwLang = 'javascript'; elseif (ereg('\\.css$', $xwArticleTitle)) $xwLang = 'css'; elseif (ereg('^#![/a-zA-Z0-9]+\\/perl', $xwArticle)) $xwLang = 'perl'; elseif (ereg('^#![/a-zA-Z0-9]+sh', $xwArticle)) $xwLang = 'bash'; elseif (ereg('^<?xml', $xwArticle)) $xwLang = 'xml'; elseif (ereg('^<?html', $xwArticle)) $xwLang = 'html4strict'; else { # Otherwise, parse article for internal highlights # <?,<% lang (default % is asp, default ? is php) # <script language= (default is javascript)
$xwLang = false; }
# Highlight entire article if xwLang has been set if ($xwLang) { $xwGeshi = new GeSHi($xwArticle, $xwLang, "extensions/geshi/geshi"); $xwGeshi->set_tab_width(4); $geshi->set_keywords_style(1, 'font-weight: 100;', true); #$geshi->set_comments_style #$geshi->set_escape_char_style #$geshi->set_brackets_style #$geshi->set_strings_style #$geshi->set_numbers_style #$geshi->set_methods_style #$geshi->set_symbols_style #$geshi->set_regexps_style #$geshi->set_script_style #$xwArticle = $xwGeshi->parse_code(); } }
- ---------------------------------------------------------------------------------------------------------------------- #
- ADMIN
function xwAdminPage() {
?>
|
output\\(\\);/m";
# Apply patches
if (preg_match($requireMatch[$ver], $file = join('', file('./index.php')))
&& preg_match($inputMatch[$ver], $file)
&& preg_match($outputMatch[$ver], $file)) {
$file = preg_replace($requireMatch[$ver], "wfProfileOut( 'main-misc-setup' );\nrequire_once( './xmlwiki.php' );", $file);
$file = preg_replace($inputMatch[$ver], "xwProcessInput();\nwfProfileIn( 'main-action' );", $file);
$file = preg_replace($outputMatch[$ver], "xwProcessOutput();", $file);
if ($handle = fopen('./index.php', 'w')) {
fwrite($handle, $file);
fclose($handle);
} else print xwMessage('Could not open index.php for writing!', '#d00000');
} else print xwMessage('Could not match content of index.php script!', '#d00000');
}
elseif (array_key_exists('disable', $_GET)) {
# Replace from backup
if (!copy('./index.bak', './index.php'))
print xwMessage('Could not copy index.bak over index.php', '#d00000');
}
# Print current state
if (filesize('./index.php') == filesize('./index.bak'))
print xwMessage('xmlWiki is currently disabled. [ enable ]', '#003366');
else print xwMessage('xmlWiki is currently enabled. [ disable ]', '#003366');
?>
| |||||
<?
}
?> </php>







