| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
/** |
| 23 |
* Source manager for query builder. |
| 24 |
* |
| 25 |
* @category Visualizer |
| 26 |
* @package Source |
| 27 |
*/ |
| 28 |
class Visualizer_Source_Query extends Visualizer_Source { |
| 29 |
|
| 30 |
/** |
| 31 |
* The query. |
| 32 |
* |
| 33 |
* @access protected |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $_query; |
| 37 |
|
| 38 |
/** |
| 39 |
* The chart id. |
| 40 |
* |
| 41 |
* @access protected |
| 42 |
* @var int |
| 43 |
*/ |
| 44 |
protected $_chart_id; |
| 45 |
|
| 46 |
/** |
| 47 |
* Any additional parameters (e.g. for connecting to a remote db). |
| 48 |
* |
| 49 |
* @access protected |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $_params; |
| 53 |
|
| 54 |
/** |
| 55 |
* Constructor. |
| 56 |
* |
| 57 |
* @access public |
| 58 |
* @param string $query The query. |
| 59 |
* @param int $chart_id The chart id. |
| 60 |
* @param array $params Any additional parameters (e.g. for connecting to a remote db). |
| 61 |
*/ |
| 62 |
public function __construct( $query = null, $chart_id = null, $params = null ) { |
| 63 |
$this->_query = $query; |
| 64 |
$this->_chart_id = $chart_id; |
| 65 |
$this->_params = $params; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Fetches information from source, parses it and builds series and data arrays. |
| 70 |
* |
| 71 |
* @param bool $as_html Should the result be fetched as an HTML table or as an object. |
| 72 |
* @param bool $results_as_numeric_array Should the result be fetched as ARRAY_N instead of ARRAY_A. |
| 73 |
* @param bool $raw_results Should the result be returned without processing. |
| 74 |
* @access public |
| 75 |
* @return boolean TRUE on success, otherwise FALSE. |
| 76 |
*/ |
| 77 |
public function fetch( $as_html = false, $results_as_numeric_array = false, $raw_results = false ) { |
| 78 |
if ( empty( $this->_query ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// only select queries allowed. |
| 83 |
if ( preg_match( '/^\s*(insert|delete|update|replace|create|alter|drop|truncate)\s/i', $this->_query ) ) { |
| 84 |
$this->_error = __( 'Only SELECT queries are allowed', 'visualizer' ); |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
// impose a limit if no limit clause is provided. |
| 89 |
if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) { |
| 90 |
$this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000, $this->_chart_id ); |
| 91 |
} |
| 92 |
|
| 93 |
$this->_query = apply_filters( 'visualizer_db_query', $this->_query, $this->_chart_id, $this->_params ); |
| 94 |
|
| 95 |
$results = array(); |
| 96 |
$headers = array(); |
| 97 |
|
| 98 |
// short circuit results for remote dbs. |
| 99 |
if ( false !== ( $remote_results = apply_filters( 'visualizer_db_query_execute', false, $this->_query, $as_html, $results_as_numeric_array, $raw_results, $this->_chart_id, $this->_params ) ) ) { |
| 100 |
$error = $remote_results['error']; |
| 101 |
if ( empty( $error ) ) { |
| 102 |
$results = $remote_results['results']; |
| 103 |
$headers = $remote_results['headers']; |
| 104 |
} |
| 105 |
|
| 106 |
$this->_error = $error; |
| 107 |
|
| 108 |
if ( $raw_results ) { |
| 109 |
return $results; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! ( $results && $headers ) ) { |
| 114 |
global $wpdb; |
| 115 |
$wpdb->hide_errors(); |
| 116 |
// @codingStandardsIgnoreStart |
| 117 |
$rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A ); |
| 118 |
// @codingStandardsIgnoreEnd |
| 119 |
$wpdb->show_errors(); |
| 120 |
|
| 121 |
if ( $raw_results ) { |
| 122 |
return $rows; |
| 123 |
} |
| 124 |
|
| 125 |
if ( $rows ) { |
| 126 |
$results = array(); |
| 127 |
$headers = array(); |
| 128 |
if ( $rows ) { |
| 129 |
$row_num = 0; |
| 130 |
foreach ( $rows as $row ) { |
| 131 |
$result = array(); |
| 132 |
$col_num = 0; |
| 133 |
foreach ( $row as $k => $v ) { |
| 134 |
$result[] = $v; |
| 135 |
if ( 0 === $row_num ) { |
| 136 |
$headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k ); |
| 137 |
} |
| 138 |
} |
| 139 |
$results[] = $result; |
| 140 |
$row_num++; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
$this->_error = $wpdb->last_error; |
| 145 |
} |
| 146 |
} |
| 147 |
// Query log. |
| 148 |
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Firing query %s to get results %s with error %s', $this->_query, print_r( $rows, true ), print_r( $wpdb->last_error, true ) ), 'debug', __FILE__, __LINE__ ); |
| 149 |
|
| 150 |
if ( $as_html ) { |
| 151 |
$results = $this->html( $headers, $results ); |
| 152 |
} else { |
| 153 |
$results = $this->object( $headers, $results ); |
| 154 |
} |
| 155 |
|
| 156 |
return apply_filters( 'visualizer_db_query_results', $results, $headers, $as_html, $results_as_numeric_array, $raw_results, $this->_query, $this->_chart_id, $this->_params ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get the data type of the column. |
| 161 |
* |
| 162 |
* @param int $col_num The column index in the fetched result set. |
| 163 |
* @access private |
| 164 |
* @return int |
| 165 |
*/ |
| 166 |
private function get_col_type( $col_num ) { |
| 167 |
global $wpdb; |
| 168 |
switch ( $wpdb->get_col_info( 'type', $col_num ) ) { |
| 169 |
case 0: |
| 170 |
case 5: |
| 171 |
case 4: |
| 172 |
case 9: |
| 173 |
case 3: |
| 174 |
case 2: |
| 175 |
case 246: |
| 176 |
case 8: |
| 177 |
// numeric. |
| 178 |
return 'number'; |
| 179 |
case 10: |
| 180 |
case 12: |
| 181 |
case 14: |
| 182 |
// date. |
| 183 |
return 'date'; |
| 184 |
} |
| 185 |
return 'string'; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns the HTML output. |
| 190 |
* |
| 191 |
* @param array $headers The headers of the result set. |
| 192 |
* @param array $results The data of the result set. |
| 193 |
* @access private |
| 194 |
* @return string |
| 195 |
*/ |
| 196 |
private function html( $headers, $results ) { |
| 197 |
return Visualizer_Render_Layout::show( 'db-wizard-results', $headers, $results ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Sets the series and data. |
| 202 |
* |
| 203 |
* @param array $headers The headers of the result set. |
| 204 |
* @param array $results The data of the result set. |
| 205 |
* @access private |
| 206 |
* @return bool |
| 207 |
*/ |
| 208 |
private function object( $headers, $results ) { |
| 209 |
$series = array(); |
| 210 |
foreach ( $headers as $header ) { |
| 211 |
$series[] = $header; |
| 212 |
} |
| 213 |
$this->_series = $series; |
| 214 |
|
| 215 |
$data = array(); |
| 216 |
foreach ( $results as $row ) { |
| 217 |
$data[] = $this->_normalizeData( $row ); |
| 218 |
} |
| 219 |
$this->_data = $data; |
| 220 |
return $this->_data; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns the final query. |
| 225 |
* |
| 226 |
* @access public |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
public function get_query() { |
| 230 |
return $this->_query; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Returns source name. |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
* |
| 238 |
* @access public |
| 239 |
* @return string The name of source. |
| 240 |
*/ |
| 241 |
public function getSourceName() { |
| 242 |
return __CLASS__; |
| 243 |
} |
| 244 |
} |
| 245 |
|