@@ -15,215 +15,215 @@ |
||
| 15 | 15 | |
| 16 | 16 | class RequestSearchHelper extends SearchHelperBase |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * RequestSearchHelper constructor. |
|
| 20 | - * |
|
| 21 | - * @param PdoDatabase $database |
|
| 22 | - */ |
|
| 23 | - protected function __construct(PdoDatabase $database) |
|
| 24 | - { |
|
| 25 | - parent::__construct($database, 'request', Request::class); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Initiates a search for requests |
|
| 30 | - * |
|
| 31 | - * @param PdoDatabase $database |
|
| 32 | - * @param int|null $domain |
|
| 33 | - * |
|
| 34 | - * @return RequestSearchHelper |
|
| 35 | - */ |
|
| 36 | - public static function get(PdoDatabase $database, ?int $domain) |
|
| 37 | - { |
|
| 38 | - $helper = new RequestSearchHelper($database); |
|
| 39 | - |
|
| 40 | - if ($domain !== null) { |
|
| 41 | - $helper->whereClause .= ' AND domain = ?'; |
|
| 42 | - $helper->parameterList[] = $domain; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return $helper; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Filters the results by IP address |
|
| 50 | - * |
|
| 51 | - * @param string $ipAddress |
|
| 52 | - * |
|
| 53 | - * @return $this |
|
| 54 | - */ |
|
| 55 | - public function byIp($ipAddress) |
|
| 56 | - { |
|
| 57 | - $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
| 58 | - $this->parameterList[] = $ipAddress; |
|
| 59 | - $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
| 60 | - |
|
| 61 | - return $this; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Filters the results by email address |
|
| 66 | - * |
|
| 67 | - * @param string $emailAddress |
|
| 68 | - * |
|
| 69 | - * @return $this |
|
| 70 | - */ |
|
| 71 | - public function byEmailAddress($emailAddress) |
|
| 72 | - { |
|
| 73 | - $this->whereClause .= ' AND email LIKE ?'; |
|
| 74 | - $this->parameterList[] = $emailAddress; |
|
| 75 | - |
|
| 76 | - return $this; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Filters the results by name |
|
| 81 | - * |
|
| 82 | - * @param string $name |
|
| 83 | - * |
|
| 84 | - * @return $this |
|
| 85 | - */ |
|
| 86 | - public function byName($name) |
|
| 87 | - { |
|
| 88 | - $this->whereClause .= ' AND name LIKE ?'; |
|
| 89 | - $this->parameterList[] = $name; |
|
| 90 | - |
|
| 91 | - return $this; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Filters the results by comment |
|
| 96 | - * |
|
| 97 | - * @param string $comment |
|
| 98 | - * |
|
| 99 | - * @return $this |
|
| 100 | - */ |
|
| 101 | - public function byComment($comment) |
|
| 102 | - { |
|
| 103 | - $this->modifiersClause = 'DISTINCT'; |
|
| 104 | - $this->joinClause .= ' INNER JOIN comment c ON origin.id = c.request'; |
|
| 105 | - $this->whereClause .= ' AND c.comment LIKE ?'; |
|
| 106 | - $this->parameterList[] = $comment; |
|
| 107 | - |
|
| 108 | - return $this; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Filters the results by comment security |
|
| 113 | - * |
|
| 114 | - * @param array $security List of allowed values for the security clause |
|
| 115 | - * |
|
| 116 | - * @return $this |
|
| 117 | - */ |
|
| 118 | - public function byCommentSecurity(array $security) |
|
| 119 | - { |
|
| 120 | - $this->inClause('c.visibility', $security); |
|
| 121 | - |
|
| 122 | - return $this; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Filters the requests to those with a defined status |
|
| 127 | - * |
|
| 128 | - * @param $status |
|
| 129 | - * |
|
| 130 | - * @return $this |
|
| 131 | - */ |
|
| 132 | - public function byStatus($status) |
|
| 133 | - { |
|
| 134 | - $this->whereClause .= ' AND status = ?'; |
|
| 135 | - $this->parameterList[] = $status; |
|
| 136 | - |
|
| 137 | - return $this; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Excludes a request from the results |
|
| 142 | - * |
|
| 143 | - * @param int $requestId |
|
| 144 | - * |
|
| 145 | - * @return $this |
|
| 146 | - */ |
|
| 147 | - public function excludingRequest($requestId) |
|
| 148 | - { |
|
| 149 | - $this->whereClause .= ' AND id <> ?'; |
|
| 150 | - $this->parameterList[] = $requestId; |
|
| 151 | - |
|
| 152 | - return $this; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Filters the results to only those with a confirmed email address |
|
| 157 | - * |
|
| 158 | - * @return $this |
|
| 159 | - */ |
|
| 160 | - public function withConfirmedEmail() |
|
| 161 | - { |
|
| 162 | - $this->whereClause .= ' AND emailconfirm = ?'; |
|
| 163 | - $this->parameterList[] = 'Confirmed'; |
|
| 164 | - |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Filters the results to exclude purged data |
|
| 170 | - * |
|
| 171 | - * @param SiteConfiguration $configuration |
|
| 172 | - * |
|
| 173 | - * @return $this |
|
| 174 | - */ |
|
| 175 | - public function excludingPurgedData(SiteConfiguration $configuration) |
|
| 176 | - { |
|
| 177 | - $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
| 178 | - $this->parameterList[] = $configuration->getDataClearIp(); |
|
| 179 | - $this->parameterList[] = $configuration->getDataClearEmail(); |
|
| 180 | - |
|
| 181 | - return $this; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Filters the requests to those without a defined status |
|
| 186 | - * |
|
| 187 | - * @param $status |
|
| 188 | - * |
|
| 189 | - * @return $this |
|
| 190 | - */ |
|
| 191 | - public function excludingStatus($status) |
|
| 192 | - { |
|
| 193 | - $this->whereClause .= ' AND status <> ?'; |
|
| 194 | - $this->parameterList[] = $status; |
|
| 195 | - |
|
| 196 | - return $this; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Filters the requests to those which have failed an auto-creation |
|
| 201 | - * |
|
| 202 | - * @return $this |
|
| 203 | - */ |
|
| 204 | - public function isHospitalised() |
|
| 205 | - { |
|
| 206 | - $this->whereClause .= ' AND status = ?'; |
|
| 207 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 208 | - |
|
| 209 | - return $this; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Filters the requests to those which have not failed an auto-creation |
|
| 214 | - * |
|
| 215 | - * @return $this |
|
| 216 | - */ |
|
| 217 | - public function notHospitalised() |
|
| 218 | - { |
|
| 219 | - $this->whereClause .= ' AND status <> ?'; |
|
| 220 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 221 | - |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - public function fetchByQueue($queues) |
|
| 226 | - { |
|
| 227 | - return $this->fetchByParameter(' AND queue = ?', $queues); |
|
| 228 | - } |
|
| 18 | + /** |
|
| 19 | + * RequestSearchHelper constructor. |
|
| 20 | + * |
|
| 21 | + * @param PdoDatabase $database |
|
| 22 | + */ |
|
| 23 | + protected function __construct(PdoDatabase $database) |
|
| 24 | + { |
|
| 25 | + parent::__construct($database, 'request', Request::class); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Initiates a search for requests |
|
| 30 | + * |
|
| 31 | + * @param PdoDatabase $database |
|
| 32 | + * @param int|null $domain |
|
| 33 | + * |
|
| 34 | + * @return RequestSearchHelper |
|
| 35 | + */ |
|
| 36 | + public static function get(PdoDatabase $database, ?int $domain) |
|
| 37 | + { |
|
| 38 | + $helper = new RequestSearchHelper($database); |
|
| 39 | + |
|
| 40 | + if ($domain !== null) { |
|
| 41 | + $helper->whereClause .= ' AND domain = ?'; |
|
| 42 | + $helper->parameterList[] = $domain; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return $helper; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Filters the results by IP address |
|
| 50 | + * |
|
| 51 | + * @param string $ipAddress |
|
| 52 | + * |
|
| 53 | + * @return $this |
|
| 54 | + */ |
|
| 55 | + public function byIp($ipAddress) |
|
| 56 | + { |
|
| 57 | + $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
| 58 | + $this->parameterList[] = $ipAddress; |
|
| 59 | + $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
| 60 | + |
|
| 61 | + return $this; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Filters the results by email address |
|
| 66 | + * |
|
| 67 | + * @param string $emailAddress |
|
| 68 | + * |
|
| 69 | + * @return $this |
|
| 70 | + */ |
|
| 71 | + public function byEmailAddress($emailAddress) |
|
| 72 | + { |
|
| 73 | + $this->whereClause .= ' AND email LIKE ?'; |
|
| 74 | + $this->parameterList[] = $emailAddress; |
|
| 75 | + |
|
| 76 | + return $this; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Filters the results by name |
|
| 81 | + * |
|
| 82 | + * @param string $name |
|
| 83 | + * |
|
| 84 | + * @return $this |
|
| 85 | + */ |
|
| 86 | + public function byName($name) |
|
| 87 | + { |
|
| 88 | + $this->whereClause .= ' AND name LIKE ?'; |
|
| 89 | + $this->parameterList[] = $name; |
|
| 90 | + |
|
| 91 | + return $this; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Filters the results by comment |
|
| 96 | + * |
|
| 97 | + * @param string $comment |
|
| 98 | + * |
|
| 99 | + * @return $this |
|
| 100 | + */ |
|
| 101 | + public function byComment($comment) |
|
| 102 | + { |
|
| 103 | + $this->modifiersClause = 'DISTINCT'; |
|
| 104 | + $this->joinClause .= ' INNER JOIN comment c ON origin.id = c.request'; |
|
| 105 | + $this->whereClause .= ' AND c.comment LIKE ?'; |
|
| 106 | + $this->parameterList[] = $comment; |
|
| 107 | + |
|
| 108 | + return $this; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Filters the results by comment security |
|
| 113 | + * |
|
| 114 | + * @param array $security List of allowed values for the security clause |
|
| 115 | + * |
|
| 116 | + * @return $this |
|
| 117 | + */ |
|
| 118 | + public function byCommentSecurity(array $security) |
|
| 119 | + { |
|
| 120 | + $this->inClause('c.visibility', $security); |
|
| 121 | + |
|
| 122 | + return $this; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Filters the requests to those with a defined status |
|
| 127 | + * |
|
| 128 | + * @param $status |
|
| 129 | + * |
|
| 130 | + * @return $this |
|
| 131 | + */ |
|
| 132 | + public function byStatus($status) |
|
| 133 | + { |
|
| 134 | + $this->whereClause .= ' AND status = ?'; |
|
| 135 | + $this->parameterList[] = $status; |
|
| 136 | + |
|
| 137 | + return $this; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Excludes a request from the results |
|
| 142 | + * |
|
| 143 | + * @param int $requestId |
|
| 144 | + * |
|
| 145 | + * @return $this |
|
| 146 | + */ |
|
| 147 | + public function excludingRequest($requestId) |
|
| 148 | + { |
|
| 149 | + $this->whereClause .= ' AND id <> ?'; |
|
| 150 | + $this->parameterList[] = $requestId; |
|
| 151 | + |
|
| 152 | + return $this; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Filters the results to only those with a confirmed email address |
|
| 157 | + * |
|
| 158 | + * @return $this |
|
| 159 | + */ |
|
| 160 | + public function withConfirmedEmail() |
|
| 161 | + { |
|
| 162 | + $this->whereClause .= ' AND emailconfirm = ?'; |
|
| 163 | + $this->parameterList[] = 'Confirmed'; |
|
| 164 | + |
|
| 165 | + return $this; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Filters the results to exclude purged data |
|
| 170 | + * |
|
| 171 | + * @param SiteConfiguration $configuration |
|
| 172 | + * |
|
| 173 | + * @return $this |
|
| 174 | + */ |
|
| 175 | + public function excludingPurgedData(SiteConfiguration $configuration) |
|
| 176 | + { |
|
| 177 | + $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
| 178 | + $this->parameterList[] = $configuration->getDataClearIp(); |
|
| 179 | + $this->parameterList[] = $configuration->getDataClearEmail(); |
|
| 180 | + |
|
| 181 | + return $this; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Filters the requests to those without a defined status |
|
| 186 | + * |
|
| 187 | + * @param $status |
|
| 188 | + * |
|
| 189 | + * @return $this |
|
| 190 | + */ |
|
| 191 | + public function excludingStatus($status) |
|
| 192 | + { |
|
| 193 | + $this->whereClause .= ' AND status <> ?'; |
|
| 194 | + $this->parameterList[] = $status; |
|
| 195 | + |
|
| 196 | + return $this; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Filters the requests to those which have failed an auto-creation |
|
| 201 | + * |
|
| 202 | + * @return $this |
|
| 203 | + */ |
|
| 204 | + public function isHospitalised() |
|
| 205 | + { |
|
| 206 | + $this->whereClause .= ' AND status = ?'; |
|
| 207 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 208 | + |
|
| 209 | + return $this; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Filters the requests to those which have not failed an auto-creation |
|
| 214 | + * |
|
| 215 | + * @return $this |
|
| 216 | + */ |
|
| 217 | + public function notHospitalised() |
|
| 218 | + { |
|
| 219 | + $this->whereClause .= ' AND status <> ?'; |
|
| 220 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 221 | + |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + public function fetchByQueue($queues) |
|
| 226 | + { |
|
| 227 | + return $this->fetchByParameter(' AND queue = ?', $queues); |
|
| 228 | + } |
|
| 229 | 229 | } |
@@ -16,276 +16,276 @@ |
||
| 16 | 16 | |
| 17 | 17 | abstract class SearchHelperBase |
| 18 | 18 | { |
| 19 | - /** @var PdoDatabase */ |
|
| 20 | - protected $database; |
|
| 21 | - /** @var array */ |
|
| 22 | - protected $parameterList = array(); |
|
| 23 | - /** @var null|int */ |
|
| 24 | - private $limit = null; |
|
| 25 | - /** @var null|int */ |
|
| 26 | - private $offset = null; |
|
| 27 | - protected $orderBy; |
|
| 28 | - /** |
|
| 29 | - * @var string The where clause. |
|
| 30 | - * |
|
| 31 | - * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
| 32 | - * that we use positional parameters instead of named parameters because we don't know many times different options |
|
| 33 | - * will be called (looking at excluding() here, but there's the option for others). |
|
| 34 | - */ |
|
| 35 | - protected $whereClause = ' WHERE 1 = 1'; |
|
| 36 | - /** @var string */ |
|
| 37 | - protected $table; |
|
| 38 | - protected $joinClause = ''; |
|
| 39 | - protected $groupByClause = ''; |
|
| 40 | - protected $modifiersClause = ''; |
|
| 41 | - private $targetClass; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * SearchHelperBase constructor. |
|
| 45 | - * |
|
| 46 | - * @param PdoDatabase $database |
|
| 47 | - * @param string $table |
|
| 48 | - * @param $targetClass |
|
| 49 | - * @param null|string $order Order by clause, excluding ORDER BY. |
|
| 50 | - */ |
|
| 51 | - protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
| 52 | - { |
|
| 53 | - $this->database = $database; |
|
| 54 | - $this->table = $table; |
|
| 55 | - $this->orderBy = $order; |
|
| 56 | - $this->targetClass = $targetClass; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Finalises the database query, and executes it, returning a set of objects. |
|
| 61 | - * |
|
| 62 | - * @return DataObject[] |
|
| 63 | - */ |
|
| 64 | - public function fetch() |
|
| 65 | - { |
|
| 66 | - $statement = $this->getData(); |
|
| 67 | - |
|
| 68 | - /** @var DataObject[] $returnedObjects */ |
|
| 69 | - $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
| 70 | - foreach ($returnedObjects as $req) { |
|
| 71 | - $req->setDatabase($this->database); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return $returnedObjects; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param string $whereClauseSection |
|
| 79 | - * @param array $values |
|
| 80 | - * |
|
| 81 | - * @return array |
|
| 82 | - */ |
|
| 83 | - protected function fetchByParameter($whereClauseSection, $values) |
|
| 84 | - { |
|
| 85 | - $this->whereClause .= $whereClauseSection; |
|
| 86 | - |
|
| 87 | - $countQuery = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
| 88 | - $countQuery .= $this->joinClause . $this->whereClause; |
|
| 89 | - |
|
| 90 | - $query = $this->buildQuery(array('*')); |
|
| 91 | - $query .= $this->applyOrder(); |
|
| 92 | - |
|
| 93 | - // shuffle around parameters |
|
| 94 | - // applyLimit() appends parameters to the parameter list, which is useless when we want to run |
|
| 95 | - // many queries with different parameters. As such, we back up the parameter list, wipe it, apply the limit |
|
| 96 | - // parameters, and hold them separately, merging again prior to running the actual query. |
|
| 97 | - $localParameterList = $this->parameterList; |
|
| 98 | - $this->parameterList = array(); |
|
| 99 | - $query .= $this->applyLimit(); |
|
| 100 | - $limitParameters = $this->parameterList; |
|
| 101 | - |
|
| 102 | - $statement = $this->database->prepare($query); |
|
| 103 | - $countStatement = $this->database->prepare($countQuery); |
|
| 104 | - |
|
| 105 | - $result = array(); |
|
| 106 | - foreach ($values as $v) { |
|
| 107 | - // reset parameter list |
|
| 108 | - $params = $localParameterList; |
|
| 109 | - $params[] = $v; |
|
| 110 | - |
|
| 111 | - $countStatement->execute($params); |
|
| 112 | - |
|
| 113 | - // reapply the limit parameters |
|
| 114 | - $params = array_merge($params, $limitParameters); |
|
| 115 | - |
|
| 116 | - $statement->execute($params); |
|
| 117 | - |
|
| 118 | - /** @var DataObject[] $returnedObjects */ |
|
| 119 | - $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
| 120 | - foreach ($returnedObjects as $req) { |
|
| 121 | - $req->setDatabase($this->database); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $result[$v] = array( |
|
| 125 | - 'count' => $countStatement->fetchColumn(0), |
|
| 126 | - 'data' => $returnedObjects, |
|
| 127 | - ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - return $result; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Finalises the database query, and executes it, returning only the requested column. |
|
| 135 | - * |
|
| 136 | - * @param string $column The required column |
|
| 137 | - * |
|
| 138 | - * @param bool $distinct |
|
| 139 | - * |
|
| 140 | - * @return array |
|
| 141 | - * @throws ApplicationLogicException |
|
| 142 | - */ |
|
| 143 | - public function fetchColumn($column, $distinct = false) |
|
| 144 | - { |
|
| 145 | - if ($distinct) { |
|
| 146 | - if ($this->groupByClause !== '') { |
|
| 147 | - throw new ApplicationLogicException('Cannot apply distinct to column fetch already using group by'); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $this->groupByClause = ' GROUP BY origin.' . $column; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $statement = $this->getData(array($column)); |
|
| 154 | - |
|
| 155 | - return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function fetchMap($column) |
|
| 159 | - { |
|
| 160 | - $statement = $this->getData(array('id', $column)); |
|
| 161 | - |
|
| 162 | - $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
| 163 | - $map = array(); |
|
| 164 | - |
|
| 165 | - foreach ($data as $row) { |
|
| 166 | - $map[$row['id']] = $row[$column]; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $map; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @param int $count Returns the record count of the result set |
|
| 174 | - * |
|
| 175 | - * @return $this |
|
| 176 | - */ |
|
| 177 | - public function getRecordCount(&$count) |
|
| 178 | - { |
|
| 179 | - $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
| 180 | - $query .= $this->joinClause . $this->whereClause; |
|
| 181 | - |
|
| 182 | - $statement = $this->database->prepare($query); |
|
| 183 | - $statement->execute($this->parameterList); |
|
| 184 | - |
|
| 185 | - $count = $statement->fetchColumn(0); |
|
| 186 | - $statement->closeCursor(); |
|
| 187 | - |
|
| 188 | - return $this; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Limits the results |
|
| 193 | - * |
|
| 194 | - * @param integer $limit |
|
| 195 | - * @param integer|null $offset |
|
| 196 | - * |
|
| 197 | - * @return $this |
|
| 198 | - * |
|
| 199 | - */ |
|
| 200 | - public function limit($limit, $offset = null) |
|
| 201 | - { |
|
| 202 | - $this->limit = $limit; |
|
| 203 | - $this->offset = $offset; |
|
| 204 | - |
|
| 205 | - return $this; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - private function applyLimit() |
|
| 209 | - { |
|
| 210 | - $clause = ''; |
|
| 211 | - if ($this->limit !== null) { |
|
| 212 | - $clause = ' LIMIT ?'; |
|
| 213 | - $this->parameterList[] = $this->limit; |
|
| 214 | - |
|
| 215 | - if ($this->offset !== null) { |
|
| 216 | - $clause .= ' OFFSET ?'; |
|
| 217 | - $this->parameterList[] = $this->offset; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - return $clause; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - private function applyOrder() |
|
| 225 | - { |
|
| 226 | - if ($this->orderBy !== null) { |
|
| 227 | - return ' ORDER BY ' . $this->orderBy; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return ''; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @param array $columns |
|
| 235 | - * |
|
| 236 | - * @return PDOStatement |
|
| 237 | - */ |
|
| 238 | - private function getData($columns = array('*')) |
|
| 239 | - { |
|
| 240 | - $query = $this->buildQuery($columns); |
|
| 241 | - $query .= $this->applyOrder(); |
|
| 242 | - $query .= $this->applyLimit(); |
|
| 243 | - |
|
| 244 | - /** @var PDOStatement $statement */ |
|
| 245 | - $statement = $this->database->prepare($query); |
|
| 246 | - $statement->execute($this->parameterList); |
|
| 247 | - |
|
| 248 | - return $statement; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * @param array $columns |
|
| 253 | - * |
|
| 254 | - * @return string |
|
| 255 | - */ |
|
| 256 | - protected function buildQuery($columns) |
|
| 257 | - { |
|
| 258 | - $colData = array(); |
|
| 259 | - foreach ($columns as $c) { |
|
| 260 | - $colData[] = 'origin.' . $c; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - $query = "SELECT {$this->modifiersClause} /* " . get_called_class() . " */ " . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
| 264 | - $query .= $this->joinClause . $this->whereClause . $this->groupByClause; |
|
| 265 | - |
|
| 266 | - return $query; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - public function inIds($idList) |
|
| 270 | - { |
|
| 271 | - $this->inClause('id', $idList); |
|
| 272 | - |
|
| 273 | - return $this; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - protected function inClause($column, $values) |
|
| 277 | - { |
|
| 278 | - if (count($values) === 0) { |
|
| 279 | - $this->whereClause .= ' AND 1 = 2 /* empty IN() */'; |
|
| 280 | - return; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - // You can't use IN() with parameters directly, so let's munge something together. |
|
| 284 | - // Let's create a string of question marks, which will do as positional parameters. |
|
| 285 | - $valueCount = count($values); |
|
| 286 | - $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
| 287 | - |
|
| 288 | - $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
| 289 | - $this->parameterList = array_merge($this->parameterList, $values); |
|
| 290 | - } |
|
| 19 | + /** @var PdoDatabase */ |
|
| 20 | + protected $database; |
|
| 21 | + /** @var array */ |
|
| 22 | + protected $parameterList = array(); |
|
| 23 | + /** @var null|int */ |
|
| 24 | + private $limit = null; |
|
| 25 | + /** @var null|int */ |
|
| 26 | + private $offset = null; |
|
| 27 | + protected $orderBy; |
|
| 28 | + /** |
|
| 29 | + * @var string The where clause. |
|
| 30 | + * |
|
| 31 | + * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
| 32 | + * that we use positional parameters instead of named parameters because we don't know many times different options |
|
| 33 | + * will be called (looking at excluding() here, but there's the option for others). |
|
| 34 | + */ |
|
| 35 | + protected $whereClause = ' WHERE 1 = 1'; |
|
| 36 | + /** @var string */ |
|
| 37 | + protected $table; |
|
| 38 | + protected $joinClause = ''; |
|
| 39 | + protected $groupByClause = ''; |
|
| 40 | + protected $modifiersClause = ''; |
|
| 41 | + private $targetClass; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * SearchHelperBase constructor. |
|
| 45 | + * |
|
| 46 | + * @param PdoDatabase $database |
|
| 47 | + * @param string $table |
|
| 48 | + * @param $targetClass |
|
| 49 | + * @param null|string $order Order by clause, excluding ORDER BY. |
|
| 50 | + */ |
|
| 51 | + protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
| 52 | + { |
|
| 53 | + $this->database = $database; |
|
| 54 | + $this->table = $table; |
|
| 55 | + $this->orderBy = $order; |
|
| 56 | + $this->targetClass = $targetClass; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Finalises the database query, and executes it, returning a set of objects. |
|
| 61 | + * |
|
| 62 | + * @return DataObject[] |
|
| 63 | + */ |
|
| 64 | + public function fetch() |
|
| 65 | + { |
|
| 66 | + $statement = $this->getData(); |
|
| 67 | + |
|
| 68 | + /** @var DataObject[] $returnedObjects */ |
|
| 69 | + $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
| 70 | + foreach ($returnedObjects as $req) { |
|
| 71 | + $req->setDatabase($this->database); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return $returnedObjects; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param string $whereClauseSection |
|
| 79 | + * @param array $values |
|
| 80 | + * |
|
| 81 | + * @return array |
|
| 82 | + */ |
|
| 83 | + protected function fetchByParameter($whereClauseSection, $values) |
|
| 84 | + { |
|
| 85 | + $this->whereClause .= $whereClauseSection; |
|
| 86 | + |
|
| 87 | + $countQuery = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
| 88 | + $countQuery .= $this->joinClause . $this->whereClause; |
|
| 89 | + |
|
| 90 | + $query = $this->buildQuery(array('*')); |
|
| 91 | + $query .= $this->applyOrder(); |
|
| 92 | + |
|
| 93 | + // shuffle around parameters |
|
| 94 | + // applyLimit() appends parameters to the parameter list, which is useless when we want to run |
|
| 95 | + // many queries with different parameters. As such, we back up the parameter list, wipe it, apply the limit |
|
| 96 | + // parameters, and hold them separately, merging again prior to running the actual query. |
|
| 97 | + $localParameterList = $this->parameterList; |
|
| 98 | + $this->parameterList = array(); |
|
| 99 | + $query .= $this->applyLimit(); |
|
| 100 | + $limitParameters = $this->parameterList; |
|
| 101 | + |
|
| 102 | + $statement = $this->database->prepare($query); |
|
| 103 | + $countStatement = $this->database->prepare($countQuery); |
|
| 104 | + |
|
| 105 | + $result = array(); |
|
| 106 | + foreach ($values as $v) { |
|
| 107 | + // reset parameter list |
|
| 108 | + $params = $localParameterList; |
|
| 109 | + $params[] = $v; |
|
| 110 | + |
|
| 111 | + $countStatement->execute($params); |
|
| 112 | + |
|
| 113 | + // reapply the limit parameters |
|
| 114 | + $params = array_merge($params, $limitParameters); |
|
| 115 | + |
|
| 116 | + $statement->execute($params); |
|
| 117 | + |
|
| 118 | + /** @var DataObject[] $returnedObjects */ |
|
| 119 | + $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
| 120 | + foreach ($returnedObjects as $req) { |
|
| 121 | + $req->setDatabase($this->database); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $result[$v] = array( |
|
| 125 | + 'count' => $countStatement->fetchColumn(0), |
|
| 126 | + 'data' => $returnedObjects, |
|
| 127 | + ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + return $result; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Finalises the database query, and executes it, returning only the requested column. |
|
| 135 | + * |
|
| 136 | + * @param string $column The required column |
|
| 137 | + * |
|
| 138 | + * @param bool $distinct |
|
| 139 | + * |
|
| 140 | + * @return array |
|
| 141 | + * @throws ApplicationLogicException |
|
| 142 | + */ |
|
| 143 | + public function fetchColumn($column, $distinct = false) |
|
| 144 | + { |
|
| 145 | + if ($distinct) { |
|
| 146 | + if ($this->groupByClause !== '') { |
|
| 147 | + throw new ApplicationLogicException('Cannot apply distinct to column fetch already using group by'); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $this->groupByClause = ' GROUP BY origin.' . $column; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $statement = $this->getData(array($column)); |
|
| 154 | + |
|
| 155 | + return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function fetchMap($column) |
|
| 159 | + { |
|
| 160 | + $statement = $this->getData(array('id', $column)); |
|
| 161 | + |
|
| 162 | + $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
| 163 | + $map = array(); |
|
| 164 | + |
|
| 165 | + foreach ($data as $row) { |
|
| 166 | + $map[$row['id']] = $row[$column]; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $map; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @param int $count Returns the record count of the result set |
|
| 174 | + * |
|
| 175 | + * @return $this |
|
| 176 | + */ |
|
| 177 | + public function getRecordCount(&$count) |
|
| 178 | + { |
|
| 179 | + $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
| 180 | + $query .= $this->joinClause . $this->whereClause; |
|
| 181 | + |
|
| 182 | + $statement = $this->database->prepare($query); |
|
| 183 | + $statement->execute($this->parameterList); |
|
| 184 | + |
|
| 185 | + $count = $statement->fetchColumn(0); |
|
| 186 | + $statement->closeCursor(); |
|
| 187 | + |
|
| 188 | + return $this; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Limits the results |
|
| 193 | + * |
|
| 194 | + * @param integer $limit |
|
| 195 | + * @param integer|null $offset |
|
| 196 | + * |
|
| 197 | + * @return $this |
|
| 198 | + * |
|
| 199 | + */ |
|
| 200 | + public function limit($limit, $offset = null) |
|
| 201 | + { |
|
| 202 | + $this->limit = $limit; |
|
| 203 | + $this->offset = $offset; |
|
| 204 | + |
|
| 205 | + return $this; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + private function applyLimit() |
|
| 209 | + { |
|
| 210 | + $clause = ''; |
|
| 211 | + if ($this->limit !== null) { |
|
| 212 | + $clause = ' LIMIT ?'; |
|
| 213 | + $this->parameterList[] = $this->limit; |
|
| 214 | + |
|
| 215 | + if ($this->offset !== null) { |
|
| 216 | + $clause .= ' OFFSET ?'; |
|
| 217 | + $this->parameterList[] = $this->offset; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + return $clause; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + private function applyOrder() |
|
| 225 | + { |
|
| 226 | + if ($this->orderBy !== null) { |
|
| 227 | + return ' ORDER BY ' . $this->orderBy; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return ''; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @param array $columns |
|
| 235 | + * |
|
| 236 | + * @return PDOStatement |
|
| 237 | + */ |
|
| 238 | + private function getData($columns = array('*')) |
|
| 239 | + { |
|
| 240 | + $query = $this->buildQuery($columns); |
|
| 241 | + $query .= $this->applyOrder(); |
|
| 242 | + $query .= $this->applyLimit(); |
|
| 243 | + |
|
| 244 | + /** @var PDOStatement $statement */ |
|
| 245 | + $statement = $this->database->prepare($query); |
|
| 246 | + $statement->execute($this->parameterList); |
|
| 247 | + |
|
| 248 | + return $statement; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * @param array $columns |
|
| 253 | + * |
|
| 254 | + * @return string |
|
| 255 | + */ |
|
| 256 | + protected function buildQuery($columns) |
|
| 257 | + { |
|
| 258 | + $colData = array(); |
|
| 259 | + foreach ($columns as $c) { |
|
| 260 | + $colData[] = 'origin.' . $c; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + $query = "SELECT {$this->modifiersClause} /* " . get_called_class() . " */ " . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
| 264 | + $query .= $this->joinClause . $this->whereClause . $this->groupByClause; |
|
| 265 | + |
|
| 266 | + return $query; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + public function inIds($idList) |
|
| 270 | + { |
|
| 271 | + $this->inClause('id', $idList); |
|
| 272 | + |
|
| 273 | + return $this; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + protected function inClause($column, $values) |
|
| 277 | + { |
|
| 278 | + if (count($values) === 0) { |
|
| 279 | + $this->whereClause .= ' AND 1 = 2 /* empty IN() */'; |
|
| 280 | + return; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + // You can't use IN() with parameters directly, so let's munge something together. |
|
| 284 | + // Let's create a string of question marks, which will do as positional parameters. |
|
| 285 | + $valueCount = count($values); |
|
| 286 | + $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
| 287 | + |
|
| 288 | + $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
| 289 | + $this->parameterList = array_merge($this->parameterList, $values); |
|
| 290 | + } |
|
| 291 | 291 | } |
@@ -13,85 +13,85 @@ |
||
| 13 | 13 | |
| 14 | 14 | class JobQueueSearchHelper extends SearchHelperBase |
| 15 | 15 | { |
| 16 | - protected function __construct(PdoDatabase $database) |
|
| 17 | - { |
|
| 18 | - parent::__construct($database, 'jobqueue', JobQueue::class, null); |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @param PdoDatabase $database |
|
| 23 | - * @param int $domain |
|
| 24 | - * |
|
| 25 | - * @return JobQueueSearchHelper |
|
| 26 | - */ |
|
| 27 | - public static function get(PdoDatabase $database, int $domain) |
|
| 28 | - { |
|
| 29 | - $helper = new JobQueueSearchHelper($database); |
|
| 30 | - |
|
| 31 | - $helper->whereClause .= ' AND domain = ?'; |
|
| 32 | - $helper->parameterList[] = $domain; |
|
| 33 | - |
|
| 34 | - return $helper; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param string[] $statuses |
|
| 39 | - * |
|
| 40 | - * @return $this |
|
| 41 | - */ |
|
| 42 | - public function statusIn($statuses) |
|
| 43 | - { |
|
| 44 | - $this->inClause('status', $statuses); |
|
| 45 | - |
|
| 46 | - return $this; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @return $this |
|
| 51 | - */ |
|
| 52 | - public function notAcknowledged() |
|
| 53 | - { |
|
| 54 | - $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
| 55 | - |
|
| 56 | - return $this; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function byTask($task) |
|
| 60 | - { |
|
| 61 | - $this->whereClause .= ' AND task = ?'; |
|
| 62 | - $this->parameterList[] = $task; |
|
| 63 | - |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - public function byUser($userId) |
|
| 68 | - { |
|
| 69 | - $this->whereClause .= ' AND user = ?'; |
|
| 70 | - $this->parameterList[] = $userId; |
|
| 71 | - |
|
| 72 | - return $this; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function byStatus($status) |
|
| 76 | - { |
|
| 77 | - $this->whereClause .= ' AND status = ?'; |
|
| 78 | - $this->parameterList[] = $status; |
|
| 79 | - |
|
| 80 | - return $this; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public function byRequest(int $request) : JobQueueSearchHelper |
|
| 84 | - { |
|
| 85 | - $this->whereClause .= ' AND request = ?'; |
|
| 86 | - $this->parameterList[] = $request; |
|
| 87 | - |
|
| 88 | - return $this; |
|
| 89 | - } |
|
| 16 | + protected function __construct(PdoDatabase $database) |
|
| 17 | + { |
|
| 18 | + parent::__construct($database, 'jobqueue', JobQueue::class, null); |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @param PdoDatabase $database |
|
| 23 | + * @param int $domain |
|
| 24 | + * |
|
| 25 | + * @return JobQueueSearchHelper |
|
| 26 | + */ |
|
| 27 | + public static function get(PdoDatabase $database, int $domain) |
|
| 28 | + { |
|
| 29 | + $helper = new JobQueueSearchHelper($database); |
|
| 30 | + |
|
| 31 | + $helper->whereClause .= ' AND domain = ?'; |
|
| 32 | + $helper->parameterList[] = $domain; |
|
| 33 | + |
|
| 34 | + return $helper; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param string[] $statuses |
|
| 39 | + * |
|
| 40 | + * @return $this |
|
| 41 | + */ |
|
| 42 | + public function statusIn($statuses) |
|
| 43 | + { |
|
| 44 | + $this->inClause('status', $statuses); |
|
| 45 | + |
|
| 46 | + return $this; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @return $this |
|
| 51 | + */ |
|
| 52 | + public function notAcknowledged() |
|
| 53 | + { |
|
| 54 | + $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
| 55 | + |
|
| 56 | + return $this; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function byTask($task) |
|
| 60 | + { |
|
| 61 | + $this->whereClause .= ' AND task = ?'; |
|
| 62 | + $this->parameterList[] = $task; |
|
| 63 | + |
|
| 64 | + return $this; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + public function byUser($userId) |
|
| 68 | + { |
|
| 69 | + $this->whereClause .= ' AND user = ?'; |
|
| 70 | + $this->parameterList[] = $userId; |
|
| 71 | + |
|
| 72 | + return $this; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function byStatus($status) |
|
| 76 | + { |
|
| 77 | + $this->whereClause .= ' AND status = ?'; |
|
| 78 | + $this->parameterList[] = $status; |
|
| 79 | + |
|
| 80 | + return $this; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public function byRequest(int $request) : JobQueueSearchHelper |
|
| 84 | + { |
|
| 85 | + $this->whereClause .= ' AND request = ?'; |
|
| 86 | + $this->parameterList[] = $request; |
|
| 87 | + |
|
| 88 | + return $this; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - public function newestFirst() |
|
| 92 | - { |
|
| 93 | - $this->orderBy = 'id DESC'; |
|
| 91 | + public function newestFirst() |
|
| 92 | + { |
|
| 93 | + $this->orderBy = 'id DESC'; |
|
| 94 | 94 | |
| 95 | - return $this; |
|
| 96 | - } |
|
| 95 | + return $this; |
|
| 96 | + } |
|
| 97 | 97 | } |
@@ -13,98 +13,98 @@ |
||
| 13 | 13 | |
| 14 | 14 | class LogSearchHelper extends SearchHelperBase |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * LogSearchHelper constructor. |
|
| 18 | - * |
|
| 19 | - * @param PdoDatabase $database |
|
| 20 | - */ |
|
| 21 | - protected function __construct(PdoDatabase $database) |
|
| 22 | - { |
|
| 23 | - parent::__construct($database, 'log', Log::class, 'timestamp DESC'); |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * LogSearchHelper constructor. |
|
| 18 | + * |
|
| 19 | + * @param PdoDatabase $database |
|
| 20 | + */ |
|
| 21 | + protected function __construct(PdoDatabase $database) |
|
| 22 | + { |
|
| 23 | + parent::__construct($database, 'log', Log::class, 'timestamp DESC'); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Initiates a search for logs |
|
| 28 | - * |
|
| 29 | - * @param PdoDatabase $database |
|
| 30 | - * @param int|null $domain The domain to search for. This will retrieve both logs for the specified domain and |
|
| 31 | - * global logs if a value is specified. If null, only global logs are returned. |
|
| 32 | - * |
|
| 33 | - * @return LogSearchHelper |
|
| 34 | - */ |
|
| 35 | - public static function get(PdoDatabase $database, ?int $domain) |
|
| 36 | - { |
|
| 37 | - $helper = new LogSearchHelper($database); |
|
| 26 | + /** |
|
| 27 | + * Initiates a search for logs |
|
| 28 | + * |
|
| 29 | + * @param PdoDatabase $database |
|
| 30 | + * @param int|null $domain The domain to search for. This will retrieve both logs for the specified domain and |
|
| 31 | + * global logs if a value is specified. If null, only global logs are returned. |
|
| 32 | + * |
|
| 33 | + * @return LogSearchHelper |
|
| 34 | + */ |
|
| 35 | + public static function get(PdoDatabase $database, ?int $domain) |
|
| 36 | + { |
|
| 37 | + $helper = new LogSearchHelper($database); |
|
| 38 | 38 | |
| 39 | - if ($domain === null) { |
|
| 40 | - $helper->whereClause .= ' AND domain IS NULL'; |
|
| 41 | - } |
|
| 42 | - else { |
|
| 43 | - $helper->whereClause .= ' AND coalesce(domain, ?) = ?'; |
|
| 44 | - $helper->parameterList[] = $domain; |
|
| 45 | - $helper->parameterList[] = $domain; |
|
| 46 | - } |
|
| 39 | + if ($domain === null) { |
|
| 40 | + $helper->whereClause .= ' AND domain IS NULL'; |
|
| 41 | + } |
|
| 42 | + else { |
|
| 43 | + $helper->whereClause .= ' AND coalesce(domain, ?) = ?'; |
|
| 44 | + $helper->parameterList[] = $domain; |
|
| 45 | + $helper->parameterList[] = $domain; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - return $helper; |
|
| 49 | - } |
|
| 48 | + return $helper; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Filters the results by user |
|
| 53 | - * |
|
| 54 | - * @param int $userId |
|
| 55 | - * |
|
| 56 | - * @return $this |
|
| 57 | - */ |
|
| 58 | - public function byUser($userId) |
|
| 59 | - { |
|
| 60 | - $this->whereClause .= ' AND user = ?'; |
|
| 61 | - $this->parameterList[] = $userId; |
|
| 51 | + /** |
|
| 52 | + * Filters the results by user |
|
| 53 | + * |
|
| 54 | + * @param int $userId |
|
| 55 | + * |
|
| 56 | + * @return $this |
|
| 57 | + */ |
|
| 58 | + public function byUser($userId) |
|
| 59 | + { |
|
| 60 | + $this->whereClause .= ' AND user = ?'; |
|
| 61 | + $this->parameterList[] = $userId; |
|
| 62 | 62 | |
| 63 | - return $this; |
|
| 64 | - } |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Filters the results by log action |
|
| 68 | - * |
|
| 69 | - * @param string $action |
|
| 70 | - * |
|
| 71 | - * @return $this |
|
| 72 | - */ |
|
| 73 | - public function byAction($action) |
|
| 74 | - { |
|
| 75 | - $this->whereClause .= ' AND action = ?'; |
|
| 76 | - $this->parameterList[] = $action; |
|
| 66 | + /** |
|
| 67 | + * Filters the results by log action |
|
| 68 | + * |
|
| 69 | + * @param string $action |
|
| 70 | + * |
|
| 71 | + * @return $this |
|
| 72 | + */ |
|
| 73 | + public function byAction($action) |
|
| 74 | + { |
|
| 75 | + $this->whereClause .= ' AND action = ?'; |
|
| 76 | + $this->parameterList[] = $action; |
|
| 77 | 77 | |
| 78 | - return $this; |
|
| 79 | - } |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Filters the results by object type |
|
| 83 | - * |
|
| 84 | - * @param string $objectType |
|
| 85 | - * |
|
| 86 | - * @return $this |
|
| 87 | - */ |
|
| 88 | - public function byObjectType($objectType) |
|
| 89 | - { |
|
| 90 | - $this->whereClause .= ' AND objecttype = ?'; |
|
| 91 | - $this->parameterList[] = $objectType; |
|
| 81 | + /** |
|
| 82 | + * Filters the results by object type |
|
| 83 | + * |
|
| 84 | + * @param string $objectType |
|
| 85 | + * |
|
| 86 | + * @return $this |
|
| 87 | + */ |
|
| 88 | + public function byObjectType($objectType) |
|
| 89 | + { |
|
| 90 | + $this->whereClause .= ' AND objecttype = ?'; |
|
| 91 | + $this->parameterList[] = $objectType; |
|
| 92 | 92 | |
| 93 | - return $this; |
|
| 94 | - } |
|
| 93 | + return $this; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Filters the results by object type |
|
| 98 | - * |
|
| 99 | - * @param integer $objectId |
|
| 100 | - * |
|
| 101 | - * @return $this |
|
| 102 | - */ |
|
| 103 | - public function byObjectId($objectId) |
|
| 104 | - { |
|
| 105 | - $this->whereClause .= ' AND objectid = ?'; |
|
| 106 | - $this->parameterList[] = $objectId; |
|
| 96 | + /** |
|
| 97 | + * Filters the results by object type |
|
| 98 | + * |
|
| 99 | + * @param integer $objectId |
|
| 100 | + * |
|
| 101 | + * @return $this |
|
| 102 | + */ |
|
| 103 | + public function byObjectId($objectId) |
|
| 104 | + { |
|
| 105 | + $this->whereClause .= ' AND objectid = ?'; |
|
| 106 | + $this->parameterList[] = $objectId; |
|
| 107 | 107 | |
| 108 | - return $this; |
|
| 109 | - } |
|
| 108 | + return $this; |
|
| 109 | + } |
|
| 110 | 110 | } |
| 111 | 111 | \ No newline at end of file |
@@ -38,8 +38,7 @@ |
||
| 38 | 38 | |
| 39 | 39 | if ($domain === null) { |
| 40 | 40 | $helper->whereClause .= ' AND domain IS NULL'; |
| 41 | - } |
|
| 42 | - else { |
|
| 41 | + } else { |
|
| 43 | 42 | $helper->whereClause .= ' AND coalesce(domain, ?) = ?'; |
| 44 | 43 | $helper->parameterList[] = $domain; |
| 45 | 44 | $helper->parameterList[] = $domain; |
@@ -23,124 +23,124 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | abstract class DataObject |
| 25 | 25 | { |
| 26 | - /** @var int|null ID of the object */ |
|
| 27 | - protected $id = null; |
|
| 28 | - /** @var int update version for optimistic locking */ |
|
| 29 | - protected $updateversion = 0; |
|
| 30 | - /** |
|
| 31 | - * @var PdoDatabase |
|
| 32 | - */ |
|
| 33 | - protected $dbObject; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Retrieves a data object by it's row ID. |
|
| 37 | - * |
|
| 38 | - * @param int $id |
|
| 39 | - * @param PdoDatabase $database |
|
| 40 | - * |
|
| 41 | - * @return DataObject|false |
|
| 42 | - */ |
|
| 43 | - public static function getById($id, PdoDatabase $database) |
|
| 44 | - { |
|
| 45 | - $array = explode('\\', get_called_class()); |
|
| 46 | - $realClassName = strtolower(end($array)); |
|
| 47 | - |
|
| 48 | - $statement = $database->prepare("SELECT * FROM {$realClassName} WHERE id = :id LIMIT 1;"); |
|
| 49 | - $statement->bindValue(":id", $id); |
|
| 50 | - |
|
| 51 | - $statement->execute(); |
|
| 52 | - |
|
| 53 | - $resultObject = $statement->fetchObject(get_called_class()); |
|
| 54 | - |
|
| 55 | - if ($resultObject != false) { |
|
| 56 | - $resultObject->setDatabase($database); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - return $resultObject; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function setDatabase(PdoDatabase $db) |
|
| 63 | - { |
|
| 64 | - $this->dbObject = $db; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Gets the database associated with this data object. |
|
| 69 | - * @return PdoDatabase |
|
| 70 | - */ |
|
| 71 | - public function getDatabase() |
|
| 72 | - { |
|
| 73 | - return $this->dbObject; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Saves a data object to the database, either updating or inserting a record. |
|
| 78 | - * |
|
| 79 | - * @return void |
|
| 80 | - */ |
|
| 81 | - abstract public function save(); |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Retrieves the ID attribute |
|
| 85 | - */ |
|
| 86 | - public function getId() |
|
| 87 | - { |
|
| 88 | - return (int)$this->id; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Deletes the object from the database |
|
| 93 | - */ |
|
| 94 | - public function delete() |
|
| 95 | - { |
|
| 96 | - if ($this->id === null) { |
|
| 97 | - // wtf? |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $array = explode('\\', get_called_class()); |
|
| 102 | - $realClassName = strtolower(end($array)); |
|
| 103 | - |
|
| 104 | - $deleteQuery = "DELETE FROM {$realClassName} WHERE id = :id AND updateversion = :updateversion;"; |
|
| 105 | - $statement = $this->dbObject->prepare($deleteQuery); |
|
| 106 | - |
|
| 107 | - $statement->bindValue(":id", $this->id); |
|
| 108 | - $statement->bindValue(":updateversion", $this->updateversion); |
|
| 109 | - $statement->execute(); |
|
| 110 | - |
|
| 111 | - if ($statement->rowCount() !== 1) { |
|
| 112 | - throw new OptimisticLockFailedException(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $this->id = null; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return int |
|
| 120 | - */ |
|
| 121 | - public function getUpdateVersion() |
|
| 122 | - { |
|
| 123 | - return $this->updateversion; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Sets the update version. |
|
| 128 | - * |
|
| 129 | - * You should never call this to change the value of the update version. You should only call it when passing user |
|
| 130 | - * input through. |
|
| 131 | - * |
|
| 132 | - * @param int $updateVersion |
|
| 133 | - */ |
|
| 134 | - public function setUpdateVersion($updateVersion) |
|
| 135 | - { |
|
| 136 | - $this->updateversion = $updateVersion; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @return bool |
|
| 141 | - */ |
|
| 142 | - public function isNew() |
|
| 143 | - { |
|
| 144 | - return $this->id === null; |
|
| 145 | - } |
|
| 26 | + /** @var int|null ID of the object */ |
|
| 27 | + protected $id = null; |
|
| 28 | + /** @var int update version for optimistic locking */ |
|
| 29 | + protected $updateversion = 0; |
|
| 30 | + /** |
|
| 31 | + * @var PdoDatabase |
|
| 32 | + */ |
|
| 33 | + protected $dbObject; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Retrieves a data object by it's row ID. |
|
| 37 | + * |
|
| 38 | + * @param int $id |
|
| 39 | + * @param PdoDatabase $database |
|
| 40 | + * |
|
| 41 | + * @return DataObject|false |
|
| 42 | + */ |
|
| 43 | + public static function getById($id, PdoDatabase $database) |
|
| 44 | + { |
|
| 45 | + $array = explode('\\', get_called_class()); |
|
| 46 | + $realClassName = strtolower(end($array)); |
|
| 47 | + |
|
| 48 | + $statement = $database->prepare("SELECT * FROM {$realClassName} WHERE id = :id LIMIT 1;"); |
|
| 49 | + $statement->bindValue(":id", $id); |
|
| 50 | + |
|
| 51 | + $statement->execute(); |
|
| 52 | + |
|
| 53 | + $resultObject = $statement->fetchObject(get_called_class()); |
|
| 54 | + |
|
| 55 | + if ($resultObject != false) { |
|
| 56 | + $resultObject->setDatabase($database); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + return $resultObject; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function setDatabase(PdoDatabase $db) |
|
| 63 | + { |
|
| 64 | + $this->dbObject = $db; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Gets the database associated with this data object. |
|
| 69 | + * @return PdoDatabase |
|
| 70 | + */ |
|
| 71 | + public function getDatabase() |
|
| 72 | + { |
|
| 73 | + return $this->dbObject; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Saves a data object to the database, either updating or inserting a record. |
|
| 78 | + * |
|
| 79 | + * @return void |
|
| 80 | + */ |
|
| 81 | + abstract public function save(); |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Retrieves the ID attribute |
|
| 85 | + */ |
|
| 86 | + public function getId() |
|
| 87 | + { |
|
| 88 | + return (int)$this->id; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Deletes the object from the database |
|
| 93 | + */ |
|
| 94 | + public function delete() |
|
| 95 | + { |
|
| 96 | + if ($this->id === null) { |
|
| 97 | + // wtf? |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $array = explode('\\', get_called_class()); |
|
| 102 | + $realClassName = strtolower(end($array)); |
|
| 103 | + |
|
| 104 | + $deleteQuery = "DELETE FROM {$realClassName} WHERE id = :id AND updateversion = :updateversion;"; |
|
| 105 | + $statement = $this->dbObject->prepare($deleteQuery); |
|
| 106 | + |
|
| 107 | + $statement->bindValue(":id", $this->id); |
|
| 108 | + $statement->bindValue(":updateversion", $this->updateversion); |
|
| 109 | + $statement->execute(); |
|
| 110 | + |
|
| 111 | + if ($statement->rowCount() !== 1) { |
|
| 112 | + throw new OptimisticLockFailedException(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $this->id = null; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return int |
|
| 120 | + */ |
|
| 121 | + public function getUpdateVersion() |
|
| 122 | + { |
|
| 123 | + return $this->updateversion; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Sets the update version. |
|
| 128 | + * |
|
| 129 | + * You should never call this to change the value of the update version. You should only call it when passing user |
|
| 130 | + * input through. |
|
| 131 | + * |
|
| 132 | + * @param int $updateVersion |
|
| 133 | + */ |
|
| 134 | + public function setUpdateVersion($updateVersion) |
|
| 135 | + { |
|
| 136 | + $this->updateversion = $updateVersion; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @return bool |
|
| 141 | + */ |
|
| 142 | + public function isNew() |
|
| 143 | + { |
|
| 144 | + return $this->id === null; |
|
| 145 | + } |
|
| 146 | 146 | } |
@@ -26,133 +26,133 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | class IdentificationVerifier |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
| 31 | - * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
| 32 | - * of these values is *not* necessary; this is done automatically. |
|
| 33 | - * |
|
| 34 | - * @var string[] |
|
| 35 | - * @category Security-Critical |
|
| 36 | - */ |
|
| 37 | - private static $apiQueryParameters = array( |
|
| 38 | - 'action' => 'query', |
|
| 39 | - 'format' => 'json', |
|
| 40 | - 'prop' => 'links', |
|
| 41 | - // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
| 42 | - 'titles' => '', |
|
| 43 | - // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
| 44 | - 'pltitles' => '', |
|
| 45 | - ); |
|
| 46 | - /** @var HttpHelper */ |
|
| 47 | - private $httpHelper; |
|
| 48 | - /** @var SiteConfiguration */ |
|
| 49 | - private $siteConfiguration; |
|
| 50 | - /** @var PdoDatabase */ |
|
| 51 | - private $dbObject; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * IdentificationVerifier constructor. |
|
| 55 | - * |
|
| 56 | - * @param HttpHelper $httpHelper |
|
| 57 | - * @param SiteConfiguration $siteConfiguration |
|
| 58 | - * @param PdoDatabase $dbObject |
|
| 59 | - */ |
|
| 60 | - public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 61 | - { |
|
| 62 | - $this->httpHelper = $httpHelper; |
|
| 63 | - $this->siteConfiguration = $siteConfiguration; |
|
| 64 | - $this->dbObject = $dbObject; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Checks if the given user is identified to the Wikimedia Foundation. |
|
| 69 | - * |
|
| 70 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 71 | - * |
|
| 72 | - * @return bool |
|
| 73 | - * @category Security-Critical |
|
| 74 | - * @throws EnvironmentException |
|
| 75 | - */ |
|
| 76 | - public function isUserIdentified($onWikiName) |
|
| 77 | - { |
|
| 78 | - if ($this->checkIdentificationCache($onWikiName)) { |
|
| 79 | - return true; |
|
| 80 | - } |
|
| 81 | - else { |
|
| 82 | - if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
| 83 | - $this->cacheIdentificationStatus($onWikiName); |
|
| 84 | - |
|
| 85 | - return true; |
|
| 86 | - } |
|
| 87 | - else { |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Checks if the given user has a valid entry in the idcache table. |
|
| 95 | - * |
|
| 96 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 97 | - * |
|
| 98 | - * @return bool |
|
| 99 | - * @category Security-Critical |
|
| 100 | - */ |
|
| 101 | - private function checkIdentificationCache($onWikiName) |
|
| 102 | - { |
|
| 103 | - $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
| 104 | - |
|
| 105 | - $query = <<<SQL |
|
| 29 | + /** |
|
| 30 | + * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
| 31 | + * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
| 32 | + * of these values is *not* necessary; this is done automatically. |
|
| 33 | + * |
|
| 34 | + * @var string[] |
|
| 35 | + * @category Security-Critical |
|
| 36 | + */ |
|
| 37 | + private static $apiQueryParameters = array( |
|
| 38 | + 'action' => 'query', |
|
| 39 | + 'format' => 'json', |
|
| 40 | + 'prop' => 'links', |
|
| 41 | + // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
| 42 | + 'titles' => '', |
|
| 43 | + // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
| 44 | + 'pltitles' => '', |
|
| 45 | + ); |
|
| 46 | + /** @var HttpHelper */ |
|
| 47 | + private $httpHelper; |
|
| 48 | + /** @var SiteConfiguration */ |
|
| 49 | + private $siteConfiguration; |
|
| 50 | + /** @var PdoDatabase */ |
|
| 51 | + private $dbObject; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * IdentificationVerifier constructor. |
|
| 55 | + * |
|
| 56 | + * @param HttpHelper $httpHelper |
|
| 57 | + * @param SiteConfiguration $siteConfiguration |
|
| 58 | + * @param PdoDatabase $dbObject |
|
| 59 | + */ |
|
| 60 | + public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 61 | + { |
|
| 62 | + $this->httpHelper = $httpHelper; |
|
| 63 | + $this->siteConfiguration = $siteConfiguration; |
|
| 64 | + $this->dbObject = $dbObject; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Checks if the given user is identified to the Wikimedia Foundation. |
|
| 69 | + * |
|
| 70 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 71 | + * |
|
| 72 | + * @return bool |
|
| 73 | + * @category Security-Critical |
|
| 74 | + * @throws EnvironmentException |
|
| 75 | + */ |
|
| 76 | + public function isUserIdentified($onWikiName) |
|
| 77 | + { |
|
| 78 | + if ($this->checkIdentificationCache($onWikiName)) { |
|
| 79 | + return true; |
|
| 80 | + } |
|
| 81 | + else { |
|
| 82 | + if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
| 83 | + $this->cacheIdentificationStatus($onWikiName); |
|
| 84 | + |
|
| 85 | + return true; |
|
| 86 | + } |
|
| 87 | + else { |
|
| 88 | + return false; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Checks if the given user has a valid entry in the idcache table. |
|
| 95 | + * |
|
| 96 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 97 | + * |
|
| 98 | + * @return bool |
|
| 99 | + * @category Security-Critical |
|
| 100 | + */ |
|
| 101 | + private function checkIdentificationCache($onWikiName) |
|
| 102 | + { |
|
| 103 | + $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
| 104 | + |
|
| 105 | + $query = <<<SQL |
|
| 106 | 106 | SELECT COUNT(`id`) |
| 107 | 107 | FROM `idcache` |
| 108 | 108 | WHERE `onwikiusername` = :onwikiname |
| 109 | 109 | AND DATE_ADD(`checktime`, INTERVAL {$interval}) >= NOW(); |
| 110 | 110 | SQL; |
| 111 | - $stmt = $this->dbObject->prepare($query); |
|
| 112 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 113 | - $stmt->execute(); |
|
| 114 | - |
|
| 115 | - // Guaranteed by the query to only return a single row with a single column |
|
| 116 | - $results = $stmt->fetch(PDO::FETCH_NUM); |
|
| 117 | - |
|
| 118 | - // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
| 119 | - // unique key - but meh. |
|
| 120 | - return $results[0] != 0; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
| 125 | - * idcache table. Meant to be called periodically by a maintenance script. |
|
| 126 | - * |
|
| 127 | - * @param SiteConfiguration $siteConfiguration |
|
| 128 | - * @param PdoDatabase $dbObject |
|
| 129 | - * |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 133 | - { |
|
| 134 | - $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
| 135 | - |
|
| 136 | - $query = <<<SQL |
|
| 111 | + $stmt = $this->dbObject->prepare($query); |
|
| 112 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 113 | + $stmt->execute(); |
|
| 114 | + |
|
| 115 | + // Guaranteed by the query to only return a single row with a single column |
|
| 116 | + $results = $stmt->fetch(PDO::FETCH_NUM); |
|
| 117 | + |
|
| 118 | + // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
| 119 | + // unique key - but meh. |
|
| 120 | + return $results[0] != 0; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
| 125 | + * idcache table. Meant to be called periodically by a maintenance script. |
|
| 126 | + * |
|
| 127 | + * @param SiteConfiguration $siteConfiguration |
|
| 128 | + * @param PdoDatabase $dbObject |
|
| 129 | + * |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
| 133 | + { |
|
| 134 | + $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
| 135 | + |
|
| 136 | + $query = <<<SQL |
|
| 137 | 137 | DELETE FROM `idcache` |
| 138 | 138 | WHERE DATE_ADD(`checktime`, INTERVAL {$interval}) < NOW(); |
| 139 | 139 | SQL; |
| 140 | - $dbObject->prepare($query)->execute(); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
| 145 | - * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
| 146 | - * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
| 147 | - * |
|
| 148 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 149 | - * |
|
| 150 | - * @return void |
|
| 151 | - * @category Security-Critical |
|
| 152 | - */ |
|
| 153 | - private function cacheIdentificationStatus($onWikiName) |
|
| 154 | - { |
|
| 155 | - $query = <<<SQL |
|
| 140 | + $dbObject->prepare($query)->execute(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
| 145 | + * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
| 146 | + * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
| 147 | + * |
|
| 148 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 149 | + * |
|
| 150 | + * @return void |
|
| 151 | + * @category Security-Critical |
|
| 152 | + */ |
|
| 153 | + private function cacheIdentificationStatus($onWikiName) |
|
| 154 | + { |
|
| 155 | + $query = <<<SQL |
|
| 156 | 156 | INSERT INTO `idcache` |
| 157 | 157 | (`onwikiusername`) |
| 158 | 158 | VALUES |
@@ -161,46 +161,46 @@ discard block |
||
| 161 | 161 | `onwikiusername` = VALUES(`onwikiusername`), |
| 162 | 162 | `checktime` = CURRENT_TIMESTAMP; |
| 163 | 163 | SQL; |
| 164 | - $stmt = $this->dbObject->prepare($query); |
|
| 165 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 166 | - $stmt->execute(); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
| 171 | - * |
|
| 172 | - * @param string $onWikiName The Wikipedia username of the user |
|
| 173 | - * |
|
| 174 | - * @return bool |
|
| 175 | - * @throws EnvironmentException |
|
| 176 | - * @category Security-Critical |
|
| 177 | - */ |
|
| 178 | - private function isIdentifiedOnWiki($onWikiName) |
|
| 179 | - { |
|
| 180 | - $strings = new StringFunctions(); |
|
| 181 | - |
|
| 182 | - // First character of Wikipedia usernames is always capitalized. |
|
| 183 | - $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
| 184 | - |
|
| 185 | - $parameters = self::$apiQueryParameters; |
|
| 186 | - $parameters['pltitles'] = "User:" . $onWikiName; |
|
| 187 | - $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
| 188 | - |
|
| 189 | - try { |
|
| 190 | - $endpoint = $this->siteConfiguration->getIdentificationNoticeboardWebserviceEndpoint(); |
|
| 191 | - $response = $this->httpHelper->get($endpoint, $parameters); |
|
| 192 | - $response = json_decode($response, true); |
|
| 193 | - } |
|
| 194 | - catch (CurlException $ex) { |
|
| 195 | - // failed getting identification status, so throw a nicer error. |
|
| 196 | - $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
| 197 | - . 'This is probably a transient error, so please try again.'; |
|
| 198 | - |
|
| 199 | - throw new EnvironmentException($message); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $page = @array_pop($response['query']['pages']); |
|
| 203 | - |
|
| 204 | - return @$page['links'][0]['title'] === "User:" . $onWikiName; |
|
| 205 | - } |
|
| 164 | + $stmt = $this->dbObject->prepare($query); |
|
| 165 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
| 166 | + $stmt->execute(); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
| 171 | + * |
|
| 172 | + * @param string $onWikiName The Wikipedia username of the user |
|
| 173 | + * |
|
| 174 | + * @return bool |
|
| 175 | + * @throws EnvironmentException |
|
| 176 | + * @category Security-Critical |
|
| 177 | + */ |
|
| 178 | + private function isIdentifiedOnWiki($onWikiName) |
|
| 179 | + { |
|
| 180 | + $strings = new StringFunctions(); |
|
| 181 | + |
|
| 182 | + // First character of Wikipedia usernames is always capitalized. |
|
| 183 | + $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
| 184 | + |
|
| 185 | + $parameters = self::$apiQueryParameters; |
|
| 186 | + $parameters['pltitles'] = "User:" . $onWikiName; |
|
| 187 | + $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
| 188 | + |
|
| 189 | + try { |
|
| 190 | + $endpoint = $this->siteConfiguration->getIdentificationNoticeboardWebserviceEndpoint(); |
|
| 191 | + $response = $this->httpHelper->get($endpoint, $parameters); |
|
| 192 | + $response = json_decode($response, true); |
|
| 193 | + } |
|
| 194 | + catch (CurlException $ex) { |
|
| 195 | + // failed getting identification status, so throw a nicer error. |
|
| 196 | + $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
| 197 | + . 'This is probably a transient error, so please try again.'; |
|
| 198 | + |
|
| 199 | + throw new EnvironmentException($message); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $page = @array_pop($response['query']['pages']); |
|
| 203 | + |
|
| 204 | + return @$page['links'][0]['title'] === "User:" . $onWikiName; |
|
| 205 | + } |
|
| 206 | 206 | } |
@@ -77,14 +77,12 @@ |
||
| 77 | 77 | { |
| 78 | 78 | if ($this->checkIdentificationCache($onWikiName)) { |
| 79 | 79 | return true; |
| 80 | - } |
|
| 81 | - else { |
|
| 80 | + } else { |
|
| 82 | 81 | if ($this->isIdentifiedOnWiki($onWikiName)) { |
| 83 | 82 | $this->cacheIdentificationStatus($onWikiName); |
| 84 | 83 | |
| 85 | 84 | return true; |
| 86 | - } |
|
| 87 | - else { |
|
| 85 | + } else { |
|
| 88 | 86 | return false; |
| 89 | 87 | } |
| 90 | 88 | } |
@@ -13,22 +13,22 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | class ExceptionHandler |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Global exception handler |
|
| 18 | - * |
|
| 19 | - * Smarty would be nice to use, but it COULD BE smarty that throws the errors. |
|
| 20 | - * Let's build something ourselves, and hope it works. |
|
| 21 | - * |
|
| 22 | - * @param $exception |
|
| 23 | - * |
|
| 24 | - * @category Security-Critical - has the potential to leak data when exception is thrown. |
|
| 25 | - */ |
|
| 26 | - public static function exceptionHandler(Throwable $exception) |
|
| 27 | - { |
|
| 28 | - /** @global $siteConfiguration SiteConfiguration */ |
|
| 29 | - global $siteConfiguration; |
|
| 30 | - |
|
| 31 | - $errorDocument = <<<HTML |
|
| 16 | + /** |
|
| 17 | + * Global exception handler |
|
| 18 | + * |
|
| 19 | + * Smarty would be nice to use, but it COULD BE smarty that throws the errors. |
|
| 20 | + * Let's build something ourselves, and hope it works. |
|
| 21 | + * |
|
| 22 | + * @param $exception |
|
| 23 | + * |
|
| 24 | + * @category Security-Critical - has the potential to leak data when exception is thrown. |
|
| 25 | + */ |
|
| 26 | + public static function exceptionHandler(Throwable $exception) |
|
| 27 | + { |
|
| 28 | + /** @global $siteConfiguration SiteConfiguration */ |
|
| 29 | + global $siteConfiguration; |
|
| 30 | + |
|
| 31 | + $errorDocument = <<<HTML |
|
| 32 | 32 | <!DOCTYPE html> |
| 33 | 33 | <html lang="en"><head> |
| 34 | 34 | <meta charset="utf-8"> |
@@ -48,104 +48,104 @@ discard block |
||
| 48 | 48 | </div></body></html> |
| 49 | 49 | HTML; |
| 50 | 50 | |
| 51 | - list($errorData, $errorId) = self::logExceptionToDisk($exception, $siteConfiguration, true); |
|
| 52 | - |
|
| 53 | - // clear and discard any content that's been saved to the output buffer |
|
| 54 | - if (ob_get_level() > 0) { |
|
| 55 | - ob_end_clean(); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - // push error ID into the document. |
|
| 59 | - $message = str_replace('$1$', $errorId, $errorDocument); |
|
| 60 | - |
|
| 61 | - if ($siteConfiguration->getDebuggingTraceEnabled()) { |
|
| 62 | - ob_start(); |
|
| 63 | - var_dump($errorData); |
|
| 64 | - $textErrorData = ob_get_contents(); |
|
| 65 | - ob_end_clean(); |
|
| 66 | - |
|
| 67 | - $message = str_replace('$2$', $textErrorData, $message); |
|
| 68 | - } |
|
| 69 | - else { |
|
| 70 | - $message = str_replace('$2$', "", $message); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // While we *shouldn't* have sent headers by now due to the output buffering, PHPUnit does weird things. |
|
| 74 | - // This is "only" needed for the tests, but it's a good idea to wrap this anyway. |
|
| 75 | - if (!headers_sent()) { |
|
| 76 | - header('HTTP/1.1 500 Internal Server Error'); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - // output the document |
|
| 80 | - print $message; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @param int $errorSeverity The severity level of the exception. |
|
| 85 | - * @param string $errorMessage The Exception message to throw. |
|
| 86 | - * @param string $errorFile The filename where the exception is thrown. |
|
| 87 | - * @param int $errorLine The line number where the exception is thrown. |
|
| 88 | - * |
|
| 89 | - * @throws ErrorException |
|
| 90 | - */ |
|
| 91 | - public static function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine) |
|
| 92 | - { |
|
| 93 | - // call into the main exception handler above |
|
| 94 | - throw new ErrorException($errorMessage, 0, $errorSeverity, $errorFile, $errorLine); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @param Throwable $exception |
|
| 99 | - * |
|
| 100 | - * @return null|array |
|
| 101 | - */ |
|
| 102 | - public static function getExceptionData($exception) |
|
| 103 | - { |
|
| 104 | - if ($exception == null) { |
|
| 105 | - return null; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $array = array( |
|
| 109 | - 'exception' => get_class($exception), |
|
| 110 | - 'message' => $exception->getMessage(), |
|
| 111 | - 'stack' => $exception->getTraceAsString(), |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - $array['previous'] = self::getExceptionData($exception->getPrevious()); |
|
| 115 | - |
|
| 116 | - return $array; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - public static function logExceptionToDisk( |
|
| 120 | - Throwable $exception, |
|
| 121 | - SiteConfiguration $siteConfiguration, |
|
| 122 | - bool $fromGlobalHandler = false |
|
| 123 | - ): array { |
|
| 124 | - $errorData = self::getExceptionData($exception); |
|
| 125 | - $errorData['server'] = $_SERVER; |
|
| 126 | - $errorData['get'] = $_GET; |
|
| 127 | - $errorData['post'] = $_POST; |
|
| 128 | - |
|
| 129 | - $redactions = ['password', 'newpassword', 'newpasswordconfirm', 'otp']; |
|
| 130 | - foreach ($redactions as $redaction) { |
|
| 131 | - if (isset($errorData['post'][$redaction])) { |
|
| 132 | - $errorData['post'][$redaction] = '<redacted>'; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - if ($fromGlobalHandler) { |
|
| 137 | - $errorData['globalHandler'] = true; |
|
| 138 | - } |
|
| 139 | - else { |
|
| 140 | - $errorData['globalHandler'] = false; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $state = serialize($errorData); |
|
| 144 | - $errorId = sha1($state); |
|
| 145 | - |
|
| 146 | - // Save the error for later analysis |
|
| 147 | - file_put_contents($siteConfiguration->getErrorLog() . '/' . $errorId . '.log', $state); |
|
| 148 | - |
|
| 149 | - return array($errorData, $errorId); |
|
| 150 | - } |
|
| 51 | + list($errorData, $errorId) = self::logExceptionToDisk($exception, $siteConfiguration, true); |
|
| 52 | + |
|
| 53 | + // clear and discard any content that's been saved to the output buffer |
|
| 54 | + if (ob_get_level() > 0) { |
|
| 55 | + ob_end_clean(); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + // push error ID into the document. |
|
| 59 | + $message = str_replace('$1$', $errorId, $errorDocument); |
|
| 60 | + |
|
| 61 | + if ($siteConfiguration->getDebuggingTraceEnabled()) { |
|
| 62 | + ob_start(); |
|
| 63 | + var_dump($errorData); |
|
| 64 | + $textErrorData = ob_get_contents(); |
|
| 65 | + ob_end_clean(); |
|
| 66 | + |
|
| 67 | + $message = str_replace('$2$', $textErrorData, $message); |
|
| 68 | + } |
|
| 69 | + else { |
|
| 70 | + $message = str_replace('$2$', "", $message); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // While we *shouldn't* have sent headers by now due to the output buffering, PHPUnit does weird things. |
|
| 74 | + // This is "only" needed for the tests, but it's a good idea to wrap this anyway. |
|
| 75 | + if (!headers_sent()) { |
|
| 76 | + header('HTTP/1.1 500 Internal Server Error'); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + // output the document |
|
| 80 | + print $message; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @param int $errorSeverity The severity level of the exception. |
|
| 85 | + * @param string $errorMessage The Exception message to throw. |
|
| 86 | + * @param string $errorFile The filename where the exception is thrown. |
|
| 87 | + * @param int $errorLine The line number where the exception is thrown. |
|
| 88 | + * |
|
| 89 | + * @throws ErrorException |
|
| 90 | + */ |
|
| 91 | + public static function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine) |
|
| 92 | + { |
|
| 93 | + // call into the main exception handler above |
|
| 94 | + throw new ErrorException($errorMessage, 0, $errorSeverity, $errorFile, $errorLine); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @param Throwable $exception |
|
| 99 | + * |
|
| 100 | + * @return null|array |
|
| 101 | + */ |
|
| 102 | + public static function getExceptionData($exception) |
|
| 103 | + { |
|
| 104 | + if ($exception == null) { |
|
| 105 | + return null; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $array = array( |
|
| 109 | + 'exception' => get_class($exception), |
|
| 110 | + 'message' => $exception->getMessage(), |
|
| 111 | + 'stack' => $exception->getTraceAsString(), |
|
| 112 | + ); |
|
| 113 | + |
|
| 114 | + $array['previous'] = self::getExceptionData($exception->getPrevious()); |
|
| 115 | + |
|
| 116 | + return $array; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + public static function logExceptionToDisk( |
|
| 120 | + Throwable $exception, |
|
| 121 | + SiteConfiguration $siteConfiguration, |
|
| 122 | + bool $fromGlobalHandler = false |
|
| 123 | + ): array { |
|
| 124 | + $errorData = self::getExceptionData($exception); |
|
| 125 | + $errorData['server'] = $_SERVER; |
|
| 126 | + $errorData['get'] = $_GET; |
|
| 127 | + $errorData['post'] = $_POST; |
|
| 128 | + |
|
| 129 | + $redactions = ['password', 'newpassword', 'newpasswordconfirm', 'otp']; |
|
| 130 | + foreach ($redactions as $redaction) { |
|
| 131 | + if (isset($errorData['post'][$redaction])) { |
|
| 132 | + $errorData['post'][$redaction] = '<redacted>'; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + if ($fromGlobalHandler) { |
|
| 137 | + $errorData['globalHandler'] = true; |
|
| 138 | + } |
|
| 139 | + else { |
|
| 140 | + $errorData['globalHandler'] = false; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $state = serialize($errorData); |
|
| 144 | + $errorId = sha1($state); |
|
| 145 | + |
|
| 146 | + // Save the error for later analysis |
|
| 147 | + file_put_contents($siteConfiguration->getErrorLog() . '/' . $errorId . '.log', $state); |
|
| 148 | + |
|
| 149 | + return array($errorData, $errorId); |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -65,8 +65,7 @@ discard block |
||
| 65 | 65 | ob_end_clean(); |
| 66 | 66 | |
| 67 | 67 | $message = str_replace('$2$', $textErrorData, $message); |
| 68 | - } |
|
| 69 | - else { |
|
| 68 | + } else { |
|
| 70 | 69 | $message = str_replace('$2$', "", $message); |
| 71 | 70 | } |
| 72 | 71 | |
@@ -135,8 +134,7 @@ discard block |
||
| 135 | 134 | |
| 136 | 135 | if ($fromGlobalHandler) { |
| 137 | 136 | $errorData['globalHandler'] = true; |
| 138 | - } |
|
| 139 | - else { |
|
| 137 | + } else { |
|
| 140 | 138 | $errorData['globalHandler'] = false; |
| 141 | 139 | } |
| 142 | 140 | |
@@ -36,111 +36,111 @@ |
||
| 36 | 36 | |
| 37 | 37 | trait NavigationMenuAccessControl |
| 38 | 38 | { |
| 39 | - protected abstract function assign($name, $value); |
|
| 39 | + protected abstract function assign($name, $value); |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return SecurityManager |
|
| 43 | - */ |
|
| 44 | - protected abstract function getSecurityManager(); |
|
| 41 | + /** |
|
| 42 | + * @return SecurityManager |
|
| 43 | + */ |
|
| 44 | + protected abstract function getSecurityManager(); |
|
| 45 | 45 | |
| 46 | - public abstract function getDomainAccessManager(): DomainAccessManager; |
|
| 46 | + public abstract function getDomainAccessManager(): DomainAccessManager; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @param $currentUser |
|
| 50 | - */ |
|
| 51 | - protected function setupNavMenuAccess($currentUser) |
|
| 52 | - { |
|
| 53 | - $this->assign('nav__canRequests', $this->getSecurityManager() |
|
| 54 | - ->allows(PageMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 48 | + /** |
|
| 49 | + * @param $currentUser |
|
| 50 | + */ |
|
| 51 | + protected function setupNavMenuAccess($currentUser) |
|
| 52 | + { |
|
| 53 | + $this->assign('nav__canRequests', $this->getSecurityManager() |
|
| 54 | + ->allows(PageMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 55 | 55 | |
| 56 | - $this->assign('nav__canLogs', $this->getSecurityManager() |
|
| 57 | - ->allows(PageLog::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 58 | - $this->assign('nav__canUsers', $this->getSecurityManager() |
|
| 59 | - ->allows(StatsUsers::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 60 | - $this->assign('nav__canSearch', $this->getSecurityManager() |
|
| 61 | - ->allows(PageSearch::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 62 | - $this->assign('nav__canStats', $this->getSecurityManager() |
|
| 63 | - ->allows(StatsMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 56 | + $this->assign('nav__canLogs', $this->getSecurityManager() |
|
| 57 | + ->allows(PageLog::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 58 | + $this->assign('nav__canUsers', $this->getSecurityManager() |
|
| 59 | + ->allows(StatsUsers::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 60 | + $this->assign('nav__canSearch', $this->getSecurityManager() |
|
| 61 | + ->allows(PageSearch::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 62 | + $this->assign('nav__canStats', $this->getSecurityManager() |
|
| 63 | + ->allows(StatsMain::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 64 | 64 | |
| 65 | - $this->assign('nav__canBan', $this->getSecurityManager() |
|
| 66 | - ->allows(PageBan::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 67 | - $this->assign('nav__canEmailMgmt', $this->getSecurityManager() |
|
| 68 | - ->allows(PageEmailManagement::class, RoleConfiguration::MAIN, |
|
| 69 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 70 | - $this->assign('nav__canWelcomeMgmt', $this->getSecurityManager() |
|
| 71 | - ->allows(PageWelcomeTemplateManagement::class, RoleConfiguration::MAIN, |
|
| 72 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 73 | - $this->assign('nav__canSiteNoticeMgmt', $this->getSecurityManager() |
|
| 74 | - ->allows(PageSiteNotice::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 75 | - $this->assign('nav__canUserMgmt', $this->getSecurityManager() |
|
| 76 | - ->allows(PageUserManagement::class, RoleConfiguration::MAIN, |
|
| 77 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 78 | - $this->assign('nav__canJobQueue', $this->getSecurityManager() |
|
| 79 | - ->allows(PageJobQueue::class, RoleConfiguration::MAIN, |
|
| 80 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 81 | - $this->assign('nav__canDomainMgmt', $this->getSecurityManager() |
|
| 82 | - ->allows(PageDomainManagement::class, RoleConfiguration::MAIN, |
|
| 83 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 84 | - $this->assign('nav__canFlaggedComments', $this->getSecurityManager() |
|
| 85 | - ->allows(PageListFlaggedComments::class, RoleConfiguration::MAIN, |
|
| 86 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 87 | - $this->assign('nav__canQueueMgmt', $this->getSecurityManager() |
|
| 88 | - ->allows(PageQueueManagement::class, RoleConfiguration::MAIN, |
|
| 89 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 90 | - $this->assign('nav__canFormMgmt', $this->getSecurityManager() |
|
| 91 | - ->allows(PageRequestFormManagement::class, RoleConfiguration::MAIN, |
|
| 92 | - $currentUser) === SecurityManager::ALLOWED); |
|
| 93 | - $this->assign('nav__canErrorLog', $this->getSecurityManager() |
|
| 94 | - ->allows(PageErrorLogViewer::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 65 | + $this->assign('nav__canBan', $this->getSecurityManager() |
|
| 66 | + ->allows(PageBan::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 67 | + $this->assign('nav__canEmailMgmt', $this->getSecurityManager() |
|
| 68 | + ->allows(PageEmailManagement::class, RoleConfiguration::MAIN, |
|
| 69 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 70 | + $this->assign('nav__canWelcomeMgmt', $this->getSecurityManager() |
|
| 71 | + ->allows(PageWelcomeTemplateManagement::class, RoleConfiguration::MAIN, |
|
| 72 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 73 | + $this->assign('nav__canSiteNoticeMgmt', $this->getSecurityManager() |
|
| 74 | + ->allows(PageSiteNotice::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 75 | + $this->assign('nav__canUserMgmt', $this->getSecurityManager() |
|
| 76 | + ->allows(PageUserManagement::class, RoleConfiguration::MAIN, |
|
| 77 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 78 | + $this->assign('nav__canJobQueue', $this->getSecurityManager() |
|
| 79 | + ->allows(PageJobQueue::class, RoleConfiguration::MAIN, |
|
| 80 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 81 | + $this->assign('nav__canDomainMgmt', $this->getSecurityManager() |
|
| 82 | + ->allows(PageDomainManagement::class, RoleConfiguration::MAIN, |
|
| 83 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 84 | + $this->assign('nav__canFlaggedComments', $this->getSecurityManager() |
|
| 85 | + ->allows(PageListFlaggedComments::class, RoleConfiguration::MAIN, |
|
| 86 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 87 | + $this->assign('nav__canQueueMgmt', $this->getSecurityManager() |
|
| 88 | + ->allows(PageQueueManagement::class, RoleConfiguration::MAIN, |
|
| 89 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 90 | + $this->assign('nav__canFormMgmt', $this->getSecurityManager() |
|
| 91 | + ->allows(PageRequestFormManagement::class, RoleConfiguration::MAIN, |
|
| 92 | + $currentUser) === SecurityManager::ALLOWED); |
|
| 93 | + $this->assign('nav__canErrorLog', $this->getSecurityManager() |
|
| 94 | + ->allows(PageErrorLogViewer::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 95 | 95 | |
| 96 | - $this->assign('nav__canViewRequest', $this->getSecurityManager() |
|
| 97 | - ->allows(PageViewRequest::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 96 | + $this->assign('nav__canViewRequest', $this->getSecurityManager() |
|
| 97 | + ->allows(PageViewRequest::class, RoleConfiguration::MAIN, $currentUser) === SecurityManager::ALLOWED); |
|
| 98 | 98 | |
| 99 | - $this->assign('nav__domainList', []); |
|
| 100 | - if ($this->getDomainAccessManager() !== null) { |
|
| 101 | - $this->assign('nav__domainList', $this->getDomainAccessManager()->getAllowedDomains($currentUser)); |
|
| 102 | - } |
|
| 103 | - } |
|
| 99 | + $this->assign('nav__domainList', []); |
|
| 100 | + if ($this->getDomainAccessManager() !== null) { |
|
| 101 | + $this->assign('nav__domainList', $this->getDomainAccessManager()->getAllowedDomains($currentUser)); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Sets up the badges to draw attention to issues on various admin pages. |
|
| 107 | - * |
|
| 108 | - * This function checks to see if a user can access the pages, and if so checks the count of problem areas. |
|
| 109 | - * If problem areas are found, a number greater than 0 will cause the badge to show up. |
|
| 110 | - * |
|
| 111 | - * @param User $currentUser The current user |
|
| 112 | - * @param PdoDatabase $database Database instance |
|
| 113 | - * |
|
| 114 | - * @return void |
|
| 115 | - */ |
|
| 116 | - public function setUpNavBarBadges(User $currentUser, PdoDatabase $database) { |
|
| 117 | - // Set up some variables. |
|
| 118 | - // A size of 0 causes nothing to show up on the page (checked on navigation-menu.tpl) so leaving it 0 here is fine. |
|
| 119 | - $countOfFlagged = 0; |
|
| 120 | - $countOfJobQueue = 0; |
|
| 105 | + /** |
|
| 106 | + * Sets up the badges to draw attention to issues on various admin pages. |
|
| 107 | + * |
|
| 108 | + * This function checks to see if a user can access the pages, and if so checks the count of problem areas. |
|
| 109 | + * If problem areas are found, a number greater than 0 will cause the badge to show up. |
|
| 110 | + * |
|
| 111 | + * @param User $currentUser The current user |
|
| 112 | + * @param PdoDatabase $database Database instance |
|
| 113 | + * |
|
| 114 | + * @return void |
|
| 115 | + */ |
|
| 116 | + public function setUpNavBarBadges(User $currentUser, PdoDatabase $database) { |
|
| 117 | + // Set up some variables. |
|
| 118 | + // A size of 0 causes nothing to show up on the page (checked on navigation-menu.tpl) so leaving it 0 here is fine. |
|
| 119 | + $countOfFlagged = 0; |
|
| 120 | + $countOfJobQueue = 0; |
|
| 121 | 121 | |
| 122 | - // Count of flagged comments: |
|
| 123 | - if($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 124 | - // We want all flagged comments that haven't been acknowledged if we can visit the page. |
|
| 125 | - $countOfFlagged = sizeof(Comment::getFlaggedComments($database, 1)); // FIXME: domains |
|
| 126 | - } |
|
| 122 | + // Count of flagged comments: |
|
| 123 | + if($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 124 | + // We want all flagged comments that haven't been acknowledged if we can visit the page. |
|
| 125 | + $countOfFlagged = sizeof(Comment::getFlaggedComments($database, 1)); // FIXME: domains |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - // Count of failed job queue changes: |
|
| 129 | - if($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 130 | - // We want all failed jobs that haven't been acknowledged if we can visit the page. |
|
| 131 | - JobQueueSearchHelper::get($database, 1) // FIXME: domains |
|
| 132 | - ->statusIn([JobQueue::STATUS_FAILED]) |
|
| 133 | - ->notAcknowledged() |
|
| 134 | - ->getRecordCount($countOfJobQueue); |
|
| 135 | - } |
|
| 128 | + // Count of failed job queue changes: |
|
| 129 | + if($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 130 | + // We want all failed jobs that haven't been acknowledged if we can visit the page. |
|
| 131 | + JobQueueSearchHelper::get($database, 1) // FIXME: domains |
|
| 132 | + ->statusIn([JobQueue::STATUS_FAILED]) |
|
| 133 | + ->notAcknowledged() |
|
| 134 | + ->getRecordCount($countOfJobQueue); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - // To generate the main badge, add both up. |
|
| 138 | - // If we add more badges in the future, don't forget to add them here! |
|
| 139 | - $countOfAll = $countOfFlagged + $countOfJobQueue; |
|
| 137 | + // To generate the main badge, add both up. |
|
| 138 | + // If we add more badges in the future, don't forget to add them here! |
|
| 139 | + $countOfAll = $countOfFlagged + $countOfJobQueue; |
|
| 140 | 140 | |
| 141 | - // Set badge variables |
|
| 142 | - $this->assign("nav__numFlaggedComments", $countOfFlagged); |
|
| 143 | - $this->assign("nav__numJobQueueFailed", $countOfJobQueue); |
|
| 144 | - $this->assign("nav__numAdmin", $countOfAll); |
|
| 145 | - } |
|
| 141 | + // Set badge variables |
|
| 142 | + $this->assign("nav__numFlaggedComments", $countOfFlagged); |
|
| 143 | + $this->assign("nav__numJobQueueFailed", $countOfJobQueue); |
|
| 144 | + $this->assign("nav__numAdmin", $countOfAll); |
|
| 145 | + } |
|
| 146 | 146 | } |
@@ -120,13 +120,13 @@ |
||
| 120 | 120 | $countOfJobQueue = 0; |
| 121 | 121 | |
| 122 | 122 | // Count of flagged comments: |
| 123 | - if($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 123 | + if ($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageListFlaggedComments::class)) { |
|
| 124 | 124 | // We want all flagged comments that haven't been acknowledged if we can visit the page. |
| 125 | 125 | $countOfFlagged = sizeof(Comment::getFlaggedComments($database, 1)); // FIXME: domains |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // Count of failed job queue changes: |
| 129 | - if($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 129 | + if ($this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageJobQueue::class)) { |
|
| 130 | 130 | // We want all failed jobs that haven't been acknowledged if we can visit the page. |
| 131 | 131 | JobQueueSearchHelper::get($database, 1) // FIXME: domains |
| 132 | 132 | ->statusIn([JobQueue::STATUS_FAILED]) |
@@ -113,7 +113,8 @@ |
||
| 113 | 113 | * |
| 114 | 114 | * @return void |
| 115 | 115 | */ |
| 116 | - public function setUpNavBarBadges(User $currentUser, PdoDatabase $database) { |
|
| 116 | + public function setUpNavBarBadges(User $currentUser, PdoDatabase $database) |
|
| 117 | + { |
|
| 117 | 118 | // Set up some variables. |
| 118 | 119 | // A size of 0 causes nothing to show up on the page (checked on navigation-menu.tpl) so leaving it 0 here is fine. |
| 119 | 120 | $countOfFlagged = 0; |
@@ -97,8 +97,7 @@ |
||
| 97 | 97 | // all over the rest of the code |
| 98 | 98 | if ($this->hasActiveTransaction) { |
| 99 | 99 | return false; |
| 100 | - } |
|
| 101 | - else { |
|
| 100 | + } else { |
|
| 102 | 101 | $accessMode = 'READ WRITE'; |
| 103 | 102 | |
| 104 | 103 | switch ($isolationLevel) { |
@@ -15,124 +15,124 @@ |
||
| 15 | 15 | |
| 16 | 16 | class PdoDatabase extends PDO |
| 17 | 17 | { |
| 18 | - public const ISOLATION_SERIALIZABLE = 'SERIALIZABLE'; |
|
| 19 | - public const ISOLATION_READ_COMMITTED = 'READ COMMITTED'; |
|
| 20 | - public const ISOLATION_READ_ONLY = 'READ ONLY'; |
|
| 21 | - |
|
| 22 | - private static PdoDatabase $connection; |
|
| 23 | - /** |
|
| 24 | - * @var bool True if a transaction is active |
|
| 25 | - */ |
|
| 26 | - protected bool $hasActiveTransaction = false; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Unless you're doing low-level work, this is not the function you want. |
|
| 30 | - * |
|
| 31 | - * @throws Exception |
|
| 32 | - */ |
|
| 33 | - public static function getDatabaseConnection(SiteConfiguration $configuration): PdoDatabase |
|
| 34 | - { |
|
| 35 | - if (!isset(self::$connection)) { |
|
| 36 | - $dbConfig = $configuration->getDatabaseConfig(); |
|
| 37 | - |
|
| 38 | - try { |
|
| 39 | - $databaseObject = new PdoDatabase( |
|
| 40 | - $dbConfig['datasource'], |
|
| 41 | - $dbConfig['username'], |
|
| 42 | - $dbConfig['password'], |
|
| 43 | - [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci'] |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - catch (PDOException $ex) { |
|
| 47 | - // wrap around any potential stack traces which may include passwords |
|
| 48 | - throw new EnvironmentException('Error connecting to database: ' . $ex->getMessage()); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 52 | - |
|
| 53 | - // emulating prepared statements gives a performance boost on MySQL. |
|
| 54 | - // |
|
| 55 | - // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
| 56 | - // the prepared statements, so we're forced to turn this off for now. |
|
| 57 | - // -- stw 2014-02-11 |
|
| 58 | - // |
|
| 59 | - // and that's not the only problem with emulated prepares. We've now got code that relies |
|
| 60 | - // on real prepares. |
|
| 61 | - // -- stw 2023-09-30 |
|
| 62 | - $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
| 63 | - |
|
| 64 | - // Set the default transaction mode |
|
| 65 | - $databaseObject->exec("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;"); |
|
| 66 | - |
|
| 67 | - self::$connection = $databaseObject; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return self::$connection; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Determines if this connection has a transaction in progress or not |
|
| 75 | - * @return boolean true if there is a transaction in progress. |
|
| 76 | - */ |
|
| 77 | - public function hasActiveTransaction(): bool |
|
| 78 | - { |
|
| 79 | - return $this->hasActiveTransaction; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function beginTransaction(string $isolationLevel = self::ISOLATION_READ_COMMITTED): bool |
|
| 83 | - { |
|
| 84 | - // Override the pre-existing method, which doesn't stop you from |
|
| 85 | - // starting transactions within transactions - which doesn't work and |
|
| 86 | - // will throw an exception. This eliminates the need to catch exceptions |
|
| 87 | - // all over the rest of the code |
|
| 88 | - if ($this->hasActiveTransaction) { |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - else { |
|
| 92 | - $accessMode = 'READ WRITE'; |
|
| 93 | - |
|
| 94 | - switch ($isolationLevel) { |
|
| 95 | - case self::ISOLATION_SERIALIZABLE: |
|
| 96 | - case self::ISOLATION_READ_COMMITTED: |
|
| 97 | - break; |
|
| 98 | - case self::ISOLATION_READ_ONLY: |
|
| 99 | - $isolationLevel = self::ISOLATION_READ_COMMITTED; |
|
| 100 | - $accessMode = 'READ ONLY'; |
|
| 101 | - break; |
|
| 102 | - default: |
|
| 103 | - throw new Exception("Invalid transaction isolation level"); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // set the transaction isolation level for every transaction. |
|
| 107 | - // string substitution is safe here; values can only be one of the above constants |
|
| 108 | - parent::exec("SET TRANSACTION ISOLATION LEVEL ${isolationLevel}, ${accessMode};"); |
|
| 109 | - |
|
| 110 | - // start a new transaction, and return whether the start was successful |
|
| 111 | - $this->hasActiveTransaction = parent::beginTransaction(); |
|
| 112 | - |
|
| 113 | - return $this->hasActiveTransaction; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Commits the active transaction |
|
| 119 | - */ |
|
| 120 | - public function commit(): void |
|
| 121 | - { |
|
| 122 | - if ($this->hasActiveTransaction) { |
|
| 123 | - parent::commit(); |
|
| 124 | - $this->hasActiveTransaction = false; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Rolls back a transaction |
|
| 130 | - */ |
|
| 131 | - public function rollBack(): void |
|
| 132 | - { |
|
| 133 | - if ($this->hasActiveTransaction) { |
|
| 134 | - parent::rollback(); |
|
| 135 | - $this->hasActiveTransaction = false; |
|
| 136 | - } |
|
| 137 | - } |
|
| 18 | + public const ISOLATION_SERIALIZABLE = 'SERIALIZABLE'; |
|
| 19 | + public const ISOLATION_READ_COMMITTED = 'READ COMMITTED'; |
|
| 20 | + public const ISOLATION_READ_ONLY = 'READ ONLY'; |
|
| 21 | + |
|
| 22 | + private static PdoDatabase $connection; |
|
| 23 | + /** |
|
| 24 | + * @var bool True if a transaction is active |
|
| 25 | + */ |
|
| 26 | + protected bool $hasActiveTransaction = false; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Unless you're doing low-level work, this is not the function you want. |
|
| 30 | + * |
|
| 31 | + * @throws Exception |
|
| 32 | + */ |
|
| 33 | + public static function getDatabaseConnection(SiteConfiguration $configuration): PdoDatabase |
|
| 34 | + { |
|
| 35 | + if (!isset(self::$connection)) { |
|
| 36 | + $dbConfig = $configuration->getDatabaseConfig(); |
|
| 37 | + |
|
| 38 | + try { |
|
| 39 | + $databaseObject = new PdoDatabase( |
|
| 40 | + $dbConfig['datasource'], |
|
| 41 | + $dbConfig['username'], |
|
| 42 | + $dbConfig['password'], |
|
| 43 | + [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci'] |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + catch (PDOException $ex) { |
|
| 47 | + // wrap around any potential stack traces which may include passwords |
|
| 48 | + throw new EnvironmentException('Error connecting to database: ' . $ex->getMessage()); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 52 | + |
|
| 53 | + // emulating prepared statements gives a performance boost on MySQL. |
|
| 54 | + // |
|
| 55 | + // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
| 56 | + // the prepared statements, so we're forced to turn this off for now. |
|
| 57 | + // -- stw 2014-02-11 |
|
| 58 | + // |
|
| 59 | + // and that's not the only problem with emulated prepares. We've now got code that relies |
|
| 60 | + // on real prepares. |
|
| 61 | + // -- stw 2023-09-30 |
|
| 62 | + $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
| 63 | + |
|
| 64 | + // Set the default transaction mode |
|
| 65 | + $databaseObject->exec("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;"); |
|
| 66 | + |
|
| 67 | + self::$connection = $databaseObject; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return self::$connection; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Determines if this connection has a transaction in progress or not |
|
| 75 | + * @return boolean true if there is a transaction in progress. |
|
| 76 | + */ |
|
| 77 | + public function hasActiveTransaction(): bool |
|
| 78 | + { |
|
| 79 | + return $this->hasActiveTransaction; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function beginTransaction(string $isolationLevel = self::ISOLATION_READ_COMMITTED): bool |
|
| 83 | + { |
|
| 84 | + // Override the pre-existing method, which doesn't stop you from |
|
| 85 | + // starting transactions within transactions - which doesn't work and |
|
| 86 | + // will throw an exception. This eliminates the need to catch exceptions |
|
| 87 | + // all over the rest of the code |
|
| 88 | + if ($this->hasActiveTransaction) { |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + else { |
|
| 92 | + $accessMode = 'READ WRITE'; |
|
| 93 | + |
|
| 94 | + switch ($isolationLevel) { |
|
| 95 | + case self::ISOLATION_SERIALIZABLE: |
|
| 96 | + case self::ISOLATION_READ_COMMITTED: |
|
| 97 | + break; |
|
| 98 | + case self::ISOLATION_READ_ONLY: |
|
| 99 | + $isolationLevel = self::ISOLATION_READ_COMMITTED; |
|
| 100 | + $accessMode = 'READ ONLY'; |
|
| 101 | + break; |
|
| 102 | + default: |
|
| 103 | + throw new Exception("Invalid transaction isolation level"); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // set the transaction isolation level for every transaction. |
|
| 107 | + // string substitution is safe here; values can only be one of the above constants |
|
| 108 | + parent::exec("SET TRANSACTION ISOLATION LEVEL ${isolationLevel}, ${accessMode};"); |
|
| 109 | + |
|
| 110 | + // start a new transaction, and return whether the start was successful |
|
| 111 | + $this->hasActiveTransaction = parent::beginTransaction(); |
|
| 112 | + |
|
| 113 | + return $this->hasActiveTransaction; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Commits the active transaction |
|
| 119 | + */ |
|
| 120 | + public function commit(): void |
|
| 121 | + { |
|
| 122 | + if ($this->hasActiveTransaction) { |
|
| 123 | + parent::commit(); |
|
| 124 | + $this->hasActiveTransaction = false; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Rolls back a transaction |
|
| 130 | + */ |
|
| 131 | + public function rollBack(): void |
|
| 132 | + { |
|
| 133 | + if ($this->hasActiveTransaction) { |
|
| 134 | + parent::rollback(); |
|
| 135 | + $this->hasActiveTransaction = false; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | 138 | } |