Index: trunk/extensions/LuaFoo/includes/SpecialLuaTranslation.php |
— | — | @@ -10,8 +10,6 @@ |
11 | 11 | } |
12 | 12 | |
13 | 13 | function execute( $par ) { |
14 | | - global $wgRequest; |
15 | | - |
16 | 14 | $this->setHeaders(); |
17 | 15 | |
18 | 16 | $form = new HTMLForm( array( |
Index: trunk/extensions/LuaFoo/includes/Converter.php |
— | — | @@ -24,6 +24,11 @@ |
25 | 25 | 'pfunc_if' => 'pfunc_if' |
26 | 26 | ); |
27 | 27 | |
| 28 | + /** |
| 29 | + * @param $title Title |
| 30 | + * @param $language |
| 31 | + * @return string |
| 32 | + */ |
28 | 33 | static function convert( $title, $language ) { |
29 | 34 | $converter = new self( $language ); |
30 | 35 | $code = $converter->convertTemplate( $title ); |
— | — | @@ -42,6 +47,10 @@ |
43 | 48 | } |
44 | 49 | } |
45 | 50 | |
| 51 | + /** |
| 52 | + * @param $title Title |
| 53 | + * @return LuaFoo_Converter_Block |
| 54 | + */ |
46 | 55 | function convertTemplate( $title ) { |
47 | 56 | $funcName = $this->titleToIdentifier( $title ); |
48 | 57 | $deps = $this->newDeps(); |
— | — | @@ -63,10 +72,18 @@ |
64 | 73 | return $block; |
65 | 74 | } |
66 | 75 | |
| 76 | + /** |
| 77 | + * @return LuaFoo_Converter_Deps |
| 78 | + */ |
67 | 79 | function newDeps() { |
68 | 80 | return new LuaFoo_Converter_Deps; |
69 | 81 | } |
70 | 82 | |
| 83 | + /** |
| 84 | + * @param $value |
| 85 | + * @return LuaFoo_Converter_Expression |
| 86 | + * @throws MWException |
| 87 | + */ |
71 | 88 | function newLiteral( $value ) { |
72 | 89 | if ( !is_string( $value ) && !is_numeric( $value ) && !is_array( $value ) ) { |
73 | 90 | throw new MWException( __METHOD__.': invalid literal type' ); |
— | — | @@ -74,12 +91,20 @@ |
75 | 92 | return new LuaFoo_Converter_Expression( 'literal', array( $value ) ); |
76 | 93 | } |
77 | 94 | |
| 95 | + /** |
| 96 | + * @param $hash |
| 97 | + * @return LuaFoo_Converter_Expression |
| 98 | + */ |
78 | 99 | function newHash( $hash ) { |
79 | 100 | return new LuaFoo_Converter_Expression( 'hash', array_merge( |
80 | 101 | array_map( array( $this, 'newLiteral' ), array_keys( $hash ) ), |
81 | 102 | array_values( $hash ) ) ); |
82 | 103 | } |
83 | 104 | |
| 105 | + /** |
| 106 | + * @param $items |
| 107 | + * @return LuaFoo_Converter_Expression|mixed |
| 108 | + */ |
84 | 109 | function newConcat( $items ) { |
85 | 110 | $filteredItems = array(); |
86 | 111 | foreach ( $items as $item ) { |
— | — | @@ -142,6 +167,11 @@ |
143 | 168 | } |
144 | 169 | } |
145 | 170 | |
| 171 | + /** |
| 172 | + * @param $expr |
| 173 | + * @param $deps |
| 174 | + * @return LuaFoo_Converter_Expression |
| 175 | + */ |
146 | 176 | function newTrim( $expr, $deps ) { |
147 | 177 | if ( $expr->op === 'literal' ) { |
148 | 178 | // Trim a literal |
— | — | @@ -166,6 +196,12 @@ |
167 | 197 | } |
168 | 198 | } |
169 | 199 | |
| 200 | + /** |
| 201 | + * @param $name |
| 202 | + * @param $args |
| 203 | + * @param $deps |
| 204 | + * @return LuaFoo_Converter_Expression |
| 205 | + */ |
170 | 206 | function newParserFunctionCall( $name, $args, $deps ) { |
171 | 207 | if ( isset( $this->inlineFunctions[$name] ) ) { |
172 | 208 | $inlineFunc = $this->inlineFunctions[$name]; |
— | — | @@ -177,10 +213,19 @@ |
178 | 214 | return $this->newExpression( 'call', $args ); |
179 | 215 | } |
180 | 216 | |
| 217 | + /** |
| 218 | + * @param $op |
| 219 | + * @param $args |
| 220 | + * @return LuaFoo_Converter_Expression |
| 221 | + */ |
181 | 222 | function newExpression( $op, $args ) { |
182 | 223 | return new LuaFoo_Converter_Expression( $op, $args ); |
183 | 224 | } |
184 | 225 | |
| 226 | + /** |
| 227 | + * @param $title Title |
| 228 | + * @return mixed|string |
| 229 | + */ |
185 | 230 | function titleToIdentifier( $title ) { |
186 | 231 | $id = ''; |
187 | 232 | if ( $title->getNamespace() == NS_TEMPLATE ) { |
— | — | @@ -195,11 +240,20 @@ |
196 | 241 | return $id; |
197 | 242 | } |
198 | 243 | |
| 244 | + /** |
| 245 | + * @param $name string |
| 246 | + * @return string |
| 247 | + */ |
199 | 248 | function parserFunctionToIdentifier( $name ) { |
200 | 249 | $name = preg_replace( '/[^\w\177-\377]/', '_', $name ); |
201 | 250 | return "pfunc_$name"; |
202 | 251 | } |
203 | 252 | |
| 253 | + /** |
| 254 | + * @param $title Title |
| 255 | + * @param $deps |
| 256 | + * @return LuaFoo_Converter_Expression|mixed |
| 257 | + */ |
204 | 258 | function getTemplateExpression( $title, $deps ) { |
205 | 259 | $rev = Revision::newFromTitle( $title ); |
206 | 260 | if ( !$rev ) { |
— | — | @@ -217,6 +271,12 @@ |
218 | 272 | return $expression; |
219 | 273 | } |
220 | 274 | |
| 275 | + /** |
| 276 | + * @param $contextNode PPNode_Hash_Array|PPNode_Hash_Attr|PPNode_Hash_Text|PPNode_Hash_Tree |
| 277 | + * @param $deps |
| 278 | + * @return LuaFoo_Converter_Expression|mixed |
| 279 | + * @throws MWException |
| 280 | + */ |
221 | 281 | function expand( $contextNode, $deps ) { |
222 | 282 | if ( is_string( $contextNode ) ) { |
223 | 283 | return $this->newLiteral( $contextNode ); |
— | — | @@ -256,6 +316,11 @@ |
257 | 317 | } |
258 | 318 | } |
259 | 319 | |
| 320 | + /** |
| 321 | + * @param $bits |
| 322 | + * @param $deps |
| 323 | + * @return LuaFoo_Converter_Expression|mixed |
| 324 | + */ |
260 | 325 | function expandTemplate( $bits, $deps ) { |
261 | 326 | global $wgContLang; |
262 | 327 | |
— | — | @@ -365,6 +430,11 @@ |
366 | 431 | } |
367 | 432 | } |
368 | 433 | |
| 434 | + /** |
| 435 | + * @param $piece |
| 436 | + * @param $deps |
| 437 | + * @return LuaFoo_Converter_Expression |
| 438 | + */ |
369 | 439 | function expandTemplateArg( $piece, $deps ) { |
370 | 440 | $parts = $piece['parts']; |
371 | 441 | $nameExpr = $this->newTrim( $this->expand( $piece['title'], $deps ), $deps ); |
— | — | @@ -394,6 +464,11 @@ |
395 | 465 | ) ); |
396 | 466 | } |
397 | 467 | |
| 468 | + /** |
| 469 | + * @param $args |
| 470 | + * @param $deps |
| 471 | + * @return LuaFoo_Converter_Expression |
| 472 | + */ |
398 | 473 | function pfunc_if( $args, $deps ) { |
399 | 474 | if ( isset( $args[0] ) ) { |
400 | 475 | $condition = $this->newExpression( 'equals', array( |
— | — | @@ -428,14 +503,24 @@ |
429 | 504 | $this->converter = $converter; |
430 | 505 | } |
431 | 506 | |
| 507 | + /** |
| 508 | + * @return LuaFoo_Converter_Block |
| 509 | + */ |
432 | 510 | function newBlock() { |
433 | 511 | return new LuaFoo_Converter_Block( $this ); |
434 | 512 | } |
435 | 513 | |
| 514 | + /** |
| 515 | + * @param $seed |
| 516 | + * @return LuaFoo_Converter_Frame |
| 517 | + */ |
436 | 518 | function newFrame( $seed ) { |
437 | 519 | return new LuaFoo_Converter_Frame( $this, $seed ); |
438 | 520 | } |
439 | 521 | |
| 522 | + /** |
| 523 | + * @param $name |
| 524 | + */ |
440 | 525 | function getRuntimeBlock( $name ) { |
441 | 526 | if ( $this->runtimeBlocks === null ) { |
442 | 527 | $this->setupRuntime(); |
— | — | @@ -485,6 +570,12 @@ |
486 | 571 | 'while' |
487 | 572 | ); |
488 | 573 | |
| 574 | + /** |
| 575 | + * @param $name |
| 576 | + * @param $argNames |
| 577 | + * @param $expression |
| 578 | + * @return LuaFoo_Converter_Block |
| 579 | + */ |
489 | 580 | public function makeFunction( $name, $argNames, $expression ) { |
490 | 581 | $frame = $this->newFrame( $name ); |
491 | 582 | $block = $this->expressionToBlock( $expression, $frame ); |
— | — | @@ -504,10 +595,19 @@ |
505 | 596 | return $func; |
506 | 597 | } |
507 | 598 | |
| 599 | + /** |
| 600 | + * @return array |
| 601 | + */ |
508 | 602 | public function getKeywords() { |
509 | 603 | return $this->keywords; |
510 | 604 | } |
511 | 605 | |
| 606 | + /** |
| 607 | + * @param $expression |
| 608 | + * @param $frame LuaFoo_Converter_Frame |
| 609 | + * @return LuaFoo_Converter_Block |
| 610 | + * @throws MWException |
| 611 | + */ |
512 | 612 | protected function expressionToBlock( $expression, $frame ) { |
513 | 613 | if ( !($expression instanceof LuaFoo_Converter_Expression ) ) { |
514 | 614 | throw new MWException( 'Non-expression passed to ' . __METHOD__ ); |
— | — | @@ -616,11 +716,20 @@ |
617 | 717 | return $block; |
618 | 718 | } |
619 | 719 | |
| 720 | + /** |
| 721 | + * @param $name string |
| 722 | + * @return bool |
| 723 | + */ |
620 | 724 | public function isName( $name ) { |
621 | 725 | return !in_array( $name, $this->keywords ) |
622 | 726 | && preg_match( '/^[a-zA-Z_][a-zA-Z0-9_]*$/', $name ); |
623 | 727 | } |
624 | 728 | |
| 729 | + /** |
| 730 | + * @param $input |
| 731 | + * @return float|string |
| 732 | + * @throws MWException |
| 733 | + */ |
625 | 734 | protected function encodeLiteral( $input ) { |
626 | 735 | if ( is_string( $input ) ) { |
627 | 736 | $result = strtr( $input, array( |
— | — | @@ -654,6 +763,11 @@ |
655 | 764 | } |
656 | 765 | } |
657 | 766 | |
| 767 | + /** |
| 768 | + * @param $context |
| 769 | + * @param $arg |
| 770 | + * @return bool |
| 771 | + */ |
658 | 772 | protected function needsBrackets( $context, $arg ) { |
659 | 773 | if ( isset( $this->binaryPrecedence[ $context->op ] ) |
660 | 774 | && isset( $this->binaryPrecedence[ $arg->op ] ) ) |
— | — | @@ -720,6 +834,10 @@ |
721 | 835 | } |
722 | 836 | } |
723 | 837 | |
| 838 | + /** |
| 839 | + * @param $name |
| 840 | + * @return LuaFoo_Converter_Block |
| 841 | + */ |
724 | 842 | protected function getRuntimeUnimplementedBlock( $name ) { |
725 | 843 | $block = $this->newBlock(); |
726 | 844 | $block->addItems( array( |
— | — | @@ -732,6 +850,10 @@ |
733 | 851 | return $block; |
734 | 852 | } |
735 | 853 | |
| 854 | + /** |
| 855 | + * @param $block LuaFoo_Converter_Block |
| 856 | + * @param $frame |
| 857 | + */ |
736 | 858 | protected function addLocalDeclarations( $block, $frame ) { |
737 | 859 | foreach ( $frame->variables as $var => $dummy ) { |
738 | 860 | $block->prependItem( "local $var" ); |
— | — | @@ -784,10 +906,16 @@ |
785 | 907 | return $out; |
786 | 908 | } |
787 | 909 | |
| 910 | + /** |
| 911 | + * @return LuaFoo_Converter_Indent |
| 912 | + */ |
788 | 913 | function indent() { |
789 | 914 | return new LuaFoo_Converter_Indent; |
790 | 915 | } |
791 | 916 | |
| 917 | + /** |
| 918 | + * @return LuaFoo_Converter_Unindent |
| 919 | + */ |
792 | 920 | function unindent() { |
793 | 921 | return new LuaFoo_Converter_Unindent; |
794 | 922 | } |
— | — | @@ -822,7 +950,7 @@ |
823 | 951 | } |
824 | 952 | |
825 | 953 | class LuaFoo_Converter_Frame { |
826 | | - static $consonants = 'bdfghjklmnprstvwz'; |
| 954 | + static $consonants = 'bcdfghjklmnpqrstvwxyz'; |
827 | 955 | static $vowels = 'aeiou'; |
828 | 956 | |
829 | 957 | var $variables = array(); |
— | — | @@ -834,6 +962,9 @@ |
835 | 963 | srand( crc32( $seed ) ); |
836 | 964 | } |
837 | 965 | |
| 966 | + /** |
| 967 | + * @return string |
| 968 | + */ |
838 | 969 | public function newVariable() { |
839 | 970 | do { |
840 | 971 | $length = rand( 5, 8 ); |
— | — | @@ -852,6 +983,10 @@ |
853 | 984 | return $s; |
854 | 985 | } |
855 | 986 | |
| 987 | + /** |
| 988 | + * @param $expr |
| 989 | + * @return bool |
| 990 | + */ |
856 | 991 | public function getVariable( $expr ) { |
857 | 992 | $hash = $expr->getHash(); |
858 | 993 | if ( isset( $this->variablesByHash[$hash] ) ) { |
Index: trunk/extensions/LuaFoo/LuaFoo.i18n.php |
— | — | @@ -2,6 +2,7 @@ |
3 | 3 | |
4 | 4 | $messages = array(); |
5 | 5 | $messages['en'] = array( |
| 6 | + 'luafoo-desc' => 'Translates templates to Lua', |
6 | 7 | 'luafoo-luatranslation' => 'Translate template to Lua', |
7 | 8 | 'luafoo-convert-title' => 'Template to convert to Lua:', |
8 | 9 | 'luafoo-convert-submit' => 'Convert', |
Index: trunk/extensions/LuaFoo/LuaFoo.php |
— | — | @@ -4,7 +4,16 @@ |
5 | 5 | exit( 1 ); |
6 | 6 | } |
7 | 7 | |
| 8 | +$wgExtensionCredits['specialpage'][] = array( |
| 9 | + 'path' => __FILE__, |
| 10 | + 'name' => 'LuaFoo', |
| 11 | + 'url' => 'https://www.mediawiki.org/wiki/Extension:LuaFoo', |
| 12 | + 'author' => 'Tim Starling', |
| 13 | + 'descriptionmsg' => 'luafoo-desc', |
| 14 | +); |
| 15 | + |
8 | 16 | $lfip = dirname( __FILE__ ); |
| 17 | + |
9 | 18 | $wgAutoloadClasses['LuaFoo_SpecialLuaTranslation'] = "$lfip/includes/SpecialLuaTranslation.php"; |
10 | 19 | $wgAutoloadClasses['LuaFoo_Converter'] = "$lfip/includes/Converter.php"; |
11 | 20 | |