Index: trunk/extensions/WiktionaryInflection/Inflection.php |
— | — | @@ -1,107 +0,0 @@ |
2 | | -<?php |
3 | | -$wgExtensionFunctions[] = "wfInflectionExtension"; |
4 | | - |
5 | | -function wfInflectionExtension() { |
6 | | - global $wgParser; |
7 | | - $wgParser->setHook("infl", "renderInflection"); |
8 | | -} |
9 | | - |
10 | | -class InflectionRule { |
11 | | - var $entryMatchPattern; |
12 | | - var $inflKeyPattern; |
13 | | - var $generationReplacementPattern; |
14 | | - |
15 | | - function InflectionRule ($ruleAttributes) |
16 | | - { |
17 | | - $this->generationReplacementPattern = array(); |
18 | | - foreach ($ruleAttributes as $key=>$value) { |
19 | | - switch($key) { |
20 | | - case "entry": |
21 | | - $this->entryMatchPattern = $value; break; |
22 | | - case "key": |
23 | | - $this->inflKeyPattern = $value; break; |
24 | | - default: |
25 | | - $this->generationReplacementPattern[$key] = $value; |
26 | | - } |
27 | | - } |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -function readRules($lang, $pos) |
32 | | -{ |
33 | | - $ruleSettingsTitle = Title::newFromText("infl-$lang-$pos", NS_MEDIAWIKI); |
34 | | - $revision = Revision::newFromTitle($ruleSettingsTitle); |
35 | | - if(!$revision) |
36 | | - throw new Exception("missing MediaWiki:infl-$lang-$pos"); |
37 | | - $ruleXml = $revision->getText(); |
38 | | - $parser = xml_parser_create(); |
39 | | - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
40 | | - xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); |
41 | | - xml_parse_into_struct($parser, $ruleXml, $ruleTagValues, $ruleTagIndices); |
42 | | - xml_parser_free($parser); |
43 | | - |
44 | | - for ($i=0; $i < count($ruleTagIndices["rule"]); $i+=2) { |
45 | | - $offset = $ruleTagIndices["rule"][$i] + 1; |
46 | | - $len = $ruleTagIndices["rule"][$i + 1] - $offset; |
47 | | - $rules[] = parseRuleTags(array_slice($ruleTagValues, $offset, $len)); |
48 | | - } |
49 | | - |
50 | | - return $rules; |
51 | | -} |
52 | | - |
53 | | -function parseRuleTags($ruleTagValues) |
54 | | -{ |
55 | | - for ($i=0; $i < count($ruleTagValues); $i++) { |
56 | | - $ruleAttributes[$ruleTagValues[$i]["tag"]] = $ruleTagValues[$i]["value"]; |
57 | | - } |
58 | | - return new InflectionRule($ruleAttributes); |
59 | | -} |
60 | | - |
61 | | -function renderInflection($input, $argv) { |
62 | | - global $wgParser, $wgUser; |
63 | | - |
64 | | - $key = $wgParser->replaceVariables($input, end( $wgParser->mArgStack )); |
65 | | - if(array_key_exists("lang", $argv)) |
66 | | - $language = $argv["lang"]; |
67 | | - else |
68 | | - return "?<!-- missing required \"lang\" parameter. -->"; |
69 | | - if(array_key_exists("pos", $argv)) |
70 | | - $partOfSpeech = $argv["pos"]; |
71 | | - else |
72 | | - return "?<!-- missing required \"pos\" parameter. -->"; |
73 | | - if(array_key_exists("generate", $argv)) |
74 | | - $inflectionTypeToGenerate = $argv["generate"]; |
75 | | - else |
76 | | - return "?<!-- missing required \"generate\" parameter. -->"; |
77 | | - |
78 | | - $entry = $wgParser->mTitle->getText(); |
79 | | - |
80 | | - try { |
81 | | - $rules = readRules($language, $partOfSpeech); |
82 | | - foreach($rules as $rule) { |
83 | | - if($rule->entryMatchPattern) |
84 | | - { |
85 | | - if(!$rule->inflKeyPattern || |
86 | | - ($rule->inflKeyPattern && preg_match("/" . $rule->inflKeyPattern . "/", $wgParser->replaceVariables($key, $wgParser->mArgStack)))) |
87 | | - { |
88 | | - $inflectedForm = preg_replace( |
89 | | - "/" . $rule->entryMatchPattern . "/", |
90 | | - $rule->generationReplacementPattern[$inflectionTypeToGenerate], |
91 | | - $entry, -1, $count); |
92 | | - if($count >= 1) |
93 | | - return $inflectedForm; |
94 | | - } |
95 | | - } elseif($rule->inflKeyPattern) { |
96 | | - $inflectedForm = preg_replace( |
97 | | - "/" . $rule->inflKeyPattern . "/", |
98 | | - $rule->generationReplacementPattern[$inflectionTypeToGenerate], |
99 | | - $wgParser->replaceVariables($key, $wgParser->mArgStack), -1, $count); |
100 | | - if($count >= 1) |
101 | | - return $inflectedForm; |
102 | | - } |
103 | | - } |
104 | | - } catch (Exception $e) { |
105 | | - return "?<!-- " . $e->getMessage() . " -->"; |
106 | | - } |
107 | | -} |
108 | | - |
Index: trunk/extensions/WiktionaryInflection/WiktionaryInflection.php |
— | — | @@ -0,0 +1,107 @@ |
| 2 | +<?php |
| 3 | +$wgExtensionFunctions[] = "wfInflectionExtension"; |
| 4 | + |
| 5 | +function wfInflectionExtension() { |
| 6 | + global $wgParser; |
| 7 | + $wgParser->setHook("infl", "renderInflection"); |
| 8 | +} |
| 9 | + |
| 10 | +class InflectionRule { |
| 11 | + var $entryMatchPattern; |
| 12 | + var $inflKeyPattern; |
| 13 | + var $generationReplacementPattern; |
| 14 | + |
| 15 | + function InflectionRule ($ruleAttributes) |
| 16 | + { |
| 17 | + $this->generationReplacementPattern = array(); |
| 18 | + foreach ($ruleAttributes as $key=>$value) { |
| 19 | + switch($key) { |
| 20 | + case "entry": |
| 21 | + $this->entryMatchPattern = $value; break; |
| 22 | + case "key": |
| 23 | + $this->inflKeyPattern = $value; break; |
| 24 | + default: |
| 25 | + $this->generationReplacementPattern[$key] = $value; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function readRules($lang, $pos) |
| 32 | +{ |
| 33 | + $ruleSettingsTitle = Title::newFromText("infl-$lang-$pos", NS_MEDIAWIKI); |
| 34 | + $revision = Revision::newFromTitle($ruleSettingsTitle); |
| 35 | + if(!$revision) |
| 36 | + throw new Exception("missing MediaWiki:infl-$lang-$pos"); |
| 37 | + $ruleXml = $revision->getText(); |
| 38 | + $parser = xml_parser_create(); |
| 39 | + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
| 40 | + xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); |
| 41 | + xml_parse_into_struct($parser, $ruleXml, $ruleTagValues, $ruleTagIndices); |
| 42 | + xml_parser_free($parser); |
| 43 | + |
| 44 | + for ($i=0; $i < count($ruleTagIndices["rule"]); $i+=2) { |
| 45 | + $offset = $ruleTagIndices["rule"][$i] + 1; |
| 46 | + $len = $ruleTagIndices["rule"][$i + 1] - $offset; |
| 47 | + $rules[] = parseRuleTags(array_slice($ruleTagValues, $offset, $len)); |
| 48 | + } |
| 49 | + |
| 50 | + return $rules; |
| 51 | +} |
| 52 | + |
| 53 | +function parseRuleTags($ruleTagValues) |
| 54 | +{ |
| 55 | + for ($i=0; $i < count($ruleTagValues); $i++) { |
| 56 | + $ruleAttributes[$ruleTagValues[$i]["tag"]] = $ruleTagValues[$i]["value"]; |
| 57 | + } |
| 58 | + return new InflectionRule($ruleAttributes); |
| 59 | +} |
| 60 | + |
| 61 | +function renderInflection($input, $argv) { |
| 62 | + global $wgParser, $wgUser; |
| 63 | + |
| 64 | + $key = $wgParser->replaceVariables($input, end( $wgParser->mArgStack )); |
| 65 | + if(array_key_exists("lang", $argv)) |
| 66 | + $language = $argv["lang"]; |
| 67 | + else |
| 68 | + return "?<!-- missing required \"lang\" parameter. -->"; |
| 69 | + if(array_key_exists("pos", $argv)) |
| 70 | + $partOfSpeech = $argv["pos"]; |
| 71 | + else |
| 72 | + return "?<!-- missing required \"pos\" parameter. -->"; |
| 73 | + if(array_key_exists("generate", $argv)) |
| 74 | + $inflectionTypeToGenerate = $argv["generate"]; |
| 75 | + else |
| 76 | + return "?<!-- missing required \"generate\" parameter. -->"; |
| 77 | + |
| 78 | + $entry = $wgParser->mTitle->getText(); |
| 79 | + |
| 80 | + try { |
| 81 | + $rules = readRules($language, $partOfSpeech); |
| 82 | + foreach($rules as $rule) { |
| 83 | + if($rule->entryMatchPattern) |
| 84 | + { |
| 85 | + if(!$rule->inflKeyPattern || |
| 86 | + ($rule->inflKeyPattern && preg_match("/" . $rule->inflKeyPattern . "/", $wgParser->replaceVariables($key, $wgParser->mArgStack)))) |
| 87 | + { |
| 88 | + $inflectedForm = preg_replace( |
| 89 | + "/" . $rule->entryMatchPattern . "/", |
| 90 | + $rule->generationReplacementPattern[$inflectionTypeToGenerate], |
| 91 | + $entry, -1, $count); |
| 92 | + if($count >= 1) |
| 93 | + return $inflectedForm; |
| 94 | + } |
| 95 | + } elseif($rule->inflKeyPattern) { |
| 96 | + $inflectedForm = preg_replace( |
| 97 | + "/" . $rule->inflKeyPattern . "/", |
| 98 | + $rule->generationReplacementPattern[$inflectionTypeToGenerate], |
| 99 | + $wgParser->replaceVariables($key, $wgParser->mArgStack), -1, $count); |
| 100 | + if($count >= 1) |
| 101 | + return $inflectedForm; |
| 102 | + } |
| 103 | + } |
| 104 | + } catch (Exception $e) { |
| 105 | + return "?<!-- " . $e->getMessage() . " -->"; |
| 106 | + } |
| 107 | +} |
| 108 | + |
Property changes on: trunk/extensions/WiktionaryInflection/WiktionaryInflection.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 109 | + native |