Difference between revisions of "Extension:Workflow.php"
(don't use #tag just use #workflow) |
(extend catlinks info by extending user's skin class) |
||
Line 7: | Line 7: | ||
if (!defined('MEDIAWIKI')) die('Not an entry point.'); | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
− | define('WORKFLOW_VERSION','0.0. | + | define('WORKFLOW_VERSION','0.0.1, 2007-10-23'); |
$wgWorkflowMagic = "workflow"; | $wgWorkflowMagic = "workflow"; | ||
Line 16: | Line 16: | ||
'name' => 'Workflow', | 'name' => 'Workflow', | ||
'author' => '[http://www.organicdesign.co.nz/nad User:Nad]', | 'author' => '[http://www.organicdesign.co.nz/nad User:Nad]', | ||
− | 'description' => ' | + | 'description' => 'Adds the ability for articles to be part of workflow sequences and easily moved dynamically between phases in the sequence using AJAX.', |
'url' => 'http://www.mediawiki.org/wiki/Extension:Workflow', | 'url' => 'http://www.mediawiki.org/wiki/Extension:Workflow', | ||
'version' => WORKFLOW_VERSION | 'version' => WORKFLOW_VERSION | ||
Line 32: | Line 32: | ||
# - 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 wfWorkflowExpand() { | function wfWorkflowExpand() { | ||
+ | |||
+ | # Extend catlinks information to include workflows | ||
+ | wfWorkflowExtendCatlinks(); | ||
# Populate $argv with both named and numeric parameters | # Populate $argv with both named and numeric parameters | ||
Line 43: | Line 46: | ||
# - could also render all non-visible items here too, but with a cloned parser object so that the categorisation doesn't occur | # - 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 | # - that way the JS can instantly cycle through all rendered states, then set new state and get updated catlinks via ajax | ||
− | $text = ''; | + | $text = 'done'; |
foreach ($argv as $k => $v) { | foreach ($argv as $k => $v) { | ||
if ($format == 'tag') { | if ($format == 'tag') { | ||
Line 60: | Line 63: | ||
function wfWorkflowUpdateState($pagename,$name,$state) { | function wfWorkflowUpdateState($pagename,$name,$state) { | ||
global $wgWorkflowMagic,$wgWorkflowState; | global $wgWorkflowMagic,$wgWorkflowState; | ||
− | $title = Title:newFromtext($pagename); | + | $title = Title::newFromtext($pagename); |
if (is_object($title) && $title->exists()) { | if (is_object($title) && $title->exists()) { | ||
$article = new Article($title); | $article = new Article($title); | ||
Line 76: | Line 79: | ||
if ($count < 1) $m[0] = preg_replace("/(\\{\\{#$wgWorkflowMagic:/","$0state=$wgWorkflowState|",$m[0]); | if ($count < 1) $m[0] = preg_replace("/(\\{\\{#$wgWorkflowMagic:/","$0state=$wgWorkflowState|",$m[0]); | ||
return $m[0]; | return $m[0]; | ||
+ | } | ||
+ | |||
+ | # Extend catlinks information to include workflows | ||
+ | function wfWorkflowExtendCatlinks() { | ||
+ | static $done = 0; | ||
+ | if ($done++) return; | ||
+ | global $wgUser; | ||
+ | $skin = $wgUser->getSkin(); | ||
+ | |||
+ | # Create a new Skin class (Skin2) by extending the existing one with overridden getCategoryLinks method | ||
+ | $class = get_class($skin); | ||
+ | eval("class WorkflowSkin extends {$class} ".'{ | ||
+ | function getCategoryLinks() { | ||
+ | $catlinks = parent::getCategoryLinks(); | ||
+ | $catlinks .= "<br>foo"; | ||
+ | return $catlinks; | ||
+ | } | ||
+ | }'); | ||
+ | |||
+ | # Replace user's skin with a sub-classed replica | ||
+ | $wgUser->mSkin = new WorkflowSkin(); | ||
+ | foreach (array_keys(get_class_vars($class)) as $k) $wgUser->mSkin->$k = $skin->$k; | ||
} | } | ||
Revision as of 05:48, 23 October 2007
<?php
- Extension:Workflow
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: User:NadCategory:Extensions created with Template:Extension
- - Started: 2007-10-06
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('WORKFLOW_VERSION','0.0.1, 2007-10-23');
$wgWorkflowMagic = "workflow"; $wgExtensionFunctions[] = 'wfSetupWorkflow'; $wgHooks['LanguageGetMagic'][] = 'wfWorkflowLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array( 'name' => 'Workflow', 'author' => 'User:Nad', 'description' => 'Adds the ability for articles to be part of workflow sequences and easily moved dynamically between phases in the sequence using AJAX.', 'url' => 'http://www.mediawiki.org/wiki/Extension:Workflow', 'version' => WORKFLOW_VERSION );
- 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'] == 'wfWorkflowUpdateState' && is_array($_REQUEST['rsargs'])) { list($_REQUEST['title'],$wgWorkflowName,$wgWorkflowState) = $_REQUEST['rsargs']; $wgHooks['OutputPageBeforeHTML'][] = 'wfWorkflowReturnCatlinks'; $wgWorkflowUpdateState = true; $_REQUEST['action'] = 'render'; } else $wgWorkflowUpdateState = false;
- 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
function wfWorkflowExpand() {
# Extend catlinks information to include workflows wfWorkflowExtendCatlinks();
# 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; }
# 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 = 'done'; foreach ($argv as $k => $v) { if ($format == 'tag') {
$text = "
";
} elseif ($format == 'list') {
$text .= "
\n"; $text = "
";
} }
return $text; }
- Update a tag state in an article
function wfWorkflowUpdateState($pagename,$name,$state) { global $wgWorkflowMagic,$wgWorkflowState; $title = Title::newFromtext($pagename); if (is_object($title) && $title->exists()) { $article = new Article($title); $text = $article->getContent(); $wgWorkflowState = $state; preg_replace_callback("/\\{\\{#$wgWorkflowMagic:.*?id\\s*=\\s*['\"]?$id\\W.*?\\}\\}/s",'wfWorkflowUpdateStateCB',$text,1,$count); if ($count) $article->updateArticle($text,"summary",false,false); } }
- Replacement callback for updating tag state
function wfWorkflowUpdateStateCB($m) { global $wgWorkflowMagic,$wgWorkflowState; $m[0] = preg_replace("/(state\\s*=\\s*['\"]?)\\w+/","$1$wgWorkflowState",$m[0],1,$count); if ($count < 1) $m[0] = preg_replace("/(\\{\\{#$wgWorkflowMagic:/","$0state=$wgWorkflowState|",$m[0]); return $m[0]; }
- Extend catlinks information to include workflows
function wfWorkflowExtendCatlinks() { static $done = 0; if ($done++) return; global $wgUser; $skin = $wgUser->getSkin();
# Create a new Skin class (Skin2) by extending the existing one with overridden getCategoryLinks method
$class = get_class($skin);
eval("class WorkflowSkin extends {$class} ".'{
function getCategoryLinks() {
$catlinks = parent::getCategoryLinks();
$catlinks .= "
foo";
return $catlinks;
}
}');
# Replace user's skin with a sub-classed replica $wgUser->mSkin = new WorkflowSkin(); foreach (array_keys(get_class_vars($class)) as $k) $wgUser->mSkin->$k = $skin->$k; }
- 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; }
- Called from $wgExtensionFunctions array when initialising extensions
function wfSetupWorkflow() { global $wgParser,$wgWorkflowMagic,$wgWorkflowUpdateState; $wgParser->setFunctionHook($wgWorkflowMagic,'wfWorkflowExpand'); if ($wgWorkflowUpdateState) wfWorkflowUpdateState($_REQUEST['title'],$wgWorkflowName,$wgWorkflowState); }
- 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; } ?>