Complex classes like Doku_Renderer_metadata often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Doku_Renderer_metadata, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Doku_Renderer_metadata extends Doku_Renderer |
||
14 | { |
||
15 | /** the approximate byte lenght to capture for the abstract */ |
||
16 | const ABSTRACT_LEN = 250; |
||
17 | |||
18 | /** the maximum UTF8 character length for the abstract */ |
||
19 | const ABSTRACT_MAX = 500; |
||
20 | |||
21 | /** @var array transient meta data, will be reset on each rendering */ |
||
22 | public $meta = array(); |
||
23 | |||
24 | /** @var array persistent meta data, will be kept until explicitly deleted */ |
||
25 | public $persistent = array(); |
||
26 | |||
27 | /** @var array the list of headers used to create unique link ids */ |
||
28 | protected $headers = array(); |
||
29 | |||
30 | /** @var string temporary $doc store */ |
||
31 | protected $store = ''; |
||
32 | |||
33 | /** @var string keeps the first image reference */ |
||
34 | protected $firstimage = ''; |
||
35 | |||
36 | /** @var bool whether or not data is being captured for the abstract, public to be accessible by plugins */ |
||
37 | public $capturing = true; |
||
38 | |||
39 | /** @var bool determines if enough data for the abstract was collected, yet */ |
||
40 | public $capture = true; |
||
41 | |||
42 | /** @var int number of bytes captured for abstract */ |
||
43 | protected $captured = 0; |
||
44 | |||
45 | /** |
||
46 | * Returns the format produced by this renderer. |
||
47 | * |
||
48 | * @return string always 'metadata' |
||
49 | */ |
||
50 | public function getFormat() |
||
54 | |||
55 | /** |
||
56 | * Initialize the document |
||
57 | * |
||
58 | * Sets up some of the persistent info about the page if it doesn't exist, yet. |
||
59 | */ |
||
60 | public function document_start() |
||
61 | { |
||
62 | global $ID; |
||
63 | |||
64 | $this->headers = array(); |
||
65 | |||
66 | // external pages are missing create date |
||
67 | if (!isset($this->persistent['date']['created']) || !$this->persistent['date']['created']) { |
||
68 | $this->persistent['date']['created'] = filectime(wikiFN($ID)); |
||
69 | } |
||
70 | if (!isset($this->persistent['user'])) { |
||
71 | $this->persistent['user'] = ''; |
||
72 | } |
||
73 | if (!isset($this->persistent['creator'])) { |
||
74 | $this->persistent['creator'] = ''; |
||
75 | } |
||
76 | // reset metadata to persistent values |
||
77 | $this->meta = $this->persistent; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Finalize the document |
||
82 | * |
||
83 | * Stores collected data in the metadata |
||
84 | */ |
||
85 | public function document_end() |
||
107 | |||
108 | /** |
||
109 | * Render plain text data |
||
110 | * |
||
111 | * This function takes care of the amount captured data and will stop capturing when |
||
112 | * enough abstract data is available |
||
113 | * |
||
114 | * @param $text |
||
115 | */ |
||
116 | public function cdata($text) |
||
129 | |||
130 | /** |
||
131 | * Add an item to the TOC |
||
132 | * |
||
133 | * @param string $id the hash link |
||
134 | * @param string $text the text to display |
||
135 | * @param int $level the nesting level |
||
136 | */ |
||
137 | public function toc_additem($id, $text, $level) |
||
152 | |||
153 | /** |
||
154 | * Render a heading |
||
155 | * |
||
156 | * @param string $text the text to display |
||
157 | * @param int $level header level |
||
158 | * @param int $pos byte position in the original source |
||
159 | */ |
||
160 | public function header($text, $level, $pos) |
||
173 | |||
174 | /** |
||
175 | * Open a paragraph |
||
176 | */ |
||
177 | public function p_open() |
||
181 | |||
182 | /** |
||
183 | * Close a paragraph |
||
184 | */ |
||
185 | public function p_close() |
||
189 | |||
190 | /** |
||
191 | * Create a line break |
||
192 | */ |
||
193 | public function linebreak() |
||
197 | |||
198 | /** |
||
199 | * Create a horizontal line |
||
200 | */ |
||
201 | public function hr() |
||
205 | |||
206 | /** |
||
207 | * Callback for footnote start syntax |
||
208 | * |
||
209 | * All following content will go to the footnote instead of |
||
210 | * the document. To achieve this the previous rendered content |
||
211 | * is moved to $store and $doc is cleared |
||
212 | * |
||
213 | * @author Andreas Gohr <[email protected]> |
||
214 | */ |
||
215 | public function footnote_open() |
||
227 | |||
228 | /** |
||
229 | * Callback for footnote end syntax |
||
230 | * |
||
231 | * All content rendered whilst within footnote syntax mode is discarded, |
||
232 | * the previously rendered content is restored and capturing is re-enabled. |
||
233 | * |
||
234 | * @author Andreas Gohr |
||
235 | */ |
||
236 | public function footnote_close() |
||
246 | |||
247 | /** |
||
248 | * Open an unordered list |
||
249 | */ |
||
250 | public function listu_open() |
||
254 | |||
255 | /** |
||
256 | * Open an ordered list |
||
257 | */ |
||
258 | public function listo_open() |
||
262 | |||
263 | /** |
||
264 | * Open a list item |
||
265 | * |
||
266 | * @param int $level the nesting level |
||
267 | * @param bool $node true when a node; false when a leaf |
||
268 | */ |
||
269 | public function listitem_open($level, $node=false) |
||
273 | |||
274 | /** |
||
275 | * Close a list item |
||
276 | */ |
||
277 | public function listitem_close() |
||
281 | |||
282 | /** |
||
283 | * Output preformatted text |
||
284 | * |
||
285 | * @param string $text |
||
286 | */ |
||
287 | public function preformatted($text) |
||
291 | |||
292 | /** |
||
293 | * Start a block quote |
||
294 | */ |
||
295 | public function quote_open() |
||
299 | |||
300 | /** |
||
301 | * Stop a block quote |
||
302 | */ |
||
303 | public function quote_close() |
||
307 | |||
308 | /** |
||
309 | * Display text as file content, optionally syntax highlighted |
||
310 | * |
||
311 | * @param string $text text to show |
||
312 | * @param string $lang programming language to use for syntax highlighting |
||
313 | * @param string $file file path label |
||
314 | */ |
||
315 | public function file($text, $lang = null, $file = null) |
||
319 | |||
320 | /** |
||
321 | * Display text as code content, optionally syntax highlighted |
||
322 | * |
||
323 | * @param string $text text to show |
||
324 | * @param string $language programming language to use for syntax highlighting |
||
325 | * @param string $file file path label |
||
326 | */ |
||
327 | public function code($text, $language = null, $file = null) |
||
331 | |||
332 | /** |
||
333 | * Format an acronym |
||
334 | * |
||
335 | * Uses $this->acronyms |
||
336 | * |
||
337 | * @param string $acronym |
||
338 | */ |
||
339 | public function acronym($acronym) |
||
343 | |||
344 | /** |
||
345 | * Format a smiley |
||
346 | * |
||
347 | * Uses $this->smiley |
||
348 | * |
||
349 | * @param string $smiley |
||
350 | */ |
||
351 | public function smiley($smiley) |
||
355 | |||
356 | /** |
||
357 | * Format an entity |
||
358 | * |
||
359 | * Entities are basically small text replacements |
||
360 | * |
||
361 | * Uses $this->entities |
||
362 | * |
||
363 | * @param string $entity |
||
364 | */ |
||
365 | public function entity($entity) |
||
369 | |||
370 | /** |
||
371 | * Typographically format a multiply sign |
||
372 | * |
||
373 | * Example: ($x=640, $y=480) should result in "640×480" |
||
374 | * |
||
375 | * @param string|int $x first value |
||
376 | * @param string|int $y second value |
||
377 | */ |
||
378 | public function multiplyentity($x, $y) |
||
382 | |||
383 | /** |
||
384 | * Render an opening single quote char (language specific) |
||
385 | */ |
||
386 | public function singlequoteopening() |
||
391 | |||
392 | /** |
||
393 | * Render a closing single quote char (language specific) |
||
394 | */ |
||
395 | public function singlequoteclosing() |
||
400 | |||
401 | /** |
||
402 | * Render an apostrophe char (language specific) |
||
403 | */ |
||
404 | public function apostrophe() |
||
409 | |||
410 | /** |
||
411 | * Render an opening double quote char (language specific) |
||
412 | */ |
||
413 | public function doublequoteopening() |
||
418 | |||
419 | /** |
||
420 | * Render an closinging double quote char (language specific) |
||
421 | */ |
||
422 | public function doublequoteclosing() |
||
427 | |||
428 | /** |
||
429 | * Render a CamelCase link |
||
430 | * |
||
431 | * @param string $link The link name |
||
432 | * @see http://en.wikipedia.org/wiki/CamelCase |
||
433 | */ |
||
434 | public function camelcaselink($link) |
||
438 | |||
439 | /** |
||
440 | * Render a page local link |
||
441 | * |
||
442 | * @param string $hash hash link identifier |
||
443 | * @param string $name name for the link |
||
444 | */ |
||
445 | public function locallink($hash, $name = null) |
||
454 | |||
455 | /** |
||
456 | * keep track of internal links in $this->meta['relation']['references'] |
||
457 | * |
||
458 | * @param string $id page ID to link to. eg. 'wiki:syntax' |
||
459 | * @param string|array|null $name name for the link, array for media file |
||
460 | */ |
||
461 | public function internallink($id, $name = null) |
||
494 | |||
495 | /** |
||
496 | * Render an external link |
||
497 | * |
||
498 | * @param string $url full URL with scheme |
||
499 | * @param string|array|null $name name for the link, array for media file |
||
500 | */ |
||
501 | public function externallink($url, $name = null) |
||
514 | |||
515 | /** |
||
516 | * Render an interwiki link |
||
517 | * |
||
518 | * You may want to use $this->_resolveInterWiki() here |
||
519 | * |
||
520 | * @param string $match original link - probably not much use |
||
521 | * @param string|array $name name for the link, array for media file |
||
522 | * @param string $wikiName indentifier (shortcut) for the remote wiki |
||
523 | * @param string $wikiUri the fragment parsed from the original link |
||
524 | */ |
||
525 | public function interwikilink($match, $name, $wikiName, $wikiUri) |
||
540 | |||
541 | /** |
||
542 | * Link to windows share |
||
543 | * |
||
544 | * @param string $url the link |
||
545 | * @param string|array $name name for the link, array for media file |
||
546 | */ |
||
547 | public function windowssharelink($url, $name = null) |
||
564 | |||
565 | /** |
||
566 | * Render a linked E-Mail Address |
||
567 | * |
||
568 | * Should honor $conf['mailguard'] setting |
||
569 | * |
||
570 | * @param string $address Email-Address |
||
571 | * @param string|array $name name for the link, array for media file |
||
572 | */ |
||
573 | public function emaillink($address, $name = null) |
||
590 | |||
591 | /** |
||
592 | * Render an internal media file |
||
593 | * |
||
594 | * @param string $src media ID |
||
595 | * @param string $title descriptive text |
||
596 | * @param string $align left|center|right |
||
597 | * @param int $width width of media in pixel |
||
598 | * @param int $height height of media in pixel |
||
599 | * @param string $cache cache|recache|nocache |
||
600 | * @param string $linking linkonly|detail|nolink |
||
601 | */ |
||
602 | public function internalmedia($src, $title = null, $align = null, $width = null, |
||
611 | |||
612 | /** |
||
613 | * Render an external media file |
||
614 | * |
||
615 | * @param string $src full media URL |
||
616 | * @param string $title descriptive text |
||
617 | * @param string $align left|center|right |
||
618 | * @param int $width width of media in pixel |
||
619 | * @param int $height height of media in pixel |
||
620 | * @param string $cache cache|recache|nocache |
||
621 | * @param string $linking linkonly|detail|nolink |
||
622 | */ |
||
623 | public function externalmedia($src, $title = null, $align = null, $width = null, |
||
631 | |||
632 | /** |
||
633 | * Render the output of an RSS feed |
||
634 | * |
||
635 | * @param string $url URL of the feed |
||
636 | * @param array $params Finetuning of the output |
||
637 | */ |
||
638 | public function rss($url, $params) |
||
647 | |||
648 | #region Utils |
||
649 | |||
650 | /** |
||
651 | * Removes any Namespace from the given name but keeps |
||
652 | * casing and special chars |
||
653 | * |
||
654 | * @author Andreas Gohr <[email protected]> |
||
655 | * |
||
656 | * @param string $name |
||
657 | * |
||
658 | * @return mixed|string |
||
659 | */ |
||
660 | public function _simpleTitle($name) |
||
678 | |||
679 | /** |
||
680 | * Construct a title and handle images in titles |
||
681 | * |
||
682 | * @author Harry Fuecks <[email protected]> |
||
683 | * @param string|array|null $title either string title or media array |
||
684 | * @param string $default default title if nothing else is found |
||
685 | * @param null|string $id linked page id (used to extract title from first heading) |
||
686 | * @return string title text |
||
687 | */ |
||
688 | public function _getLinkTitle($title, $default, $id = null) |
||
708 | |||
709 | /** |
||
710 | * Remember first image |
||
711 | * |
||
712 | * @param string $src image URL or ID |
||
713 | */ |
||
714 | protected function _firstimage($src) |
||
730 | |||
731 | /** |
||
732 | * Store list of used media files in metadata |
||
733 | * |
||
734 | * @param string $src media ID |
||
735 | */ |
||
736 | protected function _recordMediaUsage($src) |
||
747 | |||
748 | #endregion |
||
749 | } |
||
750 | |||
752 |