Index: trunk/extensions/SemanticForms/includes/SF_AutoEditAjaxHandler.php |
— | — | @@ -19,7 +19,7 @@ |
20 | 20 | |
21 | 21 | static function handleAutoEdit ( $optionsString = null, $prefillFromExisting = 'true' ) { |
22 | 22 | $handler = new self( $optionsString ); |
23 | | - return $handler -> storeSemanticData( $prefillFromExisting === 'true' ); |
| 23 | + return $handler->storeSemanticData( $prefillFromExisting === 'true' ); |
24 | 24 | } |
25 | 25 | |
26 | 26 | function __construct ( $options ) { |
— | — | @@ -30,22 +30,22 @@ |
31 | 31 | |
32 | 32 | // if ( version_compare( substr( $wgVersion, 0, 4 ), '1.17', '<' ) ) { |
33 | 33 | if ( !StubObject::isRealObject( $wgParser ) ) |
34 | | - $wgParser -> _unstub(); |
| 34 | + $wgParser->_unstub(); |
35 | 35 | |
36 | 36 | // perform offensive operation |
37 | | - $wgParser -> startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true ); |
| 37 | + $wgParser->startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true ); |
38 | 38 | // } else { |
39 | | -// $wgParser -> startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true ); |
| 39 | +// $wgParser->startExternalParse( $title, ParserOptions::newFromUser( $wgUser ), Parser::OT_HTML, true ); |
40 | 40 | // } |
41 | 41 | |
42 | 42 | // parse options |
43 | | - $this -> parseDataFromQueryString( $this -> mOptions, $options, true ); |
| 43 | + $this->parseDataFromQueryString( $this->mOptions, $options, true ); |
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
47 | 47 | * |
48 | | - * @global $wgOut |
49 | | - * @global $wgRequest |
| 48 | + * @global $wgOut |
| 49 | + * @global $wgRequest |
50 | 50 | * @global <type> $wgUser |
51 | 51 | * @global <type> $wgParser |
52 | 52 | * @return <type> |
— | — | @@ -54,12 +54,12 @@ |
55 | 55 | |
56 | 56 | global $wgOut, $wgRequest, $wgParser, $wgTitle; |
57 | 57 | |
58 | | - if ( !array_key_exists( 'ok text', $this -> mOptions ) ) { |
59 | | - $this -> mOptions[ 'ok text' ] = wfMsg( 'sf_autoedit_success' ); |
| 58 | + if ( !array_key_exists( 'ok text', $this->mOptions ) ) { |
| 59 | + $this->mOptions['ok text'] = wfMsg( 'sf_autoedit_success' ); |
60 | 60 | } |
61 | 61 | |
62 | | - if ( !array_key_exists( 'error text', $this -> mOptions ) ) { |
63 | | - $this -> mOptions[ 'error text' ] = '$1'; |
| 62 | + if ( !array_key_exists( 'error text', $this->mOptions ) ) { |
| 63 | + $this->mOptions['error text'] = '$1'; |
64 | 64 | } |
65 | 65 | |
66 | 66 | |
— | — | @@ -71,17 +71,17 @@ |
72 | 72 | } |
73 | 73 | |
74 | 74 | // If we have no target article and no form we might as well stop right away |
75 | | - if ( !array_key_exists( 'target', $this -> mOptions ) |
76 | | - && !array_key_exists( 'form', $this -> mOptions ) ) { |
| 75 | + if ( !array_key_exists( 'target', $this->mOptions ) |
| 76 | + && !array_key_exists( 'form', $this->mOptions ) ) { |
77 | 77 | return 'autoedit-notargetspecified'; |
78 | 78 | } |
79 | 79 | |
80 | 80 | // check if form was specified |
81 | | - if ( !array_key_exists( 'form', $this -> mOptions ) ) { |
| 81 | + if ( !array_key_exists( 'form', $this->mOptions ) ) { |
82 | 82 | |
83 | | - // no form specified, find one |
84 | | - // get title object and id for requested target article |
85 | | - $title = Title::newFromText( $this -> mOptions[ 'target' ] ); |
| 83 | + // If no form was specified, find the default one for |
| 84 | + // this page. |
| 85 | + $title = Title::newFromText( $this->mOptions['target'] ); |
86 | 86 | $form_names = SFFormLinker::getDefaultFormsForPage( $title ); |
87 | 87 | |
88 | 88 | // if no form can be found, return |
— | — | @@ -94,84 +94,83 @@ |
95 | 95 | return 'autoedit-toomanyformsfound'; |
96 | 96 | } |
97 | 97 | |
98 | | - // use the first found form |
99 | | - $this -> mOptions[ 'form' ] = $form_names[ 0 ]; |
| 98 | + // There should now be exactly one form. |
| 99 | + $this->mOptions['form'] = $form_names[0]; |
100 | 100 | } |
101 | 101 | |
102 | 102 | // we only care for the form's body |
103 | | - $wgOut -> setArticleBodyOnly( true ); |
| 103 | + $wgOut->setArticleBodyOnly( true ); |
104 | 104 | |
105 | 105 | $formedit = new SFFormEdit(); |
106 | 106 | $data = array( ); |
107 | 107 | |
108 | | - //////////////////////////////////////////////////////////////////////// |
109 | | - // First get the Semantic Form and extract its data (if requested) |
110 | | - // and modify or set as specified in the options string |
111 | | - |
| 108 | + // Get the form definition and target page (if there is one), |
| 109 | + // as specified in the options string, then create the actual |
| 110 | + // HTML form from them, and call that form to modify or create |
| 111 | + // the page. |
112 | 112 | if ( $prefillFromExisting ) { |
| 113 | + $wgRequest = new FauxRequest( $this->mOptions, true ); |
113 | 114 | |
114 | | - $wgRequest = new FauxRequest( $this -> mOptions, true ); |
115 | | - |
116 | 115 | // get the Semantic Form |
117 | | - if ( array_key_exists( 'target', $this -> mOptions ) ) { |
118 | | - $formedit -> execute( $this -> mOptions[ 'form' ] . '/' . $this -> mOptions[ 'target' ] ); |
| 116 | + if ( array_key_exists( 'target', $this->mOptions ) ) { |
| 117 | + $formedit->execute( $this->mOptions['form'] . '/' . $this->mOptions['target'] ); |
119 | 118 | } else { |
120 | | - $formedit -> execute( $this -> mOptions[ 'form' ] ); |
| 119 | + $formedit->execute( $this->mOptions['form'] ); |
121 | 120 | } |
122 | 121 | |
123 | 122 | // extract its data |
124 | | - $form = $this -> parseDataFromHTMLFrag( $data, trim( $wgOut -> getHTML() ), 'sfForm' ); |
| 123 | + $form = $this->parseDataFromHTMLFrag( $data, trim( $wgOut->getHTML() ), 'sfForm' ); |
125 | 124 | |
126 | 125 | if ( !$form ) { |
127 | 126 | // something went wrong |
128 | 127 | return array( |
129 | 128 | 'autoedit-nosemanticform', |
130 | 129 | array( |
131 | | - $this -> mOptions[ 'target' ], |
132 | | - $this -> mOptions[ 'form' ] |
| 130 | + $this->mOptions['target'], |
| 131 | + $this->mOptions['form'] |
133 | 132 | ) |
134 | 133 | ); |
135 | 134 | } |
136 | 135 | } else { |
137 | | - $this -> addToArray( $data, "wpSave", "Save" ); |
| 136 | + $this->addToArray( $data, "wpSave", "Save" ); |
138 | 137 | } |
139 | 138 | // and modify as specified |
140 | | - $data = $this -> array_merge_recursive_distinct( $data, $this -> mOptions ); |
| 139 | + $data = $this->array_merge_recursive_distinct( $data, $this->mOptions ); |
141 | 140 | |
142 | 141 | //////////////////////////////////////////////////////////////////////// |
143 | 142 | // Store the modified form |
144 | | - //$wgOut -> clearHTML(); |
| 143 | + //$wgOut->clearHTML(); |
145 | 144 | $wgRequest = new FauxRequest( $data, true ); |
146 | 145 | |
147 | 146 | // get the MW form |
148 | | - if ( array_key_exists( 'target', $this -> mOptions ) ) { |
149 | | - $formedit -> execute( $this -> mOptions[ 'form' ] . '/' . $this -> mOptions[ 'target' ], false ); |
| 147 | + if ( array_key_exists( 'target', $this->mOptions ) ) { |
| 148 | + $formedit->execute( $this->mOptions['form'] . '/' . $this->mOptions['target'], false ); |
150 | 149 | } else { |
151 | | - $formedit -> execute( $this -> mOptions[ 'form' ], false ); |
| 150 | + $formedit->execute( $this->mOptions['form'], false ); |
152 | 151 | } |
153 | 152 | |
154 | | - $wgParser -> getOptions() -> enableLimitReport( false ); |
| 153 | + $wgParser->getOptions()->enableLimitReport( false ); |
155 | 154 | |
156 | | - if ( $formedit -> mError ) { |
| 155 | + if ( $formedit->mError ) { |
| 156 | + // TODO Should this be sanitized? i.e. all HTML tags removed? |
| 157 | + $msg = $formedit->mError; |
157 | 158 | |
158 | | - $msg = $formedit -> mError; // Should this be sanitized? I.e. all html tags removed? |
159 | | - |
160 | | - $msg = $wgParser -> parse( |
161 | | - wfMsgReplaceArgs( $this -> mOptions[ 'error text' ], array( $msg ) ), |
| 159 | + $msg = $wgParser->parse( |
| 160 | + wfMsgReplaceArgs( $this->mOptions['error text'], array( $msg ) ), |
162 | 161 | $wgTitle, |
163 | | - $wgParser -> getOptions() |
164 | | - ) -> getText(); |
| 162 | + $wgParser->getOptions() |
| 163 | + )->getText(); |
165 | 164 | |
166 | 165 | $result = new AjaxResponse( $msg ); |
167 | | - $result -> setResponseCode( '400 Bad Request' ); |
| 166 | + $result->setResponseCode( '400 Bad Request' ); |
168 | 167 | return $result; |
169 | 168 | } else { |
170 | 169 | |
171 | | - header( "X-Location: " . $wgOut -> getRedirect() ); |
172 | | - header( "X-Form: " . $formedit -> mForm ); |
173 | | - header( "X-Target: " . $formedit -> mTarget ); |
| 170 | + header( "X-Location: " . $wgOut->getRedirect() ); |
| 171 | + header( "X-Form: " . $formedit->mForm ); |
| 172 | + header( "X-Target: " . $formedit->mTarget ); |
174 | 173 | |
175 | | - $msg = $wgParser -> recursiveTagParse( wfMsgReplaceArgs( $this -> mOptions[ 'ok text' ], array( $formedit -> mTarget, $formedit -> mForm ) ) ); |
| 174 | + $msg = $wgParser->recursiveTagParse( wfMsgReplaceArgs( $this->mOptions['ok text'], array( $formedit->mTarget, $formedit->mForm ) ) ); |
176 | 175 | |
177 | 176 | $result = new AjaxResponse( $msg ); |
178 | 177 | return $result; |
— | — | @@ -180,26 +179,26 @@ |
181 | 180 | |
182 | 181 | private function parseDataFromHTMLFrag ( &$data, $html, $formID ) { |
183 | 182 | $doc = new DOMDocument(); |
184 | | - @$doc -> loadHTML( |
| 183 | + @$doc->loadHTML( |
185 | 184 | '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body>' |
186 | 185 | . $html |
187 | 186 | . '</body></html>' |
188 | 187 | ); |
189 | 188 | |
190 | | - $form = $doc -> getElementById( $formID ); |
| 189 | + $form = $doc->getElementById( $formID ); |
191 | 190 | |
192 | 191 | if ( !$form ) { |
193 | 192 | return null; |
194 | 193 | } |
195 | 194 | |
196 | 195 | // Process input tags |
197 | | - $inputs = $form -> getElementsByTagName( 'input' ); |
| 196 | + $inputs = $form->getElementsByTagName( 'input' ); |
198 | 197 | |
199 | | - for ( $i = 0; $i < $inputs -> length; $i++ ) { |
| 198 | + for ( $i = 0; $i < $inputs->length; $i++ ) { |
200 | 199 | |
201 | | - $input = $inputs -> item( $i ); |
202 | | - $type = $input -> getAttribute( 'type' ); |
203 | | - $name = trim( $input -> getAttribute( 'name' ) ); |
| 200 | + $input = $inputs->item( $i ); |
| 201 | + $type = $input->getAttribute( 'type' ); |
| 202 | + $name = trim( $input->getAttribute( 'name' ) ); |
204 | 203 | |
205 | 204 | if ( !$name ) |
206 | 205 | continue; |
— | — | @@ -210,8 +209,8 @@ |
211 | 210 | switch ( $type ) { |
212 | 211 | case 'checkbox': |
213 | 212 | case 'radio': |
214 | | - if ( $input -> getAttribute( 'checked' ) ) |
215 | | - $this -> addToArray( $data, $name, $input -> getAttribute( 'value' ) ); |
| 213 | + if ( $input->getAttribute( 'checked' ) ) |
| 214 | + $this->addToArray( $data, $name, $input->getAttribute( 'value' ) ); |
216 | 215 | break; |
217 | 216 | |
218 | 217 | //case 'button': |
— | — | @@ -221,51 +220,51 @@ |
222 | 221 | //case 'reset': |
223 | 222 | //case 'submit': |
224 | 223 | case 'text': |
225 | | - $this -> addToArray( $data, $name, $input -> getAttribute( 'value' ) ); |
| 224 | + $this->addToArray( $data, $name, $input->getAttribute( 'value' ) ); |
226 | 225 | break; |
227 | 226 | |
228 | 227 | case 'submit': |
229 | 228 | if ( $name == "wpSave" ) |
230 | | - $this -> addToArray( $data, $name, $input -> getAttribute( 'value' ) ); |
| 229 | + $this->addToArray( $data, $name, $input->getAttribute( 'value' ) ); |
231 | 230 | } |
232 | 231 | } |
233 | 232 | |
234 | 233 | // Process select tags |
235 | | - $selects = $form -> getElementsByTagName( 'select' ); |
| 234 | + $selects = $form->getElementsByTagName( 'select' ); |
236 | 235 | |
237 | | - for ( $i = 0; $i < $selects -> length; $i++ ) { |
| 236 | + for ( $i = 0; $i < $selects->length; $i++ ) { |
238 | 237 | |
239 | | - $select = $selects -> item( $i ); |
240 | | - $name = trim( $select -> getAttribute( 'name' ) ); |
| 238 | + $select = $selects->item( $i ); |
| 239 | + $name = trim( $select->getAttribute( 'name' ) ); |
241 | 240 | |
242 | 241 | if ( !$name ) |
243 | 242 | continue; |
244 | 243 | |
245 | 244 | $values = array( ); |
246 | | - $options = $select -> getElementsByTagName( 'option' ); |
| 245 | + $options = $select->getElementsByTagName( 'option' ); |
247 | 246 | |
248 | | - if ( count( $options ) && (!$select -> hasAttribute( "multiple" ) || $options -> item( 0 ) -> hasAttribute( 'selected' ) ) ) { |
249 | | - $this -> addToArray( $data, $name, $options -> item( 0 ) -> getAttribute( 'value' ) ); |
| 247 | + if ( count( $options ) && (!$select->hasAttribute( "multiple" ) || $options->item( 0 )->hasAttribute( 'selected' ) ) ) { |
| 248 | + $this->addToArray( $data, $name, $options->item( 0 )->getAttribute( 'value' ) ); |
250 | 249 | } |
251 | 250 | |
252 | | - for ( $o = 1; $o < $options -> length; $o++ ) { |
253 | | - if ( $options -> item( $o ) -> hasAttribute( 'selected' ) ) |
254 | | - $this -> addToArray( $data, $name, $options -> item( $o ) -> getAttribute( 'value' ) ); |
| 251 | + for ( $o = 1; $o < $options->length; $o++ ) { |
| 252 | + if ( $options->item( $o )->hasAttribute( 'selected' ) ) |
| 253 | + $this->addToArray( $data, $name, $options->item( $o )->getAttribute( 'value' ) ); |
255 | 254 | } |
256 | 255 | } |
257 | 256 | |
258 | 257 | // Process textarea tags |
259 | | - $textareas = $form -> getElementsByTagName( 'textarea' ); |
| 258 | + $textareas = $form->getElementsByTagName( 'textarea' ); |
260 | 259 | |
261 | | - for ( $i = 0; $i < $textareas -> length; $i++ ) { |
| 260 | + for ( $i = 0; $i < $textareas->length; $i++ ) { |
262 | 261 | |
263 | | - $textarea = $textareas -> item( $i ); |
264 | | - $name = trim( $textarea -> getAttribute( 'name' ) ); |
| 262 | + $textarea = $textareas->item( $i ); |
| 263 | + $name = trim( $textarea->getAttribute( 'name' ) ); |
265 | 264 | |
266 | 265 | if ( !$name ) |
267 | 266 | continue; |
268 | 267 | |
269 | | - $this -> addToArray( $data, $name, $textarea -> textContent ); |
| 268 | + $this->addToArray( $data, $name, $textarea->textContent ); |
270 | 269 | } |
271 | 270 | |
272 | 271 | return $form; |
— | — | @@ -278,11 +277,12 @@ |
279 | 278 | * @global $wgOut |
280 | 279 | * @param Array $data |
281 | 280 | * @param String $queryString |
282 | | - * @param Boolean $expand If this is set to true, field values will get urldecoded and expanded |
283 | | - * This allows to slip parser functions by the MW parser on page creation |
284 | | - * (by urlencoding them) and to pass them to autoedit to expand them now. |
285 | | - * Expanding parser functions on page creation already might lead to cache |
286 | | - * issues, e.g. for the {{#time:}} parser function |
| 281 | + * @param Boolean $expand If this is set to true, field values will get |
| 282 | + * urldecoded and expanded. This allows us to have parser functions |
| 283 | + * skip handling by the MW parser on page creation (since they will be |
| 284 | + * URL-encoded), and to instead have them handled by #autoedit. |
| 285 | + * This can prevent cache issues, such as when dealing with the |
| 286 | + * {{#time:}} parser function. |
287 | 287 | * @return <type> |
288 | 288 | */ |
289 | 289 | private function parseDataFromQueryString ( &$data, $queryString, $expand = false ) { |
— | — | @@ -291,15 +291,15 @@ |
292 | 292 | foreach ( $params as $i => $param ) { |
293 | 293 | $elements = explode( '=', $param, 2 ); |
294 | 294 | |
295 | | - $key = trim( urldecode( $elements[ 0 ] ) ); |
296 | | - $value = count( $elements ) > 1 ? urldecode( $elements[ 1 ] ) : null; |
| 295 | + $key = trim( urldecode( $elements[0] ) ); |
| 296 | + $value = count( $elements ) > 1 ? urldecode( $elements[1] ) : null; |
297 | 297 | |
298 | 298 | if ( $key == "query string" ) { |
299 | | - $this -> parseDataFromQueryString( $data, $value, $expand ); |
| 299 | + $this->parseDataFromQueryString( $data, $value, $expand ); |
300 | 300 | } elseif ( $expand ) { |
301 | | - $this -> addToArray( $data, $key, $value ); |
| 301 | + $this->addToArray( $data, $key, $value ); |
302 | 302 | } else { |
303 | | - $this -> addToArray( $data, $key, $value ); |
| 303 | + $this->addToArray( $data, $key, $value ); |
304 | 304 | } |
305 | 305 | } |
306 | 306 | |
— | — | @@ -316,20 +316,20 @@ |
317 | 317 | |
318 | 318 | if ( preg_match( '/^([^\[\]]*)\[([^\[\]]*)\](.*)/', $key, $matches ) ) { |
319 | 319 | |
320 | | - $key = str_replace( ' ', '_', $matches[ 1 ] ); |
| 320 | + $key = str_replace( ' ', '_', $matches[1] ); |
321 | 321 | $key = str_replace( '.', '_', $key ); |
322 | 322 | |
323 | 323 | if ( !array_key_exists( $key, $array ) ) |
324 | | - $array[ $key ] = array( ); |
| 324 | + $array[$key] = array( ); |
325 | 325 | |
326 | | - $this -> addToArray( $array[ $key ], $matches[ 2 ] . $matches[ 3 ], $value ); |
| 326 | + $this->addToArray( $array[$key], $matches[2] . $matches[3], $value ); |
327 | 327 | } else { |
328 | 328 | |
329 | 329 | if ( $key ) { |
330 | 330 | // $key = str_replace( ' ', '_', $key ); |
331 | 331 | // $key = str_replace( '.', '_', $key ); |
332 | 332 | // var_dump($key); |
333 | | - $array[ $key ] = $value; |
| 333 | + $array[$key] = $value; |
334 | 334 | } else { |
335 | 335 | array_push( $array, $value ); |
336 | 336 | } |
— | — | @@ -360,10 +360,10 @@ |
361 | 361 | $merged = $array1; |
362 | 362 | |
363 | 363 | foreach ( $array2 as $key => &$value ) { |
364 | | - if ( is_array( $value ) && isset( $merged [ $key ] ) && is_array( $merged [ $key ] ) ) { |
365 | | - $merged [ $key ] = $this -> array_merge_recursive_distinct( $merged [ $key ], $value ); |
| 364 | + if ( is_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) { |
| 365 | + $merged[$key] = $this->array_merge_recursive_distinct( $merged[$key], $value ); |
366 | 366 | } else { |
367 | | - $merged [ $key ] = $value; |
| 367 | + $merged[$key] = $value; |
368 | 368 | } |
369 | 369 | } |
370 | 370 | |