[Mediawiki-l] Reading contents from file to speed up development on localhost...

Antonio Orlando ant.o at libero.it
Mon Mar 1 10:48:30 UTC 2010


Hallo to all of you there.

I'm trying to have a way to speed up development of the initial setup of  
my wiki before moving to the remote server. I mean, I want to do and see  
changes in a wiki page with *just* the following steps (not one more!):

1) edit a file in my favourite text editor

2) Alt-TAB to the web browser window with the wiki page shown

3) press F5

(Of course this can also be reduced to just two steps if a keyboard  
shortcut is configured to focus on the web browser window and force a page  
refresh).

Summying up, what I would like to accomplish is editing all (or almost  
all) the wiki by editing plain .txt files, with all the wiki pages on  
localhost containing just the custom magic word  
"__GET_CONTENT_FROM_FILE__" (or a {{gcff:}} functions, or whatever).

So, first I've disabled cache in LocalSettings.php:

$wgEnableParserCache = false;
$wgCachePages = false;

Then, I've tried hacking "includes/parser/Parser.php", function parse(),  
like specified in the bottom of this message [1] (yes I know that hooks  
probably can be used for this sort of things, but I've not studied them  
and I'm in a hurry, so just making an attempt to see if and how this is  
feasible: I need to speed up wiki development because I'm short of time ;)

There I check for the presence of the custom magic word  
"__GET_CONTENT_FROM_FILE__" at the beginning of the $text variable, and if  
found I replace $text with contents retrieved from a file following some  
conventions for the filename, if it exists.

This is sorta working, but not like I would like: let me explain.

I'm using SMW and Semantic Forms (but probably this is not important  
here); anyway, I have e.g. a template called "Test", and a form which  
generates pages that use the Test template when accessed and rendered.
So let's say I have the page "Test Page", showing content using the "Test"  
template.

If I am with browser on page "Template:Test", I can immediately see any  
change I do into the "Template;Test.txt" file, just by hitting F5.
But, if I am with browser on page "Test Page", it doesn't reflect changes  
done in the "Template;Test.txt" file: in fact, it uses the Test template  
content retrieved from the database, i.e. by MediaWiki standard behaviour.

So I'm here asking: how can I hack things in order to have everything  
retrieved from txt files, as long as the first word in the content is  
"__GET_CONTENT_FROM_FILE__" and the file (following a name convention,  
e.g. replacing ":" with ";" to the page name and appending ".txt") exists?  
I don't need a clean solution, just a way to make it work and let me  
continue development (i.e. fine-tuning templates, forms appearance,  
several start-up pages contents, etc.), without having to write code in  
the textarea and do a button click to save, and then another click to  
re-edit, etc.).

Or, is someone able to arrange an extension doing what I've explained?

I repeat the "summying up": what I would like to accomplish is editing all  
(or almost all) the wiki by editing plain .txt files, with all the wiki  
pages on localhost containing just the custom magic word  
"__GET_CONTENT_FROM_FILE__" (or a {{gcff:}} functions, or whatever).

I underline this is only for local development, then the "real" wiki (on  
the remote server) will be a standard one. So security issues and whatever  
are not a concern.


---------------------------------------------------------------------
[1] Here it is my hack attempt (works for simple page but fails for my  
main purposes as explained above):

public function parse( $text, Title $title, ParserOptions $options,
$linestart = true, $clearState = true, $revid = null ) {

	/* hack: Get Content From File */
	if (substr($text, 0, 25) == '__GET_CONTENT_FROM_FILE__'){

		$gcff__directory = 'extensions/ContentFromFile/sources';

		// if the path has a slash at the end we remove it here
		if(substr($gcff__directory,-1) == '/')
		{
			$gcff__directory = substr($gcff__directory, 0, -1);
		}

		// if the path is not valid or is not a directory ...
		if(!file_exists($gcff__directory) || !is_dir($gcff__directory) ||
!is_readable($gcff__directory))
		{
			// do nothing, go on with normal behaviour using the page content
retrieved from the database
		} else {

			// we open the directory
			if($gcff__handle = opendir($gcff__directory)){

				// calculate the file name (via conventions)
				$gcff__filename = str_replace(':', ';', $title->mPrefixedText) .'.txt';

				$gcff__filepath = $gcff__directory .'/'. $gcff__filename;

				if(is_file($gcff__filepath)){						

					// get the page content from the file
					$text = utf8_encode(file_get_contents($gcff__filepath));
				}

				// close the directory
				closedir($gcff__handle);
			}
		}			
	}
#.........(and then here follows the original code of MediaWiki parse
function)............
}

-- 
Antonio



More information about the MediaWiki-l mailing list