Conditions | 48 |
Paths | 2 |
Total Lines | 266 |
Code Lines | 195 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
189 | function rss_buildItems(&$rss, &$data, $opt) { |
||
190 | global $conf; |
||
191 | global $lang; |
||
192 | /* @var DokuWiki_Auth_Plugin $auth */ |
||
193 | global $auth; |
||
194 | |||
195 | $eventData = array( |
||
196 | 'rss' => &$rss, |
||
197 | 'data' => &$data, |
||
198 | 'opt' => &$opt, |
||
199 | ); |
||
200 | $event = new Doku_Event('FEED_DATA_PROCESS', $eventData); |
||
201 | if($event->advise_before(false)) { |
||
202 | foreach($data as $ditem) { |
||
203 | if(!is_array($ditem)) { |
||
204 | // not an array? then only a list of IDs was given |
||
205 | $ditem = array('id' => $ditem); |
||
206 | } |
||
207 | |||
208 | $item = new FeedItem(); |
||
209 | $id = $ditem['id']; |
||
210 | if(!$ditem['media']) { |
||
211 | $meta = p_get_metadata($id); |
||
212 | } else { |
||
213 | $meta = array(); |
||
214 | } |
||
215 | |||
216 | // add date |
||
217 | if($ditem['date']) { |
||
218 | $date = $ditem['date']; |
||
219 | } elseif ($ditem['media']) { |
||
220 | $date = @filemtime(mediaFN($id)); |
||
221 | } elseif (file_exists(wikiFN($id))) { |
||
222 | $date = @filemtime(wikiFN($id)); |
||
223 | } elseif($meta['date']['modified']) { |
||
224 | $date = $meta['date']['modified']; |
||
225 | } else { |
||
226 | $date = 0; |
||
227 | } |
||
228 | if($date) $item->date = date('r', $date); |
||
229 | |||
230 | // add title |
||
231 | if($conf['useheading'] && $meta['title']) { |
||
232 | $item->title = $meta['title']; |
||
233 | } else { |
||
234 | $item->title = $ditem['id']; |
||
235 | } |
||
236 | if($conf['rss_show_summary'] && !empty($ditem['sum'])) { |
||
237 | $item->title .= ' - '.strip_tags($ditem['sum']); |
||
238 | } |
||
239 | |||
240 | // add item link |
||
241 | switch($opt['link_to']) { |
||
242 | case 'page': |
||
243 | if($ditem['media']) { |
||
244 | $item->link = media_managerURL( |
||
245 | array( |
||
246 | 'image' => $id, |
||
247 | 'ns' => getNS($id), |
||
248 | 'rev' => $date |
||
249 | ), '&', true |
||
250 | ); |
||
251 | } else { |
||
252 | $item->link = wl($id, 'rev='.$date, true, '&'); |
||
253 | } |
||
254 | break; |
||
255 | case 'rev': |
||
256 | if($ditem['media']) { |
||
257 | $item->link = media_managerURL( |
||
258 | array( |
||
259 | 'image' => $id, |
||
260 | 'ns' => getNS($id), |
||
261 | 'rev' => $date, |
||
262 | 'tab_details' => 'history' |
||
263 | ), '&', true |
||
264 | ); |
||
265 | } else { |
||
266 | $item->link = wl($id, 'do=revisions&rev='.$date, true, '&'); |
||
267 | } |
||
268 | break; |
||
269 | case 'current': |
||
270 | if($ditem['media']) { |
||
271 | $item->link = media_managerURL( |
||
272 | array( |
||
273 | 'image' => $id, |
||
274 | 'ns' => getNS($id) |
||
275 | ), '&', true |
||
276 | ); |
||
277 | } else { |
||
278 | $item->link = wl($id, '', true, '&'); |
||
279 | } |
||
280 | break; |
||
281 | case 'diff': |
||
282 | default: |
||
283 | if($ditem['media']) { |
||
284 | $item->link = media_managerURL( |
||
285 | array( |
||
286 | 'image' => $id, |
||
287 | 'ns' => getNS($id), |
||
288 | 'rev' => $date, |
||
289 | 'tab_details' => 'history', |
||
290 | 'mediado' => 'diff' |
||
291 | ), '&', true |
||
292 | ); |
||
293 | } else { |
||
294 | $item->link = wl($id, 'rev='.$date.'&do=diff', true, '&'); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | // add item content |
||
299 | switch($opt['item_content']) { |
||
300 | case 'diff': |
||
301 | case 'htmldiff': |
||
302 | if($ditem['media']) { |
||
303 | $medialog = new MediaChangeLog($id); |
||
304 | $revs = $medialog->getRevisions(0, 1); |
||
305 | $rev = $revs[0]; |
||
306 | $src_r = ''; |
||
307 | $src_l = ''; |
||
308 | |||
309 | if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) { |
||
310 | $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id)); |
||
311 | $src_r = ml($id, $more, true, '&', true); |
||
312 | } |
||
313 | if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) { |
||
314 | $more = 'rev='.$rev.'&w='.$size[0].'&h='.$size[1]; |
||
315 | $src_l = ml($id, $more, true, '&', true); |
||
316 | } |
||
317 | $content = ''; |
||
318 | if($src_r) { |
||
319 | $content = '<table>'; |
||
320 | $content .= '<tr><th width="50%">'.$rev.'</th>'; |
||
321 | $content .= '<th width="50%">'.$lang['current'].'</th></tr>'; |
||
322 | $content .= '<tr align="center"><td><img src="'.$src_l.'" alt="" /></td><td>'; |
||
323 | $content .= '<img src="'.$src_r.'" alt="'.$id.'" /></td></tr>'; |
||
324 | $content .= '</table>'; |
||
325 | } |
||
326 | |||
327 | } else { |
||
328 | require_once(DOKU_INC.'inc/DifferenceEngine.php'); |
||
329 | $pagelog = new PageChangeLog($id); |
||
330 | $revs = $pagelog->getRevisions(0, 1); |
||
331 | $rev = $revs[0]; |
||
332 | |||
333 | if($rev) { |
||
334 | $df = new Diff(explode("\n", rawWiki($id, $rev)), |
||
335 | explode("\n", rawWiki($id, ''))); |
||
336 | } else { |
||
337 | $df = new Diff(array(''), |
||
338 | explode("\n", rawWiki($id, ''))); |
||
339 | } |
||
340 | |||
341 | if($opt['item_content'] == 'htmldiff') { |
||
342 | // note: no need to escape diff output, TableDiffFormatter provides 'safe' html |
||
343 | $tdf = new TableDiffFormatter(); |
||
344 | $content = '<table>'; |
||
345 | $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>'; |
||
346 | $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>'; |
||
347 | $content .= $tdf->format($df); |
||
348 | $content .= '</table>'; |
||
349 | } else { |
||
350 | // note: diff output must be escaped, UnifiedDiffFormatter provides plain text |
||
351 | $udf = new UnifiedDiffFormatter(); |
||
352 | $content = "<pre>\n".hsc($udf->format($df))."\n</pre>"; |
||
353 | } |
||
354 | } |
||
355 | break; |
||
356 | case 'html': |
||
357 | if($ditem['media']) { |
||
358 | if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) { |
||
359 | $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id)); |
||
360 | $src = ml($id, $more, true, '&', true); |
||
361 | $content = '<img src="'.$src.'" alt="'.$id.'" />'; |
||
362 | } else { |
||
363 | $content = ''; |
||
364 | } |
||
365 | } else { |
||
366 | if (@filemtime(wikiFN($id)) === $date) { |
||
367 | $content = p_wiki_xhtml($id, '', false); |
||
368 | } else { |
||
369 | $content = p_wiki_xhtml($id, $date, false); |
||
370 | } |
||
371 | // no TOC in feeds |
||
372 | $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content); |
||
373 | |||
374 | // add alignment for images |
||
375 | $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content); |
||
376 | $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content); |
||
377 | |||
378 | // make URLs work when canonical is not set, regexp instead of rerendering! |
||
379 | if(!$conf['canonical']) { |
||
380 | $base = preg_quote(DOKU_REL, '/'); |
||
381 | $content = preg_replace('/(<a href|<img src)="('.$base.')/s', '$1="'.DOKU_URL, $content); |
||
382 | } |
||
383 | } |
||
384 | |||
385 | break; |
||
386 | case 'abstract': |
||
387 | default: |
||
388 | if($ditem['media']) { |
||
389 | if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) { |
||
390 | $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id)); |
||
391 | $src = ml($id, $more, true, '&', true); |
||
392 | $content = '<img src="'.$src.'" alt="'.$id.'" />'; |
||
393 | } else { |
||
394 | $content = ''; |
||
395 | } |
||
396 | } else { |
||
397 | $content = $meta['description']['abstract']; |
||
398 | } |
||
399 | } |
||
400 | $item->description = $content; //FIXME a plugin hook here could be senseful |
||
401 | |||
402 | // add user |
||
403 | # FIXME should the user be pulled from metadata as well? |
||
404 | $user = @$ditem['user']; // the @ spares time repeating lookup |
||
405 | if(blank($user)) { |
||
406 | $item->author = 'Anonymous'; |
||
407 | $item->authorEmail = '[email protected]'; |
||
408 | } else { |
||
409 | $item->author = $user; |
||
410 | $item->authorEmail = $user . '@undisclosed.example.com'; |
||
411 | |||
412 | // get real user name if configured |
||
413 | if($conf['useacl'] && $auth) { |
||
414 | $userInfo = $auth->getUserData($user); |
||
415 | if($userInfo) { |
||
416 | switch($conf['showuseras']) { |
||
417 | case 'username': |
||
418 | case 'username_link': |
||
419 | $item->author = $userInfo['name']; |
||
420 | break; |
||
421 | default: |
||
422 | $item->author = $user; |
||
423 | break; |
||
424 | } |
||
425 | } else { |
||
426 | $item->author = $user; |
||
427 | } |
||
428 | } |
||
429 | } |
||
430 | |||
431 | // add category |
||
432 | if(isset($meta['subject'])) { |
||
433 | $item->category = $meta['subject']; |
||
434 | } else { |
||
435 | $cat = getNS($id); |
||
436 | if($cat) $item->category = $cat; |
||
437 | } |
||
438 | |||
439 | // finally add the item to the feed object, after handing it to registered plugins |
||
440 | $evdata = array( |
||
441 | 'item' => &$item, |
||
442 | 'opt' => &$opt, |
||
443 | 'ditem' => &$ditem, |
||
444 | 'rss' => &$rss |
||
445 | ); |
||
446 | $evt = new Doku_Event('FEED_ITEM_ADD', $evdata); |
||
447 | if($evt->advise_before()) { |
||
448 | $rss->addItem($item); |
||
449 | } |
||
450 | $evt->advise_after(); // for completeness |
||
451 | } |
||
452 | } |
||
453 | $event->advise_after(); |
||
454 | } |
||
455 | |||
511 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.