Exemple #1
0
 /**
  * Return an array of objects representing search results.
  *
  * See: http://www.bugzilla.org/docs/4.2/en/html/api/Bugzilla/WebService/Bug.html#search
  *
  * @param array $params search fields => values
  * @return array of stdClass bug objects for given search
  * @return boolean false if search failed altogether (I think)
  */
 public function search(array $params, $return_count_only = false)
 {
     $client = new Client($this->_uri . 'jsonrpc.cgi');
     try {
         $result = $client->__call('Bug.search', array($params));
     } catch (\Exception $e) {
         Fail::log($e);
         return false;
     }
     if ($return_count_only) {
         return count($result['bugs']);
     }
     $bugs = array();
     for ($i = 0, $c = count($result['bugs']); $i < $c; ++$i) {
         $bugs[] = (object) $result['bugs'][$i];
     }
     return $bugs;
 }
Exemple #2
0
 /**
  * Set a Content-Type
  *
  * @param $type string content type
  * @return string content type
  */
 protected function setType($type)
 {
     if ($type !== 'text/html') {
         Fail::$errorLogAll = true;
     }
     // suppress some pesky HTML generation
     $this->_type = $type;
     return $type;
 }
Exemple #3
0
 /**
  * Executes an array of statements
  *
  * Will catch and throw PDO errors
  *
  * All statments are executed with the same database connection.
  *
  * @param $sql_array Array of sql statements.
  */
 public static function exec_statements($sql_array)
 {
     $dbh = self::pdo();
     $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
     foreach ($sql_array as $stmt) {
         try {
             $dbh->exec($stmt);
         } catch (PDOException $e) {
             Fail::log($e);
         }
     }
 }
Exemple #4
0
 /**
  * Performs a jsonRCP request and gets the results as an array
  *
  * @param string $method
  * @param array $params
  * @return array
  */
 public function __call($method, $params)
 {
     // check
     if (!is_scalar($method)) {
         throw new Exception('Method name has no scalar value');
     }
     // check
     if (is_array($params)) {
         // no keys
         // TODO: this bit strikes me as broken:
         // $params = array_values($params);
     } else {
         throw new Exception('Params must be given as array');
     }
     // sets notification or request task
     if ($this->notification) {
         $currentId = null;
     } else {
         $currentId = $this->id;
     }
     // prepares the request
     $request = array('method' => $method, 'params' => $params, 'id' => $currentId);
     $request = json_encode($request);
     $this->debug && Fail::log('***** Request *****' . "\n" . $request . "\n" . '***** End Of request *****' . "\n\n");
     // performs the HTTP POST
     $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/json', 'content' => $request));
     $context = stream_context_create($opts);
     if ($fp = fopen($this->url, 'r', false, $context)) {
         stream_set_timeout($fp, $this->timeout);
         $response = '';
         while ($row = fgets($fp)) {
             $stream_info = stream_get_meta_data($fp);
             if ($stream_info['timed_out']) {
                 throw new Exception('timed out reading from server');
             }
             $response .= trim($row) . "\n";
         }
         $this->debug && Fail::log('***** Server response *****' . "\n" . $response . '***** End of server response *****' . "\n");
         $response = json_decode($response, true);
     } else {
         throw new Exception('Unable to connect to ' . $this->url);
     }
     // final checks and return
     if (!$this->notification) {
         // check
         if ($response['id'] != $currentId) {
             throw new Exception('Incorrect response id (request id: ' . $currentId . ', response id: ' . $response['id'] . ')');
         }
         if (!is_null($response['error'])) {
             throw new Exception('Request error: ' . print_r($response['error'], 1));
         }
         return $response['result'];
     } else {
         return true;
     }
 }
Exemple #5
0
 /**
  * Render a template with an alternative variable substitution
  * syntax, rather than executing it as PHP.
  *
  * Unless you're doing some kind of PHP code generation, you
  * probably don't want or need to use this.
  */
 public function preprocess($template = null)
 {
     // Use $this->_activeTemplate to avoid collisions with variables
     // extracted into the current scope from $this->_context
     $this->_activeTemplate = $template ?: $this->_template;
     try {
         $output = \file_get_contents($this->_templateDir . \DIRECTORY_SEPARATOR . $this->_activeTemplate);
         $this->_activeTemplate = null;
         $pattern = array();
         $replace = array();
         foreach ($this->_context as $key => $val) {
             if (\is_string($val)) {
                 $pattern[] = '/::' . $key . '::/';
                 $replace[] = $val;
             }
         }
         $output = \preg_replace($pattern, $replace, $output);
         return $output;
     } catch (\Exception $e) {
         Fail::log($e);
         throw $e;
     }
 }
Exemple #6
0
 /**
  * Load data based on an id.
  */
 protected function loadFromId($id)
 {
     // Handle caching
     $values = $this->_useCache ? $this->getCache($id) : null;
     // Cache values after loading?
     $savecache = false;
     if (!$values) {
         $savecache = true;
         $dbh = DB::getInstance();
         $tn = static::$_tableName;
         $tk = static::$_tableKey;
         $query = "SELECT * FROM \"{$tn}\" WHERE \"{$tk}\" = :id;";
         try {
             $sth = $dbh->prepare($query);
             $sth->execute(['id' => $id]);
             if (true === constant('\\DB_LOGQUERIES')) {
                 \SparkLib\Fail::log($sth->queryString, 'info');
             }
         } catch (Exception $e) {
             throw new SparkRecordException("Query failed: {$query}: " . $e->getMessage());
         }
         $count = $sth->rowCount();
         if ($count != 1) {
             throw new SparkRecordException("{$count} record(s) found using {$query} - id should be unique, id given: {$id}");
         }
         $values = $sth->fetch(\PDO::FETCH_ASSOC);
     }
     $this->loadFromRow($values);
     if ($this->_useCache && $savecache) {
         $this->setCache();
     }
 }
Exemple #7
0
 /**
  * Talk to our friend, the database, and stash results appropriately.
  */
 protected function doQuery()
 {
     if (constant('DB_LOGQUERIES')) {
         \SparkLib\Fail::log($this->_query);
         if (is_array($this->_params)) {
             \SparkLib\Fail::log($this->_params);
         }
     }
     try {
         // Get a resultset and stash it:
         if (is_array($this->_params)) {
             $this->_resultSet = $this->getDBH()->prepare($this->_query);
             $this->_resultSet->execute($this->_params);
         } else {
             // If we have no parameters, there's no real purpose to prepping
             // the query, so we may as well spare ourselves the performance hit:
             $this->_resultSet = $this->getDBH()->query($this->_query);
         }
     } catch (\PDOException $e) {
         $param_str = '';
         if (isset($this->_params)) {
             $param_str = "Params: " . implode(', ', $this->_params) . "\n";
         }
         $message = 'Query failed in SparkFinder: ' . $e->getMessage() . "\n" . $param_str . "\nQuery: " . $this->_query;
         throw new Exception($message);
     }
     // Pre-load our first result:
     $this->_row = $this->_resultSet->fetch(\PDO::FETCH_ASSOC);
 }