Index: trunk/extensions/SpecialForm/SpecialForm.body.php |
— | — | @@ -17,150 +17,157 @@ |
18 | 18 | * along with this program; if not, write to the Free Software |
19 | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | 20 | * |
| 21 | + * @file |
| 22 | + * @ingroup Extensions |
21 | 23 | * @author Evan Prodromou <evan@vinismo.com> |
22 | | - * @ingroup Extensions |
23 | 24 | */ |
24 | 25 | |
25 | | -if (!defined('MEDIAWIKI')) { |
| 26 | +if( !defined( 'MEDIAWIKI' ) ) { |
26 | 27 | exit( 1 ); |
27 | 28 | } |
28 | 29 | |
29 | 30 | require_once('XmlFunctions.php'); |
30 | 31 | |
31 | 32 | class SpecialForm extends SpecialPage { |
32 | | - function SpecialForm() { |
33 | | - SpecialPage::SpecialPage("Form"); |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructor |
| 36 | + */ |
| 37 | + public function __construct() { |
| 38 | + parent::__construct( 'Form' ); |
34 | 39 | } |
35 | 40 | |
36 | | - function execute( $par ) { |
| 41 | + /** |
| 42 | + * Show the special page |
| 43 | + * |
| 44 | + * @param $par Mixed: parameter passed to the page or null |
| 45 | + */ |
| 46 | + public function execute( $par ) { |
37 | 47 | global $wgRequest, $wgOut; |
38 | 48 | |
39 | 49 | wfLoadExtensionMessages( 'Form' ); |
40 | 50 | |
41 | 51 | # Must have a name, like Special:Form/Nameofform |
42 | 52 | # XXX: instead of an error, show a list of available forms |
43 | | - |
44 | | - if (!$par) { |
45 | | - $wgOut->showErrorPage('formnoname', 'formnonametext'); |
| 53 | + if( !$par ) { |
| 54 | + $wgOut->showErrorPage( 'formnoname', 'formnonametext' ); |
46 | 55 | return; |
47 | 56 | } |
48 | 57 | |
49 | | - $form = $this->loadForm($par); |
| 58 | + $form = $this->loadForm( $par ); |
50 | 59 | |
51 | 60 | # Bad form |
52 | | - |
53 | | - if (!$form) { |
54 | | - $wgOut->showErrorPage('formbadname', 'formbadnametext'); |
| 61 | + if( !$form ) { |
| 62 | + $wgOut->showErrorPage( 'formbadname', 'formbadnametext' ); |
55 | 63 | return; |
56 | 64 | } |
57 | 65 | |
58 | | - if ($wgRequest->wasPosted()) { |
| 66 | + if( $wgRequest->wasPosted() ) { |
59 | 67 | # POST is to create an article |
60 | | - $this->createArticle($form); |
| 68 | + $this->createArticle( $form ); |
61 | 69 | } else { |
62 | 70 | # GET (HEAD?) is to show the form |
63 | | - $this->showForm($form); |
| 71 | + $this->showForm( $form ); |
64 | 72 | } |
65 | 73 | } |
66 | 74 | |
67 | 75 | # Load and parse a form article from the DB |
| 76 | + function loadForm( $name ) { |
| 77 | + $nt = Title::makeTitleSafe( NS_MEDIAWIKI, wfMsgForContent( 'formpattern', $name ) ); |
68 | 78 | |
69 | | - function loadForm($name) { |
70 | | - $nt = Title::makeTitleSafe(NS_MEDIAWIKI, wfMsgForContent('formpattern', $name)); |
71 | | - |
72 | 79 | # article exists? |
73 | | - |
74 | | - if (!$nt || $nt->getArticleID() == 0) { |
| 80 | + if( !$nt || $nt->getArticleID() == 0 ) { |
75 | 81 | return NULL; |
76 | 82 | } |
77 | 83 | |
78 | | - $article = new Article($nt); |
| 84 | + $article = new Article( $nt ); |
79 | 85 | |
80 | | - assert($article); |
| 86 | + assert( $article ); |
81 | 87 | |
82 | | - $text = $article->getContent(true); |
| 88 | + $text = $article->getContent( true ); |
83 | 89 | |
84 | 90 | # Form constructor does the parsing |
85 | | - |
86 | | - return new Form($name, $text); |
| 91 | + return new Form( $name, $text ); |
87 | 92 | } |
88 | 93 | |
89 | | - function showForm($form, $errmsg = NULL) { |
| 94 | + function showForm( $form, $errmsg = NULL ) { |
90 | 95 | global $wgOut, $wgRequest, $wgParser, $wgUser, $wgSpecialFormRecaptcha; |
91 | 96 | |
92 | | - $self = SpecialPage::getTitleFor(wfMsgForContent('form') . '/' . $form->name); |
| 97 | + $self = SpecialPage::getTitleFor( wfMsgForContent( 'form' ) . '/' . $form->name ); |
93 | 98 | |
94 | | - $wgOut->setPageTitle($form->title); |
| 99 | + $wgOut->setPageTitle( $form->title ); |
95 | 100 | |
96 | | - if (!is_null($form->instructions)) { |
| 101 | + if( !is_null( $form->instructions ) ) { |
97 | 102 | |
98 | | - $wgOut->addHTML(Xml::openElement('div', array('class' => 'instructions')) . |
99 | | - $wgOut->parse($form->instructions) . |
100 | | - Xml::closeElement('div') . |
101 | | - Xml::element('br')); |
| 103 | + $wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'instructions' ) ) . |
| 104 | + $wgOut->parse( $form->instructions ) . |
| 105 | + Xml::closeElement( 'div' ) . |
| 106 | + Xml::element( 'br' ) ); |
102 | 107 | } |
103 | 108 | |
104 | | - if (!is_null($errmsg)) { |
105 | | - $wgOut->addHTML(Xml::openElement('div', array('class' => 'error')) . |
106 | | - $wgOut->parse($errmsg) . |
107 | | - Xml::closeElement('div') . |
108 | | - Xml::element('br')); |
| 109 | + if( !is_null( $errmsg ) ) { |
| 110 | + $wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'error' ) ) . |
| 111 | + $wgOut->parse( $errmsg ) . |
| 112 | + Xml::closeElement( 'div' ) . |
| 113 | + Xml::element( 'br' ) ); |
109 | 114 | } |
110 | 115 | |
111 | | - $wgOut->addHTML(Xml::openElement('form', |
112 | | - array('method' => 'POST', |
113 | | - 'action' => $self->getLocalURL()))); |
| 116 | + $wgOut->addHTML( |
| 117 | + Xml::openElement( 'form', array( |
| 118 | + 'method' => 'post', |
| 119 | + 'action' => $self->getLocalURL() |
| 120 | + ) |
| 121 | + ) |
| 122 | + ); |
114 | 123 | |
115 | | - foreach ($form->fields as $field) { |
116 | | - $wgOut->addHTML($field->render($wgRequest->getText($field->name)) . Xml::element('br') . "\n"); |
| 124 | + foreach( $form->fields as $field ) { |
| 125 | + $wgOut->addHTML( $field->render( $wgRequest->getText( $field->name ) ) . Xml::element( 'br' ) . "\n" ); |
117 | 126 | } |
118 | 127 | |
119 | | - if ($wgUser->getId() == 0 && $wgSpecialFormRecaptcha) { # Anonymous user, use recaptcha |
120 | | - require_once('recaptchalib.php'); |
| 128 | + # Anonymous user, use recaptcha |
| 129 | + if( $wgUser->getId() == 0 && $wgSpecialFormRecaptcha ) { |
| 130 | + require_once( 'recaptchalib.php' ); |
121 | 131 | global $recaptcha_public_key; # same as used by Recaptcha plugin |
122 | | - $wgOut->addHTML(recaptcha_get_html($recaptcha_public_key)); |
| 132 | + $wgOut->addHTML( recaptcha_get_html( $recaptcha_public_key ) ); |
123 | 133 | } |
124 | 134 | |
125 | | - $wgOut->addHTML(Xml::element('input', array('type' => 'submit', |
126 | | - 'value' => wfMsg('formsave')))); |
| 135 | + $wgOut->addHTML( Xml::element( 'input', array( 'type' => 'submit', |
| 136 | + 'value' => wfMsg( 'formsave' ) ) ) ); |
127 | 137 | |
128 | | - $wgOut->addHTML(Xml::closeElement('form')); |
| 138 | + $wgOut->addHTML( Xml::closeElement( 'form' ) ); |
129 | 139 | } |
130 | 140 | |
131 | | - function createArticle($form) { |
132 | | - |
| 141 | + function createArticle( $form ) { |
133 | 142 | global $wgOut, $wgRequest, $wgLang, $wgUser, $wgSpecialFormRecaptcha; |
134 | 143 | |
135 | 144 | # Check recaptcha |
136 | | - |
137 | | - if ($wgUser->getId() == 0 && $wgSpecialFormRecaptcha) { |
138 | | - |
139 | | - require_once('recaptchalib.php'); |
| 145 | + if( $wgUser->getId() == 0 && $wgSpecialFormRecaptcha ) { |
| 146 | + require_once( 'recaptchalib.php' ); |
140 | 147 | global $recaptcha_private_key; # same as used by Recaptcha plugin |
141 | | - $resp = recaptcha_check_answer($recaptcha_private_key, |
142 | | - $_SERVER["REMOTE_ADDR"], |
143 | | - $wgRequest->getText("recaptcha_challenge_field"), |
144 | | - $wgRequest->getText("recaptcha_response_field")); |
| 148 | + $resp = recaptcha_check_answer( |
| 149 | + $recaptcha_private_key, |
| 150 | + $_SERVER['REMOTE_ADDR'], |
| 151 | + $wgRequest->getText( 'recaptcha_challenge_field' ), |
| 152 | + $wgRequest->getText( 'recaptcha_response_field' ) |
| 153 | + ); |
145 | 154 | |
146 | | - if (!$resp->is_valid) { |
147 | | - $this->showForm($form, wfMsg('formbadrecaptcha')); |
| 155 | + if( !$resp->is_valid ) { |
| 156 | + $this->showForm( $form, wfMsg( 'formbadrecaptcha' ) ); |
148 | 157 | return; |
149 | 158 | } |
150 | 159 | } |
151 | 160 | |
152 | 161 | # Check for required fields |
153 | | - |
154 | 162 | $missedFields = array(); |
155 | 163 | |
156 | | - foreach ($form->fields as $name => $field) { |
157 | | - $value = $wgRequest->getText($name); |
158 | | - if ($field->isOptionTrue('required') && (is_null($value) || strlen($value) == 0)) { |
| 164 | + foreach( $form->fields as $name => $field ) { |
| 165 | + $value = $wgRequest->getText( $name ); |
| 166 | + if( $field->isOptionTrue( 'required' ) && ( is_null( $value ) || strlen( $value ) == 0 ) ) { |
159 | 167 | $missedFields[] = $field->label; |
160 | 168 | } |
161 | 169 | } |
162 | 170 | |
163 | 171 | # On error, show the form again with some error text. |
164 | | - |
165 | 172 | $missedFieldsCount = count( $missedFields ); |
166 | 173 | if ( $missedFieldsCount > 0 ) { |
167 | 174 | $msg = wfMsgExt( 'formrequiredfielderror', 'parsemag', $wgLang->listToText( $missedFields ), $missedFieldsCount ); |
— | — | @@ -169,126 +176,121 @@ |
170 | 177 | } |
171 | 178 | |
172 | 179 | # First, we make sure we have all the titles |
173 | | - |
174 | 180 | $nt = array(); |
175 | 181 | |
176 | | - for ($i = 0; $i < count($form->template); $i++) { |
| 182 | + for( $i = 0; $i < count( $form->template ); $i++ ) { |
177 | 183 | |
178 | 184 | $namePattern = $form->namePattern[$i]; |
179 | 185 | $template = $form->template[$i]; |
180 | 186 | |
181 | | - if (!$namePattern || !$template) { |
182 | | - $wgOut->showErrorPage('formindexmismatch-title', 'formindexmismatch', array($i)); |
| 187 | + if( !$namePattern || !$template ) { |
| 188 | + $wgOut->showErrorPage( 'formindexmismatch-title', 'formindexmismatch', array( $i ) ); |
183 | 189 | return; |
184 | 190 | } |
185 | 191 | |
186 | | - wfDebug("SpecialForm: for index '$i', namePattern = '$namePattern' and template = '$template'.\n"); |
| 192 | + wfDebug( __METHOD__ . ": for index '$i', namePattern = '$namePattern' and template = '$template'.\n" ); |
187 | 193 | |
188 | | - $title = $this->makeTitle($form, $namePattern); |
| 194 | + $title = $this->makeTitle( $form, $namePattern ); |
189 | 195 | |
190 | | - $nt[$i] = Title::newFromText($title); |
| 196 | + $nt[$i] = Title::newFromText( $title ); |
191 | 197 | |
192 | | - if (!$nt[$i]) { |
193 | | - $wgOut->showErrorPage('formbadpagename', 'formbadpagenametext', array($title)); |
| 198 | + if( !$nt[$i] ) { |
| 199 | + $wgOut->showErrorPage( 'formbadpagename', 'formbadpagenametext', array( $title ) ); |
194 | 200 | return; |
195 | 201 | } |
196 | 202 | |
197 | | - if ($nt[$i]->getArticleID() != 0) { |
198 | | - $wgOut->showErrorPage('formarticleexists', 'formarticleexists', array($title)); |
| 203 | + if( $nt[$i]->getArticleID() != 0 ) { |
| 204 | + $wgOut->showErrorPage( 'formarticleexists', 'formarticleexists', array( $title ) ); |
199 | 205 | return; |
200 | 206 | } |
201 | 207 | } |
202 | 208 | |
203 | 209 | # At this point, all $nt titles should be valid, although we're subject to race conditions. |
| 210 | + for( $i = 0; $i < count( $form->template ); $i++ ) { |
204 | 211 | |
205 | | - for ($i = 0; $i < count($form->template); $i++) { |
206 | | - |
207 | 212 | $template = $form->template[$i]; |
208 | 213 | |
209 | 214 | $text = "{{subst:$template"; |
210 | 215 | |
211 | | - foreach ($form->fields as $name => $field) { |
| 216 | + foreach( $form->fields as $name => $field ) { |
212 | 217 | # FIXME: strip/escape template-related chars (|, =, }}) |
213 | | - $text .= "|$name=" . $wgRequest->getText($name); |
| 218 | + $text .= "|$name=" . $wgRequest->getText( $name ); |
214 | 219 | } |
215 | 220 | |
216 | | - $text .= "}}"; |
| 221 | + $text .= '}}'; |
217 | 222 | |
218 | | - if (!$this->checkSave($nt[$i], $text)) { |
| 223 | + if( !$this->checkSave( $nt[$i], $text ) ) { |
219 | 224 | # Just break here; output already sent |
220 | 225 | return; |
221 | 226 | } |
222 | 227 | |
223 | 228 | $title = $nt[$i]->GetPrefixedText(); |
224 | 229 | |
225 | | - wfDebug("SpecialForm: saving article with index '$i' and title '$title'\n"); |
| 230 | + wfDebug( __METHOD__ . ": saving article with index '$i' and title '$title'\n" ); |
226 | 231 | |
227 | | - $article = new Article($nt[$i]); |
| 232 | + $article = new Article( $nt[$i] ); |
228 | 233 | |
229 | | - $status = $article->doEdit($text, wfMsg('formsavesummary', $form->name), EDIT_NEW); |
| 234 | + $status = $article->doEdit( $text, wfMsg( 'formsavesummary', $form->name ), EDIT_NEW ); |
230 | 235 | if ( $status === false || ( is_object( $status ) && !$status->isOK() ) ) { |
231 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext', array($title)); |
| 236 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext', array( $title ) ); |
232 | 237 | return; # Don't continue |
233 | 238 | } |
234 | 239 | } |
235 | 240 | |
236 | 241 | # Redirect to the first article |
237 | | - |
238 | | - if ($nt && $nt[0]) { |
239 | | - $wgOut->redirect($nt[0]->getFullURL()); |
| 242 | + if( $nt && $nt[0] ) { |
| 243 | + $wgOut->redirect( $nt[0]->getFullURL() ); |
240 | 244 | } |
241 | 245 | } |
242 | 246 | |
243 | | - function makeTitle($form, $pattern) { |
| 247 | + function makeTitle( $form, $pattern ) { |
244 | 248 | global $wgRequest; |
245 | 249 | |
246 | 250 | $title = $pattern; |
247 | 251 | |
248 | | - foreach ($form->fields as $name => $field) { |
249 | | - $title = preg_replace("/{{\{$name\}}}/", $wgRequest->getText($name), $title); |
| 252 | + foreach( $form->fields as $name => $field ) { |
| 253 | + $title = preg_replace( "/{{\{$name\}}}/", $wgRequest->getText( $name ), $title ); |
250 | 254 | } |
251 | 255 | |
252 | 256 | return $title; |
253 | 257 | } |
254 | 258 | |
255 | 259 | # Had to crib some checks from EditPage.php, since they're not done in Article.php |
256 | | - |
257 | | - function checkSave($nt, $text) { |
| 260 | + function checkSave( $nt, $text ) { |
258 | 261 | global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgMaxArticleSize, $wgOut; |
259 | 262 | |
260 | 263 | $matches = array(); |
261 | | - $errortext = ""; |
| 264 | + $errortext = ''; |
262 | 265 | |
263 | | - $editPage = new FakeEditPage($nt); |
| 266 | + $editPage = new FakeEditPage( $nt ); |
264 | 267 | |
265 | 268 | # FIXME: more specific errors, copied from EditPage.php |
266 | | - |
267 | | - if ($wgSpamRegex && preg_match($wgSpamRegex, $text, $matches)) { |
268 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 269 | + if( $wgSpamRegex && preg_match( $wgSpamRegex, $text, $matches ) ) { |
| 270 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
269 | 271 | return false; |
270 | | - } else if ($wgFilterCallback && $wgFilterCallback($nt, $text, 0)) { |
271 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 272 | + } else if( $wgFilterCallback && $wgFilterCallback( $nt, $text, 0 ) ) { |
| 273 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
272 | 274 | return false; |
273 | | - } else if (!wfRunHooks('EditFilter', array($editPage, $text, 0, &$errortext))) { |
| 275 | + } else if( !wfRunHooks( 'EditFilter', array( $editPage, $text, 0, &$errortext ) ) ) { |
274 | 276 | # Hooks usually print their own error |
275 | 277 | return false; |
276 | | - } else if ($errortext != '') { |
277 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 278 | + } else if( $errortext != '' ) { |
| 279 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
278 | 280 | return false; |
279 | | - } else if ($wgUser->isBlockedFrom($nt, false)) { |
280 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 281 | + } else if( $wgUser->isBlockedFrom( $nt, false ) ) { |
| 282 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
281 | 283 | return false; |
282 | | - } else if ((int)(strlen($text) / 1024) > $wgMaxArticleSize) { |
283 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 284 | + } else if( (int)(strlen($text) / 1024) > $wgMaxArticleSize ) { |
| 285 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
284 | 286 | return false; |
285 | | - } else if (!$wgUser->isAllowed('edit')) { |
286 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 287 | + } else if( !$wgUser->isAllowed( 'edit' ) ) { |
| 288 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
287 | 289 | return false; |
288 | | - } else if (wfReadOnly()) { |
289 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 290 | + } else if( wfReadOnly() ) { |
| 291 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
290 | 292 | return false; |
291 | | - } else if ($wgUser->pingLimiter()) { |
292 | | - $wgOut->showErrorPage('formsaveerror', 'formsaveerrortext'); |
| 293 | + } else if( $wgUser->pingLimiter() ) { |
| 294 | + $wgOut->showErrorPage( 'formsaveerror', 'formsaveerrortext' ); |
293 | 295 | return false; |
294 | 296 | } |
295 | 297 | |
— | — | @@ -297,12 +299,10 @@ |
298 | 300 | } |
299 | 301 | |
300 | 302 | # Dummy class for extensions that support EditFilter hook |
301 | | - |
302 | 303 | class FakeEditPage { |
303 | | - |
304 | 304 | var $mTitle; |
305 | 305 | |
306 | | - function FakeEditPage(&$nt) { |
| 306 | + function FakeEditPage( &$nt ) { |
307 | 307 | $this->mTitle = $nt; |
308 | 308 | } |
309 | 309 | } |
— | — | @@ -315,69 +315,66 @@ |
316 | 316 | var $fields; |
317 | 317 | var $namePattern; |
318 | 318 | |
319 | | - function Form($name, $text) { |
320 | | - |
| 319 | + function Form( $name, $text ) { |
321 | 320 | $this->name = $name; |
322 | | - $this->title = wfMsgForContent('formtitlepattern', $name); |
| 321 | + $this->title = wfMsgForContent( 'formtitlepattern', $name ); |
323 | 322 | $this->template = array(); |
324 | | - $this->template[0] = wfMsgForContent('formtemplatepattern', $name); |
| 323 | + $this->template[0] = wfMsgForContent( 'formtemplatepattern', $name ); |
325 | 324 | |
326 | 325 | $this->fields = array(); |
327 | 326 | $this->namePattern = array(); |
328 | 327 | $this->instructions = NULL; |
329 | 328 | |
330 | 329 | # XXX: may be some faster ways to do this |
| 330 | + $lines = explode( "\n", $text ); |
331 | 331 | |
332 | | - $lines = explode("\n", $text); |
| 332 | + foreach( $lines as $line ) { |
333 | 333 | |
334 | | - foreach ($lines as $line) { |
335 | | - |
336 | | - if (preg_match('/^(\w+)=(.*)$/', $line, $matches)) { |
337 | | - if (strcasecmp($matches[1], 'template') == 0) { |
| 334 | + if( preg_match( '/^(\w+)=(.*)$/', $line, $matches ) ) { |
| 335 | + if( strcasecmp( $matches[1], 'template' ) == 0 ) { |
338 | 336 | $this->template[0] = $matches[2]; |
339 | | - } else if (preg_match('/template(\d+)/i', $matches[1], $tmatches)) { |
| 337 | + } else if( preg_match( '/template(\d+)/i', $matches[1], $tmatches ) ) { |
340 | 338 | $this->template[intval($tmatches[1])] = $matches[2]; |
341 | | - } else if (strcasecmp($matches[1], 'namePattern') == 0) { |
| 339 | + } else if( strcasecmp( $matches[1], 'namePattern' ) == 0 ) { |
342 | 340 | $this->namePattern[0] = $matches[2]; |
343 | | - } else if (preg_match('/namePattern(\d+)/i', $matches[1], $tmatches)) { |
| 341 | + } else if( preg_match( '/namePattern(\d+)/i', $matches[1], $tmatches ) ) { |
344 | 342 | $this->namePattern[intval($tmatches[1])] = $matches[2]; |
345 | | - } else if (strcasecmp($matches[1], 'title') == 0) { |
| 343 | + } else if( strcasecmp( $matches[1], 'title' ) == 0 ) { |
346 | 344 | $this->title = $matches[2]; |
347 | | - } else if (strcasecmp($matches[1], 'instructions') == 0) { |
| 345 | + } else if( strcasecmp( $matches[1], 'instructions' ) == 0 ) { |
348 | 346 | $this->instructions = $matches[2]; |
349 | | - wfDebug("Got instructions: '" . $this->instructions . "'.\n"); |
| 347 | + wfDebug( __METHOD__ . ": Got instructions: '" . $this->instructions . "'.\n" ); |
350 | 348 | } else { |
351 | | - wfDebug("SpecialForm: unknown form attribute '$matches[1]'; skipping.\n"); |
| 349 | + wfDebug( __METHOD__ . ": unknown form attribute '$matches[1]'; skipping.\n" ); |
352 | 350 | } |
353 | | - } else if (preg_match('/^(\w+)\|([^\|]+)\|(\w+)(\|([^\|]+)(\|(.*))?)?$/', $line, $matches)) { |
| 351 | + } else if( preg_match( '/^(\w+)\|([^\|]+)\|(\w+)(\|([^\|]+)(\|(.*))?)?$/', $line, $matches ) ) { |
354 | 352 | # XXX: build an inheritance tree for different kinds of fields |
355 | 353 | $field = new FormField(); |
356 | | - $field->setName($matches[1]); |
357 | | - $field->setLabel($matches[2]); |
358 | | - $field->setFieldType($matches[3]); |
359 | | - if (count($matches) > 4 && $matches[4]) { |
360 | | - $field->setDescription($matches[5]); |
361 | | - if (count($matches) > 6 && $matches[6]) { |
362 | | - $rawOptions = explode(',', $matches[7]); |
363 | | - foreach ($rawOptions as $rawOption) { |
364 | | - if (preg_match('/^(\w+)=(.+)/', $rawOption, $optMatches)) { |
365 | | - $field->setOption($optMatches[1], $optMatches[2]); |
| 354 | + $field->setName( $matches[1] ); |
| 355 | + $field->setLabel( $matches[2] ); |
| 356 | + $field->setFieldType( $matches[3] ); |
| 357 | + if( count( $matches ) > 4 && $matches[4] ) { |
| 358 | + $field->setDescription( $matches[5] ); |
| 359 | + if( count( $matches ) > 6 && $matches[6] ) { |
| 360 | + $rawOptions = explode( ',', $matches[7] ); |
| 361 | + foreach( $rawOptions as $rawOption ) { |
| 362 | + if( preg_match( '/^(\w+)=(.+)/', $rawOption, $optMatches ) ) { |
| 363 | + $field->setOption( $optMatches[1], $optMatches[2] ); |
366 | 364 | } else { |
367 | | - wfDebug("SpecialForm: unrecognized form field option: '$rawOption'; skipping.\n"); |
| 365 | + wfDebug( __METHOD__ . ": unrecognized form field option: '$rawOption'; skipping.\n" ); |
368 | 366 | } |
369 | 367 | } |
370 | 368 | } |
371 | 369 | } |
372 | 370 | $this->fields[$field->name] = $field; |
373 | 371 | } else { |
374 | | - wfDebug("SpecialForm: unrecognized form line: '$line'; skipping.\n"); |
| 372 | + wfDebug( __METHOD__ . ": unrecognized form line: '$line'; skipping.\n" ); |
375 | 373 | } |
376 | 374 | } |
377 | 375 | } |
378 | 376 | } |
379 | 377 | |
380 | 378 | class FormField { |
381 | | - |
382 | 379 | var $name; |
383 | 380 | var $type; |
384 | 381 | var $label; |
— | — | @@ -392,111 +389,122 @@ |
393 | 390 | $this->options = array(); |
394 | 391 | } |
395 | 392 | |
396 | | - function setName($name) { |
| 393 | + function setName( $name ) { |
397 | 394 | $this->name = $name; |
398 | 395 | } |
399 | 396 | |
400 | | - function setFieldType($type) { |
| 397 | + function setFieldType( $type ) { |
401 | 398 | $this->type = $type; |
402 | 399 | } |
403 | 400 | |
404 | | - function setLabel($label) { |
| 401 | + function setLabel( $label ) { |
405 | 402 | $this->label = $label; |
406 | 403 | } |
407 | 404 | |
408 | | - function setDescription($description) { |
| 405 | + function setDescription( $description ) { |
409 | 406 | $this->description = $description; |
410 | 407 | } |
411 | 408 | |
412 | | - function setOption($key, $value) { |
| 409 | + function setOption( $key, $value ) { |
413 | 410 | $this->options[$key] = $value; |
414 | 411 | } |
415 | 412 | |
416 | | - function getOption($key, $default = NULL) { |
417 | | - if (array_key_exists($key, $this->options)) { |
| 413 | + function getOption( $key, $default = NULL ) { |
| 414 | + if( array_key_exists( $key, $this->options ) ) { |
418 | 415 | return $this->options[$key]; |
419 | 416 | } else { |
420 | 417 | return $default; |
421 | 418 | } |
422 | 419 | } |
423 | 420 | |
424 | | - function isOptionTrue($key, $default = false) { |
425 | | - $value = $this->getOption($key, $default); |
426 | | - return ((strcasecmp($value, 'on') == 0) || |
427 | | - (strcasecmp($value, 'yes') == 0) || |
428 | | - (strcasecmp($value, 'true') == 0) || |
429 | | - (strcasecmp($value, '1') == 0)); |
| 421 | + function isOptionTrue( $key, $default = false ) { |
| 422 | + $value = $this->getOption( $key, $default ); |
| 423 | + return ( ( strcasecmp( $value, 'on' ) == 0 ) || |
| 424 | + ( strcasecmp( $value, 'yes' ) == 0 ) || |
| 425 | + ( strcasecmp( $value, 'true' ) == 0 ) || |
| 426 | + ( strcasecmp( $value, '1' ) == 0 ) ); |
430 | 427 | } |
431 | 428 | |
432 | | - function render($def = NULL) { |
| 429 | + function render( $def = NULL ) { |
433 | 430 | global $wgOut; |
434 | 431 | |
435 | | - switch ($this->type) { |
436 | | - case 'textarea': |
437 | | - return Xml::openElement('h2') . |
438 | | - Xml::element('label', array('for' => $this->name), $this->label) . |
439 | | - Xml::closeElement('h2') . |
440 | | - (($this->description) ? |
441 | | - (Xml::openElement('div') . $wgOut->parse($this->description) . Xml::closeElement('div')) : '') . |
442 | | - Xml::openElement('textarea', array('name' => $this->name, |
443 | | - 'id' => $this->name, |
444 | | - 'rows' => $this->getOption('rows', 6), |
445 | | - 'cols' => $this->getOption('cols', 80))) . |
446 | | - ((is_null($def)) ? '' : $def) . |
447 | | - Xml::closeElement('textarea'); |
| 432 | + switch( $this->type ) { |
| 433 | + case 'textarea': |
| 434 | + return Xml::openElement( 'h2' ) . |
| 435 | + Xml::element( 'label', array( 'for' => $this->name ), $this->label ) . |
| 436 | + Xml::closeElement( 'h2' ) . |
| 437 | + ( ( $this->description) ? |
| 438 | + ( Xml::openElement( 'div' ) . $wgOut->parse( $this->description ) . Xml::closeElement( 'div' ) ) : '' ) . |
| 439 | + Xml::openElement( 'textarea', |
| 440 | + array( |
| 441 | + 'name' => $this->name, |
| 442 | + 'id' => $this->name, |
| 443 | + 'rows' => $this->getOption( 'rows', 6 ), |
| 444 | + 'cols' => $this->getOption( 'cols', 80 ) |
| 445 | + ) |
| 446 | + ) . |
| 447 | + ( ( is_null( $def ) ) ? '' : $def ) . |
| 448 | + Xml::closeElement( 'textarea' ); |
448 | 449 | break; |
449 | | - case 'text': |
450 | | - return Xml::element( 'label', array( 'for' => $this->name ), $this->label ) . wfMsg( 'colon-separator' ) . |
451 | | - Xml::element('input', array('type' => 'text', |
452 | | - 'name' => $this->name, |
453 | | - 'id' => $this->name, |
454 | | - 'value' => ((is_null($def)) ? '' : $def), |
455 | | - 'size' => $this->getOption('size', 30))); |
| 450 | + case 'text': |
| 451 | + return Xml::element( 'label', array( 'for' => $this->name ), $this->label ) . wfMsg( 'colon-separator' ) . |
| 452 | + Xml::element( 'input', |
| 453 | + array( |
| 454 | + 'type' => 'text', |
| 455 | + 'name' => $this->name, |
| 456 | + 'id' => $this->name, |
| 457 | + 'value' => ( ( is_null( $def ) ) ? '' : $def ), |
| 458 | + 'size' => $this->getOption( 'size', 30 ) |
| 459 | + ) |
| 460 | + ); |
456 | 461 | break; |
457 | | - case 'checkbox': |
458 | | - $attrs = array('type' => 'checkbox', |
459 | | - 'name' => $this->name, |
460 | | - 'id' => $this->name); |
461 | | - if ($def == 'checked') { |
462 | | - $attrs['checked'] = 'checked'; |
463 | | - } |
464 | | - return Xml::element( 'label', array( 'for' => $this->name ), $this->label ) . wfMsg( 'colon-separator' ) . |
465 | | - Xml::element('input', $attrs); |
466 | | - break; |
467 | | - case 'radio': |
468 | | - $items = array(); |
469 | | - $rawitems = explode(';', $this->getOption('items')); |
470 | | - foreach ($rawitems as $item) { |
471 | | - $attrs = array('type' => 'radio', |
472 | | - 'name' => $this->name, |
473 | | - 'value' => $item); |
474 | | - if ($item == $def) { |
| 462 | + case 'checkbox': |
| 463 | + $attrs = array( |
| 464 | + 'type' => 'checkbox', |
| 465 | + 'name' => $this->name, |
| 466 | + 'id' => $this->name |
| 467 | + ); |
| 468 | + if( $def == 'checked' ) { |
475 | 469 | $attrs['checked'] = 'checked'; |
476 | 470 | } |
477 | | - $items[] = Xml::openElement('input', $attrs) . |
478 | | - Xml::element('label', null, $item) . |
479 | | - Xml::closeElement('input'); |
480 | | - } |
481 | | - return Xml::element('span', null, $this->label) . Xml::element('br') . implode("", $items); |
| 471 | + return Xml::element( 'label', array( 'for' => $this->name ), $this->label ) . wfMsg( 'colon-separator' ) . |
| 472 | + Xml::element( 'input', $attrs ); |
482 | 473 | break; |
483 | | - case 'select': |
484 | | - $items = array(); |
485 | | - $rawitems = explode(';', $this->getOption('items')); |
486 | | - foreach ($rawitems as $item) { |
487 | | - $items[] = Xml::element('option', |
488 | | - ($item == $def) ? array('selected' => 'selected') : null, |
489 | | - $item); |
490 | | - } |
| 474 | + case 'radio': |
| 475 | + $items = array(); |
| 476 | + $rawitems = explode( ';', $this->getOption( 'items' ) ); |
| 477 | + foreach( $rawitems as $item ) { |
| 478 | + $attrs = array( |
| 479 | + 'type' => 'radio', |
| 480 | + 'name' => $this->name, |
| 481 | + 'value' => $item |
| 482 | + ); |
| 483 | + if( $item == $def ) { |
| 484 | + $attrs['checked'] = 'checked'; |
| 485 | + } |
| 486 | + $items[] = Xml::openElement( 'input', $attrs ) . |
| 487 | + Xml::element( 'label', null, $item ) . |
| 488 | + Xml::closeElement( 'input' ); |
| 489 | + } |
| 490 | + return Xml::element( 'span', null, $this->label ) . Xml::element( 'br' ) . implode( '', $items ); |
| 491 | + break; |
| 492 | + case 'select': |
| 493 | + $items = array(); |
| 494 | + $rawitems = explode( ';', $this->getOption( 'items' ) ); |
| 495 | + foreach( $rawitems as $item ) { |
| 496 | + $items[] = Xml::element( 'option', |
| 497 | + ( $item == $def ) ? array( 'selected' => 'selected' ) : null, |
| 498 | + $item ); |
| 499 | + } |
491 | 500 | |
492 | | - return Xml::element( 'label', array( 'for' => $this->name ), $this->label ) . wfMsg( 'colon-separator' ) . |
493 | | - Xml::openElement('select', array('name' => $this->name, 'id' => $this->name)) . |
494 | | - implode("", $items) . |
495 | | - Xml::closeElement('select'); |
496 | | - |
| 501 | + return Xml::element( 'label', array( 'for' => $this->name ), $this->label ) . wfMsg( 'colon-separator' ) . |
| 502 | + Xml::openElement( 'select', array( 'name' => $this->name, 'id' => $this->name ) ) . |
| 503 | + implode( '', $items ) . |
| 504 | + Xml::closeElement( 'select' ); |
497 | 505 | break; |
498 | | - default: |
499 | | - wfDebug("SpecialForm: unknown form field type '$this->type', skipping.\n"); |
500 | | - return ''; |
| 506 | + default: |
| 507 | + wfDebug( __METHOD__ . ": unknown form field type '$this->type', skipping.\n" ); |
| 508 | + return ''; |
501 | 509 | } |
502 | 510 | } |
503 | | -} |
| 511 | +} |
\ No newline at end of file |
Index: trunk/extensions/SpecialForm/SpecialForm.setup.php |
— | — | @@ -17,26 +17,27 @@ |
18 | 18 | * along with this program; if not, write to the Free Software |
19 | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | 20 | * |
| 21 | + * @file |
| 22 | + * @ingroup Extensions |
21 | 23 | * @author Evan Prodromou <evan@vinismo.com> |
22 | | - * @ingroup Extensions |
23 | 24 | */ |
24 | 25 | |
25 | | -if (!defined('MEDIAWIKI')) { |
| 26 | +if( !defined( 'MEDIAWIKI' ) ) { |
26 | 27 | exit( 1 ); |
27 | 28 | } |
28 | 29 | |
29 | | -define('SPECIALFORM_VERSION', '0.4'); |
30 | | - |
31 | | -$dir = dirname(__FILE__) . '/'; |
32 | | -$wgAutoloadClasses['SpecialForm'] = $dir . '/SpecialForm.body.php'; # Tell MediaWiki to load the extension body. |
33 | | -$wgExtensionMessagesFiles['SpecialForm'] = $dir . 'SpecialForm.i18n.php'; #Load internationalization file |
| 30 | +// Set up the new special page |
| 31 | +$dir = dirname( __FILE__ ) . '/'; |
| 32 | +$wgAutoloadClasses['SpecialForm'] = $dir . 'SpecialForm.body.php'; # Tell MediaWiki to load the extension body. |
| 33 | +$wgExtensionMessagesFiles['Form'] = $dir . 'SpecialForm.i18n.php'; #Load internationalization file |
34 | 34 | $wgExtensionAliasesFiles['Form'] = $dir . 'SpecialForm.alias.php'; |
35 | 35 | $wgSpecialPages['Form'] = 'SpecialForm'; # Let MediaWiki know about your new special page. |
36 | 36 | |
| 37 | +// Extension credits that will show up on Special:Version |
37 | 38 | $wgExtensionCredits['specialpage'][] = array( |
38 | 39 | 'path' => __FILE__, |
39 | 40 | 'name' => 'Form', |
40 | | - 'version' => SPECIALFORM_VERSION, |
| 41 | + 'version' => '0.4.1', |
41 | 42 | 'author' => 'Evan Prodromou', |
42 | 43 | 'url' => 'http://www.mediawiki.org/wiki/Extension:Form', |
43 | 44 | 'description' => 'A form interface to start new articles', |
— | — | @@ -44,4 +45,4 @@ |
45 | 46 | ); |
46 | 47 | |
47 | 48 | # Use recaptcha; default to false |
48 | | -$wgSpecialFormRecaptcha = false; |
| 49 | +$wgSpecialFormRecaptcha = false; |
\ No newline at end of file |