5
* Copyright (c) 2007-2009 by the original authors of AnyWikiDraw
6
* and all its contributors.
8
* The copyright of this software is owned by the authors and
9
* contributors of the AnyWikiDraw project ("the copyright holders").
10
* You may not use, copy or modify this software, except in
11
* accordance with the license agreement you entered into with
12
* the copyright holders. For details see accompanying license terms.
17
* WARNING: This is an extension for MediaWiki 1.12 through 1.15 only.
18
* Do not use it with other versions of MediaWiki without extensive testing!
21
* With the AnyWikiDraw extension it is possible to define new tags of the form
23
* {{#drawing:image.svg|width|height}}
25
# - The tag needs to be put into two curly braces.
26
* - The name of the tag is #drawing:
27
* - The parameter image.svg specifies the name of the image file.
28
* - If you specify an image file that does not yet exist in your Wiki, the image
29
* is created the first time you save a drawing.
30
* - The parameter width specifies the width of the image.
31
* - The parameter height specifies the height of the image.
33
* The enable drawing for the image HappyWiki.svg, which has a width of 400 and
34
* a height of 300 pixels, insert the following tag into a page:
36
* {{#drawing:HappyDrawing.svg|300|200}}
38
* To activate the extension, include it from your LocalSettings.php
41
* require_once("$IP/extensions/AnyWikiDraw/AnyWikiDraw.php");
43
* @author Werner Randelshofer
44
* @version $Id: AnyWikiDraw.php 131 2009-07-08 20:24:04Z rawcoder $
47
# Add a hook for the parser function setup function
48
$wgExtensionFunctions[] = 'efAnyWikiDrawParserFunction_Setup';
49
$wgHooks['LanguageGetMagic'][] = 'efAnyWikiDrawParserFunction_Magic';
51
# Add a hook for the special page
52
$wgAutoloadClasses['AnyWikiDraw'] = dirname(__FILE__) . '/AnyWikiDraw.body.php';
53
$wgExtensionMessagesFiles['AnyWikiDraw'] = dirname(__FILE__) . '/AnyWikiDraw.i18n.php';
54
$wgSpecialPages['AnyWikiDraw'] = 'AnyWikiDraw';
56
# Setup the AnyWikDraw parser function
57
function efAnyWikiDrawParserFunction_Setup() {
58
# Setup AnyWikiDraw Version
59
global $wgAnywikidrawVersion;
60
$wgAnyWikiDrawVersion = '0.13.2';
63
global $wgMessageCache;
64
require( dirname( __FILE__ ) . '/AnyWikiDraw.i18n.php' );
65
foreach ( $messages as $lang => $langMessages ) {
66
$wgMessageCache->addMessages( $langMessages, $lang );
69
# Setup extension credits
70
$wgExtensionCredits['parserhook'][] = array(
71
'name' => 'AnyWikiDraw',
72
'version' => $wgAnyWikiDrawVersion,
73
'author' =>'Werner Randelshofer',
74
'url' => 'http://sourceforge.net/projects/anywikidraw',
75
'description' => 'The AnyWikiDraw extensions adds a <nowiki>{{#drawing:}}</nowiki> tag to the MediaWiki parser which allows to edit SVG, PNG and JPEG images directly in a page using a Java applet.'
78
# Setup function hook associating the "drawing" magic word with our function
80
$wgParser->setFunctionHook( 'drawing', 'efAnyWikiDrawParserFunction_Render' );
84
function efAnyWikiDrawParserFunction_Magic( &$magicWords, $langCode ) {
86
# The first array element is case sensitive, in this case it is not case sensitive
87
# All remaining elements are synonyms for our parser function
88
$magicWords['drawing'] = array( 0, 'drawing' );
89
# unless we return true, other parser functions extensions won't get loaded.
93
function efAnyWikiDrawParserFunction_Render( &$parser, $name = null, $width = null, $height = null ) {
94
global $wgUser, $wgLang, $wgTitle, $wgRightsText, $wgOut, $wgArticlePath, $wgScriptPath, $wgEnableUploads;
95
$skin = $wgUser->getSkin();
97
// Don't cache pages with drawings on it
98
$parser->disableCache();
100
# Validate parameters
102
if ($name == null || strlen($name) == 0) {
103
$error .= '<br>Please specify a name for your drawing.';
105
if ($width != null &&
106
(! is_numeric($width) || $width < 1 || $width > 2000)) {
107
$error .= '<br>Please specify the width as a number between 1 and 2000 or leave it away.';
109
if ($height != null &&
110
(! is_numeric($height) || $height < 1 || $height > 2000)) {
111
$error .= '<br>Please specify the height as a number between 1 and 2000 or leave it away.';
113
if (strlen($error) > 0) {
114
$error = '<b>Sorry.</b>'.$error.'<br>'.
115
'Usage: <code>{{#drawing:<i>image.svg</i>|<i>width</i>||<i>height</i>}}</code><br>'.
116
'Example: <code>{{#drawing:HappyDrawing.svg|400|300}}</code><br>';
117
return array($error, 'isHTML'=>true, 'noparse'=>true);
120
# The parser function itself
121
# The input parameters are wikitext with templates expanded
122
# The output should be wikitext too, but in this case, it is HTML
123
#return array("param1 is $param1 and param2 is $param2", 'isHTML');
125
# Generate the image HTML as if viewed by a web request
126
$image = wfFindFile($name);
129
$isProtected = $parser->getTitle()->isProtected();
131
if ($width == null && $image != null && $image->getWidth() != -1) {
132
$width = $image->getWidth();
134
if ($height == null && $image != null && $image->getHeight() != -1) {
135
$height = $image->getHeight();
139
$output = '<table><tr><td>';
140
if ($wgEnableUploads && ! $isProtected &&
141
key_exists('drawingtitle', $_POST) &&
142
$_POST['drawingtitle'] == $name) {
144
// edit the drawing using the applet
145
$uploadURL = str_replace('$1', 'Special:AnyWikiDraw', $wgArticlePath);
147
'<a name="anywikidraw" id="anywikidraw">'.
148
'<applet codebase="/"'.
149
' archive="'.$wgScriptPath.'/extensions/AnyWikiDraw/AnyWikiDrawForMediaWiki.jar"'.
150
' code="org.anywikidraw.mediawiki.MediaWikiDrawingApplet.class"'.
151
' width="'.htmlspecialchars(min(max($width+4, 600), 800)).'"'.
152
' height="'.htmlspecialchars(min(max($height+240, 480), 600)).'"'.
156
// The following parameters are used to tell AnyWikiDraw how to communicate with MediaWiki:
158
//'<script type="text/javascript">document.write(\'<param name="Cookies" value="\'+document.cookie+\'"/>\');</script>'.
159
'<param name="DrawingName" value="'.htmlspecialchars($name).'"/>'.
160
'<param name="DrawingWidth" value="'.htmlspecialchars($width).'"/>'.
161
'<param name="DrawingHeight" value="'.htmlspecialchars($height).'"/>'.
162
(($image !== false) ? '<param name="DrawingURL" value="'.$image->getFullUrl().'">' : '').
163
'<param name="PageURL" value="'.htmlspecialchars($wgTitle->getLocalURL()).'">'.
164
'<param name="UploadURL" value="'.$uploadURL.'">'.
166
// The following parameters are used to configure the drawing applet:
168
'<param name="Locale" value="'.$wgUser->getOption('language','en').'"/>'.
170
// The following parameters are used to configure Sun's Java Plug-In:
172
'<param name="codebase_lookup" value="false"/>'.
173
'<param name="classloader_cache" value="false"/>'.
174
'<param name="java_arguments" value="-Djnlp.packEnabled=true"/>'.
175
'<param name="image" value="lib/Splash.gif"/>'.
176
'<param name="boxborder" value="false"/>'.
177
'<param name="centerimage" value="true"/>'.
183
$output .= '<div style="'.
184
'background-color: #C9CDD6; border: 1px solid #ccc; padding: 5px 10px 5px 10px; text-align: left; '.
185
'font-size: 12px; line-height: 16px; '.
187
wfMsg(($image === false) ? 'anywikidraw_license_terms_new_work' : 'anywikidraw_license_terms_derived_work',
188
'<a href='.wfMsgForContent('copyrightpage').'>'.wfMsg('copyrightpage').'</a>', '<a href="./Image:'.$name.'">Image:'.$name.'</a>').
191
// Retrieve the page object of the image to determine, whether the user may edit it
192
$filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
193
$nt = Title::newFromText( $filtered );
194
if(! is_null( $nt ) ) {
195
$nt =& Title::makeTitle( NS_IMAGE, $nt->getDBkey() );
198
// Determine if the user has permission to edit the image
199
$userCanEdit = $wgEnableUploads &&
201
(is_null($nt) || $nt->userCanEdit()) &&
202
(is_null($image) || $wgUser->isAllowed( 'reupload' ));
203
/*$output .= 'enableUploads='.$wgEnableUploads.','.
204
'isProtected='.$isProtected.','.
205
'page.userCanEdit='.(is_null($nt) ? 'null' : $nt->userCanEdit()).','.
206
'image.exists='.$image->exists().','.
207
'user.reupload allowed='.$wgUser->isAllowed( 'reupload' );
210
// Determine if the user may edit images using the specified
211
// filename extension.
213
$extension = array_pop(explode( '.', $filtered ));
214
global $wgFileExtensions;
215
$userCanEdit = in_array($extension, $wgFileExtensions);
219
// If the user can edit the image, display an edit link.
220
// We do not display the edit link, if the user is already
221
// editing a drawing.
222
if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
223
$formId = 'Form'.rand();
224
global $wgUsePathInfo;
225
if ($wgUsePathInfo) {
226
$action = $wgTitle->getLocalURL().'#anywikidraw';
228
//$action = str_replace('?','#anywikidraw?',$wgTitle->getLocalURL());
229
$action = $wgTitle->getLocalURL();
231
$output .= '<form name="'.$formId.'" method="post" action="'.$action.'">'.
232
'<input type="hidden" name="drawingtitle" value="'.htmlspecialchars($name).'">'.
234
'[<a href="javascript:document.'.$formId.'.submit();">'.wfMsg('edit').'</a>]'.
235
'<noscript><input type="submit" name="submit" value="'.wfMsg('edit').'"></input></noscript>'.
239
// render the drawing
240
if ($image === false) {
241
// the drawing does not exist yet, render an empty rectangle
242
$output .= '<div style="border:1px solid #000;text-align:center;'.
243
(($width != null) ? 'width:'.$width.'px;' : '').
244
(($height != null) ? 'height:'.$height.'px;' : '').'"'.
245
'>'.htmlspecialchars($name).'</div>';
248
// Render the image map, if it exists
249
$thumbnail = $image->getThumbnail($width);
250
$isImageMap = $thumbnail != null && file_exists($thumbnail->path.'.map');
251
$mapId = 'Map_'.$name.'_'.rand();
253
$output .= '<map name="'.$mapId.'">'.
254
file_get_contents($thumbnail->path.'.map').
259
$output .= '<a href="./Image:'.$name.'">';
261
// Note: We append the timestamp of the image to the
262
// view URL as a query string. This way, we ensure,
263
// that the browser always displays the last edited version
266
'src="'.$image->getViewUrl().
267
'?version='.$image->nextHistoryLine()->img_timestamp.'" '.
268
(($width != null) ? 'width="'.$width.'" ' : '').
269
(($height != null) ? 'height="'.$height.'" ' : '').
270
'alt="Image:'.$name.'" '.
271
'title="Image:'.$name.'" '.
272
(($isImageMap) ? 'usemap="#'.$mapId.'" ' : '').
278
// If the user can edit the image, display an edit link.
279
// We do not display the edit link, if the user is already
280
// editing a drawing.
281
if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
282
$output .= '</form>';
287
$output .= '</tr></td></table>';
289
return array($output, 'isHTML'=>true, 'noparse'=>true);
b'\\ No newline at end of file'