@@ -15,209 +15,209 @@ |
||
| 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 | - * |
|
| 33 | - * @return RequestSearchHelper |
|
| 34 | - */ |
|
| 35 | - public static function get(PdoDatabase $database) |
|
| 36 | - { |
|
| 37 | - $helper = new RequestSearchHelper($database); |
|
| 38 | - |
|
| 39 | - return $helper; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Filters the results by IP address |
|
| 44 | - * |
|
| 45 | - * @param string $ipAddress |
|
| 46 | - * |
|
| 47 | - * @return $this |
|
| 48 | - */ |
|
| 49 | - public function byIp($ipAddress) |
|
| 50 | - { |
|
| 51 | - $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
| 52 | - $this->parameterList[] = $ipAddress; |
|
| 53 | - $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Filters the results by email address |
|
| 60 | - * |
|
| 61 | - * @param string $emailAddress |
|
| 62 | - * |
|
| 63 | - * @return $this |
|
| 64 | - */ |
|
| 65 | - public function byEmailAddress($emailAddress) |
|
| 66 | - { |
|
| 67 | - $this->whereClause .= ' AND email LIKE ?'; |
|
| 68 | - $this->parameterList[] = $emailAddress; |
|
| 69 | - |
|
| 70 | - return $this; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Filters the results by name |
|
| 75 | - * |
|
| 76 | - * @param string $name |
|
| 77 | - * |
|
| 78 | - * @return $this |
|
| 79 | - */ |
|
| 80 | - public function byName($name) |
|
| 81 | - { |
|
| 82 | - $this->whereClause .= ' AND name LIKE ?'; |
|
| 83 | - $this->parameterList[] = $name; |
|
| 84 | - |
|
| 85 | - return $this; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Filters the results by comment |
|
| 90 | - * |
|
| 91 | - * @param string $comment |
|
| 92 | - * |
|
| 93 | - * @return $this |
|
| 94 | - */ |
|
| 95 | - public function byComment($comment) |
|
| 96 | - { |
|
| 97 | - $this->modifiersClause = 'DISTINCT'; |
|
| 98 | - $this->joinClause .= ' INNER JOIN comment c ON origin.id = c.request'; |
|
| 99 | - $this->whereClause .= ' AND c.comment LIKE ?'; |
|
| 100 | - $this->parameterList[] = $comment; |
|
| 101 | - |
|
| 102 | - return $this; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Filters the results by comment security |
|
| 107 | - * |
|
| 108 | - * @param array $security List of allowed values for the security clause |
|
| 109 | - * |
|
| 110 | - * @return $this |
|
| 111 | - */ |
|
| 112 | - public function byCommentSecurity(array $security) |
|
| 113 | - { |
|
| 114 | - $this->inClause('c.visibility', $security); |
|
| 115 | - |
|
| 116 | - return $this; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Filters the requests to those with a defined status |
|
| 121 | - * |
|
| 122 | - * @param $status |
|
| 123 | - * |
|
| 124 | - * @return $this |
|
| 125 | - */ |
|
| 126 | - public function byStatus($status) |
|
| 127 | - { |
|
| 128 | - $this->whereClause .= ' AND status = ?'; |
|
| 129 | - $this->parameterList[] = $status; |
|
| 130 | - |
|
| 131 | - return $this; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Excludes a request from the results |
|
| 136 | - * |
|
| 137 | - * @param int $requestId |
|
| 138 | - * |
|
| 139 | - * @return $this |
|
| 140 | - */ |
|
| 141 | - public function excludingRequest($requestId) |
|
| 142 | - { |
|
| 143 | - $this->whereClause .= ' AND id <> ?'; |
|
| 144 | - $this->parameterList[] = $requestId; |
|
| 145 | - |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Filters the results to only those with a confirmed email address |
|
| 151 | - * |
|
| 152 | - * @return $this |
|
| 153 | - */ |
|
| 154 | - public function withConfirmedEmail() |
|
| 155 | - { |
|
| 156 | - $this->whereClause .= ' AND emailconfirm = ?'; |
|
| 157 | - $this->parameterList[] = 'Confirmed'; |
|
| 158 | - |
|
| 159 | - return $this; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Filters the results to exclude purged data |
|
| 164 | - * |
|
| 165 | - * @param SiteConfiguration $configuration |
|
| 166 | - * |
|
| 167 | - * @return $this |
|
| 168 | - */ |
|
| 169 | - public function excludingPurgedData(SiteConfiguration $configuration) |
|
| 170 | - { |
|
| 171 | - $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
| 172 | - $this->parameterList[] = $configuration->getDataClearIp(); |
|
| 173 | - $this->parameterList[] = $configuration->getDataClearEmail(); |
|
| 174 | - |
|
| 175 | - return $this; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Filters the requests to those without a defined status |
|
| 180 | - * |
|
| 181 | - * @param $status |
|
| 182 | - * |
|
| 183 | - * @return $this |
|
| 184 | - */ |
|
| 185 | - public function excludingStatus($status) |
|
| 186 | - { |
|
| 187 | - $this->whereClause .= ' AND status <> ?'; |
|
| 188 | - $this->parameterList[] = $status; |
|
| 189 | - |
|
| 190 | - return $this; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Filters the requests to those which have failed an auto-creation |
|
| 195 | - * |
|
| 196 | - * @return $this |
|
| 197 | - */ |
|
| 198 | - public function isHospitalised() |
|
| 199 | - { |
|
| 200 | - $this->whereClause .= ' AND status = ?'; |
|
| 201 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 202 | - |
|
| 203 | - return $this; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Filters the requests to those which have not failed an auto-creation |
|
| 208 | - * |
|
| 209 | - * @return $this |
|
| 210 | - */ |
|
| 211 | - public function notHospitalised() |
|
| 212 | - { |
|
| 213 | - $this->whereClause .= ' AND status <> ?'; |
|
| 214 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 215 | - |
|
| 216 | - return $this; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - public function fetchByQueue($queues) |
|
| 220 | - { |
|
| 221 | - return $this->fetchByParameter(' AND queue = ?', $queues); |
|
| 222 | - } |
|
| 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 | + * |
|
| 33 | + * @return RequestSearchHelper |
|
| 34 | + */ |
|
| 35 | + public static function get(PdoDatabase $database) |
|
| 36 | + { |
|
| 37 | + $helper = new RequestSearchHelper($database); |
|
| 38 | + |
|
| 39 | + return $helper; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Filters the results by IP address |
|
| 44 | + * |
|
| 45 | + * @param string $ipAddress |
|
| 46 | + * |
|
| 47 | + * @return $this |
|
| 48 | + */ |
|
| 49 | + public function byIp($ipAddress) |
|
| 50 | + { |
|
| 51 | + $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
| 52 | + $this->parameterList[] = $ipAddress; |
|
| 53 | + $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Filters the results by email address |
|
| 60 | + * |
|
| 61 | + * @param string $emailAddress |
|
| 62 | + * |
|
| 63 | + * @return $this |
|
| 64 | + */ |
|
| 65 | + public function byEmailAddress($emailAddress) |
|
| 66 | + { |
|
| 67 | + $this->whereClause .= ' AND email LIKE ?'; |
|
| 68 | + $this->parameterList[] = $emailAddress; |
|
| 69 | + |
|
| 70 | + return $this; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Filters the results by name |
|
| 75 | + * |
|
| 76 | + * @param string $name |
|
| 77 | + * |
|
| 78 | + * @return $this |
|
| 79 | + */ |
|
| 80 | + public function byName($name) |
|
| 81 | + { |
|
| 82 | + $this->whereClause .= ' AND name LIKE ?'; |
|
| 83 | + $this->parameterList[] = $name; |
|
| 84 | + |
|
| 85 | + return $this; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Filters the results by comment |
|
| 90 | + * |
|
| 91 | + * @param string $comment |
|
| 92 | + * |
|
| 93 | + * @return $this |
|
| 94 | + */ |
|
| 95 | + public function byComment($comment) |
|
| 96 | + { |
|
| 97 | + $this->modifiersClause = 'DISTINCT'; |
|
| 98 | + $this->joinClause .= ' INNER JOIN comment c ON origin.id = c.request'; |
|
| 99 | + $this->whereClause .= ' AND c.comment LIKE ?'; |
|
| 100 | + $this->parameterList[] = $comment; |
|
| 101 | + |
|
| 102 | + return $this; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Filters the results by comment security |
|
| 107 | + * |
|
| 108 | + * @param array $security List of allowed values for the security clause |
|
| 109 | + * |
|
| 110 | + * @return $this |
|
| 111 | + */ |
|
| 112 | + public function byCommentSecurity(array $security) |
|
| 113 | + { |
|
| 114 | + $this->inClause('c.visibility', $security); |
|
| 115 | + |
|
| 116 | + return $this; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Filters the requests to those with a defined status |
|
| 121 | + * |
|
| 122 | + * @param $status |
|
| 123 | + * |
|
| 124 | + * @return $this |
|
| 125 | + */ |
|
| 126 | + public function byStatus($status) |
|
| 127 | + { |
|
| 128 | + $this->whereClause .= ' AND status = ?'; |
|
| 129 | + $this->parameterList[] = $status; |
|
| 130 | + |
|
| 131 | + return $this; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Excludes a request from the results |
|
| 136 | + * |
|
| 137 | + * @param int $requestId |
|
| 138 | + * |
|
| 139 | + * @return $this |
|
| 140 | + */ |
|
| 141 | + public function excludingRequest($requestId) |
|
| 142 | + { |
|
| 143 | + $this->whereClause .= ' AND id <> ?'; |
|
| 144 | + $this->parameterList[] = $requestId; |
|
| 145 | + |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Filters the results to only those with a confirmed email address |
|
| 151 | + * |
|
| 152 | + * @return $this |
|
| 153 | + */ |
|
| 154 | + public function withConfirmedEmail() |
|
| 155 | + { |
|
| 156 | + $this->whereClause .= ' AND emailconfirm = ?'; |
|
| 157 | + $this->parameterList[] = 'Confirmed'; |
|
| 158 | + |
|
| 159 | + return $this; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Filters the results to exclude purged data |
|
| 164 | + * |
|
| 165 | + * @param SiteConfiguration $configuration |
|
| 166 | + * |
|
| 167 | + * @return $this |
|
| 168 | + */ |
|
| 169 | + public function excludingPurgedData(SiteConfiguration $configuration) |
|
| 170 | + { |
|
| 171 | + $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
| 172 | + $this->parameterList[] = $configuration->getDataClearIp(); |
|
| 173 | + $this->parameterList[] = $configuration->getDataClearEmail(); |
|
| 174 | + |
|
| 175 | + return $this; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Filters the requests to those without a defined status |
|
| 180 | + * |
|
| 181 | + * @param $status |
|
| 182 | + * |
|
| 183 | + * @return $this |
|
| 184 | + */ |
|
| 185 | + public function excludingStatus($status) |
|
| 186 | + { |
|
| 187 | + $this->whereClause .= ' AND status <> ?'; |
|
| 188 | + $this->parameterList[] = $status; |
|
| 189 | + |
|
| 190 | + return $this; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Filters the requests to those which have failed an auto-creation |
|
| 195 | + * |
|
| 196 | + * @return $this |
|
| 197 | + */ |
|
| 198 | + public function isHospitalised() |
|
| 199 | + { |
|
| 200 | + $this->whereClause .= ' AND status = ?'; |
|
| 201 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 202 | + |
|
| 203 | + return $this; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Filters the requests to those which have not failed an auto-creation |
|
| 208 | + * |
|
| 209 | + * @return $this |
|
| 210 | + */ |
|
| 211 | + public function notHospitalised() |
|
| 212 | + { |
|
| 213 | + $this->whereClause .= ' AND status <> ?'; |
|
| 214 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
| 215 | + |
|
| 216 | + return $this; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + public function fetchByQueue($queues) |
|
| 220 | + { |
|
| 221 | + return $this->fetchByParameter(' AND queue = ?', $queues); |
|
| 222 | + } |
|
| 223 | 223 | } |
@@ -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 | } |
@@ -34,419 +34,419 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Logger |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * @param PdoDatabase $database |
|
| 39 | - * @param Request $object |
|
| 40 | - */ |
|
| 41 | - public static function emailConfirmed(PdoDatabase $database, Request $object) |
|
| 42 | - { |
|
| 43 | - self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param PdoDatabase $database |
|
| 48 | - * @param DataObject $object |
|
| 49 | - * @param string $logAction |
|
| 50 | - * @param null|string $comment |
|
| 51 | - * @param User $user |
|
| 52 | - * |
|
| 53 | - * @throws Exception |
|
| 54 | - */ |
|
| 55 | - private static function createLogEntry( |
|
| 56 | - PdoDatabase $database, |
|
| 57 | - DataObject $object, |
|
| 58 | - $logAction, |
|
| 59 | - $comment = null, |
|
| 60 | - $user = null |
|
| 61 | - ) { |
|
| 62 | - if ($user == null) { |
|
| 63 | - $user = User::getCurrent($database); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $objectType = get_class($object); |
|
| 67 | - if (strpos($objectType, 'Waca\\DataObjects\\') !== false) { |
|
| 68 | - $objectType = str_replace('Waca\\DataObjects\\', '', $objectType); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $log = new Log(); |
|
| 72 | - $log->setDatabase($database); |
|
| 73 | - $log->setAction($logAction); |
|
| 74 | - $log->setObjectId($object->getId()); |
|
| 75 | - $log->setObjectType($objectType); |
|
| 76 | - $log->setUser($user); |
|
| 77 | - $log->setComment($comment); |
|
| 78 | - $log->save(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - #region Users |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @param PdoDatabase $database |
|
| 85 | - * @param User $user |
|
| 86 | - */ |
|
| 87 | - public static function newUser(PdoDatabase $database, User $user) |
|
| 88 | - { |
|
| 89 | - self::createLogEntry($database, $user, 'Registered', null, User::getCommunity()); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param PdoDatabase $database |
|
| 94 | - * @param User $object |
|
| 95 | - */ |
|
| 96 | - public static function approvedUser(PdoDatabase $database, User $object) |
|
| 97 | - { |
|
| 98 | - self::createLogEntry($database, $object, "Approved"); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param PdoDatabase $database |
|
| 103 | - * @param User $object |
|
| 104 | - * @param string $comment |
|
| 105 | - */ |
|
| 106 | - public static function declinedUser(PdoDatabase $database, User $object, $comment) |
|
| 107 | - { |
|
| 108 | - self::createLogEntry($database, $object, "Declined", $comment); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param PdoDatabase $database |
|
| 113 | - * @param User $object |
|
| 114 | - * @param string $comment |
|
| 115 | - */ |
|
| 116 | - public static function suspendedUser(PdoDatabase $database, User $object, $comment) |
|
| 117 | - { |
|
| 118 | - self::createLogEntry($database, $object, "Suspended", $comment); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @param PdoDatabase $database |
|
| 123 | - * @param User $object |
|
| 124 | - * @param string $comment |
|
| 125 | - */ |
|
| 126 | - public static function demotedUser(PdoDatabase $database, User $object, $comment) |
|
| 127 | - { |
|
| 128 | - self::createLogEntry($database, $object, "Demoted", $comment); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param PdoDatabase $database |
|
| 133 | - * @param User $object |
|
| 134 | - */ |
|
| 135 | - public static function promotedUser(PdoDatabase $database, User $object) |
|
| 136 | - { |
|
| 137 | - self::createLogEntry($database, $object, "Promoted"); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param PdoDatabase $database |
|
| 142 | - * @param User $object |
|
| 143 | - * @param string $comment |
|
| 144 | - */ |
|
| 145 | - public static function renamedUser(PdoDatabase $database, User $object, $comment) |
|
| 146 | - { |
|
| 147 | - self::createLogEntry($database, $object, "Renamed", $comment); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @param PdoDatabase $database |
|
| 152 | - * @param User $object |
|
| 153 | - */ |
|
| 154 | - public static function userPreferencesChange(PdoDatabase $database, User $object) |
|
| 155 | - { |
|
| 156 | - self::createLogEntry($database, $object, "Prefchange"); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param PdoDatabase $database |
|
| 161 | - * @param User $object |
|
| 162 | - * @param string $reason |
|
| 163 | - * @param array $added |
|
| 164 | - * @param array $removed |
|
| 165 | - */ |
|
| 166 | - public static function userRolesEdited(PdoDatabase $database, User $object, $reason, $added, $removed) |
|
| 167 | - { |
|
| 168 | - $logData = serialize(array( |
|
| 169 | - 'added' => $added, |
|
| 170 | - 'removed' => $removed, |
|
| 171 | - 'reason' => $reason, |
|
| 172 | - )); |
|
| 173 | - |
|
| 174 | - self::createLogEntry($database, $object, "RoleChange", $logData); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - #endregion |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param PdoDatabase $database |
|
| 181 | - * @param SiteNotice $object |
|
| 182 | - */ |
|
| 183 | - public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object) |
|
| 184 | - { |
|
| 185 | - self::createLogEntry($database, $object, "Edited"); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - #region Welcome Templates |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @param PdoDatabase $database |
|
| 192 | - * @param WelcomeTemplate $object |
|
| 193 | - */ |
|
| 194 | - public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
|
| 195 | - { |
|
| 196 | - self::createLogEntry($database, $object, "CreatedTemplate"); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @param PdoDatabase $database |
|
| 201 | - * @param WelcomeTemplate $object |
|
| 202 | - */ |
|
| 203 | - public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
|
| 204 | - { |
|
| 205 | - self::createLogEntry($database, $object, "EditedTemplate"); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @param PdoDatabase $database |
|
| 210 | - * @param WelcomeTemplate $object |
|
| 211 | - */ |
|
| 212 | - public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
|
| 213 | - { |
|
| 214 | - self::createLogEntry($database, $object, "DeletedTemplate"); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - #endregion |
|
| 218 | - |
|
| 219 | - #region Bans |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param PdoDatabase $database |
|
| 223 | - * @param Ban $object |
|
| 224 | - * @param string $reason |
|
| 225 | - */ |
|
| 226 | - public static function banned(PdoDatabase $database, Ban $object, $reason) |
|
| 227 | - { |
|
| 228 | - self::createLogEntry($database, $object, "Banned", $reason); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * @param PdoDatabase $database |
|
| 233 | - * @param Ban $object |
|
| 234 | - * @param string $reason |
|
| 235 | - */ |
|
| 236 | - public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
|
| 237 | - { |
|
| 238 | - self::createLogEntry($database, $object, "Unbanned", $reason); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - #endregion |
|
| 242 | - |
|
| 243 | - #region Requests |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * @param PdoDatabase $database |
|
| 247 | - * @param Request $object |
|
| 248 | - * @param string $target |
|
| 249 | - */ |
|
| 250 | - public static function deferRequest(PdoDatabase $database, Request $object, $target) |
|
| 251 | - { |
|
| 252 | - self::createLogEntry($database, $object, "Deferred to $target"); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @param PdoDatabase $database |
|
| 257 | - * @param Request $object |
|
| 258 | - * @param integer $target |
|
| 259 | - * @param string $comment |
|
| 260 | - * @param User|null $logUser |
|
| 261 | - */ |
|
| 262 | - public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null) |
|
| 263 | - { |
|
| 264 | - self::createLogEntry($database, $object, "Closed $target", $comment, $logUser); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - public static function manuallyConfirmRequest(PdoDatabase $database, Request $object) |
|
| 268 | - { |
|
| 269 | - self::createLogEntry($database, $object, "Manually Confirmed"); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * @param PdoDatabase $database |
|
| 274 | - * @param Request $object |
|
| 275 | - */ |
|
| 276 | - public static function reserve(PdoDatabase $database, Request $object) |
|
| 277 | - { |
|
| 278 | - self::createLogEntry($database, $object, "Reserved"); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * @param PdoDatabase $database |
|
| 283 | - * @param Request $object |
|
| 284 | - */ |
|
| 285 | - public static function breakReserve(PdoDatabase $database, Request $object) |
|
| 286 | - { |
|
| 287 | - self::createLogEntry($database, $object, "BreakReserve"); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @param PdoDatabase $database |
|
| 292 | - * @param Request $object |
|
| 293 | - */ |
|
| 294 | - public static function unreserve(PdoDatabase $database, Request $object) |
|
| 295 | - { |
|
| 296 | - self::createLogEntry($database, $object, "Unreserved"); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @param PdoDatabase $database |
|
| 301 | - * @param Comment $object |
|
| 302 | - * @param Request $request |
|
| 303 | - */ |
|
| 304 | - public static function editComment(PdoDatabase $database, Comment $object, Request $request) |
|
| 305 | - { |
|
| 306 | - self::createLogEntry($database, $request, "EditComment-r"); |
|
| 307 | - self::createLogEntry($database, $object, "EditComment-c"); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @param PdoDatabase $database |
|
| 312 | - * @param Comment $object |
|
| 313 | - */ |
|
| 314 | - public static function flaggedComment(PdoDatabase $database, Comment $object) |
|
| 315 | - { |
|
| 316 | - self::createLogEntry($database, $object, "FlaggedComment"); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param PdoDatabase $database |
|
| 321 | - * @param Comment $object |
|
| 322 | - */ |
|
| 323 | - public static function unflaggedComment(PdoDatabase $database, Comment $object) |
|
| 324 | - { |
|
| 325 | - self::createLogEntry($database, $object, "UnflaggedComment"); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * @param PdoDatabase $database |
|
| 330 | - * @param Request $object |
|
| 331 | - * @param User $target |
|
| 332 | - */ |
|
| 333 | - public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
|
| 334 | - { |
|
| 335 | - self::createLogEntry($database, $object, "SendReserved"); |
|
| 336 | - self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * @param PdoDatabase $database |
|
| 341 | - * @param Request $object |
|
| 342 | - * @param string $comment |
|
| 343 | - */ |
|
| 344 | - public static function sentMail(PdoDatabase $database, Request $object, $comment) |
|
| 345 | - { |
|
| 346 | - self::createLogEntry($database, $object, "SentMail", $comment); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * @param PdoDatabase $database |
|
| 351 | - * @param Request $object |
|
| 352 | - */ |
|
| 353 | - public static function enqueuedJobQueue(PdoDatabase $database, Request $object) |
|
| 354 | - { |
|
| 355 | - self::createLogEntry($database, $object, 'EnqueuedJobQueue'); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - public static function hospitalised(PdoDatabase $database, Request $object) |
|
| 359 | - { |
|
| 360 | - self::createLogEntry($database, $object, 'Hospitalised'); |
|
| 361 | - } |
|
| 362 | - #endregion |
|
| 363 | - |
|
| 364 | - #region Email templates |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * @param PdoDatabase $database |
|
| 368 | - * @param EmailTemplate $object |
|
| 369 | - */ |
|
| 370 | - public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 371 | - { |
|
| 372 | - self::createLogEntry($database, $object, "CreatedEmail"); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * @param PdoDatabase $database |
|
| 377 | - * @param EmailTemplate $object |
|
| 378 | - */ |
|
| 379 | - public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 380 | - { |
|
| 381 | - self::createLogEntry($database, $object, "EditedEmail"); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - #endregion |
|
| 385 | - |
|
| 386 | - #region Display |
|
| 387 | - |
|
| 388 | - #endregion |
|
| 389 | - |
|
| 390 | - #region Automation |
|
| 391 | - |
|
| 392 | - public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job) |
|
| 393 | - { |
|
| 394 | - self::createLogEntry($database, $job, 'JobCompleted', null, User::getCommunity()); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job) |
|
| 398 | - { |
|
| 399 | - $data = array('status' => $job->getStatus(), 'error' => $job->getError()); |
|
| 400 | - self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity()); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job) |
|
| 404 | - { |
|
| 405 | - self::createLogEntry($database, $job, 'JobCancelled', $job->getError()); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job) |
|
| 409 | - { |
|
| 410 | - self::createLogEntry($database, $job, 'JobRequeued'); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null) |
|
| 414 | - { |
|
| 415 | - self::createLogEntry($database, $job, 'JobAcknowledged', $comment); |
|
| 416 | - } |
|
| 417 | - #endregion |
|
| 418 | - |
|
| 419 | - #region Request Queues |
|
| 420 | - public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue) |
|
| 421 | - { |
|
| 422 | - self::createLogEntry($database, $queue, 'QueueCreated'); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue) |
|
| 426 | - { |
|
| 427 | - self::createLogEntry($database, $queue, 'QueueEdited'); |
|
| 428 | - } |
|
| 429 | - #endregion |
|
| 430 | - #region Domains |
|
| 431 | - public static function domainCreated(PdoDatabase $database, Domain $domain) |
|
| 432 | - { |
|
| 433 | - self::createLogEntry($database, $domain, 'DomainCreated'); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - public static function domainEdited(PdoDatabase $database, Domain $domain) |
|
| 437 | - { |
|
| 438 | - self::createLogEntry($database, $domain, 'DomainEdited'); |
|
| 439 | - } |
|
| 440 | - #endregion |
|
| 441 | - #region Request Forms |
|
| 442 | - public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm) |
|
| 443 | - { |
|
| 444 | - self::createLogEntry($database, $requestForm, 'RequestFormCreated'); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm) |
|
| 448 | - { |
|
| 449 | - self::createLogEntry($database, $requestForm, 'RequestFormEdited'); |
|
| 450 | - } |
|
| 451 | - #endregion |
|
| 37 | + /** |
|
| 38 | + * @param PdoDatabase $database |
|
| 39 | + * @param Request $object |
|
| 40 | + */ |
|
| 41 | + public static function emailConfirmed(PdoDatabase $database, Request $object) |
|
| 42 | + { |
|
| 43 | + self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param PdoDatabase $database |
|
| 48 | + * @param DataObject $object |
|
| 49 | + * @param string $logAction |
|
| 50 | + * @param null|string $comment |
|
| 51 | + * @param User $user |
|
| 52 | + * |
|
| 53 | + * @throws Exception |
|
| 54 | + */ |
|
| 55 | + private static function createLogEntry( |
|
| 56 | + PdoDatabase $database, |
|
| 57 | + DataObject $object, |
|
| 58 | + $logAction, |
|
| 59 | + $comment = null, |
|
| 60 | + $user = null |
|
| 61 | + ) { |
|
| 62 | + if ($user == null) { |
|
| 63 | + $user = User::getCurrent($database); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $objectType = get_class($object); |
|
| 67 | + if (strpos($objectType, 'Waca\\DataObjects\\') !== false) { |
|
| 68 | + $objectType = str_replace('Waca\\DataObjects\\', '', $objectType); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $log = new Log(); |
|
| 72 | + $log->setDatabase($database); |
|
| 73 | + $log->setAction($logAction); |
|
| 74 | + $log->setObjectId($object->getId()); |
|
| 75 | + $log->setObjectType($objectType); |
|
| 76 | + $log->setUser($user); |
|
| 77 | + $log->setComment($comment); |
|
| 78 | + $log->save(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + #region Users |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @param PdoDatabase $database |
|
| 85 | + * @param User $user |
|
| 86 | + */ |
|
| 87 | + public static function newUser(PdoDatabase $database, User $user) |
|
| 88 | + { |
|
| 89 | + self::createLogEntry($database, $user, 'Registered', null, User::getCommunity()); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param PdoDatabase $database |
|
| 94 | + * @param User $object |
|
| 95 | + */ |
|
| 96 | + public static function approvedUser(PdoDatabase $database, User $object) |
|
| 97 | + { |
|
| 98 | + self::createLogEntry($database, $object, "Approved"); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param PdoDatabase $database |
|
| 103 | + * @param User $object |
|
| 104 | + * @param string $comment |
|
| 105 | + */ |
|
| 106 | + public static function declinedUser(PdoDatabase $database, User $object, $comment) |
|
| 107 | + { |
|
| 108 | + self::createLogEntry($database, $object, "Declined", $comment); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param PdoDatabase $database |
|
| 113 | + * @param User $object |
|
| 114 | + * @param string $comment |
|
| 115 | + */ |
|
| 116 | + public static function suspendedUser(PdoDatabase $database, User $object, $comment) |
|
| 117 | + { |
|
| 118 | + self::createLogEntry($database, $object, "Suspended", $comment); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param PdoDatabase $database |
|
| 123 | + * @param User $object |
|
| 124 | + * @param string $comment |
|
| 125 | + */ |
|
| 126 | + public static function demotedUser(PdoDatabase $database, User $object, $comment) |
|
| 127 | + { |
|
| 128 | + self::createLogEntry($database, $object, "Demoted", $comment); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param PdoDatabase $database |
|
| 133 | + * @param User $object |
|
| 134 | + */ |
|
| 135 | + public static function promotedUser(PdoDatabase $database, User $object) |
|
| 136 | + { |
|
| 137 | + self::createLogEntry($database, $object, "Promoted"); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param PdoDatabase $database |
|
| 142 | + * @param User $object |
|
| 143 | + * @param string $comment |
|
| 144 | + */ |
|
| 145 | + public static function renamedUser(PdoDatabase $database, User $object, $comment) |
|
| 146 | + { |
|
| 147 | + self::createLogEntry($database, $object, "Renamed", $comment); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @param PdoDatabase $database |
|
| 152 | + * @param User $object |
|
| 153 | + */ |
|
| 154 | + public static function userPreferencesChange(PdoDatabase $database, User $object) |
|
| 155 | + { |
|
| 156 | + self::createLogEntry($database, $object, "Prefchange"); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param PdoDatabase $database |
|
| 161 | + * @param User $object |
|
| 162 | + * @param string $reason |
|
| 163 | + * @param array $added |
|
| 164 | + * @param array $removed |
|
| 165 | + */ |
|
| 166 | + public static function userRolesEdited(PdoDatabase $database, User $object, $reason, $added, $removed) |
|
| 167 | + { |
|
| 168 | + $logData = serialize(array( |
|
| 169 | + 'added' => $added, |
|
| 170 | + 'removed' => $removed, |
|
| 171 | + 'reason' => $reason, |
|
| 172 | + )); |
|
| 173 | + |
|
| 174 | + self::createLogEntry($database, $object, "RoleChange", $logData); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + #endregion |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param PdoDatabase $database |
|
| 181 | + * @param SiteNotice $object |
|
| 182 | + */ |
|
| 183 | + public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object) |
|
| 184 | + { |
|
| 185 | + self::createLogEntry($database, $object, "Edited"); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + #region Welcome Templates |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @param PdoDatabase $database |
|
| 192 | + * @param WelcomeTemplate $object |
|
| 193 | + */ |
|
| 194 | + public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
|
| 195 | + { |
|
| 196 | + self::createLogEntry($database, $object, "CreatedTemplate"); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @param PdoDatabase $database |
|
| 201 | + * @param WelcomeTemplate $object |
|
| 202 | + */ |
|
| 203 | + public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
|
| 204 | + { |
|
| 205 | + self::createLogEntry($database, $object, "EditedTemplate"); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @param PdoDatabase $database |
|
| 210 | + * @param WelcomeTemplate $object |
|
| 211 | + */ |
|
| 212 | + public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
|
| 213 | + { |
|
| 214 | + self::createLogEntry($database, $object, "DeletedTemplate"); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + #endregion |
|
| 218 | + |
|
| 219 | + #region Bans |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param PdoDatabase $database |
|
| 223 | + * @param Ban $object |
|
| 224 | + * @param string $reason |
|
| 225 | + */ |
|
| 226 | + public static function banned(PdoDatabase $database, Ban $object, $reason) |
|
| 227 | + { |
|
| 228 | + self::createLogEntry($database, $object, "Banned", $reason); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * @param PdoDatabase $database |
|
| 233 | + * @param Ban $object |
|
| 234 | + * @param string $reason |
|
| 235 | + */ |
|
| 236 | + public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
|
| 237 | + { |
|
| 238 | + self::createLogEntry($database, $object, "Unbanned", $reason); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + #endregion |
|
| 242 | + |
|
| 243 | + #region Requests |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * @param PdoDatabase $database |
|
| 247 | + * @param Request $object |
|
| 248 | + * @param string $target |
|
| 249 | + */ |
|
| 250 | + public static function deferRequest(PdoDatabase $database, Request $object, $target) |
|
| 251 | + { |
|
| 252 | + self::createLogEntry($database, $object, "Deferred to $target"); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @param PdoDatabase $database |
|
| 257 | + * @param Request $object |
|
| 258 | + * @param integer $target |
|
| 259 | + * @param string $comment |
|
| 260 | + * @param User|null $logUser |
|
| 261 | + */ |
|
| 262 | + public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null) |
|
| 263 | + { |
|
| 264 | + self::createLogEntry($database, $object, "Closed $target", $comment, $logUser); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + public static function manuallyConfirmRequest(PdoDatabase $database, Request $object) |
|
| 268 | + { |
|
| 269 | + self::createLogEntry($database, $object, "Manually Confirmed"); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * @param PdoDatabase $database |
|
| 274 | + * @param Request $object |
|
| 275 | + */ |
|
| 276 | + public static function reserve(PdoDatabase $database, Request $object) |
|
| 277 | + { |
|
| 278 | + self::createLogEntry($database, $object, "Reserved"); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @param PdoDatabase $database |
|
| 283 | + * @param Request $object |
|
| 284 | + */ |
|
| 285 | + public static function breakReserve(PdoDatabase $database, Request $object) |
|
| 286 | + { |
|
| 287 | + self::createLogEntry($database, $object, "BreakReserve"); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @param PdoDatabase $database |
|
| 292 | + * @param Request $object |
|
| 293 | + */ |
|
| 294 | + public static function unreserve(PdoDatabase $database, Request $object) |
|
| 295 | + { |
|
| 296 | + self::createLogEntry($database, $object, "Unreserved"); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @param PdoDatabase $database |
|
| 301 | + * @param Comment $object |
|
| 302 | + * @param Request $request |
|
| 303 | + */ |
|
| 304 | + public static function editComment(PdoDatabase $database, Comment $object, Request $request) |
|
| 305 | + { |
|
| 306 | + self::createLogEntry($database, $request, "EditComment-r"); |
|
| 307 | + self::createLogEntry($database, $object, "EditComment-c"); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @param PdoDatabase $database |
|
| 312 | + * @param Comment $object |
|
| 313 | + */ |
|
| 314 | + public static function flaggedComment(PdoDatabase $database, Comment $object) |
|
| 315 | + { |
|
| 316 | + self::createLogEntry($database, $object, "FlaggedComment"); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param PdoDatabase $database |
|
| 321 | + * @param Comment $object |
|
| 322 | + */ |
|
| 323 | + public static function unflaggedComment(PdoDatabase $database, Comment $object) |
|
| 324 | + { |
|
| 325 | + self::createLogEntry($database, $object, "UnflaggedComment"); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * @param PdoDatabase $database |
|
| 330 | + * @param Request $object |
|
| 331 | + * @param User $target |
|
| 332 | + */ |
|
| 333 | + public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
|
| 334 | + { |
|
| 335 | + self::createLogEntry($database, $object, "SendReserved"); |
|
| 336 | + self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * @param PdoDatabase $database |
|
| 341 | + * @param Request $object |
|
| 342 | + * @param string $comment |
|
| 343 | + */ |
|
| 344 | + public static function sentMail(PdoDatabase $database, Request $object, $comment) |
|
| 345 | + { |
|
| 346 | + self::createLogEntry($database, $object, "SentMail", $comment); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * @param PdoDatabase $database |
|
| 351 | + * @param Request $object |
|
| 352 | + */ |
|
| 353 | + public static function enqueuedJobQueue(PdoDatabase $database, Request $object) |
|
| 354 | + { |
|
| 355 | + self::createLogEntry($database, $object, 'EnqueuedJobQueue'); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + public static function hospitalised(PdoDatabase $database, Request $object) |
|
| 359 | + { |
|
| 360 | + self::createLogEntry($database, $object, 'Hospitalised'); |
|
| 361 | + } |
|
| 362 | + #endregion |
|
| 363 | + |
|
| 364 | + #region Email templates |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * @param PdoDatabase $database |
|
| 368 | + * @param EmailTemplate $object |
|
| 369 | + */ |
|
| 370 | + public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 371 | + { |
|
| 372 | + self::createLogEntry($database, $object, "CreatedEmail"); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * @param PdoDatabase $database |
|
| 377 | + * @param EmailTemplate $object |
|
| 378 | + */ |
|
| 379 | + public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
|
| 380 | + { |
|
| 381 | + self::createLogEntry($database, $object, "EditedEmail"); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + #endregion |
|
| 385 | + |
|
| 386 | + #region Display |
|
| 387 | + |
|
| 388 | + #endregion |
|
| 389 | + |
|
| 390 | + #region Automation |
|
| 391 | + |
|
| 392 | + public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job) |
|
| 393 | + { |
|
| 394 | + self::createLogEntry($database, $job, 'JobCompleted', null, User::getCommunity()); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job) |
|
| 398 | + { |
|
| 399 | + $data = array('status' => $job->getStatus(), 'error' => $job->getError()); |
|
| 400 | + self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity()); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job) |
|
| 404 | + { |
|
| 405 | + self::createLogEntry($database, $job, 'JobCancelled', $job->getError()); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job) |
|
| 409 | + { |
|
| 410 | + self::createLogEntry($database, $job, 'JobRequeued'); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null) |
|
| 414 | + { |
|
| 415 | + self::createLogEntry($database, $job, 'JobAcknowledged', $comment); |
|
| 416 | + } |
|
| 417 | + #endregion |
|
| 418 | + |
|
| 419 | + #region Request Queues |
|
| 420 | + public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue) |
|
| 421 | + { |
|
| 422 | + self::createLogEntry($database, $queue, 'QueueCreated'); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue) |
|
| 426 | + { |
|
| 427 | + self::createLogEntry($database, $queue, 'QueueEdited'); |
|
| 428 | + } |
|
| 429 | + #endregion |
|
| 430 | + #region Domains |
|
| 431 | + public static function domainCreated(PdoDatabase $database, Domain $domain) |
|
| 432 | + { |
|
| 433 | + self::createLogEntry($database, $domain, 'DomainCreated'); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + public static function domainEdited(PdoDatabase $database, Domain $domain) |
|
| 437 | + { |
|
| 438 | + self::createLogEntry($database, $domain, 'DomainEdited'); |
|
| 439 | + } |
|
| 440 | + #endregion |
|
| 441 | + #region Request Forms |
|
| 442 | + public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm) |
|
| 443 | + { |
|
| 444 | + self::createLogEntry($database, $requestForm, 'RequestFormCreated'); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm) |
|
| 448 | + { |
|
| 449 | + self::createLogEntry($database, $requestForm, 'RequestFormEdited'); |
|
| 450 | + } |
|
| 451 | + #endregion |
|
| 452 | 452 | } |
@@ -12,37 +12,37 @@ |
||
| 12 | 12 | |
| 13 | 13 | class EmailHelper implements IEmailHelper |
| 14 | 14 | { |
| 15 | - /** @var string */ |
|
| 16 | - private $emailFrom; |
|
| 17 | - private $instance; |
|
| 18 | - |
|
| 19 | - public function __construct(string $emailFrom, $instance) |
|
| 20 | - { |
|
| 21 | - $this->emailFrom = $emailFrom; |
|
| 22 | - $this->instance = $instance; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @param string|null $replyAddress |
|
| 27 | - * @param string $to |
|
| 28 | - * @param string $subject |
|
| 29 | - * @param string $content |
|
| 30 | - * @param array $headers Extra headers to include |
|
| 31 | - */ |
|
| 32 | - public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()) |
|
| 33 | - { |
|
| 34 | - if ($replyAddress !== null) { |
|
| 35 | - $headers['Reply-To'] = $replyAddress; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - $headers['From'] = $this->emailFrom; |
|
| 39 | - $headers['X-ACC-Instance'] = $this->instance; |
|
| 40 | - $headerString = ''; |
|
| 41 | - |
|
| 42 | - foreach ($headers as $header => $headerValue) { |
|
| 43 | - $headerString .= $header . ': ' . $headerValue . "\r\n"; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - mail($to, $subject, $content, $headerString); |
|
| 47 | - } |
|
| 15 | + /** @var string */ |
|
| 16 | + private $emailFrom; |
|
| 17 | + private $instance; |
|
| 18 | + |
|
| 19 | + public function __construct(string $emailFrom, $instance) |
|
| 20 | + { |
|
| 21 | + $this->emailFrom = $emailFrom; |
|
| 22 | + $this->instance = $instance; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @param string|null $replyAddress |
|
| 27 | + * @param string $to |
|
| 28 | + * @param string $subject |
|
| 29 | + * @param string $content |
|
| 30 | + * @param array $headers Extra headers to include |
|
| 31 | + */ |
|
| 32 | + public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()) |
|
| 33 | + { |
|
| 34 | + if ($replyAddress !== null) { |
|
| 35 | + $headers['Reply-To'] = $replyAddress; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + $headers['From'] = $this->emailFrom; |
|
| 39 | + $headers['X-ACC-Instance'] = $this->instance; |
|
| 40 | + $headerString = ''; |
|
| 41 | + |
|
| 42 | + foreach ($headers as $header => $headerValue) { |
|
| 43 | + $headerString .= $header . ': ' . $headerValue . "\r\n"; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + mail($to, $subject, $content, $headerString); |
|
| 47 | + } |
|
| 48 | 48 | } |
| 49 | 49 | \ No newline at end of file |
@@ -18,98 +18,98 @@ |
||
| 18 | 18 | |
| 19 | 19 | class BlacklistHelper implements IBlacklistHelper |
| 20 | 20 | { |
| 21 | - /** @var HttpHelper */ |
|
| 22 | - private $httpHelper; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Cache of previously requested usernames |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $cache = array(); |
|
| 29 | - |
|
| 30 | - /** @var PdoDatabase */ |
|
| 31 | - private $database; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * BlacklistHelper constructor. |
|
| 35 | - * |
|
| 36 | - * @param HttpHelper $httpHelper |
|
| 37 | - * @param PdoDatabase $database |
|
| 38 | - */ |
|
| 39 | - public function __construct(HttpHelper $httpHelper, PdoDatabase $database) |
|
| 40 | - { |
|
| 41 | - $this->httpHelper = $httpHelper; |
|
| 42 | - $this->database = $database; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Returns a value indicating whether the provided username is blacklisted by the on-wiki title blacklist |
|
| 47 | - * |
|
| 48 | - * @param string $username |
|
| 49 | - * |
|
| 50 | - * @return false|string False if the username is not blacklisted, else the blacklist entry. |
|
| 51 | - */ |
|
| 52 | - public function isBlacklisted($username) |
|
| 53 | - { |
|
| 54 | - if (isset($this->cache[$username])) { |
|
| 55 | - $result = $this->cache[$username]; |
|
| 56 | - if ($result === false) { |
|
| 57 | - return false; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $result['line']; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - try { |
|
| 64 | - $result = $this->performWikiLookup($username); |
|
| 65 | - } |
|
| 66 | - catch (CurlException $ex) { |
|
| 67 | - // LOGME log this, but fail gracefully. |
|
| 68 | - ExceptionHandler::logExceptionToDisk($ex, $this->siteConfiguration); |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if ($result['result'] === 'ok') { |
|
| 73 | - // not blacklisted |
|
| 74 | - $this->cache[$username] = false; |
|
| 75 | - |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 78 | - else { |
|
| 79 | - $this->cache[$username] = $result; |
|
| 80 | - |
|
| 81 | - return $result['line']; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Performs a fetch to MediaWiki for the relevant title blacklist entry |
|
| 87 | - * |
|
| 88 | - * @param string $username The username to look up |
|
| 89 | - * |
|
| 90 | - * @return array |
|
| 91 | - * @throws CurlException |
|
| 92 | - */ |
|
| 93 | - private function performWikiLookup($username) |
|
| 94 | - { |
|
| 95 | - // FIXME: domains! |
|
| 96 | - /** @var Domain $domain */ |
|
| 97 | - $domain = Domain::getById(1, $this->database); |
|
| 98 | - |
|
| 99 | - $endpoint = $domain->getWikiApiPath(); |
|
| 100 | - |
|
| 101 | - $parameters = array( |
|
| 102 | - 'action' => 'titleblacklist', |
|
| 103 | - 'format' => 'php', |
|
| 104 | - 'tbtitle' => $username, |
|
| 105 | - 'tbaction' => 'new-account', |
|
| 106 | - 'tbnooverride' => true, |
|
| 107 | - ); |
|
| 108 | - |
|
| 109 | - $apiResult = $this->httpHelper->get($endpoint, $parameters); |
|
| 110 | - |
|
| 111 | - $data = unserialize($apiResult); |
|
| 112 | - |
|
| 113 | - return $data['titleblacklist']; |
|
| 114 | - } |
|
| 21 | + /** @var HttpHelper */ |
|
| 22 | + private $httpHelper; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Cache of previously requested usernames |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $cache = array(); |
|
| 29 | + |
|
| 30 | + /** @var PdoDatabase */ |
|
| 31 | + private $database; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * BlacklistHelper constructor. |
|
| 35 | + * |
|
| 36 | + * @param HttpHelper $httpHelper |
|
| 37 | + * @param PdoDatabase $database |
|
| 38 | + */ |
|
| 39 | + public function __construct(HttpHelper $httpHelper, PdoDatabase $database) |
|
| 40 | + { |
|
| 41 | + $this->httpHelper = $httpHelper; |
|
| 42 | + $this->database = $database; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Returns a value indicating whether the provided username is blacklisted by the on-wiki title blacklist |
|
| 47 | + * |
|
| 48 | + * @param string $username |
|
| 49 | + * |
|
| 50 | + * @return false|string False if the username is not blacklisted, else the blacklist entry. |
|
| 51 | + */ |
|
| 52 | + public function isBlacklisted($username) |
|
| 53 | + { |
|
| 54 | + if (isset($this->cache[$username])) { |
|
| 55 | + $result = $this->cache[$username]; |
|
| 56 | + if ($result === false) { |
|
| 57 | + return false; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $result['line']; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + try { |
|
| 64 | + $result = $this->performWikiLookup($username); |
|
| 65 | + } |
|
| 66 | + catch (CurlException $ex) { |
|
| 67 | + // LOGME log this, but fail gracefully. |
|
| 68 | + ExceptionHandler::logExceptionToDisk($ex, $this->siteConfiguration); |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if ($result['result'] === 'ok') { |
|
| 73 | + // not blacklisted |
|
| 74 | + $this->cache[$username] = false; |
|
| 75 | + |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | + else { |
|
| 79 | + $this->cache[$username] = $result; |
|
| 80 | + |
|
| 81 | + return $result['line']; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Performs a fetch to MediaWiki for the relevant title blacklist entry |
|
| 87 | + * |
|
| 88 | + * @param string $username The username to look up |
|
| 89 | + * |
|
| 90 | + * @return array |
|
| 91 | + * @throws CurlException |
|
| 92 | + */ |
|
| 93 | + private function performWikiLookup($username) |
|
| 94 | + { |
|
| 95 | + // FIXME: domains! |
|
| 96 | + /** @var Domain $domain */ |
|
| 97 | + $domain = Domain::getById(1, $this->database); |
|
| 98 | + |
|
| 99 | + $endpoint = $domain->getWikiApiPath(); |
|
| 100 | + |
|
| 101 | + $parameters = array( |
|
| 102 | + 'action' => 'titleblacklist', |
|
| 103 | + 'format' => 'php', |
|
| 104 | + 'tbtitle' => $username, |
|
| 105 | + 'tbaction' => 'new-account', |
|
| 106 | + 'tbnooverride' => true, |
|
| 107 | + ); |
|
| 108 | + |
|
| 109 | + $apiResult = $this->httpHelper->get($endpoint, $parameters); |
|
| 110 | + |
|
| 111 | + $data = unserialize($apiResult); |
|
| 112 | + |
|
| 113 | + return $data['titleblacklist']; |
|
| 114 | + } |
|
| 115 | 115 | } |
| 116 | 116 | \ No newline at end of file |
@@ -18,135 +18,135 @@ |
||
| 18 | 18 | |
| 19 | 19 | class OAuthProtocolHelper implements Interfaces\IOAuthProtocolHelper |
| 20 | 20 | { |
| 21 | - private $authUrl; |
|
| 22 | - /** |
|
| 23 | - * @var PdoDatabase |
|
| 24 | - */ |
|
| 25 | - private $database; |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private $consumerKey; |
|
| 30 | - /** |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - private $consumerSecret; |
|
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $userAgent; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * OAuthHelper constructor. |
|
| 41 | - * |
|
| 42 | - * @param string $consumerKey |
|
| 43 | - * @param string $consumerSecret |
|
| 44 | - * @param PdoDatabase $database |
|
| 45 | - * @param string $userAgent |
|
| 46 | - */ |
|
| 47 | - public function __construct( |
|
| 48 | - $consumerKey, |
|
| 49 | - $consumerSecret, |
|
| 50 | - PdoDatabase $database, |
|
| 51 | - $userAgent |
|
| 52 | - ) { |
|
| 53 | - $this->consumerKey = $consumerKey; |
|
| 54 | - $this->consumerSecret = $consumerSecret; |
|
| 55 | - $this->userAgent = $userAgent; |
|
| 56 | - $this->database = $database; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @inheritDoc |
|
| 61 | - */ |
|
| 62 | - public function getRequestToken() |
|
| 63 | - { |
|
| 64 | - /** @var Token $requestToken */ |
|
| 65 | - |
|
| 66 | - // FIXME: domains! |
|
| 67 | - /** @var Domain $domain */ |
|
| 68 | - $domain = Domain::getById(1, $this->database); |
|
| 69 | - |
|
| 70 | - list($authUrl, $requestToken) = $this->getClient($domain)->initiate(); |
|
| 71 | - $this->authUrl = $authUrl; |
|
| 72 | - return $requestToken; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @inheritDoc |
|
| 77 | - */ |
|
| 78 | - public function getAuthoriseUrl($requestToken) |
|
| 79 | - { |
|
| 80 | - return $this->authUrl; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @inheritDoc |
|
| 85 | - */ |
|
| 86 | - public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier) |
|
| 87 | - { |
|
| 88 | - $requestToken = new Token($oauthRequestToken, $oauthRequestSecret); |
|
| 89 | - |
|
| 90 | - // FIXME: domains! |
|
| 91 | - /** @var Domain $domain */ |
|
| 92 | - $domain = Domain::getById(1, $this->database); |
|
| 93 | - |
|
| 94 | - return $this->getClient($domain)->complete($requestToken, $oauthVerifier); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @inheritDoc |
|
| 99 | - */ |
|
| 100 | - public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret) |
|
| 101 | - { |
|
| 102 | - // FIXME: domains! |
|
| 103 | - /** @var Domain $domain */ |
|
| 104 | - $domain = Domain::getById(1, $this->database); |
|
| 105 | - |
|
| 106 | - return $this->getClient($domain)->identify(new Token($oauthAccessToken, $oauthAccessSecret)); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @inheritDoc |
|
| 111 | - */ |
|
| 112 | - public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET') |
|
| 113 | - { |
|
| 114 | - $userToken = new Token($accessToken, $accessSecret); |
|
| 115 | - |
|
| 116 | - $apiParams['format'] = 'json'; |
|
| 117 | - |
|
| 118 | - if ($apiParams === null || !is_array($apiParams)) { |
|
| 119 | - throw new CurlException("Invalid API call"); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // FIXME: domains! |
|
| 123 | - /** @var Domain $domain */ |
|
| 124 | - $domain = Domain::getById(1, $this->database); |
|
| 125 | - |
|
| 126 | - $url = $domain->getWikiApiPath(); |
|
| 127 | - $isPost = ($method === 'POST'); |
|
| 128 | - |
|
| 129 | - if ($method === 'GET') { |
|
| 130 | - $query = http_build_query($apiParams); |
|
| 131 | - $url .= '?' . $query; |
|
| 132 | - $apiParams = null; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $data = $this->getClient($domain)->makeOAuthCall($userToken, $url, $isPost, $apiParams); |
|
| 136 | - |
|
| 137 | - return json_decode($data); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $oauthEndpoint |
|
| 142 | - * |
|
| 143 | - * @return Client |
|
| 144 | - */ |
|
| 145 | - protected function getClient(Domain $domain) : Client |
|
| 146 | - { |
|
| 147 | - $oauthClientConfig = new ClientConfig($domain->getWikiArticlePath() . "?title=Special:OAuth"); |
|
| 148 | - $oauthClientConfig->setConsumer(new Consumer($this->consumerKey, $this->consumerSecret)); |
|
| 149 | - $oauthClientConfig->setUserAgent($this->userAgent); |
|
| 150 | - return new Client($oauthClientConfig); |
|
| 151 | - } |
|
| 21 | + private $authUrl; |
|
| 22 | + /** |
|
| 23 | + * @var PdoDatabase |
|
| 24 | + */ |
|
| 25 | + private $database; |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private $consumerKey; |
|
| 30 | + /** |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + private $consumerSecret; |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $userAgent; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * OAuthHelper constructor. |
|
| 41 | + * |
|
| 42 | + * @param string $consumerKey |
|
| 43 | + * @param string $consumerSecret |
|
| 44 | + * @param PdoDatabase $database |
|
| 45 | + * @param string $userAgent |
|
| 46 | + */ |
|
| 47 | + public function __construct( |
|
| 48 | + $consumerKey, |
|
| 49 | + $consumerSecret, |
|
| 50 | + PdoDatabase $database, |
|
| 51 | + $userAgent |
|
| 52 | + ) { |
|
| 53 | + $this->consumerKey = $consumerKey; |
|
| 54 | + $this->consumerSecret = $consumerSecret; |
|
| 55 | + $this->userAgent = $userAgent; |
|
| 56 | + $this->database = $database; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @inheritDoc |
|
| 61 | + */ |
|
| 62 | + public function getRequestToken() |
|
| 63 | + { |
|
| 64 | + /** @var Token $requestToken */ |
|
| 65 | + |
|
| 66 | + // FIXME: domains! |
|
| 67 | + /** @var Domain $domain */ |
|
| 68 | + $domain = Domain::getById(1, $this->database); |
|
| 69 | + |
|
| 70 | + list($authUrl, $requestToken) = $this->getClient($domain)->initiate(); |
|
| 71 | + $this->authUrl = $authUrl; |
|
| 72 | + return $requestToken; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @inheritDoc |
|
| 77 | + */ |
|
| 78 | + public function getAuthoriseUrl($requestToken) |
|
| 79 | + { |
|
| 80 | + return $this->authUrl; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @inheritDoc |
|
| 85 | + */ |
|
| 86 | + public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier) |
|
| 87 | + { |
|
| 88 | + $requestToken = new Token($oauthRequestToken, $oauthRequestSecret); |
|
| 89 | + |
|
| 90 | + // FIXME: domains! |
|
| 91 | + /** @var Domain $domain */ |
|
| 92 | + $domain = Domain::getById(1, $this->database); |
|
| 93 | + |
|
| 94 | + return $this->getClient($domain)->complete($requestToken, $oauthVerifier); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @inheritDoc |
|
| 99 | + */ |
|
| 100 | + public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret) |
|
| 101 | + { |
|
| 102 | + // FIXME: domains! |
|
| 103 | + /** @var Domain $domain */ |
|
| 104 | + $domain = Domain::getById(1, $this->database); |
|
| 105 | + |
|
| 106 | + return $this->getClient($domain)->identify(new Token($oauthAccessToken, $oauthAccessSecret)); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @inheritDoc |
|
| 111 | + */ |
|
| 112 | + public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET') |
|
| 113 | + { |
|
| 114 | + $userToken = new Token($accessToken, $accessSecret); |
|
| 115 | + |
|
| 116 | + $apiParams['format'] = 'json'; |
|
| 117 | + |
|
| 118 | + if ($apiParams === null || !is_array($apiParams)) { |
|
| 119 | + throw new CurlException("Invalid API call"); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // FIXME: domains! |
|
| 123 | + /** @var Domain $domain */ |
|
| 124 | + $domain = Domain::getById(1, $this->database); |
|
| 125 | + |
|
| 126 | + $url = $domain->getWikiApiPath(); |
|
| 127 | + $isPost = ($method === 'POST'); |
|
| 128 | + |
|
| 129 | + if ($method === 'GET') { |
|
| 130 | + $query = http_build_query($apiParams); |
|
| 131 | + $url .= '?' . $query; |
|
| 132 | + $apiParams = null; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $data = $this->getClient($domain)->makeOAuthCall($userToken, $url, $isPost, $apiParams); |
|
| 136 | + |
|
| 137 | + return json_decode($data); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $oauthEndpoint |
|
| 142 | + * |
|
| 143 | + * @return Client |
|
| 144 | + */ |
|
| 145 | + protected function getClient(Domain $domain) : Client |
|
| 146 | + { |
|
| 147 | + $oauthClientConfig = new ClientConfig($domain->getWikiArticlePath() . "?title=Special:OAuth"); |
|
| 148 | + $oauthClientConfig->setConsumer(new Consumer($this->consumerKey, $this->consumerSecret)); |
|
| 149 | + $oauthClientConfig->setUserAgent($this->userAgent); |
|
| 150 | + return new Client($oauthClientConfig); |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -30,516 +30,516 @@ |
||
| 30 | 30 | |
| 31 | 31 | class LogHelper |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * Summary of getRequestLogsWithComments |
|
| 35 | - * |
|
| 36 | - * @param int $requestId |
|
| 37 | - * @param PdoDatabase $db |
|
| 38 | - * @param SecurityManager $securityManager |
|
| 39 | - * |
|
| 40 | - * @return DataObject[] |
|
| 41 | - */ |
|
| 42 | - public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
|
| 43 | - { |
|
| 44 | - $logs = LogSearchHelper::get($db)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
| 45 | - |
|
| 46 | - $currentUser = User::getCurrent($db); |
|
| 47 | - $showRestrictedComments = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser) === SecurityManager::ALLOWED; |
|
| 48 | - $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === SecurityManager::ALLOWED; |
|
| 49 | - |
|
| 50 | - $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
|
| 51 | - |
|
| 52 | - $items = array_merge($logs, $comments); |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param DataObject $item |
|
| 56 | - * |
|
| 57 | - * @return int |
|
| 58 | - */ |
|
| 59 | - $sortKey = function(DataObject $item) { |
|
| 60 | - if ($item instanceof Log) { |
|
| 61 | - return $item->getTimestamp()->getTimestamp(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - if ($item instanceof Comment) { |
|
| 65 | - return $item->getTime()->getTimestamp(); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return 0; |
|
| 69 | - }; |
|
| 70 | - |
|
| 71 | - do { |
|
| 72 | - $flag = false; |
|
| 73 | - |
|
| 74 | - $loopLimit = (count($items) - 1); |
|
| 75 | - for ($i = 0; $i < $loopLimit; $i++) { |
|
| 76 | - // are these two items out of order? |
|
| 77 | - if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
|
| 78 | - // swap them |
|
| 79 | - $swap = $items[$i]; |
|
| 80 | - $items[$i] = $items[$i + 1]; |
|
| 81 | - $items[$i + 1] = $swap; |
|
| 82 | - |
|
| 83 | - // set a flag to say we've modified the array this time around |
|
| 84 | - $flag = true; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - while ($flag); |
|
| 89 | - |
|
| 90 | - return $items; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Summary of getLogDescription |
|
| 95 | - * |
|
| 96 | - * @param Log $entry |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public static function getLogDescription(Log $entry) |
|
| 101 | - { |
|
| 102 | - $text = "Deferred to "; |
|
| 103 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 104 | - // Deferred to a different queue |
|
| 105 | - // This is exactly what we want to display. |
|
| 106 | - return $entry->getAction(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $text = "Closed custom-n"; |
|
| 110 | - if ($entry->getAction() == $text) { |
|
| 111 | - // Custom-closed |
|
| 112 | - return "closed (custom reason - account not created)"; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $text = "Closed custom-y"; |
|
| 116 | - if ($entry->getAction() == $text) { |
|
| 117 | - // Custom-closed |
|
| 118 | - return "closed (custom reason - account created)"; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $text = "Closed 0"; |
|
| 122 | - if ($entry->getAction() == $text) { |
|
| 123 | - // Dropped the request - short-circuit the lookup |
|
| 124 | - return "dropped request"; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $text = "Closed "; |
|
| 128 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 129 | - // Closed with a reason - do a lookup here. |
|
| 130 | - $id = substr($entry->getAction(), strlen($text)); |
|
| 131 | - /** @var EmailTemplate $template */ |
|
| 132 | - $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
| 133 | - |
|
| 134 | - if ($template != false) { |
|
| 135 | - return "closed (" . $template->getName() . ")"; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Fall back to the basic stuff |
|
| 140 | - $lookup = array( |
|
| 141 | - 'Reserved' => 'reserved', |
|
| 142 | - 'Email Confirmed' => 'email-confirmed', |
|
| 143 | - 'Manually Confirmed' => 'manually confirmed the request', |
|
| 144 | - 'Unreserved' => 'unreserved', |
|
| 145 | - 'Approved' => 'approved', |
|
| 146 | - 'Suspended' => 'suspended', |
|
| 147 | - 'RoleChange' => 'changed roles', |
|
| 148 | - 'Banned' => 'banned', |
|
| 149 | - 'Edited' => 'edited interface message', |
|
| 150 | - 'Declined' => 'declined', |
|
| 151 | - 'EditComment-c' => 'edited a comment', |
|
| 152 | - 'EditComment-r' => 'edited a comment', |
|
| 153 | - 'FlaggedComment' => 'flagged a comment', |
|
| 154 | - 'UnflaggedComment' => 'unflagged a comment', |
|
| 155 | - 'Unbanned' => 'unbanned', |
|
| 156 | - 'Promoted' => 'promoted to tool admin', |
|
| 157 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
| 158 | - 'Prefchange' => 'changed user preferences', |
|
| 159 | - 'Renamed' => 'renamed', |
|
| 160 | - 'Demoted' => 'demoted from tool admin', |
|
| 161 | - 'ReceiveReserved' => 'received the reservation', |
|
| 162 | - 'SendReserved' => 'sent the reservation', |
|
| 163 | - 'EditedEmail' => 'edited email', |
|
| 164 | - 'DeletedTemplate' => 'deleted template', |
|
| 165 | - 'EditedTemplate' => 'edited template', |
|
| 166 | - 'CreatedEmail' => 'created email', |
|
| 167 | - 'CreatedTemplate' => 'created template', |
|
| 168 | - 'SentMail' => 'sent an email to the requester', |
|
| 169 | - 'Registered' => 'registered a tool account', |
|
| 170 | - 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 171 | - 'JobCompleted' => 'completed a background job', |
|
| 172 | - 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 173 | - 'JobRequeued' => 'requeued a job for re-execution', |
|
| 174 | - 'JobCancelled' => 'cancelled execution of a job', |
|
| 175 | - 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 176 | - 'Hospitalised' => 'sent to the hospital', |
|
| 177 | - 'QueueCreated' => 'created a request queue', |
|
| 178 | - 'QueueEdited' => 'edited a request queue', |
|
| 179 | - 'DomainCreated' => 'created a domain', |
|
| 180 | - 'DomainEdited' => 'edited a domain', |
|
| 181 | - 'RequestFormCreated' => 'created a request form', |
|
| 182 | - 'RequestFormEdited' => 'edited a request form', |
|
| 183 | - ); |
|
| 184 | - |
|
| 185 | - if (array_key_exists($entry->getAction(), $lookup)) { |
|
| 186 | - return $lookup[$entry->getAction()]; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // OK, I don't know what this is. Fall back to something sane. |
|
| 190 | - return "performed an unknown action ({$entry->getAction()})"; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param PdoDatabase $database |
|
| 195 | - * |
|
| 196 | - * @return array |
|
| 197 | - */ |
|
| 198 | - public static function getLogActions(PdoDatabase $database) |
|
| 199 | - { |
|
| 200 | - $lookup = array( |
|
| 201 | - "Requests" => [ |
|
| 202 | - 'Reserved' => 'reserved', |
|
| 203 | - 'Email Confirmed' => 'email-confirmed', |
|
| 204 | - 'Manually Confirmed' => 'manually confirmed', |
|
| 205 | - 'Unreserved' => 'unreserved', |
|
| 206 | - 'EditComment-c' => 'edited a comment (by comment ID)', |
|
| 207 | - 'EditComment-r' => 'edited a comment (by request)', |
|
| 208 | - 'FlaggedComment' => 'flagged a comment', |
|
| 209 | - 'UnflaggedComment' => 'unflagged a comment', |
|
| 210 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
| 211 | - 'ReceiveReserved' => 'received the reservation', |
|
| 212 | - 'SendReserved' => 'sent the reservation', |
|
| 213 | - 'SentMail' => 'sent an email to the requester', |
|
| 214 | - 'Closed 0' => 'dropped request', |
|
| 215 | - 'Closed custom-y' => 'closed (custom reason - account created)', |
|
| 216 | - 'Closed custom-n' => 'closed (custom reason - account not created)', |
|
| 217 | - ], |
|
| 218 | - 'Users' => [ |
|
| 219 | - 'Approved' => 'approved', |
|
| 220 | - 'Suspended' => 'suspended', |
|
| 221 | - 'RoleChange' => 'changed roles', |
|
| 222 | - 'Declined' => 'declined', |
|
| 223 | - 'Prefchange' => 'changed user preferences', |
|
| 224 | - 'Renamed' => 'renamed', |
|
| 225 | - 'Promoted' => 'promoted to tool admin', |
|
| 226 | - 'Demoted' => 'demoted from tool admin', |
|
| 227 | - 'Registered' => 'registered a tool account', |
|
| 228 | - ], |
|
| 229 | - "Bans" => [ |
|
| 230 | - 'Banned' => 'banned', |
|
| 231 | - 'Unbanned' => 'unbanned', |
|
| 232 | - ], |
|
| 233 | - "Site notice" => [ |
|
| 234 | - 'Edited' => 'edited interface message', |
|
| 235 | - ], |
|
| 236 | - "Email close templates" => [ |
|
| 237 | - 'EditedEmail' => 'edited email', |
|
| 238 | - 'CreatedEmail' => 'created email', |
|
| 239 | - ], |
|
| 240 | - "Welcome templates" => [ |
|
| 241 | - 'DeletedTemplate' => 'deleted template', |
|
| 242 | - 'EditedTemplate' => 'edited template', |
|
| 243 | - 'CreatedTemplate' => 'created template', |
|
| 244 | - ], |
|
| 245 | - "Job queue" => [ |
|
| 246 | - 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 247 | - 'JobCompleted' => 'completed a background job', |
|
| 248 | - 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 249 | - 'JobRequeued' => 'requeued a job for re-execution', |
|
| 250 | - 'JobCancelled' => 'cancelled execution of a job', |
|
| 251 | - 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 252 | - 'Hospitalised' => 'sent to the hospital', |
|
| 253 | - ], |
|
| 254 | - "Request queues" => [ |
|
| 255 | - 'QueueCreated' => 'created a request queue', |
|
| 256 | - 'QueueEdited' => 'edited a request queue', |
|
| 257 | - ], |
|
| 258 | - "Domains" => [ |
|
| 259 | - 'DomainCreated' => 'created a domain', |
|
| 260 | - 'DomainEdited' => 'edited a domain', |
|
| 261 | - ], |
|
| 262 | - "Request forms" => [ |
|
| 263 | - 'RequestFormCreated' => 'created a request form', |
|
| 264 | - 'RequestFormEdited' => 'edited a request form', |
|
| 265 | - ], |
|
| 266 | - ); |
|
| 267 | - |
|
| 268 | - $databaseDrivenLogKeys = $database->query(<<<SQL |
|
| 33 | + /** |
|
| 34 | + * Summary of getRequestLogsWithComments |
|
| 35 | + * |
|
| 36 | + * @param int $requestId |
|
| 37 | + * @param PdoDatabase $db |
|
| 38 | + * @param SecurityManager $securityManager |
|
| 39 | + * |
|
| 40 | + * @return DataObject[] |
|
| 41 | + */ |
|
| 42 | + public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
|
| 43 | + { |
|
| 44 | + $logs = LogSearchHelper::get($db)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
| 45 | + |
|
| 46 | + $currentUser = User::getCurrent($db); |
|
| 47 | + $showRestrictedComments = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser) === SecurityManager::ALLOWED; |
|
| 48 | + $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === SecurityManager::ALLOWED; |
|
| 49 | + |
|
| 50 | + $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
|
| 51 | + |
|
| 52 | + $items = array_merge($logs, $comments); |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param DataObject $item |
|
| 56 | + * |
|
| 57 | + * @return int |
|
| 58 | + */ |
|
| 59 | + $sortKey = function(DataObject $item) { |
|
| 60 | + if ($item instanceof Log) { |
|
| 61 | + return $item->getTimestamp()->getTimestamp(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + if ($item instanceof Comment) { |
|
| 65 | + return $item->getTime()->getTimestamp(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return 0; |
|
| 69 | + }; |
|
| 70 | + |
|
| 71 | + do { |
|
| 72 | + $flag = false; |
|
| 73 | + |
|
| 74 | + $loopLimit = (count($items) - 1); |
|
| 75 | + for ($i = 0; $i < $loopLimit; $i++) { |
|
| 76 | + // are these two items out of order? |
|
| 77 | + if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
|
| 78 | + // swap them |
|
| 79 | + $swap = $items[$i]; |
|
| 80 | + $items[$i] = $items[$i + 1]; |
|
| 81 | + $items[$i + 1] = $swap; |
|
| 82 | + |
|
| 83 | + // set a flag to say we've modified the array this time around |
|
| 84 | + $flag = true; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + while ($flag); |
|
| 89 | + |
|
| 90 | + return $items; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Summary of getLogDescription |
|
| 95 | + * |
|
| 96 | + * @param Log $entry |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public static function getLogDescription(Log $entry) |
|
| 101 | + { |
|
| 102 | + $text = "Deferred to "; |
|
| 103 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 104 | + // Deferred to a different queue |
|
| 105 | + // This is exactly what we want to display. |
|
| 106 | + return $entry->getAction(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $text = "Closed custom-n"; |
|
| 110 | + if ($entry->getAction() == $text) { |
|
| 111 | + // Custom-closed |
|
| 112 | + return "closed (custom reason - account not created)"; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $text = "Closed custom-y"; |
|
| 116 | + if ($entry->getAction() == $text) { |
|
| 117 | + // Custom-closed |
|
| 118 | + return "closed (custom reason - account created)"; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $text = "Closed 0"; |
|
| 122 | + if ($entry->getAction() == $text) { |
|
| 123 | + // Dropped the request - short-circuit the lookup |
|
| 124 | + return "dropped request"; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $text = "Closed "; |
|
| 128 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
| 129 | + // Closed with a reason - do a lookup here. |
|
| 130 | + $id = substr($entry->getAction(), strlen($text)); |
|
| 131 | + /** @var EmailTemplate $template */ |
|
| 132 | + $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
| 133 | + |
|
| 134 | + if ($template != false) { |
|
| 135 | + return "closed (" . $template->getName() . ")"; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Fall back to the basic stuff |
|
| 140 | + $lookup = array( |
|
| 141 | + 'Reserved' => 'reserved', |
|
| 142 | + 'Email Confirmed' => 'email-confirmed', |
|
| 143 | + 'Manually Confirmed' => 'manually confirmed the request', |
|
| 144 | + 'Unreserved' => 'unreserved', |
|
| 145 | + 'Approved' => 'approved', |
|
| 146 | + 'Suspended' => 'suspended', |
|
| 147 | + 'RoleChange' => 'changed roles', |
|
| 148 | + 'Banned' => 'banned', |
|
| 149 | + 'Edited' => 'edited interface message', |
|
| 150 | + 'Declined' => 'declined', |
|
| 151 | + 'EditComment-c' => 'edited a comment', |
|
| 152 | + 'EditComment-r' => 'edited a comment', |
|
| 153 | + 'FlaggedComment' => 'flagged a comment', |
|
| 154 | + 'UnflaggedComment' => 'unflagged a comment', |
|
| 155 | + 'Unbanned' => 'unbanned', |
|
| 156 | + 'Promoted' => 'promoted to tool admin', |
|
| 157 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
| 158 | + 'Prefchange' => 'changed user preferences', |
|
| 159 | + 'Renamed' => 'renamed', |
|
| 160 | + 'Demoted' => 'demoted from tool admin', |
|
| 161 | + 'ReceiveReserved' => 'received the reservation', |
|
| 162 | + 'SendReserved' => 'sent the reservation', |
|
| 163 | + 'EditedEmail' => 'edited email', |
|
| 164 | + 'DeletedTemplate' => 'deleted template', |
|
| 165 | + 'EditedTemplate' => 'edited template', |
|
| 166 | + 'CreatedEmail' => 'created email', |
|
| 167 | + 'CreatedTemplate' => 'created template', |
|
| 168 | + 'SentMail' => 'sent an email to the requester', |
|
| 169 | + 'Registered' => 'registered a tool account', |
|
| 170 | + 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 171 | + 'JobCompleted' => 'completed a background job', |
|
| 172 | + 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 173 | + 'JobRequeued' => 'requeued a job for re-execution', |
|
| 174 | + 'JobCancelled' => 'cancelled execution of a job', |
|
| 175 | + 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 176 | + 'Hospitalised' => 'sent to the hospital', |
|
| 177 | + 'QueueCreated' => 'created a request queue', |
|
| 178 | + 'QueueEdited' => 'edited a request queue', |
|
| 179 | + 'DomainCreated' => 'created a domain', |
|
| 180 | + 'DomainEdited' => 'edited a domain', |
|
| 181 | + 'RequestFormCreated' => 'created a request form', |
|
| 182 | + 'RequestFormEdited' => 'edited a request form', |
|
| 183 | + ); |
|
| 184 | + |
|
| 185 | + if (array_key_exists($entry->getAction(), $lookup)) { |
|
| 186 | + return $lookup[$entry->getAction()]; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // OK, I don't know what this is. Fall back to something sane. |
|
| 190 | + return "performed an unknown action ({$entry->getAction()})"; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param PdoDatabase $database |
|
| 195 | + * |
|
| 196 | + * @return array |
|
| 197 | + */ |
|
| 198 | + public static function getLogActions(PdoDatabase $database) |
|
| 199 | + { |
|
| 200 | + $lookup = array( |
|
| 201 | + "Requests" => [ |
|
| 202 | + 'Reserved' => 'reserved', |
|
| 203 | + 'Email Confirmed' => 'email-confirmed', |
|
| 204 | + 'Manually Confirmed' => 'manually confirmed', |
|
| 205 | + 'Unreserved' => 'unreserved', |
|
| 206 | + 'EditComment-c' => 'edited a comment (by comment ID)', |
|
| 207 | + 'EditComment-r' => 'edited a comment (by request)', |
|
| 208 | + 'FlaggedComment' => 'flagged a comment', |
|
| 209 | + 'UnflaggedComment' => 'unflagged a comment', |
|
| 210 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
| 211 | + 'ReceiveReserved' => 'received the reservation', |
|
| 212 | + 'SendReserved' => 'sent the reservation', |
|
| 213 | + 'SentMail' => 'sent an email to the requester', |
|
| 214 | + 'Closed 0' => 'dropped request', |
|
| 215 | + 'Closed custom-y' => 'closed (custom reason - account created)', |
|
| 216 | + 'Closed custom-n' => 'closed (custom reason - account not created)', |
|
| 217 | + ], |
|
| 218 | + 'Users' => [ |
|
| 219 | + 'Approved' => 'approved', |
|
| 220 | + 'Suspended' => 'suspended', |
|
| 221 | + 'RoleChange' => 'changed roles', |
|
| 222 | + 'Declined' => 'declined', |
|
| 223 | + 'Prefchange' => 'changed user preferences', |
|
| 224 | + 'Renamed' => 'renamed', |
|
| 225 | + 'Promoted' => 'promoted to tool admin', |
|
| 226 | + 'Demoted' => 'demoted from tool admin', |
|
| 227 | + 'Registered' => 'registered a tool account', |
|
| 228 | + ], |
|
| 229 | + "Bans" => [ |
|
| 230 | + 'Banned' => 'banned', |
|
| 231 | + 'Unbanned' => 'unbanned', |
|
| 232 | + ], |
|
| 233 | + "Site notice" => [ |
|
| 234 | + 'Edited' => 'edited interface message', |
|
| 235 | + ], |
|
| 236 | + "Email close templates" => [ |
|
| 237 | + 'EditedEmail' => 'edited email', |
|
| 238 | + 'CreatedEmail' => 'created email', |
|
| 239 | + ], |
|
| 240 | + "Welcome templates" => [ |
|
| 241 | + 'DeletedTemplate' => 'deleted template', |
|
| 242 | + 'EditedTemplate' => 'edited template', |
|
| 243 | + 'CreatedTemplate' => 'created template', |
|
| 244 | + ], |
|
| 245 | + "Job queue" => [ |
|
| 246 | + 'JobIssue' => 'ran a background job unsuccessfully', |
|
| 247 | + 'JobCompleted' => 'completed a background job', |
|
| 248 | + 'JobAcknowledged' => 'acknowledged a job failure', |
|
| 249 | + 'JobRequeued' => 'requeued a job for re-execution', |
|
| 250 | + 'JobCancelled' => 'cancelled execution of a job', |
|
| 251 | + 'EnqueuedJobQueue' => 'scheduled for creation', |
|
| 252 | + 'Hospitalised' => 'sent to the hospital', |
|
| 253 | + ], |
|
| 254 | + "Request queues" => [ |
|
| 255 | + 'QueueCreated' => 'created a request queue', |
|
| 256 | + 'QueueEdited' => 'edited a request queue', |
|
| 257 | + ], |
|
| 258 | + "Domains" => [ |
|
| 259 | + 'DomainCreated' => 'created a domain', |
|
| 260 | + 'DomainEdited' => 'edited a domain', |
|
| 261 | + ], |
|
| 262 | + "Request forms" => [ |
|
| 263 | + 'RequestFormCreated' => 'created a request form', |
|
| 264 | + 'RequestFormEdited' => 'edited a request form', |
|
| 265 | + ], |
|
| 266 | + ); |
|
| 267 | + |
|
| 268 | + $databaseDrivenLogKeys = $database->query(<<<SQL |
|
| 269 | 269 | SELECT CONCAT('Closed ', id) AS k, CONCAT('closed (',name,')') AS v FROM emailtemplate |
| 270 | 270 | UNION ALL |
| 271 | 271 | SELECT CONCAT('Deferred to ', logname) AS k, CONCAT('deferred to ', displayname) AS v FROM requestqueue; |
| 272 | 272 | SQL |
| 273 | - ); |
|
| 274 | - foreach ($databaseDrivenLogKeys->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 275 | - $lookup["Requests"][$row['k']] = $row['v']; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - return $lookup; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - public static function getObjectTypes() |
|
| 282 | - { |
|
| 283 | - return array( |
|
| 284 | - 'Ban' => 'Ban', |
|
| 285 | - 'Comment' => 'Comment', |
|
| 286 | - 'EmailTemplate' => 'Email template', |
|
| 287 | - 'JobQueue' => 'Job queue item', |
|
| 288 | - 'Request' => 'Request', |
|
| 289 | - 'SiteNotice' => 'Site notice', |
|
| 290 | - 'User' => 'User', |
|
| 291 | - 'WelcomeTemplate' => 'Welcome template', |
|
| 292 | - 'RequestQueue' => 'Request queue', |
|
| 293 | - 'Domain' => 'Domain', |
|
| 294 | - 'RequestForm' => 'Request form' |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * This returns a HTML |
|
| 300 | - * |
|
| 301 | - * @param string $objectId |
|
| 302 | - * @param string $objectType |
|
| 303 | - * @param PdoDatabase $database |
|
| 304 | - * @param SiteConfiguration $configuration |
|
| 305 | - * |
|
| 306 | - * @return null|string |
|
| 307 | - * @category Security-Critical |
|
| 308 | - */ |
|
| 309 | - private static function getObjectDescription( |
|
| 310 | - $objectId, |
|
| 311 | - $objectType, |
|
| 312 | - PdoDatabase $database, |
|
| 313 | - SiteConfiguration $configuration |
|
| 314 | - ) { |
|
| 315 | - if ($objectType == '') { |
|
| 316 | - return null; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - $baseurl = $configuration->getBaseUrl(); |
|
| 320 | - |
|
| 321 | - switch ($objectType) { |
|
| 322 | - case 'Ban': |
|
| 323 | - /** @var Ban $ban */ |
|
| 324 | - $ban = Ban::getById($objectId, $database); |
|
| 325 | - |
|
| 326 | - if ($ban === false) { |
|
| 327 | - return 'Ban #' . $objectId; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - return <<<HTML |
|
| 273 | + ); |
|
| 274 | + foreach ($databaseDrivenLogKeys->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
| 275 | + $lookup["Requests"][$row['k']] = $row['v']; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + return $lookup; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + public static function getObjectTypes() |
|
| 282 | + { |
|
| 283 | + return array( |
|
| 284 | + 'Ban' => 'Ban', |
|
| 285 | + 'Comment' => 'Comment', |
|
| 286 | + 'EmailTemplate' => 'Email template', |
|
| 287 | + 'JobQueue' => 'Job queue item', |
|
| 288 | + 'Request' => 'Request', |
|
| 289 | + 'SiteNotice' => 'Site notice', |
|
| 290 | + 'User' => 'User', |
|
| 291 | + 'WelcomeTemplate' => 'Welcome template', |
|
| 292 | + 'RequestQueue' => 'Request queue', |
|
| 293 | + 'Domain' => 'Domain', |
|
| 294 | + 'RequestForm' => 'Request form' |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * This returns a HTML |
|
| 300 | + * |
|
| 301 | + * @param string $objectId |
|
| 302 | + * @param string $objectType |
|
| 303 | + * @param PdoDatabase $database |
|
| 304 | + * @param SiteConfiguration $configuration |
|
| 305 | + * |
|
| 306 | + * @return null|string |
|
| 307 | + * @category Security-Critical |
|
| 308 | + */ |
|
| 309 | + private static function getObjectDescription( |
|
| 310 | + $objectId, |
|
| 311 | + $objectType, |
|
| 312 | + PdoDatabase $database, |
|
| 313 | + SiteConfiguration $configuration |
|
| 314 | + ) { |
|
| 315 | + if ($objectType == '') { |
|
| 316 | + return null; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + $baseurl = $configuration->getBaseUrl(); |
|
| 320 | + |
|
| 321 | + switch ($objectType) { |
|
| 322 | + case 'Ban': |
|
| 323 | + /** @var Ban $ban */ |
|
| 324 | + $ban = Ban::getById($objectId, $database); |
|
| 325 | + |
|
| 326 | + if ($ban === false) { |
|
| 327 | + return 'Ban #' . $objectId; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + return <<<HTML |
|
| 331 | 331 | <a href="{$baseurl}/internal.php/bans/show?id={$objectId}">Ban #{$objectId}</a> |
| 332 | 332 | HTML; |
| 333 | - case 'EmailTemplate': |
|
| 334 | - /** @var EmailTemplate $emailTemplate */ |
|
| 335 | - $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
| 333 | + case 'EmailTemplate': |
|
| 334 | + /** @var EmailTemplate $emailTemplate */ |
|
| 335 | + $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
| 336 | 336 | |
| 337 | - if ($emailTemplate === false) { |
|
| 338 | - return 'Email Template #' . $objectId; |
|
| 339 | - } |
|
| 337 | + if ($emailTemplate === false) { |
|
| 338 | + return 'Email Template #' . $objectId; |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | - $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 341 | + $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 342 | 342 | |
| 343 | - return <<<HTML |
|
| 343 | + return <<<HTML |
|
| 344 | 344 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
| 345 | 345 | HTML; |
| 346 | - case 'SiteNotice': |
|
| 347 | - return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
| 348 | - case 'Request': |
|
| 349 | - /** @var Request $request */ |
|
| 350 | - $request = Request::getById($objectId, $database); |
|
| 346 | + case 'SiteNotice': |
|
| 347 | + return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
| 348 | + case 'Request': |
|
| 349 | + /** @var Request $request */ |
|
| 350 | + $request = Request::getById($objectId, $database); |
|
| 351 | 351 | |
| 352 | - if ($request === false) { |
|
| 353 | - return 'Request #' . $objectId; |
|
| 354 | - } |
|
| 352 | + if ($request === false) { |
|
| 353 | + return 'Request #' . $objectId; |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | - $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 356 | + $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 357 | 357 | |
| 358 | - return <<<HTML |
|
| 358 | + return <<<HTML |
|
| 359 | 359 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
| 360 | 360 | HTML; |
| 361 | - case 'User': |
|
| 362 | - /** @var User $user */ |
|
| 363 | - $user = User::getById($objectId, $database); |
|
| 364 | - |
|
| 365 | - // Some users were merged out of existence |
|
| 366 | - if ($user === false) { |
|
| 367 | - return 'User #' . $objectId; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
| 371 | - |
|
| 372 | - return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
| 373 | - case 'WelcomeTemplate': |
|
| 374 | - /** @var WelcomeTemplate $welcomeTemplate */ |
|
| 375 | - $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
| 376 | - |
|
| 377 | - // some old templates have been completely deleted and lost to the depths of time. |
|
| 378 | - if ($welcomeTemplate === false) { |
|
| 379 | - return "Welcome template #{$objectId}"; |
|
| 380 | - } |
|
| 381 | - else { |
|
| 382 | - $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
| 383 | - |
|
| 384 | - return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
| 385 | - } |
|
| 386 | - case 'JobQueue': |
|
| 387 | - /** @var JobQueue $job */ |
|
| 388 | - $job = JobQueue::getById($objectId, $database); |
|
| 389 | - |
|
| 390 | - $taskDescriptions = JobQueue::getTaskDescriptions(); |
|
| 391 | - |
|
| 392 | - if ($job === false) { |
|
| 393 | - return 'Job Queue Task #' . $objectId; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - $task = $job->getTask(); |
|
| 397 | - if (isset($taskDescriptions[$task])) { |
|
| 398 | - $description = $taskDescriptions[$task]; |
|
| 399 | - } |
|
| 400 | - else { |
|
| 401 | - $description = 'Unknown task'; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
|
| 405 | - case 'RequestQueue': |
|
| 406 | - /** @var RequestQueue $queue */ |
|
| 407 | - $queue = RequestQueue::getById($objectId, $database); |
|
| 408 | - |
|
| 409 | - if ($queue === false) { |
|
| 410 | - return "Request Queue #{$objectId}"; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - $queueHeader = htmlentities($queue->getHeader(), ENT_COMPAT, 'UTF-8'); |
|
| 414 | - |
|
| 415 | - return "<a href=\"{$baseurl}/internal.php/queueManagement/edit?queue={$objectId}\">{$queueHeader}</a>"; |
|
| 416 | - case 'Domain': |
|
| 417 | - /** @var Domain $domain */ |
|
| 418 | - $domain = Domain::getById($objectId, $database); |
|
| 419 | - |
|
| 420 | - if ($domain === false) { |
|
| 421 | - return "Domain #{$objectId}"; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - $domainName = htmlentities($domain->getShortName(), ENT_COMPAT, 'UTF-8'); |
|
| 425 | - return "<a href=\"{$baseurl}/internal.php/domainManagement/edit?domain={$objectId}\">{$domainName}</a>"; |
|
| 426 | - case 'RequestForm': |
|
| 427 | - /** @var RequestForm $queue */ |
|
| 428 | - $queue = RequestForm::getById($objectId, $database); |
|
| 429 | - |
|
| 430 | - if ($queue === false) { |
|
| 431 | - return "Request Form #{$objectId}"; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $formName = htmlentities($queue->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 435 | - |
|
| 436 | - return "<a href=\"{$baseurl}/internal.php/requestFormManagement/edit?form={$objectId}\">{$formName}</a>"; |
|
| 437 | - |
|
| 438 | - default: |
|
| 439 | - return '[' . $objectType . " " . $objectId . ']'; |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * @param Log[] $logs |
|
| 445 | - * @param PdoDatabase $database |
|
| 446 | - * @param SiteConfiguration $configuration |
|
| 447 | - * |
|
| 448 | - * @return array |
|
| 449 | - * @throws Exception |
|
| 450 | - */ |
|
| 451 | - public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
|
| 452 | - { |
|
| 453 | - $userIds = array(); |
|
| 454 | - |
|
| 455 | - foreach ($logs as $logEntry) { |
|
| 456 | - if (!$logEntry instanceof Log) { |
|
| 457 | - // if this happens, we've done something wrong with passing back the log data. |
|
| 458 | - throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - $user = $logEntry->getUser(); |
|
| 462 | - if ($user === -1) { |
|
| 463 | - continue; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - if (!array_search($user, $userIds)) { |
|
| 467 | - $userIds[] = $user; |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
| 472 | - $users[-1] = User::getCommunity()->getUsername(); |
|
| 473 | - |
|
| 474 | - $logData = array(); |
|
| 475 | - |
|
| 476 | - foreach ($logs as $logEntry) { |
|
| 477 | - $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
| 478 | - $database, $configuration); |
|
| 479 | - |
|
| 480 | - // initialise to sane default |
|
| 481 | - $comment = null; |
|
| 482 | - |
|
| 483 | - switch ($logEntry->getAction()) { |
|
| 484 | - case 'Renamed': |
|
| 485 | - $renameData = unserialize($logEntry->getComment()); |
|
| 486 | - $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
| 487 | - $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
| 488 | - $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
| 489 | - break; |
|
| 490 | - case 'RoleChange': |
|
| 491 | - $roleChangeData = unserialize($logEntry->getComment()); |
|
| 492 | - |
|
| 493 | - $removed = array(); |
|
| 494 | - foreach ($roleChangeData['removed'] as $r) { |
|
| 495 | - $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - $added = array(); |
|
| 499 | - foreach ($roleChangeData['added'] as $r) { |
|
| 500 | - $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
| 504 | - |
|
| 505 | - $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
| 506 | - $comment = $roleDelta . ' with comment: ' . $reason; |
|
| 507 | - break; |
|
| 508 | - case 'JobIssue': |
|
| 509 | - $jobIssueData = unserialize($logEntry->getComment()); |
|
| 510 | - $errorMessage = $jobIssueData['error']; |
|
| 511 | - $status = $jobIssueData['status']; |
|
| 512 | - |
|
| 513 | - $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
|
| 514 | - $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
|
| 515 | - break; |
|
| 516 | - case 'JobIssueRequest': |
|
| 517 | - case 'JobCompletedRequest': |
|
| 518 | - $jobData = unserialize($logEntry->getComment()); |
|
| 519 | - |
|
| 520 | - /** @var JobQueue $job */ |
|
| 521 | - $job = JobQueue::getById($jobData['job'], $database); |
|
| 522 | - $descs = JobQueue::getTaskDescriptions(); |
|
| 523 | - $comment = htmlentities($descs[$job->getTask()], ENT_COMPAT, 'UTF-8'); |
|
| 524 | - break; |
|
| 525 | - |
|
| 526 | - case 'JobCompleted': |
|
| 527 | - break; |
|
| 528 | - default: |
|
| 529 | - $comment = $logEntry->getComment(); |
|
| 530 | - break; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - $logData[] = array( |
|
| 534 | - 'timestamp' => $logEntry->getTimestamp(), |
|
| 535 | - 'userid' => $logEntry->getUser(), |
|
| 536 | - 'username' => $users[$logEntry->getUser()], |
|
| 537 | - 'description' => self::getLogDescription($logEntry), |
|
| 538 | - 'objectdescription' => $objectDescription, |
|
| 539 | - 'comment' => $comment, |
|
| 540 | - ); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - return array($users, $logData); |
|
| 544 | - } |
|
| 361 | + case 'User': |
|
| 362 | + /** @var User $user */ |
|
| 363 | + $user = User::getById($objectId, $database); |
|
| 364 | + |
|
| 365 | + // Some users were merged out of existence |
|
| 366 | + if ($user === false) { |
|
| 367 | + return 'User #' . $objectId; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
| 371 | + |
|
| 372 | + return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
| 373 | + case 'WelcomeTemplate': |
|
| 374 | + /** @var WelcomeTemplate $welcomeTemplate */ |
|
| 375 | + $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
| 376 | + |
|
| 377 | + // some old templates have been completely deleted and lost to the depths of time. |
|
| 378 | + if ($welcomeTemplate === false) { |
|
| 379 | + return "Welcome template #{$objectId}"; |
|
| 380 | + } |
|
| 381 | + else { |
|
| 382 | + $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
| 383 | + |
|
| 384 | + return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
| 385 | + } |
|
| 386 | + case 'JobQueue': |
|
| 387 | + /** @var JobQueue $job */ |
|
| 388 | + $job = JobQueue::getById($objectId, $database); |
|
| 389 | + |
|
| 390 | + $taskDescriptions = JobQueue::getTaskDescriptions(); |
|
| 391 | + |
|
| 392 | + if ($job === false) { |
|
| 393 | + return 'Job Queue Task #' . $objectId; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + $task = $job->getTask(); |
|
| 397 | + if (isset($taskDescriptions[$task])) { |
|
| 398 | + $description = $taskDescriptions[$task]; |
|
| 399 | + } |
|
| 400 | + else { |
|
| 401 | + $description = 'Unknown task'; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
|
| 405 | + case 'RequestQueue': |
|
| 406 | + /** @var RequestQueue $queue */ |
|
| 407 | + $queue = RequestQueue::getById($objectId, $database); |
|
| 408 | + |
|
| 409 | + if ($queue === false) { |
|
| 410 | + return "Request Queue #{$objectId}"; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + $queueHeader = htmlentities($queue->getHeader(), ENT_COMPAT, 'UTF-8'); |
|
| 414 | + |
|
| 415 | + return "<a href=\"{$baseurl}/internal.php/queueManagement/edit?queue={$objectId}\">{$queueHeader}</a>"; |
|
| 416 | + case 'Domain': |
|
| 417 | + /** @var Domain $domain */ |
|
| 418 | + $domain = Domain::getById($objectId, $database); |
|
| 419 | + |
|
| 420 | + if ($domain === false) { |
|
| 421 | + return "Domain #{$objectId}"; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + $domainName = htmlentities($domain->getShortName(), ENT_COMPAT, 'UTF-8'); |
|
| 425 | + return "<a href=\"{$baseurl}/internal.php/domainManagement/edit?domain={$objectId}\">{$domainName}</a>"; |
|
| 426 | + case 'RequestForm': |
|
| 427 | + /** @var RequestForm $queue */ |
|
| 428 | + $queue = RequestForm::getById($objectId, $database); |
|
| 429 | + |
|
| 430 | + if ($queue === false) { |
|
| 431 | + return "Request Form #{$objectId}"; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $formName = htmlentities($queue->getName(), ENT_COMPAT, 'UTF-8'); |
|
| 435 | + |
|
| 436 | + return "<a href=\"{$baseurl}/internal.php/requestFormManagement/edit?form={$objectId}\">{$formName}</a>"; |
|
| 437 | + |
|
| 438 | + default: |
|
| 439 | + return '[' . $objectType . " " . $objectId . ']'; |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * @param Log[] $logs |
|
| 445 | + * @param PdoDatabase $database |
|
| 446 | + * @param SiteConfiguration $configuration |
|
| 447 | + * |
|
| 448 | + * @return array |
|
| 449 | + * @throws Exception |
|
| 450 | + */ |
|
| 451 | + public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
|
| 452 | + { |
|
| 453 | + $userIds = array(); |
|
| 454 | + |
|
| 455 | + foreach ($logs as $logEntry) { |
|
| 456 | + if (!$logEntry instanceof Log) { |
|
| 457 | + // if this happens, we've done something wrong with passing back the log data. |
|
| 458 | + throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + $user = $logEntry->getUser(); |
|
| 462 | + if ($user === -1) { |
|
| 463 | + continue; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + if (!array_search($user, $userIds)) { |
|
| 467 | + $userIds[] = $user; |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
| 472 | + $users[-1] = User::getCommunity()->getUsername(); |
|
| 473 | + |
|
| 474 | + $logData = array(); |
|
| 475 | + |
|
| 476 | + foreach ($logs as $logEntry) { |
|
| 477 | + $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
| 478 | + $database, $configuration); |
|
| 479 | + |
|
| 480 | + // initialise to sane default |
|
| 481 | + $comment = null; |
|
| 482 | + |
|
| 483 | + switch ($logEntry->getAction()) { |
|
| 484 | + case 'Renamed': |
|
| 485 | + $renameData = unserialize($logEntry->getComment()); |
|
| 486 | + $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
| 487 | + $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
| 488 | + $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
| 489 | + break; |
|
| 490 | + case 'RoleChange': |
|
| 491 | + $roleChangeData = unserialize($logEntry->getComment()); |
|
| 492 | + |
|
| 493 | + $removed = array(); |
|
| 494 | + foreach ($roleChangeData['removed'] as $r) { |
|
| 495 | + $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + $added = array(); |
|
| 499 | + foreach ($roleChangeData['added'] as $r) { |
|
| 500 | + $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
| 504 | + |
|
| 505 | + $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
| 506 | + $comment = $roleDelta . ' with comment: ' . $reason; |
|
| 507 | + break; |
|
| 508 | + case 'JobIssue': |
|
| 509 | + $jobIssueData = unserialize($logEntry->getComment()); |
|
| 510 | + $errorMessage = $jobIssueData['error']; |
|
| 511 | + $status = $jobIssueData['status']; |
|
| 512 | + |
|
| 513 | + $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
|
| 514 | + $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
|
| 515 | + break; |
|
| 516 | + case 'JobIssueRequest': |
|
| 517 | + case 'JobCompletedRequest': |
|
| 518 | + $jobData = unserialize($logEntry->getComment()); |
|
| 519 | + |
|
| 520 | + /** @var JobQueue $job */ |
|
| 521 | + $job = JobQueue::getById($jobData['job'], $database); |
|
| 522 | + $descs = JobQueue::getTaskDescriptions(); |
|
| 523 | + $comment = htmlentities($descs[$job->getTask()], ENT_COMPAT, 'UTF-8'); |
|
| 524 | + break; |
|
| 525 | + |
|
| 526 | + case 'JobCompleted': |
|
| 527 | + break; |
|
| 528 | + default: |
|
| 529 | + $comment = $logEntry->getComment(); |
|
| 530 | + break; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + $logData[] = array( |
|
| 534 | + 'timestamp' => $logEntry->getTimestamp(), |
|
| 535 | + 'userid' => $logEntry->getUser(), |
|
| 536 | + 'username' => $users[$logEntry->getUser()], |
|
| 537 | + 'description' => self::getLogDescription($logEntry), |
|
| 538 | + 'objectdescription' => $objectDescription, |
|
| 539 | + 'comment' => $comment, |
|
| 540 | + ); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + return array($users, $logData); |
|
| 544 | + } |
|
| 545 | 545 | } |
@@ -15,35 +15,35 @@ |
||
| 15 | 15 | |
| 16 | 16 | class MarkdownRenderingHelper |
| 17 | 17 | { |
| 18 | - private $config = [ |
|
| 19 | - 'html_input' => 'escape', |
|
| 20 | - 'allow_unsafe_links' => false, |
|
| 21 | - 'max_nesting_level' => 10 |
|
| 22 | - ]; |
|
| 23 | - |
|
| 24 | - private $blockRenderer; |
|
| 25 | - private $inlineRenderer; |
|
| 26 | - |
|
| 27 | - public function __construct() |
|
| 28 | - { |
|
| 29 | - $blockEnvironment = Environment::createCommonMarkEnvironment(); |
|
| 30 | - $blockEnvironment->addExtension(new AttributesExtension()); |
|
| 31 | - $blockEnvironment->mergeConfig($this->config); |
|
| 32 | - $this->blockRenderer = new MarkdownConverter($blockEnvironment); |
|
| 33 | - |
|
| 34 | - $inlineEnvironment = new Environment(); |
|
| 35 | - $inlineEnvironment->addExtension(new AttributesExtension()); |
|
| 36 | - $inlineEnvironment->addExtension(new InlinesOnlyExtension()); |
|
| 37 | - $inlineEnvironment->mergeConfig($this->config); |
|
| 38 | - $this->inlineRenderer = new MarkdownConverter($inlineEnvironment); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function doRender(string $content): string { |
|
| 42 | - return $this->blockRenderer->convertToHtml($content); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function doRenderInline(string $content): string { |
|
| 46 | - return $this->inlineRenderer->convertToHtml($content); |
|
| 47 | - } |
|
| 18 | + private $config = [ |
|
| 19 | + 'html_input' => 'escape', |
|
| 20 | + 'allow_unsafe_links' => false, |
|
| 21 | + 'max_nesting_level' => 10 |
|
| 22 | + ]; |
|
| 23 | + |
|
| 24 | + private $blockRenderer; |
|
| 25 | + private $inlineRenderer; |
|
| 26 | + |
|
| 27 | + public function __construct() |
|
| 28 | + { |
|
| 29 | + $blockEnvironment = Environment::createCommonMarkEnvironment(); |
|
| 30 | + $blockEnvironment->addExtension(new AttributesExtension()); |
|
| 31 | + $blockEnvironment->mergeConfig($this->config); |
|
| 32 | + $this->blockRenderer = new MarkdownConverter($blockEnvironment); |
|
| 33 | + |
|
| 34 | + $inlineEnvironment = new Environment(); |
|
| 35 | + $inlineEnvironment->addExtension(new AttributesExtension()); |
|
| 36 | + $inlineEnvironment->addExtension(new InlinesOnlyExtension()); |
|
| 37 | + $inlineEnvironment->mergeConfig($this->config); |
|
| 38 | + $this->inlineRenderer = new MarkdownConverter($inlineEnvironment); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function doRender(string $content): string { |
|
| 42 | + return $this->blockRenderer->convertToHtml($content); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function doRenderInline(string $content): string { |
|
| 46 | + return $this->inlineRenderer->convertToHtml($content); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | } |
| 50 | 50 | \ No newline at end of file |
@@ -18,58 +18,58 @@ |
||
| 18 | 18 | |
| 19 | 19 | class PageExpandedRequestList extends InternalPageBase |
| 20 | 20 | { |
| 21 | - use RequestListData; |
|
| 21 | + use RequestListData; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Main function for this page, when no specific actions are called. |
|
| 25 | - * @return void |
|
| 26 | - * @todo This is very similar to the PageMain code, we could probably generalise this somehow |
|
| 27 | - */ |
|
| 28 | - protected function main() |
|
| 29 | - { |
|
| 30 | - if (WebRequest::getString('queue') === null) { |
|
| 31 | - $this->redirect(''); |
|
| 32 | - return; |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * Main function for this page, when no specific actions are called. |
|
| 25 | + * @return void |
|
| 26 | + * @todo This is very similar to the PageMain code, we could probably generalise this somehow |
|
| 27 | + */ |
|
| 28 | + protected function main() |
|
| 29 | + { |
|
| 30 | + if (WebRequest::getString('queue') === null) { |
|
| 31 | + $this->redirect(''); |
|
| 32 | + return; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - $database = $this->getDatabase(); |
|
| 35 | + $database = $this->getDatabase(); |
|
| 36 | 36 | |
| 37 | - // FIXME: domains |
|
| 38 | - $queue = RequestQueue::getByApiName($database, WebRequest::getString('queue'), 1); |
|
| 37 | + // FIXME: domains |
|
| 38 | + $queue = RequestQueue::getByApiName($database, WebRequest::getString('queue'), 1); |
|
| 39 | 39 | |
| 40 | - if ($queue === false) { |
|
| 41 | - $this->redirect(''); |
|
| 42 | - return; |
|
| 43 | - } |
|
| 40 | + if ($queue === false) { |
|
| 41 | + $this->redirect(''); |
|
| 42 | + return; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** @var SiteConfiguration $config */ |
|
| 46 | - $config = $this->getSiteConfiguration(); |
|
| 45 | + /** @var SiteConfiguration $config */ |
|
| 46 | + $config = $this->getSiteConfiguration(); |
|
| 47 | 47 | |
| 48 | - $this->assignCSRFToken(); |
|
| 48 | + $this->assignCSRFToken(); |
|
| 49 | 49 | |
| 50 | - $this->assign('queuehelp', $queue->getHelp()); |
|
| 50 | + $this->assign('queuehelp', $queue->getHelp()); |
|
| 51 | 51 | |
| 52 | - $search = RequestSearchHelper::get($database); |
|
| 53 | - $search->byStatus(RequestStatus::OPEN); |
|
| 52 | + $search = RequestSearchHelper::get($database); |
|
| 53 | + $search->byStatus(RequestStatus::OPEN); |
|
| 54 | 54 | |
| 55 | - list($defaultSort, $defaultSortDirection) = WebRequest::requestListDefaultSort(); |
|
| 56 | - $this->assign('defaultSort', $defaultSort); |
|
| 57 | - $this->assign('defaultSortDirection', $defaultSortDirection); |
|
| 55 | + list($defaultSort, $defaultSortDirection) = WebRequest::requestListDefaultSort(); |
|
| 56 | + $this->assign('defaultSort', $defaultSort); |
|
| 57 | + $this->assign('defaultSortDirection', $defaultSortDirection); |
|
| 58 | 58 | |
| 59 | - if ($config->getEmailConfirmationEnabled()) { |
|
| 60 | - $search->withConfirmedEmail(); |
|
| 61 | - } |
|
| 59 | + if ($config->getEmailConfirmationEnabled()) { |
|
| 60 | + $search->withConfirmedEmail(); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - $queuesById = [$queue->getId() => $queue]; |
|
| 64 | - $requestsByQueue = $search->fetchByQueue(array_keys($queuesById)); |
|
| 65 | - $requestData = $requestsByQueue[$queue->getId()]; |
|
| 63 | + $queuesById = [$queue->getId() => $queue]; |
|
| 64 | + $requestsByQueue = $search->fetchByQueue(array_keys($queuesById)); |
|
| 65 | + $requestData = $requestsByQueue[$queue->getId()]; |
|
| 66 | 66 | |
| 67 | - $this->assign('requests', $this->prepareRequestData($requestData['data'])); |
|
| 68 | - $this->assign('totalRequests', $requestData['count']); |
|
| 69 | - $this->assign('header', $queue->getHeader()); |
|
| 70 | - $this->assign('requestLimitShowOnly', $config->getMiserModeLimit()); |
|
| 67 | + $this->assign('requests', $this->prepareRequestData($requestData['data'])); |
|
| 68 | + $this->assign('totalRequests', $requestData['count']); |
|
| 69 | + $this->assign('header', $queue->getHeader()); |
|
| 70 | + $this->assign('requestLimitShowOnly', $config->getMiserModeLimit()); |
|
| 71 | 71 | |
| 72 | - $this->setHtmlTitle('{$header|escape}{if $totalRequests > 0} [{$totalRequests|escape}]{/if}'); |
|
| 73 | - $this->setTemplate('mainpage/expandedrequestlist.tpl'); |
|
| 74 | - } |
|
| 72 | + $this->setHtmlTitle('{$header|escape}{if $totalRequests > 0} [{$totalRequests|escape}]{/if}'); |
|
| 73 | + $this->setTemplate('mainpage/expandedrequestlist.tpl'); |
|
| 74 | + } |
|
| 75 | 75 | } |