KernelBugTrackerBridge.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. false,
  45. null,
  46. 0,
  47. null,
  48. true,
  49. true,
  50. DEFAULT_TARGET_CHARSET,
  51. false, // Do NOT remove line breaks
  52. DEFAULT_BR_TEXT,
  53. DEFAULT_SPAN_TEXT);
  54. if($html === false)
  55. returnServerError('Failed to load page!');
  56. // Store header information into private members
  57. $this->bugid = $html->find('#bugzilla-body', 0)->find('a', 0)->innertext;
  58. $this->bugdesc = $html->find('table.bugfields', 0)->find('tr', 0)->find('td', 0)->innertext;
  59. // Get and limit comments
  60. $comments = $html->find('div.bz_comment');
  61. if($limit > 0 && count($comments) > $limit) {
  62. $comments = array_slice($comments, count($comments) - $limit, $limit);
  63. }
  64. // Order comments
  65. switch($sorting) {
  66. case 'lf': $comments = array_reverse($comments, true);
  67. case 'of':
  68. default: // Nothing to do, keep original order
  69. }
  70. foreach($comments as $comment) {
  71. $comment = $this->inlineStyles($comment);
  72. $item = array();
  73. $item['uri'] = $this->getURI() . '#' . $comment->id;
  74. $item['author'] = $comment->find('span.bz_comment_user', 0)->innertext;
  75. $item['title'] = $comment->find('span.bz_comment_number', 0)->find('a', 0)->innertext;
  76. $item['timestamp'] = strtotime($comment->find('span.bz_comment_time', 0)->innertext);
  77. $item['content'] = $comment->find('pre.bz_comment_text', 0)->innertext;
  78. // Fix line breaks (they use LF)
  79. $item['content'] = str_replace("\n", '<br>', $item['content']);
  80. // Fix relative URIs
  81. $item['content'] = $this->replaceRelativeURI($item['content']);
  82. $this->items[] = $item;
  83. }
  84. }
  85. public function getURI(){
  86. switch($this->queriedContext) {
  87. case 'Bug comments':
  88. return parent::getURI()
  89. . '/show_bug.cgi?id='
  90. . $this->getInput('id');
  91. break;
  92. default: return parent::getURI();
  93. }
  94. }
  95. public function getName(){
  96. switch($this->queriedContext) {
  97. case 'Bug comments':
  98. return 'Bug '
  99. . $this->bugid
  100. . ' tracker for '
  101. . $this->bugdesc
  102. . ' - '
  103. . parent::getName();
  104. break;
  105. default: return parent::getName();
  106. }
  107. }
  108. /**
  109. * Replaces all relative URIs with absolute ones
  110. *
  111. * @param string $content The source string
  112. * @return string Returns the source string with all relative URIs replaced
  113. * by absolute ones.
  114. */
  115. private function replaceRelativeURI($content){
  116. return preg_replace('/href="(?!http)/', 'href="' . self::URI . '/', $content);
  117. }
  118. /**
  119. * Adds styles as attributes to tags with known classes
  120. *
  121. * @param object $html A simplehtmldom object
  122. * @return object Returns the original object with styles added as
  123. * attributes.
  124. */
  125. private function inlineStyles($html){
  126. foreach($html->find('.bz_obsolete') as $element) {
  127. $element->style = 'text-decoration:line-through;';
  128. }
  129. return $html;
  130. }
  131. }