It's better when it's simple

User Tools

Site Tools


Parse DokuWiki from Command Line

The following script takes DokuWiki-formatted text (wikitext) as input, and outputs the resulting (XHTML) render content. It is useful for pipes and generating content dynamically. This was posted somewhere around the DokuWiki forums or IRC channel. Its authorship may be lost in time but as this is an useful script I've pasted it here in case it is needed.

if ('cli' != php_sapi_name()) die();
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
$source = stream_get_contents(STDIN);
$info = array();
echo p_render('xhtml',p_get_instructions($source),$info);


  • PHP-CLI tools (eg.: php-cli in Debian)
  • A DokuWiki installation.
  • Save this script in your DokuWiki's bin/ folder (alongside the indexer).
  • (optional) Give the script execution permissions.


From the command line shell:

php $PATH_TO_DOKUWIKI/bin/dokucli.php < input.txt > output.html
... or
some_command_that_writes_wikicode input | php $PATH_TO_DOKUWIKI/bin/dokucli.php


  • $PATH_TO_DOKUWIKI is, of course, the Path to your DokuWiki.
  • input and output are optional (the script reads from the Standard Input and writes to the Standard Output)

If you execute the script without parameters (to type in to the standard input directly), remember that the input finishes with your platform's “end-of-file” marker, that is CTRL+D in Unix and CTRL-Z in Windows.

Added by ryan.chappelle as suggested in the IRC Channel.

If someone could discover the authorship of this code snippet in order to credit it accordingly, I at least would be very happy. — ryan.chappelle 2011/01/03 20:02

Shell Wrapper Script

Here's a shell script that I've found useful for passing documents to the PHP files. I named it doku2html and put it in the path. –Donald Merand 07.03.2011 -

You use it like this:

doku2html file1
#... or
doku2html file1 file2 file3...
#... or
some_command_that_writes_wikicode_input | doku2html
#... to output to a file instead of STDOUT:
doku2html file1 > output_file.html
#just make sure that doku2html is in your path somewhere!

If you want to make the doku2html file executable right from the command line, you could type:

chmod 775 doku2html

The advantage of using this script is that you can easily pass multiple files, which are concatenated with a blank space between them. Input that's not a valid file is ignored. If no parameters are passed, use STDIN just like the PHP script above.

#Author: Donald L. Merand
#path to the php file to run dokuwiki parsing from the CLI
#if parameters are not empty, assume files have been passed
if [ $# -gt 0 ]
	for file in "$@"
		#ignore any non-files that are passed as parameters
		if [ -f "$file" ]
			cat "$file"
			echo #line break between files
	done |
	#    ^ note the pipe
	#now make one big HTML document out of whatever is passed
	php $phpcli
	#read from stdin
	php $phpcli
tips/dokuwiki_parser_cli.txt · Last modified: 2015-08-11 10:55 by

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki