Difference between revisions of "Extension:FileSync.php"
(If the article content is now different than the file, update the file) |
(0.0.2 - $article as param to preprocess() not $text) |
||
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. | + | define('FILESYNC_VERSION','0.0.1, 2007-09-02'); |
$wgFileSyncMagic = "filesync"; | $wgFileSyncMagic = "filesync"; | ||
Line 70: | Line 70: | ||
# Preprocess the text so that the paths are evaluated | # Preprocess the text so that the paths are evaluated | ||
− | + | $this->preprocess($article); | |
# If the article content is now different than the file, update the file | # If the article content is now different than the file, update the file | ||
Line 85: | Line 85: | ||
function onArticleDelete(&$article,&$user,$reason) { | function onArticleDelete(&$article,&$user,$reason) { | ||
global $wgHooks; | global $wgHooks; | ||
− | + | $this->preprocess($article); | |
− | $this->preprocess($ | ||
$wgHooks['ArticleDeleteComplete'][] = $this; | $wgHooks['ArticleDeleteComplete'][] = $this; | ||
return true; | return true; | ||
Line 100: | Line 99: | ||
# Preprocess the text so that the paths are evaluated and templates expanded ready for writing to files | # Preprocess the text so that the paths are evaluated and templates expanded ready for writing to files | ||
− | function preprocess(&$text) | + | function preprocess(&$article) { |
+ | $title = $article->getTitle(); | ||
+ | $text = $article->getContent(); | ||
$parser = new Parser; | $parser = new Parser; | ||
− | $opt = ParserOptions | + | $opt = new ParserOptions; |
− | |||
return $parser->preprocess($text,$title,$opt); | return $parser->preprocess($text,$title,$opt); | ||
} | } |
Revision as of 05:28, 2 September 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.1, 2007-09-02');
$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['ArticleDelete'][] = $this; $this->args = array(); $this->paths = array(); }
# Reduce the magic and obtain the args and paths function magicFilesync(&$parser) { global $wgTitle; $filesync = 'FileSync: ';
# Extract paths and args 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 master file has changed and if so, update the article $file = $this->paths[0]; if (file_exists($file)) { $message = '{'."{message|icon=File:Info.png|text=$filesync: This article is synchronised with $file}".'}'; $article = new Article($wgTitle); $atext = $article->getContent(); $ftext = file_get_contents($file); if ($atext !== $ftext) { $comment = "Article updated from $file"; $message = '{'."{message|icon=File:Warning.png|text=$filesync: $comment}".'}'; $article->doEdit($text,$comment); } } else '{'."{message|icon=File:Warning.png|text=$filesync: $file does not exist!}".'}';
return $message; }
# Update any associated files with the new content # - templates are expanded so that filepaths are known function onArticleSaveComplete(&$article,&$user,&$atext) {
# Preprocess the text so that the paths are evaluated $this->preprocess($article);
# If the article content is now different than the file, update the file $file = $this->paths[0]; if (file_exists($file)) { $ftext = file_get_contents($file); if ($atext !== $ftext) file_put_contents($file,$atext); }
return true; }
# Get any filepaths before deleting the article function onArticleDelete(&$article,&$user,$reason) { global $wgHooks; $this->preprocess($article); $wgHooks['ArticleDeleteComplete'][] = $this; return true; }
# If the delete completes properly, delete the associated file function onArticleDeleteComplete(&$article,&$user,&$reason) { $file = $this->paths[0]; $reason = "(also deleting $file) $reason"; if (file_exists($file)) unlink($file); return true; }
# Preprocess the text so that the paths are evaluated and templates expanded ready for writing to files function preprocess(&$article) { $title = $article->getTitle(); $text = $article->getContent(); $parser = new Parser; $opt = new ParserOptions; 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; } ?>