r39056 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r39055‎ | r39056 | r39057 >
Date:08:43, 10 August 2008
Author:siebrand
Status:old
Tags:
Comment:
* add svn keywords
* rename body file
* add comment about conversion to $wgAutoloadClasses
* indentation, trailing whitespace
Modified paths:
  • /trunk/extensions/Lua/Lua.body.php (deleted) (history)
  • /trunk/extensions/Lua/Lua.i18n.php (modified) (history)
  • /trunk/extensions/Lua/Lua.php (modified) (history)
  • /trunk/extensions/Lua/LuaWrapper.lua (modified) (history)
  • /trunk/extensions/Lua/Lua_body.php (added) (history)
  • /trunk/extensions/Lua/Lua_body.php (added) (history)

Diff [purge]

Index: trunk/extensions/Lua/Lua.body.php
@@ -1,221 +0,0 @@
2 -<?php
3 -/**
4 - * Lua parser extensions for MediaWiki - Body
5 - *
6 - * @author Fran Rogers
7 - * @package MediaWiki
8 - * @addtogroup Extensions
9 - * @license See 'COPYING'
10 - * @file
11 - */
12 -
13 -function efLua_ParserInit() {
14 - global $wgParser;
15 - $wgParser->setHook( 'lua', 'efLua_Render' );
16 - return true;
17 -}
18 -
19 -function efLua_FunctionSetup() {
20 - global $wgParser;
21 - $wgParser->setFunctionHook('luaexpr', 'efLua_RenderExpr');
22 -}
23 -
24 -function efLua_Magic(&$magicWords, $langCode) {
25 - $magicWords['luaexpr'] = array(0, 'luaexpr');
26 - return true;
27 -}
28 -
29 -function efLua_BeforeTidy(&$parser, &$text) {
30 - if (!isset($wgLua)) {
31 - efLua_Cleanup();
32 - }
33 - return TRUE;
34 -}
35 -
36 -function efLua_Render($input, $args, &$parser) {
37 - $arglist = '';
38 - foreach ($args as $key => $value)
39 - $arglist .= (preg_replace('/\W/', '', $key) . '=\'' .
40 - addslashes($parser->recursiveTagParse($value)) .
41 - '\';');
42 - if ($arglist) {
43 - try {
44 - efLua_Eval($arglist);
45 - } catch (LuaError $e) {
46 - return $e->getMessage();
47 - }
48 - }
49 -
50 - try {
51 - return $parser->recursiveTagParse(efLua_Eval($input));
52 - } catch (LuaError $e) {
53 - return $e->getMessage();
54 - }
55 -}
56 -
57 -function efLua_RenderExpr(&$parser, $param1 = FALSE) {
58 - if ($param1 == FALSE)
59 - return '';
60 - try {
61 - return efLua_Eval("io.write($param1)");
62 - } catch (LuaError $e) {
63 - return $e->getMessage();
64 - }
65 -}
66 -
67 -
68 -/**
69 - * @todo Nearly all of these evaluate() calls should be replaced, as bugs
70 - * in the Lua PECL extension are fixed.
71 - */
72 -function efLua_Eval($input) {
73 - global $wgLuaExternalInterpreter, $wgLuaDefunct,
74 - $wgLua, $wgLuaSandbox, $wgLuaWrapperFile,
75 - $wgLuaMaxLines, $wgLuaMaxCalls;
76 - if (isset($wgLuaDefunct) && $wgLuaDefunct) {
77 - return '';
78 - } else if ($wgLuaExternalInterpreter != FALSE) {
79 - return efLua_EvalExternal($input);
80 - } else if (!class_exists('lua')) {
81 - $wgLuaDefunct = true;
82 - throw new LuaError('extension_notfound');
83 - }
84 -
85 - if (!isset($wgLua)) {
86 - $wgLua = new lua;
87 - try {
88 - $wgLua->evaluatefile($wgLuaWrapperFile);
89 - $wgLua->evaluate("sandbox = make_sandbox()");
90 - $wgLua->evaluate("function _die(reason)".
91 - " _G._DEAD = reason; end");
92 - $wgLua->evaluate("hook = make_hook($wgLuaMaxLines, ".
93 - "$wgLuaMaxCalls, _die)");
94 - // @todo Especially these ones. :/
95 - } catch (Exception $e) {
96 - throw new LuaError('error_internal');
97 - }
98 - }
99 -
100 - $wgLua->input = $input;
101 - $wgLua->evaluate('chunk, err = loadstring(input)');
102 - if ($err = $wgLua->err) {
103 - $err = preg_replace('/^\[.+?\]:(.+?):/', '$1:', $err);
104 - throw new LuaError('error', $err);
105 - }
106 - $wgLua->res = $wgLua->err = NULL;
107 -
108 - $wgLua->evaluate('res, err = wrap(chunk, sandbox, hook)');
109 -
110 - if (($err = $wgLua->_DEAD) || ($err = $wgLua->err)) {
111 - if ($err == 'RECURSION_LIMIT') {
112 - efLua_CleanupExternal();
113 - throw new LuaError('overflow_recursion');
114 - } else if ($err == 'LOC_LIMIT') {
115 - efLua_CleanupExternal();
116 - throw new LuaError('overflow_loc');
117 - } else {
118 - $err = preg_replace('/^\[.+?\]:(.+?):/', '$1:', $err);
119 - throw new LuaError('error', $err);
120 - }
121 - }
122 -
123 - $wgLua->evaluate('_OUTPUT = sandbox._OUTPUT'); // ugh, please fix!
124 - $out = $wgLua->_OUTPUT;
125 - return (trim($out) != '') ? $out : '';
126 -}
127 -
128 -function efLua_Cleanup() {
129 - global $wgLuaExternalInterpreter, $wgLuaDefunct, $wgLua;
130 - if (isset($wgLuaDefunct) && $wgLuaDefunct) {
131 - return FALSE;
132 - } else if (isset($wgLuaExternalInterpreter)) {
133 - return efLua_CleanupExternal();
134 - } else if (isset($wgLua)) {
135 - $wgLuaDefunct = TRUE;
136 - $wgLua = FALSE;
137 - return TRUE;
138 - }
139 -}
140 -
141 -function efLua_EvalExternal($input) {
142 - global $wgLuaExternalInterpreter, $wgLuaProc, $wgLuaPipes,
143 - $wgLuaWrapperFile, $wgLuaMaxLines, $wgLuaMaxCalls,
144 - $wgLuaMaxTime;
145 - if (!isset($wgLuaProc)) {
146 - $wgLua = TRUE;
147 - $luacmd = "$wgLuaExternalInterpreter $wgLuaWrapperFile $wgLuaMaxLines $wgLuaMaxCalls";
148 - $wgLuaProc = proc_open($luacmd,
149 - array(0 => array('pipe', 'r'),
150 - 1 => array('pipe', 'w')),
151 - $wgLuaPipes, NULL, NULL);
152 - if (!is_resource($wgLuaProc)) {
153 - $wgLuaDefunct = true;
154 - throw new LuaError('interp_notfound');
155 - }
156 - stream_set_blocking($wgLuaPipes[0], 0);
157 - stream_set_blocking($wgLuaPipes[1], 0);
158 - stream_set_write_buffer($wgLuaPipes[0], 0);
159 - stream_set_write_buffer($wgLuaPipes[1], 0);
160 - }
161 -
162 - $input = trim(preg_replace('/(?<=\n|^)\.(?=\n|$)/', '. --', $input));
163 - fwrite($wgLuaPipes[0], "$input\n.\n");
164 - fflush($wgLuaPipes[0]);
165 -
166 - $res = '';
167 - $read = array($wgLuaPipes[1]);
168 - $write = NULL;
169 - $except = NULL;
170 - while (!feof($wgLuaPipes[1])) {
171 - if (false === ($num_changed_streams =
172 - @stream_select($read, $write, $except,
173 - $wgLuaMaxTime))) {
174 - efLua_CleanupExternal();
175 - throw new LuaError('overflow_time');
176 - }
177 - $line = fgets($wgLuaPipes[1]);
178 - if ($line == ".\n")
179 - break;
180 - $res .= $line;
181 - }
182 -
183 - if (preg_match('/^\'(.*)\', (true|false)$/s', trim($res), $match) != 1) {
184 - efLua_CleanupExternal();
185 - throw new LuaError('error_internal');
186 - }
187 -
188 - $out = $match[1];
189 - if ($match[2] == 'true') {
190 - return (trim($out) != '') ? $out : '';
191 - } else {
192 - if ($out == 'RECURSION_LIMIT') {
193 - efLua_CleanupExternal();
194 - throw new LuaError('overflow_recursion');
195 - } else if ($out == 'LOC_LIMIT') {
196 - efLua_CleanupExternal();
197 - throw new LuaError('overflow_loc');
198 - } else {
199 - $out = preg_replace('/^\[.+?\]:(.+?):/', '$1:', $out);
200 - throw new LuaError('error', $out);
201 - }
202 - }
203 -}
204 -
205 -function efLua_CleanupExternal() {
206 - global $wgLuaExternalInterpreter, $wgLuaDefunct,
207 - $wgLuaProc, $wgLuaPipes;
208 - if (isset($wgLuaProc)) {
209 - fclose($wgLuaPipes[0]);
210 - fclose($wgLuaPipes[1]);
211 - proc_close($wgLuaProc);
212 - }
213 - $wgLuaDefunct = TRUE;
214 - return TRUE;
215 -}
216 -
217 -class LuaError extends Exception {
218 - public function __construct($msg, $parameter = ''){
219 - wfLoadExtensionMessages( 'Lua' );
220 - $this->message = '<strong class="error">' . wfMsgForContent( "lua_$msg", htmlspecialchars( $parameter ) ) . '</strong>';
221 - }
222 -}
Index: trunk/extensions/Lua/LuaWrapper.lua
@@ -210,4 +210,4 @@
211211
212212 if arg ~= nil then
213213 main()
214 -end
\ No newline at end of file
 214+end
Index: trunk/extensions/Lua/Lua_body.php
@@ -0,0 +1,221 @@
 2+<?php
 3+/**
 4+ * Lua parser extensions for MediaWiki - Body
 5+ *
 6+ * @author Fran Rogers
 7+ * @package MediaWiki
 8+ * @addtogroup Extensions
 9+ * @license See 'COPYING'
 10+ * @file
 11+ */
 12+
 13+function efLua_ParserInit() {
 14+ global $wgParser;
 15+ $wgParser->setHook( 'lua', 'efLua_Render' );
 16+ return true;
 17+}
 18+
 19+function efLua_FunctionSetup() {
 20+ global $wgParser;
 21+ $wgParser->setFunctionHook('luaexpr', 'efLua_RenderExpr');
 22+}
 23+
 24+function efLua_Magic(&$magicWords, $langCode) {
 25+ $magicWords['luaexpr'] = array(0, 'luaexpr');
 26+ return true;
 27+}
 28+
 29+function efLua_BeforeTidy(&$parser, &$text) {
 30+ if (!isset($wgLua)) {
 31+ efLua_Cleanup();
 32+ }
 33+ return TRUE;
 34+}
 35+
 36+function efLua_Render($input, $args, &$parser) {
 37+ $arglist = '';
 38+ foreach ($args as $key => $value)
 39+ $arglist .= (preg_replace('/\W/', '', $key) . '=\'' .
 40+ addslashes($parser->recursiveTagParse($value)) .
 41+ '\';');
 42+ if ($arglist) {
 43+ try {
 44+ efLua_Eval($arglist);
 45+ } catch (LuaError $e) {
 46+ return $e->getMessage();
 47+ }
 48+ }
 49+
 50+ try {
 51+ return $parser->recursiveTagParse(efLua_Eval($input));
 52+ } catch (LuaError $e) {
 53+ return $e->getMessage();
 54+ }
 55+ }
 56+
 57+function efLua_RenderExpr(&$parser, $param1 = FALSE) {
 58+ if ($param1 == FALSE)
 59+ return '';
 60+ try {
 61+ return efLua_Eval("io.write($param1)");
 62+ } catch (LuaError $e) {
 63+ return $e->getMessage();
 64+ }
 65+}
 66+
 67+
 68+/**
 69+ * @todo Nearly all of these evaluate() calls should be replaced, as bugs
 70+ * in the Lua PECL extension are fixed.
 71+ */
 72+function efLua_Eval($input) {
 73+ global $wgLuaExternalInterpreter, $wgLuaDefunct,
 74+ $wgLua, $wgLuaSandbox, $wgLuaWrapperFile,
 75+ $wgLuaMaxLines, $wgLuaMaxCalls;
 76+ if (isset($wgLuaDefunct) && $wgLuaDefunct) {
 77+ return '';
 78+ } else if ($wgLuaExternalInterpreter != FALSE) {
 79+ return efLua_EvalExternal($input);
 80+ } else if (!class_exists('lua')) {
 81+ $wgLuaDefunct = true;
 82+ throw new LuaError('extension_notfound');
 83+ }
 84+
 85+ if (!isset($wgLua)) {
 86+ $wgLua = new lua;
 87+ try {
 88+ $wgLua->evaluatefile($wgLuaWrapperFile);
 89+ $wgLua->evaluate("sandbox = make_sandbox()");
 90+ $wgLua->evaluate("function _die(reason)".
 91+ " _G._DEAD = reason; end");
 92+ $wgLua->evaluate("hook = make_hook($wgLuaMaxLines, ".
 93+ "$wgLuaMaxCalls, _die)");
 94+ // @todo Especially these ones. :/
 95+ } catch (Exception $e) {
 96+ throw new LuaError('error_internal');
 97+ }
 98+ }
 99+
 100+ $wgLua->input = $input;
 101+ $wgLua->evaluate('chunk, err = loadstring(input)');
 102+ if ($err = $wgLua->err) {
 103+ $err = preg_replace('/^\[.+?\]:(.+?):/', '$1:', $err);
 104+ throw new LuaError('error', $err);
 105+ }
 106+ $wgLua->res = $wgLua->err = NULL;
 107+
 108+ $wgLua->evaluate('res, err = wrap(chunk, sandbox, hook)');
 109+
 110+ if (($err = $wgLua->_DEAD) || ($err = $wgLua->err)) {
 111+ if ($err == 'RECURSION_LIMIT') {
 112+ efLua_CleanupExternal();
 113+ throw new LuaError('overflow_recursion');
 114+ } else if ($err == 'LOC_LIMIT') {
 115+ efLua_CleanupExternal();
 116+ throw new LuaError('overflow_loc');
 117+ } else {
 118+ $err = preg_replace('/^\[.+?\]:(.+?):/', '$1:', $err);
 119+ throw new LuaError('error', $err);
 120+ }
 121+ }
 122+
 123+ $wgLua->evaluate('_OUTPUT = sandbox._OUTPUT'); // ugh, please fix!
 124+ $out = $wgLua->_OUTPUT;
 125+ return (trim($out) != '') ? $out : '';
 126+}
 127+
 128+function efLua_Cleanup() {
 129+ global $wgLuaExternalInterpreter, $wgLuaDefunct, $wgLua;
 130+ if (isset($wgLuaDefunct) && $wgLuaDefunct) {
 131+ return FALSE;
 132+ } else if (isset($wgLuaExternalInterpreter)) {
 133+ return efLua_CleanupExternal();
 134+ } else if (isset($wgLua)) {
 135+ $wgLuaDefunct = TRUE;
 136+ $wgLua = FALSE;
 137+ return TRUE;
 138+ }
 139+}
 140+
 141+function efLua_EvalExternal($input) {
 142+ global $wgLuaExternalInterpreter, $wgLuaProc, $wgLuaPipes,
 143+ $wgLuaWrapperFile, $wgLuaMaxLines, $wgLuaMaxCalls,
 144+ $wgLuaMaxTime;
 145+ if (!isset($wgLuaProc)) {
 146+ $wgLua = TRUE;
 147+ $luacmd = "$wgLuaExternalInterpreter $wgLuaWrapperFile $wgLuaMaxLines $wgLuaMaxCalls";
 148+ $wgLuaProc = proc_open($luacmd,
 149+ array(0 => array('pipe', 'r'),
 150+ 1 => array('pipe', 'w')),
 151+ $wgLuaPipes, NULL, NULL);
 152+ if (!is_resource($wgLuaProc)) {
 153+ $wgLuaDefunct = true;
 154+ throw new LuaError('interp_notfound');
 155+ }
 156+ stream_set_blocking($wgLuaPipes[0], 0);
 157+ stream_set_blocking($wgLuaPipes[1], 0);
 158+ stream_set_write_buffer($wgLuaPipes[0], 0);
 159+ stream_set_write_buffer($wgLuaPipes[1], 0);
 160+ }
 161+
 162+ $input = trim(preg_replace('/(?<=\n|^)\.(?=\n|$)/', '. --', $input));
 163+ fwrite($wgLuaPipes[0], "$input\n.\n");
 164+ fflush($wgLuaPipes[0]);
 165+
 166+ $res = '';
 167+ $read = array($wgLuaPipes[1]);
 168+ $write = NULL;
 169+ $except = NULL;
 170+ while (!feof($wgLuaPipes[1])) {
 171+ if (false === ($num_changed_streams =
 172+ @stream_select($read, $write, $except,
 173+ $wgLuaMaxTime))) {
 174+ efLua_CleanupExternal();
 175+ throw new LuaError('overflow_time');
 176+ }
 177+ $line = fgets($wgLuaPipes[1]);
 178+ if ($line == ".\n")
 179+ break;
 180+ $res .= $line;
 181+ }
 182+
 183+ if (preg_match('/^\'(.*)\', (true|false)$/s', trim($res), $match) != 1) {
 184+ efLua_CleanupExternal();
 185+ throw new LuaError('error_internal');
 186+ }
 187+
 188+ $out = $match[1];
 189+ if ($match[2] == 'true') {
 190+ return (trim($out) != '') ? $out : '';
 191+ } else {
 192+ if ($out == 'RECURSION_LIMIT') {
 193+ efLua_CleanupExternal();
 194+ throw new LuaError('overflow_recursion');
 195+ } else if ($out == 'LOC_LIMIT') {
 196+ efLua_CleanupExternal();
 197+ throw new LuaError('overflow_loc');
 198+ } else {
 199+ $out = preg_replace('/^\[.+?\]:(.+?):/', '$1:', $out);
 200+ throw new LuaError('error', $out);
 201+ }
 202+ }
 203+}
 204+
 205+function efLua_CleanupExternal() {
 206+ global $wgLuaExternalInterpreter, $wgLuaDefunct,
 207+ $wgLuaProc, $wgLuaPipes;
 208+ if (isset($wgLuaProc)) {
 209+ fclose($wgLuaPipes[0]);
 210+ fclose($wgLuaPipes[1]);
 211+ proc_close($wgLuaProc);
 212+ }
 213+ $wgLuaDefunct = TRUE;
 214+ return TRUE;
 215+}
 216+
 217+class LuaError extends Exception {
 218+ public function __construct($msg, $parameter = ''){
 219+ wfLoadExtensionMessages( 'Lua' );
 220+ $this->message = '<strong class="error">' . wfMsgForContent( "lua_$msg", htmlspecialchars( $parameter ) ) . '</strong>';
 221+ }
 222+}
Property changes on: trunk/extensions/Lua/Lua_body.php
___________________________________________________________________
Added: svn:mergeinfo
Added: svn:eol-style
1223 + native
Index: trunk/extensions/Lua/Lua.i18n.php
@@ -12,12 +12,12 @@
1313 $messages = array();
1414
1515 $messages['en'] = array(
16 - 'lua_desc' => 'Extends the parser with support for embedded blocks of [http://www.lua.org/ Lua] code',
17 - 'lua_error' => 'Error on line $1',
18 - 'lua_extension_notfound' => 'Lua extension not configured',
19 - 'lua_interp_notfound' => 'Lua interpreter not found',
20 - 'lua_error_internal' => 'Internal error',
21 - 'lua_overflow_recursion' => 'Recursion limit reached',
22 - 'lua_overflow_loc' => 'Maximum lines of code limit reached',
23 - 'lua_overflow_time' => 'Maximum execution time reached',
 16+ 'lua_desc' => 'Extends the parser with support for embedded blocks of [http://www.lua.org/ Lua] code',
 17+ 'lua_error' => 'Error on line $1',
 18+ 'lua_extension_notfound' => 'Lua extension not configured',
 19+ 'lua_interp_notfound' => 'Lua interpreter not found',
 20+ 'lua_error_internal' => 'Internal error',
 21+ 'lua_overflow_recursion' => 'Recursion limit reached',
 22+ 'lua_overflow_loc' => 'Maximum lines of code limit reached',
 23+ 'lua_overflow_time' => 'Maximum execution time reached',
2424 );
Index: trunk/extensions/Lua/Lua.php
@@ -10,18 +10,20 @@
1111 */
1212
1313 $wgExtensionCredits['parserhook'][] = array(
14 - 'name' => 'Lua parser extensions',
15 - 'author' => 'Fran Rogers',
16 - 'svn-date' => '$LastChangedDate$',
17 - 'svn-revision' => '$LastChangedRevision$',
18 - 'url' => 'http://www.mediawiki.org/wiki/Extension:Lua',
19 - 'description' => 'Extends the parser with support for embedded blocks ofLua code',
 14+ 'name' => 'Lua parser extensions',
 15+ 'author' => 'Fran Rogers',
 16+ 'svn-date' => '$LastChangedDate$',
 17+ 'svn-revision' => '$LastChangedRevision$',
 18+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Lua',
 19+ 'description' => 'Extends the parser with support for embedded blocks ofLua code',
2020 'descriptionmsg' => 'lua_desc',
2121 );
2222
23 -$wgExtensionMessagesFiles['Lua'] = dirname(__FILE__) . '/Lua.i18n.php';
24 -require_once(dirname(__FILE__) . '/Lua.body.php');
25 -$wgLuaWrapperFile = dirname(__FILE__) . '/LuaWrapper.lua';
 23+$dir = dirname(__FILE__) . '/';
 24+$wgExtensionMessagesFiles['Lua'] = $dir . 'Lua.i18n.php';
 25+// convert me to $wgAutoloadClasses
 26+require_once( $dir . 'Lua_body.php');
 27+$wgLuaWrapperFile = $dir . 'LuaWrapper.lua';
2628
2729 if (!isset($wgLuaExternalInterpreter))
2830 $wgLuaExternalInterpreter = FALSE;
@@ -33,12 +35,12 @@
3436 $wgLuaMaxCalls = 2000;
3537 if (!isset($wgLuaMaxTime))
3638 $wgLuaMaxTime = 5;
37 -
 39+
3840 # Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
3941 if (defined('MW_SUPPORTS_PARSERFIRSTCALLINIT')) {
40 - $wgHooks['ParserFirstCallInit'][] = 'efLua_ParserInit';
 42+ $wgHooks['ParserFirstCallInit'][] = 'efLua_ParserInit';
4143 } else { // Otherwise do things the old fashioned way
42 - $wgExtensionFunctions[] = 'efLua_ParserInit';
 44+ $wgExtensionFunctions[] = 'efLua_ParserInit';
4345 }
4446 # Define a setup function
4547 $wgExtensionFunctions[] = 'efLua_FunctionSetup';
Property changes on: trunk/extensions/Lua/Lua.php
___________________________________________________________________
Added: svn:keywords
4648 + LastChangedDate LastChangedRevision

Status & tagging log