Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Commands; | |
| 4 | ||
| 5 | use Drupal\Core\Database\Connection; | |
| 6 | use Drupal\visitors\VisitorsTrackerInterface; | |
| 7 | use Drupal\visitors\VisitorsUpgradeInterface; | |
| 8 | use Drush\Commands\DrushCommands; | |
| 9 | use Symfony\Component\Console\Helper\ProgressBar; | |
| 10 | ||
| 11 | /** | |
| 12 | * Defines a Drush command. | |
| 13 | */ | |
| 14 | class VisitorsCommands extends DrushCommands { | |
| 15 | ||
| 16 | /** | |
| 17 | * The database connection. | |
| 18 | * | |
| 19 | * @var \Drupal\Core\Database\Connection | |
| 20 | */ | |
| 21 | protected $database; | |
| 22 | ||
| 23 | /** | |
| 24 | * The visitors tracker. | |
| 25 | * | |
| 26 | * @var \Drupal\visitors\VisitorsTrackerInterface | |
| 27 | */ | |
| 28 | protected $tracker; | |
| 29 | ||
| 30 | /** | |
| 31 | * The visitors upgrade. | |
| 32 | * | |
| 33 | * @var \Drupal\visitors\VisitorsUpgradeInterface | |
| 34 | */ | |
| 35 | protected $upgrade; | |
| 36 | ||
| 37 | /** | |
| 38 | * Drush commands for rebuilding logs. | |
| 39 | * | |
| 40 | * @param \Drupal\Core\Database\Connection $database | |
| 41 | * The database connection. | |
| 42 | * @param \Drupal\visitors\VisitorsTrackerInterface $tracker | |
| 43 | * The visitors tracker. | |
| 44 | * @param \Drupal\visitors\VisitorsUpgradeInterface $upgrade | |
| 45 | * The visitors upgrade. | |
| 46 | */ | |
| 47 | public function __construct( | |
| 48 | Connection $database, | |
| 49 | VisitorsTrackerInterface $tracker, | |
| 50 | VisitorsUpgradeInterface $upgrade, | |
| 51 | ) { | |
| 52 | 2 | parent::__construct(); |
| 53 | ||
| 54 | 2 | $this->database = $database; |
| 55 | 2 | $this->tracker = $tracker; |
| 56 | 2 | $this->upgrade = $upgrade; |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Regenerates routes from path. | |
| 61 | * | |
| 62 | * @command visitors:upgrade | |
| 63 | * @aliases visitors-upgrade | |
| 64 | * | |
| 65 | * @usage drush visitors:upgrade | |
| 66 | * Upgrade data from 8.x-2.x to 3.0.x. | |
| 67 | */ | |
| 68 | public function upgrade() { | |
| 69 | ||
| 70 | 1 | $total = $this->getTotal(); |
| 71 | ||
| 72 | // Get the Symfony Console output interface. | |
| 73 | 1 | $output = $this->output(); |
| 74 | ||
| 75 | 1 | $progressBar = new ProgressBar($output, $total); |
| 76 | 1 | $progressBar->setFormat('debug'); |
| 77 | 1 | $progressBar->start(); |
| 78 | ||
| 79 | do { | |
| 80 | 1 | $progressBar->advance(); |
| 81 | 1 | $this->doLog(); |
| 82 | 1 | $total -= 1; |
| 83 | 1 | } while ($total > 0); |
| 84 | ||
| 85 | // Finish the progress bar. | |
| 86 | 1 | $progressBar->finish(); |
| 87 | // Add a new line after the progress bar. | |
| 88 | 1 | $output->writeln(''); |
| 89 | ||
| 90 | // Output a completion message. | |
| 91 | 1 | $output->writeln('Task completed!'); |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Migrate the page views. | |
| 96 | */ | |
| 97 | protected function doLog() { | |
| 98 | 1 | $row = $this->getVisitorsRow(); |
| 99 | 1 | $id = $row['visitors_id']; |
| 100 | 1 | $request_time = $row['visitors_date_time']; |
| 101 | ||
| 102 | 1 | $visit = $this->upgrade->doVisitRecord($row); |
| 103 | 1 | $log = $this->upgrade->doLogRecord($row); |
| 104 | ||
| 105 | 1 | $raw_uid = $log['uid'] ?? 0; |
| 106 | 1 | $anonymous = $raw_uid === 0 || $raw_uid === '0' || $raw_uid === ''; |
| 107 | 1 | $visit['uid'] = ($anonymous || $raw_uid === NULL) ? NULL : (string) $raw_uid; |
| 108 | ||
| 109 | 1 | $visit_id = $this->tracker->getVisitId($visit, $request_time); |
| 110 | 1 | $log['visit_id'] = $visit_id; |
| 111 | 1 | $event_id = $this->tracker->writeEvent($log); |
| 112 | ||
| 113 | 1 | $this->tracker->updateVisit($visit_id, $event_id, $request_time, $visit['uid']); |
| 114 | ||
| 115 | 1 | $this->upgrade->deleteVisitorsRow($id); |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 119 | * Get the next visitors row. | |
| 120 | */ | |
| 121 | protected function getVisitorsRow() { | |
| 122 | 1 | static $ids = []; |
| 123 | ||
| 124 | 1 | if (empty($ids)) { |
| 125 | 1 | $ids = $this->upgrade->getVisitorsRows(10000); |
| 126 | } | |
| 127 | ||
| 128 | 1 | return (array) array_shift($ids); |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * The total records to convert. | |
| 133 | */ | |
| 134 | protected function getTotal() { | |
| 135 | 1 | $select = $this->database->select('visitors', 'v'); |
| 136 | 1 | $select->fields('v') |
| 137 | 1 | ->isNotNull('v.visitor_id') |
| 138 | 1 | ->countQuery(); |
| 139 | ||
| 140 | 1 | $count = $select->execute()->fetchField(); |
| 141 | ||
| 142 | 1 | return (int) $count; |
| 143 | } | |
| 144 | ||
| 145 | } |