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 | /** the approximate byte lenght to capture for the abstract */ |
||
| 15 | const ABSTRACT_LEN = 250; |
||
| 16 | |||
| 17 | /** the maximum UTF8 character length for the abstract */ |
||
| 18 | const ABSTRACT_MAX = 500; |
||
| 19 | |||
| 20 | /** @var array transient meta data, will be reset on each rendering */ |
||
| 21 | public $meta = array(); |
||
| 22 | |||
| 23 | /** @var array persistent meta data, will be kept until explicitly deleted */ |
||
| 24 | public $persistent = array(); |
||
| 25 | |||
| 26 | /** @var array the list of headers used to create unique link ids */ |
||
| 27 | protected $headers = array(); |
||
| 28 | |||
| 29 | /** @var string temporary $doc store */ |
||
| 30 | protected $store = ''; |
||
| 31 | |||
| 32 | /** @var string keeps the first image reference */ |
||
| 33 | protected $firstimage = ''; |
||
| 34 | |||
| 35 | /** @var bool determines if enough data for the abstract was collected, yet */ |
||
| 36 | public $capture = true; |
||
| 37 | |||
| 38 | /** @var int number of bytes captured for abstract */ |
||
| 39 | protected $captured = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Returns the format produced by this renderer. |
||
| 43 | * |
||
| 44 | * @return string always 'metadata' |
||
| 45 | */ |
||
| 46 | public function getFormat() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Initialize the document |
||
| 52 | * |
||
| 53 | * Sets up some of the persistent info about the page if it doesn't exist, yet. |
||
| 54 | */ |
||
| 55 | public function document_start() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Finalize the document |
||
| 76 | * |
||
| 77 | * Stores collected data in the metadata |
||
| 78 | */ |
||
| 79 | public function document_end() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Render plain text data |
||
| 104 | * |
||
| 105 | * This function takes care of the amount captured data and will stop capturing when |
||
| 106 | * enough abstract data is available |
||
| 107 | * |
||
| 108 | * @param $text |
||
| 109 | */ |
||
| 110 | public function cdata($text) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add an item to the TOC |
||
| 121 | * |
||
| 122 | * @param string $id the hash link |
||
| 123 | * @param string $text the text to display |
||
| 124 | * @param int $level the nesting level |
||
| 125 | */ |
||
| 126 | public function toc_additem($id, $text, $level) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Render a heading |
||
| 144 | * |
||
| 145 | * @param string $text the text to display |
||
| 146 | * @param int $level header level |
||
| 147 | * @param int $pos byte position in the original source |
||
| 148 | */ |
||
| 149 | public function header($text, $level, $pos) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Open a paragraph |
||
| 162 | */ |
||
| 163 | public function p_open() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Close a paragraph |
||
| 169 | */ |
||
| 170 | public function p_close() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Create a line break |
||
| 176 | */ |
||
| 177 | public function linebreak() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Create a horizontal line |
||
| 183 | */ |
||
| 184 | public function hr() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Callback for footnote start syntax |
||
| 190 | * |
||
| 191 | * All following content will go to the footnote instead of |
||
| 192 | * the document. To achieve this the previous rendered content |
||
| 193 | * is moved to $store and $doc is cleared |
||
| 194 | * |
||
| 195 | * @author Andreas Gohr <[email protected]> |
||
| 196 | */ |
||
| 197 | public function footnote_open() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Callback for footnote end syntax |
||
| 207 | * |
||
| 208 | * All rendered content is moved to the $footnotes array and the old |
||
| 209 | * content is restored from $store again |
||
| 210 | * |
||
| 211 | * @author Andreas Gohr |
||
| 212 | */ |
||
| 213 | public function footnote_close() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Open an unordered list |
||
| 223 | */ |
||
| 224 | public function listu_open() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Open an ordered list |
||
| 230 | */ |
||
| 231 | public function listo_open() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Open a list item |
||
| 237 | * |
||
| 238 | * @param int $level the nesting level |
||
| 239 | * @param bool $node true when a node; false when a leaf |
||
| 240 | */ |
||
| 241 | public function listitem_open($level,$node=false) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Close a list item |
||
| 247 | */ |
||
| 248 | public function listitem_close() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Output preformatted text |
||
| 254 | * |
||
| 255 | * @param string $text |
||
| 256 | */ |
||
| 257 | public function preformatted($text) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Start a block quote |
||
| 263 | */ |
||
| 264 | public function quote_open() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Stop a block quote |
||
| 270 | */ |
||
| 271 | public function quote_close() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Display text as file content, optionally syntax highlighted |
||
| 277 | * |
||
| 278 | * @param string $text text to show |
||
| 279 | * @param string $lang programming language to use for syntax highlighting |
||
| 280 | * @param string $file file path label |
||
| 281 | */ |
||
| 282 | public function file($text, $lang = null, $file = null) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Display text as code content, optionally syntax highlighted |
||
| 288 | * |
||
| 289 | * @param string $text text to show |
||
| 290 | * @param string $language programming language to use for syntax highlighting |
||
| 291 | * @param string $file file path label |
||
| 292 | */ |
||
| 293 | public function code($text, $language = null, $file = null) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Format an acronym |
||
| 299 | * |
||
| 300 | * Uses $this->acronyms |
||
| 301 | * |
||
| 302 | * @param string $acronym |
||
| 303 | */ |
||
| 304 | public function acronym($acronym) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Format a smiley |
||
| 310 | * |
||
| 311 | * Uses $this->smiley |
||
| 312 | * |
||
| 313 | * @param string $smiley |
||
| 314 | */ |
||
| 315 | public function smiley($smiley) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Format an entity |
||
| 321 | * |
||
| 322 | * Entities are basically small text replacements |
||
| 323 | * |
||
| 324 | * Uses $this->entities |
||
| 325 | * |
||
| 326 | * @param string $entity |
||
| 327 | */ |
||
| 328 | public function entity($entity) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Typographically format a multiply sign |
||
| 334 | * |
||
| 335 | * Example: ($x=640, $y=480) should result in "640×480" |
||
| 336 | * |
||
| 337 | * @param string|int $x first value |
||
| 338 | * @param string|int $y second value |
||
| 339 | */ |
||
| 340 | public function multiplyentity($x, $y) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Render an opening single quote char (language specific) |
||
| 346 | */ |
||
| 347 | public function singlequoteopening() { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Render a closing single quote char (language specific) |
||
| 354 | */ |
||
| 355 | public function singlequoteclosing() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Render an apostrophe char (language specific) |
||
| 362 | */ |
||
| 363 | public function apostrophe() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Render an opening double quote char (language specific) |
||
| 370 | */ |
||
| 371 | public function doublequoteopening() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Render an closinging double quote char (language specific) |
||
| 378 | */ |
||
| 379 | public function doublequoteclosing() { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Render a CamelCase link |
||
| 386 | * |
||
| 387 | * @param string $link The link name |
||
| 388 | * @see http://en.wikipedia.org/wiki/CamelCase |
||
| 389 | */ |
||
| 390 | public function camelcaselink($link) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Render a page local link |
||
| 396 | * |
||
| 397 | * @param string $hash hash link identifier |
||
| 398 | * @param string $name name for the link |
||
| 399 | */ |
||
| 400 | public function locallink($hash, $name = null) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * keep track of internal links in $this->meta['relation']['references'] |
||
| 409 | * |
||
| 410 | * @param string $id page ID to link to. eg. 'wiki:syntax' |
||
| 411 | * @param string|array|null $name name for the link, array for media file |
||
| 412 | */ |
||
| 413 | public function internallink($id, $name = null) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Render an external link |
||
| 446 | * |
||
| 447 | * @param string $url full URL with scheme |
||
| 448 | * @param string|array|null $name name for the link, array for media file |
||
| 449 | */ |
||
| 450 | public function externallink($url, $name = null) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Render an interwiki link |
||
| 463 | * |
||
| 464 | * You may want to use $this->_resolveInterWiki() here |
||
| 465 | * |
||
| 466 | * @param string $match original link - probably not much use |
||
| 467 | * @param string|array $name name for the link, array for media file |
||
| 468 | * @param string $wikiName indentifier (shortcut) for the remote wiki |
||
| 469 | * @param string $wikiUri the fragment parsed from the original link |
||
| 470 | */ |
||
| 471 | public function interwikilink($match, $name, $wikiName, $wikiUri) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Link to windows share |
||
| 486 | * |
||
| 487 | * @param string $url the link |
||
| 488 | * @param string|array $name name for the link, array for media file |
||
| 489 | */ |
||
| 490 | public function windowssharelink($url, $name = null) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Render a linked E-Mail Address |
||
| 504 | * |
||
| 505 | * Should honor $conf['mailguard'] setting |
||
| 506 | * |
||
| 507 | * @param string $address Email-Address |
||
| 508 | * @param string|array $name name for the link, array for media file |
||
| 509 | */ |
||
| 510 | public function emaillink($address, $name = null) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Render an internal media file |
||
| 524 | * |
||
| 525 | * @param string $src media ID |
||
| 526 | * @param string $title descriptive text |
||
| 527 | * @param string $align left|center|right |
||
| 528 | * @param int $width width of media in pixel |
||
| 529 | * @param int $height height of media in pixel |
||
| 530 | * @param string $cache cache|recache|nocache |
||
| 531 | * @param string $linking linkonly|detail|nolink |
||
| 532 | */ |
||
| 533 | public function internalmedia($src, $title = null, $align = null, $width = null, |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Render an external media file |
||
| 542 | * |
||
| 543 | * @param string $src full media URL |
||
| 544 | * @param string $title descriptive text |
||
| 545 | * @param string $align left|center|right |
||
| 546 | * @param int $width width of media in pixel |
||
| 547 | * @param int $height height of media in pixel |
||
| 548 | * @param string $cache cache|recache|nocache |
||
| 549 | * @param string $linking linkonly|detail|nolink |
||
| 550 | */ |
||
| 551 | public function externalmedia($src, $title = null, $align = null, $width = null, |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Render the output of an RSS feed |
||
| 559 | * |
||
| 560 | * @param string $url URL of the feed |
||
| 561 | * @param array $params Finetuning of the output |
||
| 562 | */ |
||
| 563 | public function rss($url, $params) { |
||
| 571 | |||
| 572 | #region Utils |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Removes any Namespace from the given name but keeps |
||
| 576 | * casing and special chars |
||
| 577 | * |
||
| 578 | * @author Andreas Gohr <[email protected]> |
||
| 579 | * |
||
| 580 | * @param string $name |
||
| 581 | * |
||
| 582 | * @return mixed|string |
||
| 583 | */ |
||
| 584 | public function _simpleTitle($name) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Construct a title and handle images in titles |
||
| 602 | * |
||
| 603 | * @author Harry Fuecks <[email protected]> |
||
| 604 | * @param string|array|null $title either string title or media array |
||
| 605 | * @param string $default default title if nothing else is found |
||
| 606 | * @param null|string $id linked page id (used to extract title from first heading) |
||
| 607 | * @return string title text |
||
| 608 | */ |
||
| 609 | public function _getLinkTitle($title, $default, $id = null) { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Remember first image |
||
| 629 | * |
||
| 630 | * @param string $src image URL or ID |
||
| 631 | */ |
||
| 632 | protected function _firstimage($src) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Store list of used media files in metadata |
||
| 647 | * |
||
| 648 | * @param string $src media ID |
||
| 649 | */ |
||
| 650 | protected function _recordMediaUsage($src) { |
||
| 658 | |||
| 659 | #endregion |
||
| 660 | } |
||
| 661 | |||
| 663 |