Difference between revisions of "Extension:Workflow.php"

From Organic Design wiki
m
(fill in some more)
Line 31: Line 31:
 
# Expand the #tags to reveal the current state and hide the others and add javascript
 
# Expand the #tags to reveal the current state and hide the others and add javascript
 
# - note the hidden states mustn't be rendered because they contain categorisation links which shouldn't be processed
 
# - note the hidden states mustn't be rendered because they contain categorisation links which shouldn't be processed
function wfWorkflowExpandTag(&$parser) {
+
function wfWorkflowExpandTag() {
  
 
# Populate $argv with both named and numeric parameters
 
# Populate $argv with both named and numeric parameters
Line 37: Line 37:
 
$items = array();
 
$items = array();
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $items[] = $arg;
+
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
 
}
 
}
 +
 +
# Extract reserved parameters
 +
$id      = isset($argv['id'])      ? $argv['id']    : uniqid();
 +
unset($argv['id']);
 +
$state    = isset($argv['state'])    ? $argv['state']  : 0;
 +
unset($argv['state']);
 +
$format  = isset($argv['format'])  ? $argv['format'] : 'tag';
 +
unset($argv['format']);
 +
$class    = isset($argv['class'])    ? $argv['class']  : "workflow-$format";
 +
unset($argv['class']);
 +
$location = isset($argv['location']) ? $argv['format'] : 'inline';
 +
unset($argv['location']);
 +
$options  = isset($argv['options'])  ? $argv['format'] : '';
 +
unset($argv['options']);
  
 
# Render the visible item
 
# Render the visible item
 +
# - could also render all non-visible items here too, but with a cloned parser object so that the categorisation doesn't occur
 +
# - that way the JS can instantly cycle through all rendered states, then set new state and get updated catlinks via ajax
 
$text = '';
 
$text = '';
foreach ($items as $k => $v) $text .= "<div class='workflow-item' id='$k'>$v</div>\n";
+
foreach ($argv as $k => $v) {
$text = "<div class='workflow-tag' id='$id'>\n$text</div>";
+
if ($format == 'tag') {
 +
$text = "<div class='$class' id='$id'>\n$text</div>";
 +
}
 +
elseif ($format == 'list') {
 +
$text .= "<div class='$class item' id='$k'>$v</div>\n";
 +
$text = "<div class='$class' id='$id'>\n$text</div>";
 +
}
 +
}
  
 
return $text;
 
return $text;
Line 49: Line 72:
  
 
# Update a tag state in an article
 
# Update a tag state in an article
function wfWorkflowUpdateTag($title,$id,$state) {
+
function wfWorkflowUpdateTag($pagename,$id,$state) {
 +
global $wgWorkflowMagic,$wgWorkflowTagState;
 +
$title = Title:newFromtext($pagename);
 +
if (is_object($title) && $title->exists()) {
 +
$article = new Article($title);
 +
$text = $article->getContent();
 +
$wgWorkflowTagState = $state;
 +
preg_replace_callback("/\\{\\{#$wgWorkflowMagic:.*?id\\s*=\\s*['\"]?$id\\W.*?\\}\\}/s",'wfWorkflowUpdateTagCallback,$text,1,$count);
 +
if ($count) $article->updateArticle($text,"summary",false,false);
 +
}
 +
}
 +
 
 +
# Replacement callback for updating tag state
 +
function wfWorkflowUpdateTagCallback($m) {
 +
global $wgWorkflowMagic,$wgWorkflowTagState;
 +
$m[0] = preg_replace("/(state\\s*=\\s*['\"]?)\\w+/","$1$wgWorkflowTagState",$m[0],1,$count);
 +
if ($count < 1) $m[0] = preg_replace("/(\\{\\{#$wgWorkflowMagic:/","$0state=$wgWorkflowTagState|",$m[0]);
 +
return $m[0];
 
}
 
}
  

Revision as of 01:54, 8 October 2007

<?php

  1. Extension:Workflow
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:NadCategory:Extensions created with Template:Extension
  3. - Started: 2007-10-06

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('WORKFLOW_VERSION','0.0.0, 2007-10-06');

$wgWorkflowMagic = "tag"; $wgExtensionFunctions[] = 'wfSetupWorkflow'; $wgHooks['LanguageGetMagic'][] = 'wfWorkflowLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Workflow', 'author' => 'User:Nad', 'description' => 'Allows dynamic content areas called tags which can be switched between pre-selected states by clicking. The states can contain normal content such as images and categorisation links.', 'url' => 'http://www.mediawiki.org/wiki/Extension:Workflow', 'version' => WORKFLOW_VERSION );

  1. If it's a workflow-related ajax call, don't use dispatcher (because we need the catlinks generated by normal page render)

if ($wgUseAjax && $_REQUEST['action'] == 'ajax' && $_REQUEST['rs'] == 'wfWorkflowUpdateTag' && is_array($_REQUEST['rsargs'])) { list($_REQUEST['title'],$wgWorkflowTagID,$wgWorkflowTagState) = $_REQUEST['rsargs']; $wgHooks['OutputPageBeforeHTML'][] = 'wfWorkflowReturnCatlinks'; $wgWorkflowUpdateTag = true; $_REQUEST['action'] = 'render'; } else $wgWorkflowUpdateTag = false;

  1. Expand the #tags to reveal the current state and hide the others and add javascript
  2. - note the hidden states mustn't be rendered because they contain categorisation links which shouldn't be processed

function wfWorkflowExpandTag() {

# Populate $argv with both named and numeric parameters $argv = array(); $items = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg; }

# Extract reserved parameters $id = isset($argv['id'])  ? $argv['id']  : uniqid(); unset($argv['id']); $state = isset($argv['state'])  ? $argv['state']  : 0; unset($argv['state']); $format = isset($argv['format'])  ? $argv['format'] : 'tag'; unset($argv['format']); $class = isset($argv['class'])  ? $argv['class']  : "workflow-$format"; unset($argv['class']); $location = isset($argv['location']) ? $argv['format'] : 'inline'; unset($argv['location']); $options = isset($argv['options'])  ? $argv['format'] : ; unset($argv['options']);

# Render the visible item # - could also render all non-visible items here too, but with a cloned parser object so that the categorisation doesn't occur # - that way the JS can instantly cycle through all rendered states, then set new state and get updated catlinks via ajax $text = ; foreach ($argv as $k => $v) { if ($format == 'tag') {

$text = "

\n$text

";

} elseif ($format == 'list') {

$text .= "

$v

\n"; $text = "

\n$text

";

} }

return $text; }

  1. Update a tag state in an article

function wfWorkflowUpdateTag($pagename,$id,$state) { global $wgWorkflowMagic,$wgWorkflowTagState; $title = Title:newFromtext($pagename); if (is_object($title) && $title->exists()) { $article = new Article($title); $text = $article->getContent(); $wgWorkflowTagState = $state; preg_replace_callback("/\\{\\{#$wgWorkflowMagic:.*?id\\s*=\\s*['\"]?$id\\W.*?\\}\\}/s",'wfWorkflowUpdateTagCallback,$text,1,$count); if ($count) $article->updateArticle($text,"summary",false,false); } }

  1. Replacement callback for updating tag state

function wfWorkflowUpdateTagCallback($m) { global $wgWorkflowMagic,$wgWorkflowTagState; $m[0] = preg_replace("/(state\\s*=\\s*['\"]?)\\w+/","$1$wgWorkflowTagState",$m[0],1,$count); if ($count < 1) $m[0] = preg_replace("/(\\{\\{#$wgWorkflowMagic:/","$0state=$wgWorkflowTagState|",$m[0]); return $m[0]; }

  1. Return just the catlinks to the client after updating a tag state

function wfWorkflowReturnCatlinks() { global $wgUser,$wgOut; $skin = $wgUser->getSkin(); $catlinks = is_object($skin) ? $skin->getCategoryLinks() : 'Error: no skin!'; $wgOut->disable(); while(@ob_end_clean()); if (in_array('Content-Encoding: gzip',headers_list())) $catlinks = gzencode($catlinks); echo($catlinks); return false; }

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupWorkflow() { global $wgParser,$wgWorkflowMagic,$wgWorkflowUpdateTag; $wgParser->setFunctionHook($wgWorkflowMagic,'wfWorkflowExpandTag'); if ($wgWorkflowUpdateTag) wfWorkflowUpdateTag($_REQUEST['title'],$wgWorkflowTagID,$wgWorkflowTagState); }

  1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

function wfWorkflowLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgWorkflowMagic; $magicWords[$wgWorkflowMagic] = array(0,$wgWorkflowMagic); return true; } ?>