A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.6 KiB

  1. <?php
  2. namespace SparkLib;
  3. use SparkLib\Fail;
  4. use SparkLib\jsonRPC\Client;
  5. /**
  6. * Wrap various API calls to a Bugzilla installation.
  7. */
  8. class Bugzilla {
  9. protected $_uri = null;
  10. // TODO
  11. protected $_user = null;
  12. protected $_passwd = null;
  13. /**
  14. * Get an instance of Bugzilla for the given installation URI.
  15. */
  16. public function __construct ($uri)
  17. {
  18. $this->_uri = $uri;
  19. }
  20. /**
  21. * Return an object representing a single bug.
  22. *
  23. * @param $id integer bug id
  24. * @return stdClass bug for given id
  25. * @return false if no such bug found
  26. */
  27. public function bug ($id)
  28. {
  29. $client = new Client($this->_uri . 'jsonrpc.cgi');
  30. try {
  31. $result = $client->__call('Bug.get', array(array('ids' => array($id))));
  32. } catch (\Exception $e) {
  33. Fail::log($e);
  34. return false;
  35. }
  36. // send back a stdClass for the first bug in the results
  37. return (object)($result['bugs'][0]);
  38. }
  39. /**
  40. * Return an array of objects representing search results.
  41. *
  42. * See: http://www.bugzilla.org/docs/4.2/en/html/api/Bugzilla/WebService/Bug.html#search
  43. *
  44. * @param array $params search fields => values
  45. * @return array of stdClass bug objects for given search
  46. * @return boolean false if search failed altogether (I think)
  47. */
  48. public function search (array $params, $return_count_only = false)
  49. {
  50. $client = new Client($this->_uri . 'jsonrpc.cgi');
  51. try {
  52. $result = ($client->__call('Bug.search', array($params)));
  53. } catch (\Exception $e) {
  54. Fail::log($e);
  55. return false;
  56. }
  57. if ($return_count_only)
  58. return count($result['bugs']);
  59. $bugs = array();
  60. for($i = 0, $c = count($result['bugs']); $i < $c; ++$i) {
  61. $bugs[] = (object)$result['bugs'][$i];
  62. }
  63. return $bugs;
  64. }
  65. /**
  66. * Sort and return an array of objects representing search results.
  67. *
  68. * Usage: Bugzilla::sortBugs($bugs, 'priority', SORT_ASC, 'last_change_time', SORT_DESC);
  69. * ...where $bugs is an array of stdClass objects representing bugs (e.g. the output of Bugzilla->search())
  70. *
  71. * @return array of stdClass bug objects for given search
  72. */
  73. public static function sortBugs ()
  74. {
  75. // Pull unsorted bug array off the front of the argument list
  76. $args = func_get_args();
  77. $bugs = array_shift($args);
  78. // Trivial case: kick back out if there are no bugs to sort
  79. if (! count($bugs))
  80. return $bugs;
  81. // Build the sort arrays!
  82. //
  83. // We start with an unsorted array ($bugs) and an alternating list of fields and directions (priority, SORT_ASC...)
  84. // For array_multisort we want a single array of arguments that looks like this:
  85. //
  86. // [ [Bug 0 field 0 value, Bug 1 field 0 value, ...], direction 0, [Bug 0 field 1 value, Bug 1 field 1 value, ...], direction 1, ... ]
  87. //
  88. // We do this in place by walking our array of arguments (which is already in the right order)
  89. // and turning each field name string into its corresponding array of field values from the unsorted bug list
  90. foreach ($args as $n => $field) {
  91. if (is_string($field)) {
  92. if (!strlen($field) || !isset($bugs[0]->$field))
  93. throw new Exception ("Invalid sort field: " . $field);
  94. $tmp = array();
  95. foreach ($bugs as $b => $bug)
  96. $tmp[$b] = $bug->$field;
  97. $args[$n] = $tmp;
  98. }
  99. }
  100. // Attach the unsorted bug list by reference onto the end of our arguments array.
  101. // array_multisort sorts arguments in order, so the last argument needs to be the master unserted list.
  102. // It also sorts arguments in place, hence why they must be passed by reference.
  103. $args[] = &$bugs;
  104. call_user_func_array('array_multisort', $args);
  105. return array_pop($args);
  106. }
  107. /**
  108. * Search for a substring in a custom field. Returns an array of simple
  109. * bug objects extracted from CSV. (XML can suck it.)
  110. *
  111. * @param string $field to search
  112. * @param string $string to search for
  113. * @return array of stdClass bug objects
  114. */
  115. public function searchCustomField ($field, $string, $new_only = false)
  116. {
  117. $search_url = 'buglist.cgi?query_format=advanced&f1=cf_'
  118. . urlencode($field)
  119. . '&v1='
  120. . urlencode($string)
  121. . '&o1=regexp&order=Bug&ctype=csv';
  122. if ($new_only) {
  123. $search_url .= '&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED';
  124. }
  125. $csv = file_get_contents($this->_uri . $search_url);
  126. $bugs = array();
  127. $rows = explode("\n", $csv);
  128. $fields = str_getcsv(array_shift($rows));
  129. foreach ($rows as &$row) {
  130. $values = str_getcsv($row);
  131. $bug = array();
  132. foreach ($values as $idx => $value) {
  133. $bug[ $fields[$idx] ] = $value;
  134. }
  135. $bugs[] = (object)$bug; // make a stdClass instance
  136. }
  137. return $bugs;
  138. }
  139. /**
  140. * Return a list of open bugs CC'd to a user.
  141. *
  142. * @param string $user
  143. * @return array of stdClass bug objects
  144. */
  145. public function searchCC ($user)
  146. {
  147. $search_url = 'buglist.cgi?query_format=advanced&emailcc1=1'
  148. . '&email1=' . urlencode($user)
  149. . '&emailtype1=substring&order=Bug&ctype=csv'
  150. . '&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED';
  151. $csv = file_get_contents($this->_uri . $search_url);
  152. $bugs = array();
  153. $rows = explode("\n", $csv);
  154. $fields = str_getcsv(array_shift($rows));
  155. foreach ($rows as &$row) {
  156. $values = str_getcsv($row);
  157. $bug = array();
  158. foreach ($values as $idx => $value) {
  159. $bug[ $fields[$idx] ] = $value;
  160. }
  161. $bugs[] = (object)$bug; // make a stdClass instance
  162. }
  163. return $bugs;
  164. }
  165. }