This repository was archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblockdiag.php
192 lines (160 loc) · 5.08 KB
/
blockdiag.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
<?php
/**
* @filename: blockdiag.php
* @license: LGPL (GNU Lesser General Public License) http://www.gnu.org/licenses/lgpl.html
* @author: Kazunori Kojima
*/
/**
blockdiag_example:
blockdiag {
A -> B -> C
B -> D
}
**/
if( defined( 'MEDIAWIKI' ) ) {
$wgHooks['ParserFirstCallInit'][] = 'blockdiagMain';
$wgJobClasses['uploadBlockdiag'] = 'UploadBlockdiagJob';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'blockdiag',
'author' => 'Kazunori Kojima',
'url' => '',
'description' => 'Allow the use of blockdiag syntax.'
);
function blockdiagMain( &$parser ){
$parser->setHook( 'blockdiag', 'blockdiagDisplay' );
return true;
}
function blockdiagDisplay( $input, $args, $parser ){
global $wgTmpDirectory;
global $wgUploadDirectory;
global $wgUploadPath;
$wgBlockdiagDirectory = "$wgUploadDirectory/blockdiag";
$wgBlockdiagUrl = "$wgUploadPath/blockdiag";
$newBlockdiag = new Blockdiag(
$wgBlockdiagDirectory,
$wgBlockdiagUrl,
$wgTmpDirectory,
$input
);
$html = $newBlockdiag->showImage();
return $html;
}
}
/**
* Blockdiag
**/
class Blockdiag {
private $_path_array = array(
'blockdiag' => '/usr/local/bin/blockdiag',
'seqdiag' => '/usr/local/bin/seqdiag',
'actdiag' => '/usr/local/bin/actdiag',
'nwdiag' => '/usr/local/bin/nwdiag',
'rackdiag' => '/usr/local/bin/rackdiag', # in nwdiag
'packetdiag' => '/usr/local/bin/packetdiag', # in nwdiag
);
private $_imgType = 'png';
private $_hash;
private $_source;
private $_tmpDir;
private $_blockdiagDir;
private $_blockdiagUrl;
public function __construct( $blockdiagDir, $blockdiagUrl, $tmpDir, $source )
{
$this->_blockdiagDir = $blockdiagDir;
$this->_blockdiagUrl = $blockdiagUrl;
$this->_tmpDir = $tmpDir;
$this->_hash = md5( $source );
$this->_source = $source;
}
public function showImage() {
if( file_exists( $this->_getImagePath() ) ) {
$html = $this->_mkImageTag();
} else {
$html = $this->_generate();
}
return $html;
}
private function _generate() {
if (preg_match('/^\s*(\w+)\s*{/', $this->_source, $matches)) {
$diagram_type = $matches[1];
} else {
#return $this->_error("diagtype is not specified.");
$diagram_type = 'blockdiag'; # blockdiag for default
}
$diagprog_path = $this->_path_array[$diagram_type];
if (!is_file($diagprog_path))
{
return $this->_error("$diagram_type is not found at the specified place.");
}
// temporary directory check
if( !file_exists( $this->_tmpDir ) ){
if( !wfMkdirParents( $this->_tmpDir ) ) {
return $this->_error( 'temporary directory is not found.' );
}
} elseif ( !is_dir( $this->_tmpDir ) ){
return $this->_error( 'temporary directory is not directory' );
} elseif ( !is_writable( $this->_tmpDir ) ){
return $this->_error( 'temporary directory is not writable' );
}
// create temporary file
$dstTmpName = tempnam( $this->_tmpDir, 'blockdiag' );
$srcTmpName = tempnam( $this->_tmpDir, 'blockdiag' );
// write blockdiag source
$fp = fopen( $srcTmpName, 'w');
fwrite($fp, $this->_source);
fclose($fp);
// generate blockdiag image
$cmd = $diagprog_path . ' -T ' .
escapeshellarg( $this->_imgType ) . ' -o ' .
escapeshellarg( $dstTmpName ) . ' ' .
escapeshellarg( $srcTmpName );
$res = `$cmd`;
if( filesize( $dstTmpName ) == 0 ) {
return $this->_error( 'unknown error.' );
}
// move to image directory
$hashpath = $this->_getHashPath();
if( !file_exists( $hashpath ) ) {
if( !@wfMkdirParents( $hashpath, 0755 ) ) {
return $this->_error( 'can not make blockdiag image directory', $this->_blockdiagDir );
}
} elseif( !is_dir( $hashpath ) ) {
return $this->_error( 'blockdiag image directory is already exists. but not directory' );
} elseif( !is_writable( $hashpath ) ) {
return $this->_error( 'blockdiag image directory is not writable' );
}
if( !rename( "$dstTmpName", "$hashpath/{$this->_hash}.png" ) ) {
return $this->_error( 'can not rename blockdiag image' );
}
return $this->_mkImageTag();
}
private function _mkImageTag() {
$url = $this->_getImageUrl();
return Xml::element(
'img',
array(
'class' => 'blockdiag',
'src' => $url,
)
);
}
private function _getImageUrl() {
return "{$this->_blockdiagUrl}/{$this->_getHashSubPath()}/{$this->_hash}.png";
}
private function _getImagePath() {
return "{$this->_blockdiagDir}/{$this->_getHashSubPath()}/{$this->_hash}.png";
}
private function _getHashPath() {
return "{$this->_blockdiagDir}/{$this->_getHashSubPath()}";
}
private function _getHashSubPath() {
return substr($this->_hash, 0, 1)
.'/'. substr($this->_hash, 1, 1)
.'/'. substr($this->_hash, 2, 1);
}
private function _error( $msg, $append = '' ) {
$mf = 'blockdiag';
$errmsg = htmlspecialchars( $msg . ' ' . $append );
return "<strong class='error'>$mf ($errmsg)</strong>\n";
}
}