From 48bb7c1734f2c28662b56aed9e11eb1d894eaa6a Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 10 Apr 2020 12:40:15 -0400 Subject: [PATCH] Add a new $database->columnExists() method, plus a couple other unrelated minor adjustments --- wire/core/Page.php | 4 +-- wire/core/Sanitizer.php | 5 ++-- wire/core/WireDatabasePDO.php | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/wire/core/Page.php b/wire/core/Page.php index 095a978d..0b4e383d 100644 --- a/wire/core/Page.php +++ b/wire/core/Page.php @@ -61,9 +61,9 @@ * @property int $published Unix timestamp of when the page was published. #pw-group-common #pw-group-date-time #pw-group-system * @property string $publishedStr Date/time when the page was published (formatted date/time string). #pw-group-date-time * @property int $created_users_id ID of created user. #pw-group-system - * @property User $createdUser The user that created this page. Returns a User or a NullPage. + * @property User|NullPage $createdUser The user that created this page. Returns a User or a NullPage. * @property int $modified_users_id ID of last modified user. #pw-group-system - * @property User $modifiedUser The user that last modified this page. Returns a User or a NullPage. + * @property User|NullPage $modifiedUser The user that last modified this page. Returns a User or a NullPage. * @property PagefilesManager $filesManager The object instance that manages files for this page. #pw-group-files * @property string $filesPath Get the disk path to store files for this page, creating it if it does not exist. #pw-group-files * @property string $filesUrl Get the URL to store files for this page, creating it if it does not exist. #pw-group-files diff --git a/wire/core/Sanitizer.php b/wire/core/Sanitizer.php index 88f73321..39e81487 100644 --- a/wire/core/Sanitizer.php +++ b/wire/core/Sanitizer.php @@ -2970,7 +2970,7 @@ class Sanitizer extends Wire { */ public function date($value, $format = null, array $options = array()) { $defaults = array( - 'returnFormat' => $format, // date format to return in, if different from $dateFormat + 'returnFormat' => $format, // date format to return in, if different from $format 'min' => '', // Minimum date allowed (in $dateFormat format, or a unix timestamp) 'max' => '', // Maximum date allowed (in $dateFormat format, or a unix timestamp) 'default' => null, // Default value, if date didn't resolve @@ -2978,13 +2978,14 @@ class Sanitizer extends Wire { ); $options = array_merge($defaults, $options); $datetime = $this->wire('datetime'); + $iso8601 = 'Y-m-d H:i:s'; $_value = trim($value); // original value string if(empty($value)) return $options['default']; if(!is_string($value) && !is_int($value)) $value = $this->string($value); if(ctype_digit("$value")) { // value is in unix timestamp format // make sure it resolves to a valid date - $value = strtotime(date('Y-m-d H:i:s', (int) $value)); + $value = strtotime(date($iso8601, (int) $value)); } else { /** @var WireDateTime $datetime */ $value = $datetime->stringToTimestamp($value, $format); diff --git a/wire/core/WireDatabasePDO.php b/wire/core/WireDatabasePDO.php index 85f4d527..f4146e7c 100644 --- a/wire/core/WireDatabasePDO.php +++ b/wire/core/WireDatabasePDO.php @@ -627,6 +627,59 @@ class WireDatabasePDO extends Wire implements WireDatabase { return !empty($result); } + /** + * Does the given column exist in given table? + * + * ~~~~~ + * // Standard usage: + * if($database->columnExists('pages', 'name')) { + * echo "The pages table has a 'name' column"; + * } + * + * // You can also bundle table and column together: + * if($database->columnExists('pages.name')) { + * echo "The pages table has a 'name' column"; + * } + * + * $exists = $database->columnExists('pages', 'name', true); + * if($exists) { + * // associative array with indexes: Name, Type, Null, Key, Default, Extra + * echo "The pages table has a 'name' column and here is verbose info: "; + * print_r($exists); + * } + * ~~~~~ + * + * #pw-group-custom + * + * @param string $table Specify table name (or table and column name in format "table.column"). + * @param string $column Specify column name (or omit or blank string if already specified in $table argument). + * @param bool $getInfo Return array of column info (with type info, etc.) rather than boolean? + * @return bool|array + * @since 3.0.154 + * @throws WireDatabaseException + * + */ + public function columnExists($table, $column = '', $getInfo = false) { + if(empty($column)) { + if(!strpos($table, '.')) throw new WireDatabaseException('No column specified'); + list($table, $column) = explode('.', $table, 2); + } + $exists = false; + $table = $this->escapeTable($table); + try { + $query = $this->prepare("SHOW COLUMNS FROM `$table` WHERE Field=:column"); + $query->bindValue(':column', $column, \PDO::PARAM_STR); + $query->execute(); + $numRows = (int) $query->rowCount(); + if($numRows) $exists = $getInfo ? $query->fetch(\PDO::FETCH_ASSOC) : true; + $query->closeCursor(); + } catch(\Exception $e) { + // most likely given table does not exist + $exists = false; + } + return $exists; + } + /** * Is the given string a database comparison operator? *