| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sunnysideup\VersionPruner\PruningTemplates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Sunnysideup\VersionPruner\PruningTemplatesTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class BasedOnTimeScale extends PruningTemplatesTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     protected $timeScale = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         'Minutes' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |             'Max' => 60, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |             'Interval' => 15, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         'Hours' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             'Max' => 24, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             'Interval' => 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         'Days' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'Min' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             'Max' => 7, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'Interval' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'Weeks' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'Min' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'Max' => 4, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'Interval' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'Months' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'Min' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'Max' => 12, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             'Interval' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'Years' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'Min' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'Max' => 7, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'Interval' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $otherFilters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function setOtherFilters(array $otherFilters): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->otherFilters = $otherFilters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public function setTimeScale(array $timeScale): self | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->timeScale = $timeScale; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function getTitle(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return 'Prune versions based on time ago'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function getDescription(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return 'Ones close to now are kept at a more regular interval (e.g. one per hour) and olders ones at a less regular interval (e.g. one per month).'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function run(?bool $verbose = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $toKeep = $this->buildTimeScalePatternAndOnesToKeep(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $query = $this->getBaseQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ->addWhere( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     '"RecordID" = ?' => $this->object->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     '"Version" NOT IN (' . implode(',', ($toKeep + [-1 => 0])) . ')', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 ] + | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $this->otherFilters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->toDelete[$this->getUniqueKey()] += $this->addVersionNumberToArray( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $this->toDelete[$this->getUniqueKey()], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $query->execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     protected function buildTimeScalePatternAndOnesToKeep(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $keep = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         foreach ($this->timeScale as $name => $options) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $min = $options['Min'] ?? 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $max = $options['Max'] ?? 7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $interval = (int) $options['Interval'] ?? 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             for ($i = $min; $i < $max; $i += $interval) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 $untilTs = strtotime('-' . $i . ' ' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $fromTs = strtotime('-' . ($i + $interval) . ' ' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $where = | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     '( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                         "LastEdited" > TIMESTAMP(\'' . date('Y-m-d h:i:s', $fromTs) . '\') AND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                         "LastEdited" < TIMESTAMP(\'' . date('Y-m-d h:i:s', $untilTs) . '\') AND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                         "WasPublished" = 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     )'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $query = $this->getBaseQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     ->addWhere($this->normaliseWhere([$where] + $this->otherFilters)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     ->setLimit(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $keep = $this->addVersionNumberToArray( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     $keep, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     $query->execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $keep; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  |