KernelBugTrackerBridge.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. class KernelBugTrackerBridge extends BridgeAbstract {
  3. const NAME = 'Kernel Bug Tracker';
  4. const URI = 'https://bugzilla.kernel.org';
  5. const DESCRIPTION = 'Returns feeds for bug comments';
  6. const MAINTAINER = 'logmanoriginal';
  7. const PARAMETERS = array(
  8. 'Bug comments' => array(
  9. 'id' => array(
  10. 'name' => 'Bug tracking ID',
  11. 'type' => 'number',
  12. 'required' => true,
  13. 'title' => 'Insert bug tracking ID',
  14. 'exampleValue' => 121241
  15. ),
  16. 'limit' => array(
  17. 'name' => 'Number of comments to return',
  18. 'type' => 'number',
  19. 'required' => false,
  20. 'title' => 'Specify number of comments to return',
  21. 'defaultValue' => -1
  22. ),
  23. 'sorting' => array(
  24. 'name' => 'Sorting',
  25. 'type' => 'list',
  26. 'required' => false,
  27. 'title' => 'Defines the sorting order of the comments returned',
  28. 'defaultValue' => 'of',
  29. 'values' => array(
  30. 'Oldest first' => 'of',
  31. 'Latest first' => 'lf'
  32. )
  33. )
  34. )
  35. );
  36. private $bugid = '';
  37. private $bugdesc = '';
  38. public function collectData(){
  39. $limit = $this->getInput('limit');
  40. $sorting = $this->getInput('sorting');
  41. // We use the print preview page for simplicity
  42. $html = getSimpleHTMLDOMCached($this->getURI() . '&format=multiple',
  43. 86400,
  44. null,
  45. null,
  46. true,
  47. true,
  48. DEFAULT_TARGET_CHARSET,
  49. false, // Do NOT remove line breaks
  50. DEFAULT_BR_TEXT,
  51. DEFAULT_SPAN_TEXT);
  52. if($html === false)
  53. returnServerError('Failed to load page!');
  54. // Store header information into private members
  55. $this->bugid = $html->find('#bugzilla-body', 0)->find('a', 0)->innertext;
  56. $this->bugdesc = $html->find('table.bugfields', 0)->find('tr', 0)->find('td', 0)->innertext;
  57. // Get and limit comments
  58. $comments = $html->find('div.bz_comment');
  59. if($limit > 0 && count($comments) > $limit) {
  60. $comments = array_slice($comments, count($comments) - $limit, $limit);
  61. }
  62. // Order comments
  63. switch($sorting) {
  64. case 'lf': $comments = array_reverse($comments, true);
  65. case 'of':
  66. default: // Nothing to do, keep original order
  67. }
  68. foreach($comments as $comment) {
  69. $comment = $this->inlineStyles($comment);
  70. $item = array();
  71. $item['uri'] = $this->getURI() . '#' . $comment->id;
  72. $item['author'] = $comment->find('span.bz_comment_user', 0)->innertext;
  73. $item['title'] = $comment->find('span.bz_comment_number', 0)->find('a', 0)->innertext;
  74. $item['timestamp'] = strtotime($comment->find('span.bz_comment_time', 0)->innertext);
  75. $item['content'] = $comment->find('pre.bz_comment_text', 0)->innertext;
  76. // Fix line breaks (they use LF)
  77. $item['content'] = str_replace("\n", '<br>', $item['content']);
  78. // Fix relative URIs
  79. $item['content'] = $this->replaceRelativeURI($item['content']);
  80. $this->items[] = $item;
  81. }
  82. }
  83. public function getURI(){
  84. switch($this->queriedContext) {
  85. case 'Bug comments':
  86. return parent::getURI()
  87. . '/show_bug.cgi?id='
  88. . $this->getInput('id');
  89. break;
  90. default: return parent::getURI();
  91. }
  92. }
  93. public function getName(){
  94. switch($this->queriedContext) {
  95. case 'Bug comments':
  96. return 'Bug '
  97. . $this->bugid
  98. . ' tracker for '
  99. . $this->bugdesc
  100. . ' - '
  101. . parent::getName();
  102. break;
  103. default: return parent::getName();
  104. }
  105. }
  106. /**
  107. * Replaces all relative URIs with absolute ones
  108. *
  109. * @param string $content The source string
  110. * @return string Returns the source string with all relative URIs replaced
  111. * by absolute ones.
  112. */
  113. private function replaceRelativeURI($content){
  114. return preg_replace('/href="(?!http)/', 'href="' . self::URI . '/', $content);
  115. }
  116. /**
  117. * Adds styles as attributes to tags with known classes
  118. *
  119. * @param object $html A simplehtmldom object
  120. * @return object Returns the original object with styles added as
  121. * attributes.
  122. */
  123. private function inlineStyles($html){
  124. foreach($html->find('.bz_obsolete') as $element) {
  125. $element->style = 'text-decoration:line-through;';
  126. }
  127. return $html;
  128. }
  129. }