Xmlwiki.php

From Organic Design wiki
Revision as of 01:26, 31 May 2005 by 219.88.61.75 (talk)

<php> <?php

  1. xmlWiki - MediaWiki XML Hack
  2. Nad - 2005-05-18
  3. Output Hook: Transforms output generated by MediaWiki and
  4. - Adds basic content-management, permissions and publishing to the MediaWiki
  5. - All articles are XML
  6. - Impliment by calling this script directly in browser
  1. FOR NOW:
  2. - only change rendering, not editing
  3. - rendering an xwArticle,
  4. - compiles php-transform-articles,CSS's & XSLT's from env+doc (the result of each becomes the source of the next)
  5. - execute php-transform-articles & XSLT's in order (CSS's are left to the end)
  6. - returns html-description with a CSS containing includes to CSS list
  7. - PAGE LAYOUT is just a php-transform-article which SURROUNDS the current DOM with raw layout
  8. - layouts would come with a separate collection of CSS's
  9. - CSS's need to themselves be collections of colours, headings etc
  1. LATER:
  2. use XInclude to build large docs from others
  3. use docBook or DSSSL
  4. <? is a processing instruction
  5. eg. <?xml-stylesheet href="person.css" type="text/css"?> or <?php...
  1. ---------------------------------------------------------------------------------------------------------------------- #
  1. INIT
  2. - do admin and exit if running directly
  3. - get wiki/request env
  4. - get article
  5. - permissions
  6. - build env (if action=raw, env is empty but a header is added)
  7. INPUT
  8. - validate xml and report error or,
  9. - process form-input (backslashes etc if necessary)
  10. OUTPUT
  11. - trap MediaWiki output
  12. - extract content and links
  13. - if view/preview/raw, do transform:
  14. - if docType is an xwArticle, see above
  15. - replace view/preview with transformed article
  16. - render output
  17. - if saving, foreach in publish-list,
  18. - do transform but with specified env
  19. - publish
  20. FUNCTION
  21. - get article as dom
  22. - process dom-article (pass PT,XSLT list and DOM-article, returns DOM-result)
  23. - etxract hrefs from text
  24. - message
  25. ADMIN
  26. - enable/disable patch
  1. ---------------------------------------------------------------------------------------------------------------------- #
  2. INIT
  1. If xmlwiki.php requested directly, return admin page

if (eregi("xmlwiki\\.php", $_SERVER['SCRIPT_NAME'])) xwAdminPage(); else {

# Otherwise set up xmlWiki global environment ready for input and output processing $xwArticleCache = array(); $xwEditingArticle = isset($_REQUEST['action'])&&($_REQUEST['action'] == 'edit'); $xwEditingXML = isset($_REQUEST['action'])&&($_REQUEST['action'] == 'xwXML'); $xwViewingArticle = null; # preview too $xwSavingArticle = isset($_REQUEST['wpSave']); $xwSpecialArticle = null; # do not treat content as xml $xwArticleTitle = $wgTitle->mTextform; $xwArticleDOM = xwGetArticle($xwArticleTitle); # DOM object of wiki article $xwUserName = $wgUser->mName; $xwUserDOM = xwGetArticle("User:$xwUserName"); $xwUserGroups = null; # Groups this user belongs to (incl. self)

# Get perms for this article $xwReadable = null; # True if this article is readable by this user $xwWritable = null; # True if this article is writeable by this user # Get groups this user belongs to from users' special-article $xwUserGroups[] = $xwUserName; // This allows users to be specified in permissions-lists in addition to groups

# Build transform environment (later from prefs, site etc) if ($action == 'raw') { # Raw article is requested, so env is empty $xwEnv = array(); # TODO: work out what kind of content it is (only if its an xwArticle can this work) # header("Content-type: application/pdf"); # header("Content-Disposition: attachment; filename=downloaded.pdf"); } else { # Create default environment (the name of another article) $xwEnv = array('defaultTemplate.php'); }

}

  1. ---------------------------------------------------------------------------------------------------------------------- #
  2. INPUT
  1. Parse and process input from forms
  2. - the form can just post changes for xml

function xwProcessInput() {

global $xwEditingArticle; global $xwSavingArticle; global $xwReadable; global $xwWritable; global $xwArticleDOM;

# If article is being updated, if ($xwSavingArticle) { global $_POST; # If valid xml posted, replace articleDOM if ($inputDOM = @domxml_open_mem($_POST["xw.xml"])) $articleDOM = $inputDOM; # later: parse POST updating articleDOM foreach (preg_grep("/^xw\\.(.+)$/", array_keys($_POST)) as $key) { $inputPath = split('.', $key); } # If description posted, update if (isset($_POST["wpTextbox1"])) { # write new description to articleDOM } # Convert articleDOM to text and put into POST data for MediaWiki to process $_POST["wpTextbox1"] = $articleDOM->dump_mem(false); }

}

  1. ---------------------------------------------------------------------------------------------------------------------- #
  2. OUTPUT
  1. Generate new output to send back to browser

function xwProcessOutput() {

global $wgOut;

global $xwEditingArticle; global $xwEditingXML; global $xwViewingArticle; global $xwSavingArticle; global $xwSpecialArticle; global $xwArticleTitle; global $xwArticleDOM; global $xwUserName; global $xwUserDOM; global $xwUserGroups; global $xwReadable; global $xwWritable;

$xwTransformedArticle = null; $xwPublishList = array(); $xwOutput = ;

# First trap Wiki output and extract content and links (should use $ver like Admin Page) ob_start(); $wgOut->output(); $xwWikiOutput = ob_get_clean(); $xwWikiContent = preg_match('/\(.+)/sm', $xwWikiOutput, $xwMatch) ? $xwMatch[1] : ; $xwUserLinks = preg_match('/id="p-personal"(.+?)<\/div>/s', $xwWikiOutput, $xwMatch) ? xwExtractHrefs($xwMatch[1]) : array(); $xwArticleLinks = preg_match('/id="p-cactions"(.+?)<\/div>/s', $xwWikiOutput, $xwMatch) ? xwExtractHrefs($xwMatch[1]) : array(); $xwArticleLinks[] = "<a href=\"./index.php?title=$xwArticleTitle&action=xwXML\">xml</a>";

# Test output

?>

<? print '
'.join(' | ', $xwUserLinks); print '
'.join(' | ', $xwArticleLinks); print '
'.htmlentities($xwArticleDOM->dump_mem(true)); print '
'.$xwWikiContent; ?>

<?

# Transform # - This env still needs to apply an outer layout etc # - The local-env and the DTD determines transforming/rendering # - if DTD says its just text (or special page) render normal # - The DTD's need to work outside wiki, so we need to be able to specifiy lang in request

# eg. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> # <!DOCTYPE greeting SYSTEM "hello.dtd">

# - article/input-description/input-xml/input-tree-operation # NOTE: we need to handle edit/discuss/history/login/prefs... if ($xwEditingXML) { # Editing XML, render textarea (later tree) $xwOutput .= '\n<textarea name="xw.xml">'.$xwArticleDOM->dump_mem(true).'</textarea>\n'; }

if ($xwEditingArticle || $xwSpecialArticle) { # If not viewing output wiki content $xwOutput .= $wikiContent; }

if ($xwViewingArticle) { # render article in current context (transform article) # - html, ....

# Apply stylesheets $xwTransformedArticle = null;

# Replace output-buffer content with processed article $xwOutput .= $xwTransformedArticle; }

# Publish the article if it has just been updated if ($xwSavingArticle) { foreach ($xwPublishList as $xwDestination) { if ($xwHandle = fopen($xwDestination, 'a')) { fwrite($xwHandle, $xwTransformedArticle); fclose($xwHandle); $xwOutput .= xwMessage("Article published to $cmDestination", 'green'); } else $xwOutput .= xwMessage("Failed to publish to $cmDestination", 'red'); } }

# Render output-buffer through page template print $xwOutput; }

  1. ---------------------------------------------------------------------------------------------------------------------- #
  2. FUNCTIONS
  1. Retreive XML wiki-article and return as a DOM object
  2. - DOM articles are cached in $xwArticleCache for reuse in recursive processing

function xwGetArticle($articleTitle) { # Return with results if already cached global $xwArticleCache; if (isset($xwArticleCache[$articleTitle])) return $xwArticleCache[$articleTitle]; # Get wiki article content (XML) $articleXML = new Article(Title::newFromText($articleTitle)); $articleXML = $articleXML->getContent(false); # Convert to DOM object if (!$articleDOM = @domxml_open_mem($articleXML)) $articleDOM = domxml_new_doc("1.0"); return $xwArticleCache[$articleTitle] = $articleDOM; }

  1. Return a list of the hrefs found in the passed content

function xwExtractHrefs($content) { preg_match_all('/(<a.+?<\/a>)/', $content, $match); return $match[1]; }

  1. Render a xmlWiki system message

function xwMessage($msg, $col) {

return "\n

$msg

\n";

}

  1. ---------------------------------------------------------------------------------------------------------------------- #
  2. ADMIN

function xwAdminPage() {

?>xmlWiki Admin Page

xmlWiki
xmlWiki Admin Page





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'); ?>


XHTML1.0 PHP MediaWiki



<? }

?> </php>