1<?php 2 3namespace dokuwiki\plugin\structpublish\meta; 4 5use dokuwiki\plugin\struct\meta\ConfigParser; 6use dokuwiki\plugin\struct\meta\Schema; 7use dokuwiki\plugin\struct\meta\SearchConfig; 8use dokuwiki\plugin\struct\meta\Value; 9 10class Revision 11{ 12 const STATUS_DRAFT = 'draft'; 13 const STATUS_APPROVED = 'approved'; 14 const STATUS_PUBLISHED = 'published'; 15 16 /** @var \helper_plugin_sqlite */ 17 protected $sqlite; 18 19 protected $schema; 20 21 22 protected $id; 23 protected $rev; 24 protected $status; 25 protected $version; 26 protected $user; 27 protected $date; 28 /** 29 * @var bool|\dokuwiki\plugin\struct\meta\Column 30 */ 31 protected $statusCol; 32 protected $versionCol; 33 protected $userCol; 34 protected $dateCol; 35 protected $revisionCol; 36 37 /** 38 * @param $sqlite 39 * @param string $id 40 * @param int $rev 41 */ 42 public function __construct($sqlite, $id, $rev) 43 { 44 $this->sqlite = $sqlite; 45 $this->id = $id; 46 $this->rev = $rev; 47 48 $this->schema = new Schema('structpublish'); 49 $this->statusCol = $this->schema->findColumn('status'); 50 $this->versionCol = $this->schema->findColumn('version'); 51 $this->userCol = $this->schema->findColumn('user'); 52 $this->dateCol = $this->schema->findColumn('date'); 53 $this->revisionCol = $this->schema->findColumn('revision'); 54 55 /** @var Value[] $values */ 56 $values = $this->getCoreData(); 57 58 if (!empty($values)) { 59 $this->status = $values[$this->statusCol->getColref() - 1]->getRawValue(); 60 $this->version = $values[$this->versionCol->getColref() - 1]->getRawValue(); 61 $this->user = $values[$this->userCol->getColref() - 1]->getRawValue(); 62 $this->date = $values[$this->dateCol->getColref() - 1]->getRawValue(); 63 } 64 } 65 66 public function save() 67 { 68 // drafts reference the latest version 69 if ($this->status === self::STATUS_DRAFT) { 70 //FIXME no rev yet 71 $this->setVersion($this->getVersion()); 72 } 73 74 // TODO reset publish status of older revisions 75 76 $this->updateCoreData($this->id); 77 } 78 79 /** 80 * @return int 81 */ 82 public function getVersion() 83 { 84 return (int)$this->version; 85 } 86 87 /** 88 * @param int $version 89 */ 90 public function setVersion($version): void 91 { 92 $this->version = $version; 93 } 94 95 /** 96 * @return int 97 */ 98 public function getRev() 99 { 100 return $this->rev; 101 } 102 103 /** 104 * @param int $rev 105 */ 106 public function setRev($rev): void 107 { 108 $this->rev = $rev; 109 } 110 111 /** 112 * @return string 113 */ 114 public function getStatus() 115 { 116 return $this->status; 117 } 118 119 /** 120 * @param string $status 121 */ 122 public function setStatus($status): void 123 { 124 $this->status = $status; 125 } 126 127 /** 128 * @return string 129 */ 130 public function getUser() 131 { 132 return $this->user; 133 } 134 135 /** 136 * @param string $user 137 */ 138 public function setUser($user): void 139 { 140 $this->user = $user; 141 } 142 143 public function getDate() 144 { 145 return $this->date; 146 } 147 148 public function setDate($time) 149 { 150 $this->date = date('Y-m-d', $time); 151 } 152 153 /** 154 * Update publish status in the core table 155 */ 156 protected function updateCoreData($pid, $rid = 0) 157 { 158 $data = [ 159 'status' => $this->status, 160 'user' => $this->user, 161 'date' => $this->date, 162 'revision' => $this->rev, 163 'version' => $this->version, 164 ]; 165 $schema = new Schema('structpublish', 0); 166 $access = new AccessTableStructpublish($schema, $pid, 0, $rid); 167 $access->saveData($data); 168 } 169 170 public function getCoreData($andFilter = '') 171 { 172 $lines = [ 173 'schema: structpublish', 174 'cols: *', 175 'filter: %pageid% = $ID$' 176 ]; 177 178 if ($andFilter) { 179 $lines[] = 'filter: ' . $andFilter; 180 } 181 182 $parser = new ConfigParser($lines); 183 $config = $parser->getConfig(); 184 $search = new SearchConfig($config); 185 $data = $search->execute(); 186 if (!empty($data)) { 187 return $data[array_key_last($data)]; 188 } 189 return []; 190 } 191 192 public function getLatestPublished() 193 { 194 return $this->getCoreData('status=' . self::STATUS_PUBLISHED); 195 } 196 197 public function getLatestPublishedRev() 198 { 199 return $this->getLatestPublished()[$this->revisionCol->getColref() - 1]->getRawValue(); 200 } 201} 202