Index: trunk/extensions/TagContent/TagContent.php |
— | — | @@ -30,66 +30,71 @@ |
31 | 31 | # Extension initialization |
32 | 32 | #---------------------------------------------------------------------------- |
33 | 33 | |
34 | | -$wgTagContent = array(); |
35 | | -$TagContentVersion = '0.1'; |
| 34 | +$TagContentVersion = '0.2'; |
36 | 35 | $wgExtensionCredits['parserhook'][] = array( |
37 | 36 | 'name'=>'TagContent', |
38 | 37 | 'version'=>$TagContentVersion, |
39 | 38 | 'author'=>'John Erling Blad', |
40 | 39 | 'url'=>'http://www.mediawiki.org/wiki/Extension:TagContent', |
41 | 40 | 'description' => 'Translate from tags to parser functions' |
42 | | - ); |
| 41 | +); |
43 | 42 | |
44 | | -$dir = dirname(__FILE__) . '/'; |
45 | | -$wgExtensionFunctions[] = 'wfTagContentSetup'; |
46 | | -$wgExtensionMessagesFiles['TagContent'] = $dir . 'TagContent.i18n.php'; |
| 43 | +$wgExtensionFunctions[] = 'efTagContentSetup'; |
| 44 | +$wgExtensionMessagesFiles['TagContent'] = dirname(__FILE__) . '/TagContent.i18n.php'; |
47 | 45 | |
48 | | -$wgTagContentDefine = array( |
| 46 | +$egTagContentDefine = array( |
49 | 47 | ); |
50 | 48 | |
51 | | -$wgTagContentBlacklist = array( |
| 49 | +$egTagContentBlacklist = array( |
52 | 50 | |
53 | 51 | // mediawiki |
54 | | - 'noinclude' => true, 'includeonly' => true, 'onlyinclude' => true, 'gallery' => true, |
| 52 | + 'noinclude' => true, 'includeonly' => true, 'onlyinclude' => true, 'gallery' => true, |
55 | 53 | |
56 | 54 | // html tags |
57 | | - 'address' => true, 'applet' => true, 'area' => true, 'a' => true, |
58 | | - 'base' => true, 'basefont' => true, 'big' => true, 'blockquote' => true, |
59 | | - 'body' => true, 'br' => true, 'b' => true, 'caption' => true, |
60 | | - 'center' => true, 'cite' => true, 'code' => true, 'dd' => true, |
61 | | - 'dfn' => true, 'dir' => true, 'div' => true, 'dl' => true, |
62 | | - 'dt' => true, 'em' => true, 'font' => true, 'form' => true, |
63 | | - 'h1' => true, 'h2' => true, 'h3' => true, 'h4' => true, |
64 | | - 'h5' => true, 'h6' => true, 'head' => true, 'hr' => true, |
65 | | - 'html' => true, 'img' => true, 'input' => true, 'isindex' => true, |
66 | | - 'i' => true, 'kbd' => true, 'link' => true, 'li' => true, |
67 | | - 'map' => true, 'menu' => true, 'meta' => true, 'ol' => true, |
68 | | - 'option' => true, 'param' => true, 'pre' => true, 'p' => true, |
69 | | - 'samp' => true, 'script' => true, 'select' => true, 'small' => true, |
70 | | - 'strike' => true, 'strong' => true, 'style' => true, 'sub' => true, |
71 | | - 'sup' => true, 'table' => true, 'td' => true, 'textarea' => true, |
72 | | - 'th' => true, 'title' => true, 'tr' => true, 'tt' => true, |
73 | | - 'ul' => true, 'u' => true, 'var' => true, |
| 55 | + 'address' => true, 'applet' => true, 'area' => true, 'a' => true, |
| 56 | + 'base' => true, 'basefont' => true, 'big' => true, 'blockquote' => true, |
| 57 | + 'body' => true, 'br' => true, 'b' => true, 'caption' => true, |
| 58 | + 'center' => true, 'cite' => true, 'code' => true, 'dd' => true, |
| 59 | + 'dfn' => true, 'dir' => true, 'div' => true, 'dl' => true, |
| 60 | + 'dt' => true, 'em' => true, 'font' => true, 'form' => true, |
| 61 | + 'h1' => true, 'h2' => true, 'h3' => true, 'h4' => true, |
| 62 | + 'h5' => true, 'h6' => true, 'head' => true, 'hr' => true, |
| 63 | + 'html' => true, 'img' => true, 'input' => true, 'isindex' => true, |
| 64 | + 'i' => true, 'kbd' => true, 'link' => true, 'li' => true, |
| 65 | + 'map' => true, 'menu' => true, 'meta' => true, 'ol' => true, |
| 66 | + 'option' => true, 'param' => true, 'pre' => true, 'p' => true, |
| 67 | + 'samp' => true, 'script' => true, 'select' => true, 'small' => true, |
| 68 | + 'strike' => true, 'strong' => true, 'style' => true, 'sub' => true, |
| 69 | + 'sup' => true, 'table' => true, 'td' => true, 'textarea' => true, |
| 70 | + 'th' => true, 'title' => true, 'tr' => true, 'tt' => true, |
| 71 | + 'ul' => true, 'u' => true, 'var' => true, |
74 | 72 | |
75 | 73 | ); |
76 | 74 | |
77 | 75 | class TagContent { |
78 | | - private $mParms = null; # hash |
79 | | - private $mTemplate = null; # string |
80 | | - private static $mDefinitions = null; # Title |
81 | | - private $mTag = null; # string |
82 | | - private $mChangeable = false; # bolean |
83 | | - private $mTitle = null; # Title |
| 76 | + private $mParms = null; # hash |
| 77 | + private $mTemplate = null; # string |
| 78 | + private static $mDefinitions = null; # Title |
| 79 | + private $mTag = null; # string |
| 80 | + private $mChangeable = false; # boolean |
| 81 | + private $mTitle = null; # Title |
84 | 82 | |
85 | | - // contructor |
| 83 | + /** |
| 84 | + * Contructor for the TagContent class |
| 85 | + * @param $tag Name of the new tag, that is the |
| 86 | + * @param $template Optional name of the template to use during rendering |
| 87 | + * @param $params Optional default parameters |
| 88 | + * @param $changable |
| 89 | + */ |
86 | 90 | public function TagContent( $tag, $template=null, &$params=null, $changeable=false ) { |
87 | 91 | if ($params) { |
88 | 92 | $h = array(); |
89 | 93 | $p = explode('|', htmlspecialchars($params)); |
90 | 94 | $n = 1; |
91 | 95 | foreach ($p as $item) { |
92 | | - if (false === strrpos($item, '=')) |
| 96 | + if (false === strrpos($item, '=')) { |
93 | 97 | $item = $n++ . '=' . $item; |
| 98 | + } |
94 | 99 | list($k, $v) = explode('=', $item, 2); |
95 | 100 | $h[trim($k)] = $v; |
96 | 101 | } |
— | — | @@ -100,17 +105,27 @@ |
101 | 106 | $this->mChangeable = $changeable; |
102 | 107 | } |
103 | 108 | |
104 | | - // accessor |
| 109 | + /** |
| 110 | + * Accessor function for changeable |
| 111 | + * @param $val If set the new value for the changable attribute |
| 112 | + * @return The existing or new value if it is set |
| 113 | + */ |
105 | 114 | public function changeable ( $val ) { |
106 | | - if (isset($val)) |
| 115 | + if (isset($val)) { |
107 | 116 | $this->mChangeable = $val; |
| 117 | + } |
108 | 118 | return $this->mChangeable; |
109 | 119 | } |
110 | 120 | |
111 | | - // our own reimplementation of addLink |
| 121 | + /** |
| 122 | + * Local reimplementation of addLink |
| 123 | + * @param $parser Reference to the parser instance |
| 124 | + * @param $title Title of the additional link |
| 125 | + */ |
112 | 126 | private function addLink( &$parser, &$title ) { |
113 | | - if (!$title) |
| 127 | + if (!$title) { |
114 | 128 | throw new Exception( 'none' ); |
| 129 | + } |
115 | 130 | $lc = LinkCache::singleton(); |
116 | 131 | $pdbk = $title->getPrefixedDBkey(); |
117 | 132 | if ( 0 != ( $id = $lc->getGoodLinkID( $pdbk ) ) ) { |
— | — | @@ -123,16 +138,24 @@ |
124 | 139 | else { |
125 | 140 | $id = $title->getArticleID(); |
126 | 141 | $parser->mOutput->addLink( $title, $id ); |
127 | | - if (!$id) |
| 142 | + if (!$id) { |
128 | 143 | throw new Exception( $title->getPrefixedText() ); |
| 144 | + } |
129 | 145 | } |
130 | 146 | } |
131 | 147 | |
132 | | - // callback function for inserting our own rendering |
| 148 | + /** |
| 149 | + * Callback function for inserting our own rendering |
| 150 | + * @param $text Content for the tag call |
| 151 | + * @param $params Parameters for the tag call |
| 152 | + * @param $parser Reference to the parser instance |
| 153 | + * @return Replaced content wrapped in a recursive call |
| 154 | + */ |
133 | 155 | public function onRender ( $text, $params, &$parser ) { |
134 | 156 | if ($this->mChangeable) { |
135 | | - if (!$this->mDefinitions) |
| 157 | + if (!$this->mDefinitions) { |
136 | 158 | $this->mDefinitions = Title::newFromText("Mediawiki:tags-definition"); |
| 159 | + } |
137 | 160 | try { |
138 | 161 | $this->addLink($parser, $this->mDefinitions); |
139 | 162 | } |
— | — | @@ -140,8 +163,9 @@ |
141 | 164 | return $parser->recursiveTagParse( wfMsg( 'tags-definitions-unknown', $e->getMessage() ) ); |
142 | 165 | } |
143 | 166 | } |
144 | | - if (!$this->mTitle) |
| 167 | + if (!$this->mTitle) { |
145 | 168 | $this->mTitle = Title::newFromText($this->mTemplate); |
| 169 | + } |
146 | 170 | try { |
147 | 171 | $this->addLink($parser, $this->mTitle); |
148 | 172 | } |
— | — | @@ -150,25 +174,31 @@ |
151 | 175 | } |
152 | 176 | $cont = array($this->mTemplate, $text); |
153 | 177 | foreach ($this->mParms as $k => $v) { |
154 | | - if (isset($params[$k])) |
| 178 | + if (isset($params[$k])) { |
155 | 179 | $cont[] = "$k=$params[$k]"; |
156 | | - else |
| 180 | + } |
| 181 | + else { |
157 | 182 | $cont[] = "$k=$v"; |
| 183 | + } |
158 | 184 | } |
159 | 185 | foreach ($params as $k => $v) { |
160 | | - if (!isset($this->mParms[$k])) |
| 186 | + if (!isset($this->mParms[$k])) { |
161 | 187 | $cont[] = "$k=$v"; |
| 188 | + } |
162 | 189 | } |
163 | 190 | $output = '{{' . implode('|', $cont) . '}}'; |
164 | 191 | return $parser->recursiveTagParse($output); |
165 | 192 | } |
166 | 193 | } |
167 | 194 | |
168 | | -// build necessary structures |
169 | | -function wfTagContentSetup () { |
170 | | - global $wgParser, $wgTagContentBlacklist, $wgTagContentDefine; |
| 195 | +/** |
| 196 | +* Setup function for the extension |
| 197 | +@return True is returned unconditionally |
| 198 | +*/ |
| 199 | +function efTagContentSetup () { |
| 200 | + global $wgParser, $egTagContentBlacklist, $egTagContentDefine; |
171 | 201 | wfLoadExtensionMessages('TagContent'); |
172 | | - foreach ($wgTagContentDefine as $k => $a) { |
| 202 | + foreach ($egTagContentDefine as $k => $a) { |
173 | 203 | $template = $a[0]; |
174 | 204 | $tag = strtolower($k); |
175 | 205 | $c = new TagContent($tag, $template, $a[1], false); |
— | — | @@ -181,7 +211,7 @@ |
182 | 212 | $a = explode('|', $line, 3); |
183 | 213 | $template = trim($a[1]); |
184 | 214 | $tag = strtolower(trim($a[0])); |
185 | | - if ( !$wgTagContentBlacklist[$tag] && !isset($wgTagContentDefine[$tag])) { |
| 215 | + if ( !$egTagContentBlacklist[$tag] && !isset($egTagContentDefine[$tag])) { |
186 | 216 | $c = new TagContent($tag, $template, $a[2], true); |
187 | 217 | $wgParser->setHook( $tag, array( $c, 'onRender' )); |
188 | 218 | } |