Difference between revisions of "Extension:FileSync.php"
(comment and structure) |
(Add onArticleDeleteComplete and preprocessing) |
||
Line 7: | Line 7: | ||
if (!defined('MEDIAWIKI')) die('Not an entry point.'); | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
− | define('FILESYNC_VERSION','0.0.0, 2007-08- | + | define('FILESYNC_VERSION','0.0.0, 2007-08-31'); |
$wgFileSyncMagic = "filesync"; | $wgFileSyncMagic = "filesync"; | ||
Line 30: | Line 30: | ||
global $wgHooks,$wgParser,$wgFileSyncMagic; | global $wgHooks,$wgParser,$wgFileSyncMagic; | ||
$wgParser->setFunctionHook($wgFileSyncMagic,array($this,'magicFilesync')); | $wgParser->setFunctionHook($wgFileSyncMagic,array($this,'magicFilesync')); | ||
− | $wgHooks['ArticleSaveComplete'][] = $this; | + | $wgHooks['ArticleSaveComplete'][] = $this; |
+ | $wgHooks['ArticleDeleteComplete'][] = $this; | ||
$this->args = array(); | $this->args = array(); | ||
$this->paths = array(); | $this->paths = array(); | ||
Line 45: | Line 46: | ||
# Check if file has changed, | # Check if file has changed, | ||
− | # if so, update article revision with an edit summary | + | # if so, update article revision with an edit summary |
return ''; | return ''; | ||
Line 53: | Line 54: | ||
# - templates are expanded so that filepaths are known | # - templates are expanded so that filepaths are known | ||
function onArticleSaveComplete(&$article,&$user,&$text) { | function onArticleSaveComplete(&$article,&$user,&$text) { | ||
+ | |||
+ | # Preprocess the text so that the paths are evaluated | ||
+ | $content = $this->preprocess($text); | ||
+ | |||
+ | # Process each file in paths list | ||
+ | foreach ($files as $i => $files) { | ||
+ | |||
+ | if (file_exists($file)) { | ||
+ | # If content changed, write the new content to the file (add a msg to site-notice if possible) | ||
+ | } | ||
+ | else { | ||
+ | # Master (first) file has been deleted from source, delete article (and later other files) | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | return true; | ||
+ | } | ||
+ | |||
+ | # Delete any associated files | ||
+ | function onArticleDeleteComplete(&$article,&$user,$reason) { | ||
+ | |||
+ | # Preprocess the text so that the paths are evaluated | ||
+ | $text = $article->getContent(); | ||
+ | $this->preprocess($text); | ||
+ | |||
+ | return true; | ||
+ | } | ||
+ | |||
+ | # Preprocess the text so that the paths are evaluated and templates expanded ready for writing to files | ||
+ | function preprocess(&$text) { | ||
$parser = new Parser; | $parser = new Parser; | ||
$opt = ParserOptions::newFromUser($user); | $opt = ParserOptions::newFromUser($user); | ||
$title = $article->getTitle(); | $title = $article->getTitle(); | ||
− | + | return $parser->preprocess($text,$title,$opt); | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
# Needed in some versions to prevent Special:Version from breaking | # Needed in some versions to prevent Special:Version from breaking | ||
function __toString() { return 'FileSync'; } | function __toString() { return 'FileSync'; } | ||
− | + | } | |
# Called from $wgExtensionFunctions array when initialising extensions | # Called from $wgExtensionFunctions array when initialising extensions |
Revision as of 04:51, 31 August 2007
<?php
- Extension:FileSync
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: User:NadCategory:Extensions created with Template:Extension
- - Started: 2007-08-02
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('FILESYNC_VERSION','0.0.0, 2007-08-31');
$wgFileSyncMagic = "filesync"; $wgExtensionFunctions[] = 'wfSetupFileSync'; $wgHooks['LanguageGetMagic'][] = 'wfFileSyncLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array( 'name' => 'FileSync', 'author' => 'User:Nad', 'description' => 'A template which can be added to an article to make it synchronise with a file', 'url' => 'http://www.organicdesign.co.nz/Extension:FileSync', 'version' => FILESYNC_VERSION );
class FileSync {
var $args; var $paths;
# Constructor - add hooks and initialise class function FileSync() { global $wgHooks,$wgParser,$wgFileSyncMagic; $wgParser->setFunctionHook($wgFileSyncMagic,array($this,'magicFilesync')); $wgHooks['ArticleSaveComplete'][] = $this; $wgHooks['ArticleDeleteComplete'][] = $this; $this->args = array(); $this->paths = array(); }
# Process the filesync-magic function magicFilesync(&$parser) {
# Populate $argv with both named and numeric parameters foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $this->args[$match[1]] = $match[2]; else $this->paths[] = $arg; }
# Check if file has changed, # if so, update article revision with an edit summary
return ; }
# Update any associated files with the new content # - templates are expanded so that filepaths are known function onArticleSaveComplete(&$article,&$user,&$text) {
# Preprocess the text so that the paths are evaluated $content = $this->preprocess($text);
# Process each file in paths list foreach ($files as $i => $files) {
if (file_exists($file)) { # If content changed, write the new content to the file (add a msg to site-notice if possible) } else { # Master (first) file has been deleted from source, delete article (and later other files) }
}
return true; }
# Delete any associated files function onArticleDeleteComplete(&$article,&$user,$reason) {
# Preprocess the text so that the paths are evaluated $text = $article->getContent(); $this->preprocess($text);
return true; }
# Preprocess the text so that the paths are evaluated and templates expanded ready for writing to files function preprocess(&$text) { $parser = new Parser; $opt = ParserOptions::newFromUser($user); $title = $article->getTitle(); return $parser->preprocess($text,$title,$opt); }
# Needed in some versions to prevent Special:Version from breaking function __toString() { return 'FileSync'; } }
- Called from $wgExtensionFunctions array when initialising extensions
function wfSetupFileSync() { global $wgFileSync; $wgFileSync = new FileSync(); }
- Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function wfFileSyncLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgFileSyncMagic; $magicWords[$wgFileSyncMagic] = array(0,$wgFileSyncMagic); return true; } ?>