Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Service; | |
| 6 | ||
| 7 | use Drupal\Core\Database\Connection; | |
| 8 | use Drupal\visitors\Helper\VisitorsUrl; | |
| 9 | use Drupal\visitors\VisitorsReferrerInterface; | |
| 10 | use Drupal\visitors\VisitorsTrackerInterface; | |
| 11 | use Drupal\visitors\VisitorsUpgradeInterface; | |
| 12 | ||
| 13 | /** | |
| 14 | * A class for upgrading data. | |
| 15 | */ | |
| 16 | class UpgradeService implements VisitorsUpgradeInterface { | |
| 17 | ||
| 18 | /** | |
| 19 | * The database connection. | |
| 20 | * | |
| 21 | * @var \Drupal\Core\Database\Connection | |
| 22 | */ | |
| 23 | protected $database; | |
| 24 | ||
| 25 | /** | |
| 26 | * The visitors tracker. | |
| 27 | * | |
| 28 | * @var \Drupal\visitors\VisitorsTrackerInterface | |
| 29 | */ | |
| 30 | protected $tracker; | |
| 31 | ||
| 32 | /** | |
| 33 | * The referrer parser. | |
| 34 | * | |
| 35 | * @var \Drupal\visitors\VisitorsReferrerInterface | |
| 36 | */ | |
| 37 | protected $referrer; | |
| 38 | ||
| 39 | /** | |
| 40 | * The constructor. | |
| 41 | * | |
| 42 | * @param \Drupal\Core\Database\Connection $database | |
| 43 | * The database connection. | |
| 44 | * @param \Drupal\visitors\VisitorsTrackerInterface $tracker | |
| 45 | * The visitors tracker. | |
| 46 | * @param \Drupal\visitors\VisitorsReferrerInterface $referrer | |
| 47 | * The referrer parser. | |
| 48 | */ | |
| 49 | public function __construct(Connection $database, VisitorsTrackerInterface $tracker, VisitorsReferrerInterface $referrer) { | |
| 50 | 4 | $this->database = $database; |
| 51 | 4 | $this->tracker = $tracker; |
| 52 | 4 | $this->referrer = $referrer; |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * {@inheritdoc} | |
| 57 | */ | |
| 58 | public function doLogRecord(array $row): array { | |
| 59 | 1 | $log = []; |
| 60 | 1 | $request_time = (int) $row['visitors_date_time']; |
| 61 | ||
| 62 | 1 | $url = $row['visitors_url']; |
| 63 | 1 | $visitors_url = new VisitorsUrl($url); |
| 64 | ||
| 65 | 1 | $log['url'] = $visitors_url->getUrl(); |
| 66 | 1 | $log['url_prefix'] = $visitors_url->getPrefix(); |
| 67 | ||
| 68 | 1 | $page_host = $this->referrer->hostOf((string) ($row['visitors_url'] ?? '')); |
| 69 | 1 | $referrer = $this->referrer->storageFields((string) ($row['visitors_referer'] ?? ''), $page_host); |
| 70 | ||
| 71 | 1 | $log['uid'] = $row['visitors_uid'] ?? 0; |
| 72 | 1 | $log['title'] = $row['visitors_title']; |
| 73 | 1 | $log['referrer_url'] = $referrer['referrer_url']; |
| 74 | 1 | $log['referrer_url_prefix'] = $referrer['referrer_url_prefix']; |
| 75 | 1 | $log['referrer_internal'] = $referrer['referrer_internal']; |
| 76 | 1 | $log['referrer_path'] = $referrer['referrer_path']; |
| 77 | 1 | $log['referrer_route'] = $referrer['referrer_route']; |
| 78 | 1 | $log['path'] = $row['visitors_path']; |
| 79 | 1 | $log['route'] = $row['route']; |
| 80 | 1 | $log['server'] = $row['server']; |
| 81 | 1 | $log['pf_network'] = $row['pf_network']; |
| 82 | 1 | $log['pf_server'] = $row['pf_server']; |
| 83 | 1 | $log['pf_transfer'] = $row['pf_transfer']; |
| 84 | 1 | $log['pf_dom_processing'] = $row['pf_dom_processing']; |
| 85 | 1 | $log['pf_dom_complete'] = $row['pf_dom_complete']; |
| 86 | 1 | $log['pf_on_load'] = $row['pf_on_load']; |
| 87 | 1 | $log['pf_total'] = $row['pf_total']; |
| 88 | 1 | $log['created'] = $request_time; |
| 89 | ||
| 90 | 1 | return $log; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * {@inheritdoc} | |
| 95 | */ | |
| 96 | public function doVisitRecord(array $row): array { | |
| 97 | 1 | $visit = []; |
| 98 | 1 | $request_time = (int) $row['visitors_date_time']; |
| 99 | ||
| 100 | 1 | $visit['location_browser_lang'] = $row['language']; |
| 101 | ||
| 102 | 1 | $visit['localtime'] = $row['visitor_localtime']; |
| 103 | 1 | $visit['config_browser_engine'] = $row['config_browser_engine']; |
| 104 | 1 | $visit['config_browser_name'] = $row['config_browser_name']; |
| 105 | 1 | $visit['config_browser_version'] = $row['config_browser_version']; |
| 106 | 1 | $visit['config_client_type'] = $row['config_client_type']; |
| 107 | 1 | $visit['config_device_brand'] = $row['config_device_brand']; |
| 108 | 1 | $visit['config_device_model'] = $row['config_device_model']; |
| 109 | 1 | $visit['config_device_type'] = $row['config_device_type']; |
| 110 | 1 | $visit['config_os'] = $row['config_os']; |
| 111 | 1 | $visit['config_os_version'] = $row['config_os_version']; |
| 112 | 1 | $visit['bot'] = $row['bot']; |
| 113 | 1 | $visit['config_resolution'] = $row['config_resolution']; |
| 114 | 1 | $visit['config_pdf'] = $row['config_pdf']; |
| 115 | 1 | $visit['config_flash'] = $row['config_flash']; |
| 116 | 1 | $visit['config_java'] = $row['config_java']; |
| 117 | 1 | $visit['config_quicktime'] = $row['config_quicktime']; |
| 118 | 1 | $visit['config_realplayer'] = $row['config_realplayer']; |
| 119 | 1 | $visit['config_windowsmedia'] = $row['config_windowsmedia']; |
| 120 | 1 | $visit['config_silverlight'] = $row['config_silverlight']; |
| 121 | 1 | $visit['config_cookie'] = $row['config_cookie']; |
| 122 | 1 | $visit['location_continent'] = $row['location_continent']; |
| 123 | 1 | $visit['location_country'] = $row['location_country']; |
| 124 | 1 | $visit['location_region'] = $row['location_region'] ?? NULL; |
| 125 | 1 | $visit['location_city'] = $row['location_city'] ?? NULL; |
| 126 | 1 | $visit['location_latitude'] = $row['location_latitude'] ?? NULL; |
| 127 | 1 | $visit['location_longitude'] = $row['location_longitude'] ?? NULL; |
| 128 | 1 | $visit['visitor_id'] = $row['visitor_id']; |
| 129 | 1 | $visit['total_time'] = 0; |
| 130 | 1 | $visit['total_page_views'] = 0; |
| 131 | 1 | $visit['total_events'] = 0; |
| 132 | 1 | $visit['returning'] = 0; |
| 133 | 1 | $visit['total_visits'] = 1; |
| 134 | 1 | $visit['entry_time'] = $request_time; |
| 135 | 1 | $visit['exit_time'] = $request_time; |
| 136 | 1 | $visit['location_ip'] = $row['visitors_ip']; |
| 137 | ||
| 138 | 1 | return $visit; |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * {@inheritdoc} | |
| 143 | */ | |
| 144 | public function getVisitorsRows(int $batch = 50): array { | |
| 145 | 1 | $select = $this->database->select('visitors', 'v'); |
| 146 | 1 | $rows = $select->fields('v') |
| 147 | 1 | ->orderBy('v.visitors_date_time', 'ASC') |
| 148 | 1 | ->range(0, $batch) |
| 149 | 1 | ->execute()->fetchAll(); |
| 150 | ||
| 151 | 1 | return $rows; |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * {@inheritdoc} | |
| 156 | */ | |
| 157 | public function deleteVisitorsRow($id) { | |
| 158 | 1 | $this->database |
| 159 | 1 | ->delete('visitors') |
| 160 | 1 | ->condition('visitors_id', $id) |
| 161 | 1 | ->execute(); |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * {@inheritdoc} | |
| 166 | */ | |
| 167 | public function getTotal(): int { | |
| 168 | 1 | $select = $this->database->select('visitors', 'v'); |
| 169 | 1 | $select->fields('v') |
| 170 | 1 | ->countQuery(); |
| 171 | ||
| 172 | 1 | $count = $select->execute()->fetchField(); |
| 173 | ||
| 174 | 1 | return (int) $count; |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * {@inheritdoc} | |
| 179 | */ | |
| 180 | public function upgradeRow(array $row) { | |
| 181 | 1 | $id = $row['visitors_id']; |
| 182 | 1 | $request_time = (int) $row['visitors_date_time']; |
| 183 | ||
| 184 | 1 | $visit = $this->doVisitRecord($row); |
| 185 | 1 | $event = $this->doLogRecord($row); |
| 186 | 1 | $uid = isset($event['uid']) ? (string) $event['uid'] : '0'; |
| 187 | 1 | $visit['uid'] = ($uid === '' || $uid === '0') ? NULL : $uid; |
| 188 | ||
| 189 | 1 | $visit_id = $this->tracker->getVisitId($visit, $request_time); |
| 190 | 1 | $event['visit_id'] = $visit_id; |
| 191 | 1 | $event_id = $this->tracker->writeEvent($event); |
| 192 | ||
| 193 | 1 | $this->tracker->updateVisit($visit_id, $event_id, $request_time, $visit['uid']); |
| 194 | ||
| 195 | 1 | $this->deleteVisitorsRow($id); |
| 196 | } | |
| 197 | ||
| 198 | } |