/
DrawioEditor.php
201 lines (168 loc) · 7.84 KB
/
DrawioEditor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
$wgExtensionCredits['parserhook'][] = array(
'name' => 'DrawioEditor',
'version' => "1.0",
'description' => 'draw.io flow chart creation and inline editing',
'author' => 'Markus Gebert',
'url' => 'https://github.com/mgeb/mediawiki-drawio-editor'
);
$wgHooks['ParserFirstCallInit'][] = 'DrawioEditor::onParserSetup';
$wgHooks['OutputPageParserOutput'][] = 'DrawioEditor::onOutputPageParserOutput';
$wgExtensionMessagesFiles['DrawioEditor'] = __DIR__ . '/DrawioEditor.i18n.php';
$wgResourceModules['ext.drawioeditor'] = array(
'scripts' => 'ext.drawioeditor.js',
'styles' => 'ext.drawioeditor.css',
'dependencies' => array('jquery.ui.resizable'),
'localBasePath' => __DIR__ . '/resources',
'remoteExtPath' => 'DrawioEditor/resources',
);
/* Config Defaults */
$wgDrawioEditorImageType = 'svg';
$wgDrawioEditorImageInteractive = false;
class DrawioEditor {
public static function onParserSetup(&$parser) {
$parser->setFunctionHook( 'drawio', 'DrawioEditor::parse' );
}
public static function onOutputPageParserOutput(&$outputPage, $parseroutput) {
$outputPage->addModules('ext.drawioeditor');
}
public static function parse(&$parser, $name=null) {
global $wgUser, $wgEnableUploads;
global $wgDrawioEditorImageType;
global $wgDrawioEditorImageInteractive;
/* disable caching before any output is generated */
$parser->disableCache();
/* parse named arguments */
$opts = array();
foreach (array_slice(func_get_args(), 2) as $rawopt) {
$opt = explode('=', $rawopt, 2);
$opts[trim($opt[0])] = count($opt) === 2 ? trim($opt[1]) : true;
}
$opt_type = array_key_exists('type', $opts) ? $opts['type'] : $wgDrawioEditorImageType;
$opt_interactive = array_key_exists('interactive', $opts) ? true : $wgDrawioEditorImageInteractive;
$opt_height = array_key_exists('height', $opts) ? $opts['height'] : 'auto';
$opt_width = array_key_exists('width', $opts) ? $opts['width'] : '100%';
$opt_max_width = array_key_exists('max-width', $opts) ? $opts['max-width'] : false;
/* process input */
if ($name == null || !strlen($name))
return self::errorMessage('Usage Error');
if (!in_array($opt_type, ['svg', 'png']))
return self::errorMessage('Invalid type');
$len_regex = '/^((0|auto|chart)|[0-9]+(\.[0-9]+)?(px|%|mm|cm|in|em|ex|pt|pc))$/';
$len_regex_max = '/^((0|none|chart)|[0-9]+(\.[0-9]+)?(px|%|mm|cm|in|em|ex|pt|pc))$/';
if (!preg_match($len_regex, $opt_height))
return self::errorMessage('Invalid height');
if (!preg_match($len_regex, $opt_width))
return self::errorMessage('Invalid width');
if ($opt_max_width) {
if (!preg_match('/%$/', $opt_width))
return self::errorMessage('max-width is only allowed when width is relative');
if (!preg_match($len_regex_max, $opt_max_width))
return self::errorMessage('Invalid max-width');
} else {
$opt_max_width = 'chart';
}
$name = wfStripIllegalFilenameChars($name);
$dispname = htmlspecialchars($name, ENT_QUOTES);
/* random id to reference html elements */
$id = mt_rand();
/* prepare image information */
$img_name = $name.".drawio.".$opt_type;
$img = wfFindFile($img_name);
if ($img) {
$img_url = $img->getViewUrl();
$img_url_ts = $img_url.'?ts='.$img->nextHistoryLine()->img_timestamp;
$img_desc_url = $img->getDescriptionUrl();
$img_height = $img->getHeight().'px';
$img_width = $img->getWidth().'px';
} else {
$img_url = '';
$img_url_ts = '';
$img_desc_url = '';
$img_height = 0;
$img_width = 0;
}
$css_img_height = $opt_height === 'chart' ? $img_height : $opt_height;
$css_img_width = $opt_width === 'chart' ? $img_width : $opt_width;
$css_img_max_width = $opt_max_width === 'chart' ? $img_width : $opt_max_width;
/* check for conditions that should or will prevent an edit of the chart */
$readonly = (!$wgEnableUploads
|| (!$img && !$wgUser->isAllowed('upload'))
|| ($img && !$wgUser->isAllowed('reupload'))
|| $parser->getTitle()->isProtected('edit')
);
/* prepare edit href */
$edit_ahref = sprintf("<a href='javascript:editDrawio(\"%s\", %s, \"%s\", %s, %s, %s, %s)'>",
$id,
json_encode($img_name, JSON_HEX_QUOT | JSON_HEX_APOS),
$opt_type,
$opt_interactive ? 'true' : 'false',
$opt_height === 'chart' ? 'true' : 'false',
$opt_width === 'chart' ? 'true' : 'false',
$opt_max_width === 'chart' ? 'true': 'false');
/* output begin */
$output = '<div>';
/* div around the image */
$output .= '<div id="drawio-img-box-'.$id.'">';
/* display edit link */
if (!$readonly) {
$output .= '<div align="right">';
$output .= '<span class="mw-editsection">';
$output .= '<span class="mw-editsection-bracket">[</span>';
$output .= $edit_ahref;
$output .= wfMessage('edit')->text().'</a>';
$output .= '<span class="mw-editsection-bracket">]</span>';
$output .= '</span>';
$output .= '</div>';
}
/* prepare image */
$img_style = sprintf('height: %s; width: %s; max-width: %s;',
$css_img_height, $css_img_width, $css_img_max_width);
if (!$img) {
$img_style .= ' display:none;';
}
if ($opt_interactive)
{
$img_fmt = '<object id="drawio-img-%s" data="%s" type="text/svg+xml" style="%s"></object>';
$img_html = sprintf($img_fmt, $id, $img_url_ts, $img_style);
} else {
$img_fmt = '<img id="drawio-img-%s" src="%s" title="%s" alt="%s" style="%s"></img>';
$img_html = '<a id="drawio-img-href-'.$id.'" href="'.$img_desc_url.'">';
$img_html .= sprintf($img_fmt, $id, $img_url_ts, 'drawio: '.$dispname, 'drawio: '.$dispname, $img_style);
$img_html .= '</a>';
}
/* output image and optionally a placeholder if the image does not exist yet */
if (!$img) {
// show placeholder
$output .= sprintf('<div id="drawio-placeholder-%s" class="DrawioEditorInfoBox">'.
'<b>%s</b><br/>empty draw.io chart</div> ',
$id, $dispname);
}
// the image or object element must be there' in any case (it's hidden as long as there is no content.
$output .= $img_html;
$output .= '</div>';
/* editor and overlay divs, iframe is added by javascript on demand */
$output .= '<div id="drawio-iframe-box-'.$id.'" style="display:none;">';
$output .= '<div id="drawio-iframe-overlay-'.$id.'" class="DrawioEditorOverlay" style="display:none;"></div>';
$output .= '</div>';
/* output end */
$output .= '</div>';
/*
* link the image to the ParserOutput, so that the mediawiki knows that
* it is used by the hosting page (through the DrawioEditor extension).
* Note: This only works if the page is edited after the image has been
* created (i.e. saved in the DrawioEditor for the first time).
*/
if ($img) {
$parser->getOutput()->addImage($img->getTitle()->getDBkey());
}
return array($output, 'isHTML'=>true, 'noparse'=>true);
}
private static function errorMessage($msg) {
$output = '<div class="DrawioEditorInfoBox" style="border-color:red;">';
$output .= '<p style="color: red;">DrawioEditor Usage Error:<br/>'.htmlspecialchars($msg).'</p>';
$output .= '</div>';
return array($output, 'isHTML'=>true, 'noparse'=>true);
}
}
?>