Table of Contents
getraw Plugin
Compatible with DokuWiki
- 2022-07-31 "Igor" unknown
- 2020-07-29 "Hogfather" unknown
- 2018-04-22 "Greebo" yes
- 2017-02-19 "Frusterick Manners" yes
The missing download url means that this extension cannot be installed via the Extension Manager. Please see Publishing a Plugin on dokuwiki.org. Recommended are public repository hosts like GitHub, GitLab or Bitbucket.
This isn't a standard plugin. Rather, it's a php script that lets you request the source of a wiki page (what you'd see in the editor) with a url. The request is authorized the same way as a normal dokuwiki page.
This is a useful utility for plugins that create urls. For example, a syntax plugin might produce an iframe for some other script, which wants to do something with the data of the current page. With this plug-in, you can pass in a url that gives you the data you need.
Installation
Create the following file in your base DokuWiki directory:
- getRaw.php
<?php if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/'); require_once(DOKU_INC.'inc/init.php'); $ID = cleanID(getID()); $onlyCode = $INPUT->str('onlyCode'); $insideTag = $INPUT->str('insideTag'); if (empty($conf['useacl']) || auth_quickaclcheck($ID) >= AUTH_READ) { $file = rawWiki($ID); if ($onlyCode) $file = preg_replace('/[\s\S]*<code( \w>|>)/m', '', preg_replace('/<\/code>[\s\S]*/m', '', $file)); if ($insideTag) $file = preg_replace('/[\s\S]*<' . $insideTag . '[^>]*>/m', '', preg_replace('/<\/' . $insideTag . '>[\s\S]*/m', '', $file)); print $file; } else print "Unauthorized"; ?>
Examples/Usage
Simple Case
Let's say you have a wiki page called a:b that looks like this:
==== My Wiki ==== And here's some code! <code> Stuff! and things, too! </ code>
This url:
/doku/getRaw.php?id=a:b
will get you all the wiki content. It will not be rendered as HTML.
This url:
/doku/getRaw.php?onlyCode=true&id=a:b
will get you just what's inside the code tags:
Stuff! and things, too!
Use to insert JavaScript on your page
Create a page called a:b.js:
alert('hi there!');
If you want the page to be readable, you can wrap it in code tags and use the onlyCode parameter. On another page, you can include your JavaScript like this, if you've enabled html in your settings:
<html><script src="/doku/getRaw.php?id=a:b.js&onlyCode=1"></script></html>
Development
Change Log
- 2021-07-07
- With a simple regex added to the code
'/[\s\S]*<code( \w>|>)/m'
now it accepts tags with languages declared. For example <code c>. To make things even more interesting, check the
.htaccess
suggestion below. — beco 2021-07-07 05:40
- 2015-06-19
- Removed some unnecessary code from the script. This plugin is no longer dependent on the version of DokuWiki. Tested with DokuWiki Hrun.
- 2011-08-02
- Initial release
- 2011-08-03
- Switched from direct file access to the getRaw function.
Discussion
Request for using default plugin structure of DokuWiki
You can implement your feature also by creating an action plugin component in your plugin. See also development of Action Plugins for more details.
An useful event for your case is the ACTION ACT PREPROCESS. You handle your do action here, before DokuWiki checks its default do actions.
(An quite large example, with error handling, is the dw2pdf plugin. See: https://github.com/splitbrain/dokuwiki-plugin-dw2pdf/blob/master/action.php)
If you use the plugin structure, the advantages are:
- people can update with the Extension Manager
- people can install via the extension manager, without manually copying a file
zioth: Good idea. Next time I revisit this plugin, I might give it a shot. It should be pretty easy. (2015-07-01)
Usability suggestions
For ease of use, you can set up another rewrite rule in your .htaccess so that a user only needs to add .txt to their URL to get the text format. Also, optionally, add .c to the URL to get only the the source inside the code tags.
RewriteRule ^(.*)\.txt$ getRaw.php?id=$1 [L]
RewriteRule ^(.*)\.c$ getRaw.php?id=$1&onlyCode=1 [L]
Also for readability, throw a header('Content-Type: text/plain');
in line 4, and the browser will know to interpret the newlines instead of eating them like HTML. — NReilingh 2015-11-29 07:30 — beco 2021-07-07 05:48