Difference between revisions of "Extension:Workflow.php"
($pagename should be used by ajax diapatcher) |
(category link updating done) |
||
Line 11: | Line 11: | ||
if (!defined('MEDIAWIKI')) die("Not an entry point."); | if (!defined('MEDIAWIKI')) die("Not an entry point."); | ||
− | define('WORKFLOW_VERSION', "0.1. | + | define('WORKFLOW_VERSION', "0.1.1, 2008-11-08"); |
$wgWorkflowUpdateDelay = 1000; # Delay in milliseconds after clicking state before Ajax update is made | $wgWorkflowUpdateDelay = 1000; # Delay in milliseconds after clicking state before Ajax update is made | ||
Line 134: | Line 134: | ||
} | } | ||
$name = $argv[0]; | $name = $argv[0]; | ||
− | $this-> | + | |
− | $ | + | # Get state list and current state of this workflow |
+ | $current = $this->loadStates($name); | ||
+ | $states = $this->workflowData[$name]; | ||
# Make a clone of the parser for parsing without affecting LinkHolder or catlinks arrays | # Make a clone of the parser for parsing without affecting LinkHolder or catlinks arrays | ||
Line 141: | Line 143: | ||
$opt = ParserOptions::newFromUser($wgUser); | $opt = ParserOptions::newFromUser($wgUser); | ||
− | # | + | # Build links for workflow table |
− | + | $title = Title::makeTitle(NS_WORKFLOW, $name); | |
− | |||
− | $title = Title:: | ||
$edit = $title->userCan('edit'); | $edit = $title->userCan('edit'); | ||
$url = $title->getLocalUrl(); | $url = $title->getLocalUrl(); | ||
Line 166: | Line 166: | ||
foreach ($states as $i => $state) { | foreach ($states as $i => $state) { | ||
$i++; | $i++; | ||
− | $stitle = Title:: | + | $stitle = Title::makeTitle(NS_TEMPLATE, $state); |
$surl = $stitle->getLocalUrl(); | $surl = $stitle->getLocalUrl(); | ||
$data .= ",'$state'"; | $data .= ",'$state'"; | ||
$style = "display:none"; | $style = "display:none"; | ||
− | |||
− | |||
$this->workflowData[$name][] = $state; | $this->workflowData[$name][] = $state; | ||
− | if ($current | + | if (empty($current)) $current = $state; # make current default to first item name |
$wikitext = $stitle->exists() ? '{'.'{'."$state|state=$state$args}".'}' : "[[$tmpl:$state]]"; # transclude or render a red-link | $wikitext = $stitle->exists() ? '{'.'{'."$state|state=$state$args}".'}' : "[[$tmpl:$state]]"; # transclude or render a red-link | ||
− | if ( | + | if ($state == $current) { |
$wikitext .= "[[{$this->cat}:$state]]"; # if state is current, add a category link | $wikitext .= "[[{$this->cat}:$state]]"; # if state is current, add a category link | ||
$style = ""; | $style = ""; | ||
− | |||
$ci = $i; | $ci = $i; | ||
} | } | ||
− | $content = $ | + | $content = $parser->parse($wikitext, $title, $opt, false, $state != $current)->getText(); |
# Append the menu to the state | # Append the menu to the state | ||
Line 190: | Line 187: | ||
} | } | ||
$html .= "</div><script type='$wgJsMimeType'>workflowData['$name']=[$ci$data];</script>\n"; | $html .= "</div><script type='$wgJsMimeType'>workflowData['$name']=[$ci$data];</script>\n"; | ||
− | |||
} | } | ||
Line 197: | Line 193: | ||
/** | /** | ||
− | * | + | * Extract states from Worklow:Name content if we don't have the list yet |
+ | * - returns the current state (but only when first loaded) | ||
*/ | */ | ||
function loadStates($name) { | function loadStates($name) { | ||
− | $states = array(); | + | $current = ''; |
− | + | if (isset($this->workflowData[$name])) $states = $this->workflowData[$name]; | |
− | + | else { | |
− | + | $this->workflowData[$name] = array(); | |
− | + | $title = Title::makeTitle(NS_WORKFLOW, $name); | |
− | $ | + | if (is_object($title) && $title->exists()) { |
+ | |||
+ | # Extract the links from the workflow article | ||
+ | $article = new Article($title); | ||
+ | if (preg_match_all("%^\\*\\s*\\[{2}\\s*(.+?:)?\\s*(.+?)\\s*\\]{2}%m", $article->getContent(), $match)) { | ||
+ | $this->workflowData[$name] = $match[2]; | ||
+ | } | ||
+ | |||
+ | # Extract the current state from the first matching category in the article | ||
+ | global $wgTitle; | ||
+ | $article = new Article($wgTitle); | ||
+ | $cats = join('|', $this->workflowData[$name]); | ||
+ | if (preg_match("%\s*\[\[{$this->cat}:($cats)\]\]\s*%", $article->getContent(), $m)) $current = $m[1]; | ||
} | } | ||
} | } | ||
− | return $ | + | return $current; |
} | } | ||
Line 220: | Line 229: | ||
static $i = 0; | static $i = 0; | ||
if ($i++ < 1 && isset($this->workflowData[$this->name]) && count($this->workflowData[$this->name]) > 1) { | if ($i++ < 1 && isset($this->workflowData[$this->name]) && count($this->workflowData[$this->name]) > 1) { | ||
− | $cats = $this->workflowData[$this->name] | + | $cats = join('|', $this->workflowData[$this->name]); |
− | + | $this->text = preg_replace("%\s*\[\[{$this->cat}:($cats)\]\]\s*%", "", $text)."\n[[{$this->cat}:{$this->state}]]"; | |
− | |||
$parser->disableCache(); | $parser->disableCache(); | ||
− | |||
} | } | ||
return true; | return true; | ||
Line 263: | Line 270: | ||
$title = Title::makeTitle(NS_WORKFLOW, $name); | $title = Title::makeTitle(NS_WORKFLOW, $name); | ||
$catlinks .= "$table<a href='{$title->getLocalURL()}'>$name</a>: </td><td>"; | $catlinks .= "$table<a href='{$title->getLocalURL()}'>$name</a>: </td><td>"; | ||
− | $current = | + | $current = ''; |
$sep = " "; | $sep = " "; | ||
− | foreach ($states as | + | foreach ($states as $state) { |
if ($current) { | if ($current) { | ||
− | $title = Title::makeTitle(NS_CATEGORY,$state); | + | $title = Title::makeTitle(NS_CATEGORY, $state); |
− | $class = $current == $ | + | $class = $current == $state ? 'current' : ''; |
$class .= $title->exists() ? '' : ' new'; | $class .= $title->exists() ? '' : ' new'; | ||
$catlinks .= "$sep<a class='$class' href='{$title->getLocalURL()}'>$state</a>"; | $catlinks .= "$sep<a class='$class' href='{$title->getLocalURL()}'>$state</a>"; | ||
Line 290: | Line 297: | ||
if ($this->text) { | if ($this->text) { | ||
$article = new Article($wgTitle); | $article = new Article($wgTitle); | ||
− | + | $article->doEdit($this->text, wfMsg('workflowStateUpdated', $this->name, $this->state), EDIT_UPDATE); | |
} | } | ||
$skin = $wgUser->getSkin(); | $skin = $wgUser->getSkin(); | ||
Line 298: | Line 305: | ||
header("Cache-Control: no-cache, must-revalidate"); | header("Cache-Control: no-cache, must-revalidate"); | ||
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); | header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); | ||
− | |||
print $catlinks; | print $catlinks; | ||
return false; | return false; |
Revision as of 00:03, 8 November 2008
<?php /**
* Extension:Workflow *
Template:PhpCategory:Extensions created with Template:Extension
* @package MediaWiki * @subpackage Extensions * @author Aran Dunkley User:Nad * @licence GNU General Public Licence 2.0 or later * Started: 2007-10-06 */
if (!defined('MEDIAWIKI')) die("Not an entry point.");
define('WORKFLOW_VERSION', "0.1.1, 2008-11-08");
$wgWorkflowUpdateDelay = 1000; # Delay in milliseconds after clicking state before Ajax update is made
$wgExtensionCredits['parserhook'][] = $wgExtensionCredits['specialpage'][] = 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 );
/**
* Define a new specialpage for displaying a list of workflows */
require_once "$IP/includes/SpecialPage.php"; class SpecialWorkflow extends SpecialPage {
function SpecialWorkflow() { SpecialPage::SpecialPage(wfMsg('workflow'), , true, false, false, false); }
# Render list function execute($param) { global $wgParser, $wgOut; $wgParser->disableCache(); $this->setHeaders(); $wgOut->addWikiText("Not done yet... discussion at Extension talk:Workflow.php", true); } }
/**
* Define main workflow class containing all the functionality */
class Workflow {
var $magic; # magic word used for the workflow parser-function (set from messages) var $state; # state to update a workflow to in an ajax update request var $name; # name of the workflow to update var $pagename; # the name of the current page (in which the workflows reside) var $cat; # lcoal lang name for "Category" var $title; var $workflowData = array();
/** * Constructor */ function Workflow() { global $wgHooks, $wgSiteNotice, $wgExtensionFunctions, $wgWorkflowMagic, $wgExtraNamespaces, $wgAjaxExportList;
# Require NS_WORKFLOW to be defined before installing parser-function or javascript etc if (!defined('NS_WORKFLOW')) {
$wgSiteNotice .= "
";
return; }
# The parser-function name, special-page name and links all use the word defined in $wgExtraNamespaces[NS_WORKFLOW] #$this->magic = $wgContLang->getNsText(NS_WORKFLOW); $this->magic = $wgExtraNamespaces[NS_WORKFLOW]; # $wgContLang doesn't exist yet
$wgAjaxExportList[] = $this->magic;
# Reserve the magic word for use as a parser-function $wgHooks['LanguageGetMagic'][] = array($this, 'languageGetMagic');
# If it's a workflow-related ajax call, don't use dispatcher (because we need the catlinks generated by normal page render) # - this sets the state, name, pagename and update properties $this->bypassAjaxDispatcher();
# Add the extension's setup function to the list to be called after the environment is up and running $wgExtensionFunctions[] = array($this, 'setup'); }
/** * Extension setup */ function setup() { global $wgParser, $wgLanguageCode, $wgMessageCache, $wgContLang;
$this->cat = $wgContLang->getNsText(NS_CATEGORY);
# Add the messages used (todo: move into i18n) if ($wgLanguageCode == 'en') { $wgMessageCache->addMessages(array( 'workflow' => $this->magic, # NOTE: the parser-function, special-page and link-rendering all use this word 'workflowStateUpdated' => "[[{$this->magic}:$1|$1 {$this->magic}]] state set to [[{$this->cat}:$2|$2]]." )); }
# Add the specialpage to the environment SpecialPage::addPage(new SpecialWorkflow());
# Add the parser-function hook $wgParser->setFunctionHook($this->magic, array($this, 'expandMagic'));
# Add the client-side scripts for changing states $this->addJS(); }
/** * Expand the #workflow 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 expandMagic(&$parser) { global $wgUser, $wgJsMimeType, $wgContLang; $parser->disableCache(); $tmpl = $wgContLang->getNsText(NS_TEMPLATE); $wf = $this->magic;
# Extend catlinks information to include workflows $this->extendCatlinks();
# Populate $argv with both named and numeric parameters $args =; $argv = array(); $items = array(); $name = 'Untitled'; foreach (func_get_args() as $arg) if (!is_object($arg)) { $args .= "|$arg"; if (preg_match("%^(.+?)\\s*=\\s*(.+)$%", $arg, $match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg; } $name = $argv[0];
# Get state list and current state of this workflow $current = $this->loadStates($name); $states = $this->workflowData[$name];
# Make a clone of the parser for parsing without affecting LinkHolder or catlinks arrays $psr = new parser; $opt = ParserOptions::newFromUser($wgUser);
# Build links for workflow table $title = Title::makeTitle(NS_WORKFLOW, $name); $edit = $title->userCan('edit'); $url = $title->getLocalUrl(); $href = "href='$url'"; $anchor = "$wf:$name"; if (is_object($title) && $title->exists()) { if (count($states)<1) $anchor .= " (".wfMsg('workflowMissingContent').")"; } else $href .= " class='new'"; $html = "<a $href title='$anchor'>$anchor</a>";
# Transclude each (use a parser clone for the ones which aren't current to avoid categorisation) if (count($states)) { if ($edit) {
$left = "<a href='javascript:;'><</a>"; $right = "<a href='javascript:;'>></a>";
}
else $left = $right = ""; # no menu buttons if not allowed to edit $html = "
$data = ""; $ci = 0; foreach ($states as $i => $state) { $i++; $stitle = Title::makeTitle(NS_TEMPLATE, $state); $surl = $stitle->getLocalUrl(); $data .= ",'$state'"; $style = "display:none"; $this->workflowData[$name][] = $state; if (empty($current)) $current = $state; # make current default to first item name $wikitext = $stitle->exists() ? '{'.'{'."$state|state=$state$args}".'}' : "$tmpl:$state"; # transclude or render a red-link if ($state == $current) { $wikitext .= "[[{$this->cat}:$state]]"; # if state is current, add a category link $style = ""; $ci = $i; } $content = $parser->parse($wikitext, $title, $opt, false, $state != $current)->getText();
# Append the menu to the state
$html .= "$content | ||
<a href='$surl' title='$tmpl:$state'>$anchor</a> |
}
$html .= "<script type='$wgJsMimeType'>workflowData['$name']=[$ci$data];</script>\n";
}
return array($html, 'isHTML' => true, 'noparse' => true); }
/** * Extract states from Worklow:Name content if we don't have the list yet * - returns the current state (but only when first loaded) */ function loadStates($name) { $current = ; if (isset($this->workflowData[$name])) $states = $this->workflowData[$name]; else { $this->workflowData[$name] = array(); $title = Title::makeTitle(NS_WORKFLOW, $name); if (is_object($title) && $title->exists()) {
# Extract the links from the workflow article $article = new Article($title); if (preg_match_all("%^\\*\\s*\\[{2}\\s*(.+?:)?\\s*(.+?)\\s*\\]{2}%m", $article->getContent(), $match)) { $this->workflowData[$name] = $match[2]; }
# Extract the current state from the first matching category in the article global $wgTitle; $article = new Article($wgTitle); $cats = join('|', $this->workflowData[$name]); if (preg_match("%\s*\[\[{$this->cat}:($cats)\]\]\s*%", $article->getContent(), $m)) $current = $m[1]; } } return $current; }
/** * Update the current state of a workflow item in the requested article * - the actual article edit is done in returnCatlinks after all parsing finished * - this disables the parser-cache */ function updateState(&$parser, &$text) { $this->loadStates($this->name); static $i = 0; if ($i++ < 1 && isset($this->workflowData[$this->name]) && count($this->workflowData[$this->name]) > 1) { $cats = join('|', $this->workflowData[$this->name]); $this->text = preg_replace("%\s*\[\[{$this->cat}:($cats)\]\]\s*%", "", $text)."\n[[{$this->cat}:{$this->state}]]"; $parser->disableCache(); } return true; }
/** * Extend catlinks information to include workflows */ function extendCatlinks() { static $done = 0; if ($done++) return; global $wgUser; $skin = $wgUser->getSkin();
# Create a new Skin class (WorkflowSkin) by extending the existing one with overridden getCategoryLinks method $class = get_class($skin); eval("class WorkflowSkin extends {$class} ".'{ function getCategories() { global $wgWorkflow; return $wgWorkflow->renderWorkflowInfo(parent::getCategories()); } }');
# Replace user's skin with a WorkflowSkin replica $wgUser->mSkin = new WorkflowSkin(); foreach (array_keys(get_class_vars($class)) as $k) $wgUser->mSkin->$k = $skin->$k; }
/** * Render the workflow info which appears in the catlinks area */ function renderWorkflowInfo(&$catlinks) { if (count($this->workflowData)) { //$title = Title::makeTitle(NS_SPECIAL,wfMsg('workflow'));
//$table = "
<a href='{$title->getLocalURL()}'>".wfMsg('workflow')."</a>: | ";
$table = "
} return $catlinks; } /** * Return just the catlinks to the client after updating a tag state * - the article is updated here if replacement text has been set */ function returnCatlinks(&$out, &$text) { global $wgUser, $wgOut, $wgTitle; if ($this->text) { $article = new Article($wgTitle); $article->doEdit($this->text, wfMsg('workflowStateUpdated', $this->name, $this->state), EDIT_UPDATE); } $skin = $wgUser->getSkin(); $catlinks = is_object($skin) ? $skin->getCategories() : "Error: no skin!"; $wgOut->disable(); wfResetOutputBuffers(); header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); print $catlinks; return false; } /** * Make necessary Javascript functions available to the page */ function addJS() { global $wgOut, $wgJsMimeType, $wgWorkflowUpdateDelay; $wgOut->addScript("<script type='$wgJsMimeType'> var workflowData = []; var workflowUpdate = 0; var workflowLastState = 0; function workflowUpdateState(name) { clearTimeout(workflowUpdate); workflowLastState = workflowData[name][0]; var state = workflowData[name][workflowLastState]; sajax_do_call('{$this->magic}',[wgPageName,name,state],document.getElementById('catlinks')); } function workflowSwitchState(name,dir) { clearTimeout(workflowUpdate); document.getElementById('workflow-'+name+'-'+workflowData[name][0]).setAttribute('style','display:none'); if (workflowLastState == 0) workflowLastState = workflowData[name][0]; workflowData[name][0] += dir; if (workflowData[name][0] < 1) workflowData[name][0] = workflowData[name].length-1; if (workflowData[name][0] > workflowData[name].length-1) workflowData[name][0] = 1; var state = workflowData[name][0]; document.getElementById('workflow-'+name+'-'+state).setAttribute('style',); if (workflowLastState != state) workflowUpdate = setTimeout('workflowUpdateState(\"'+name+'\")',$wgWorkflowUpdateDelay); } </script>"); } /** * If it's a workflow-related ajax call, don't use dispatcher (because we need the catlinks generated by normal page render) * - this sets the state, name, pagename and update properties */ function bypassAjaxDispatcher() { global $wgUseAjax, $wgHooks; if ($wgUseAjax && isset($_GET['action']) && $_GET['action'] == 'ajax' && isset($_GET['rs']) && $_GET['rs'] == $this->magic && isset($_GET['rsargs']) && is_array($_GET['rsargs']) ) { list($title, $this->name, $this->state) = $_GET['rsargs']; $wgHooks['ParserBeforeStrip'][] = array($this, 'updateState'); $wgHooks['OutputPageBeforeHTML'][] = array($this, 'returnCatlinks'); $_GET = $_REQUEST = array('title' => $title, 'action' => 'view'); } $this->pagename = isset($_REQUEST['title']) ? $_REQUEST['title'] : ; $this->text = ; } /** * Needed in some versions to prevent Special:Version from breaking */ function __toString() { return 'Workflow'; } /** * Set up the magic words */ function languageGetMagic(&$magicWords, $langCode = 0) { $magicWords[$this->magic] = array($langCode, $this->magic); return true; } } $wgWorkflow = new Workflow(); |