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 protected $id; 22 protected $rev; 23 protected $published; 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 $this->published = 0; 48 49 $this->schema = new Schema('structpublish'); 50 $this->statusCol = $this->schema->findColumn('status'); 51 $this->versionCol = $this->schema->findColumn('version'); 52 $this->userCol = $this->schema->findColumn('user'); 53 $this->dateCol = $this->schema->findColumn('date'); 54 $this->revisionCol = $this->schema->findColumn('revision'); 55 56 /** @var Value[] $values */ 57 $values = $this->getCoreData('revision=' . $this->rev); 58 59 if (!empty($values)) { 60 $this->status = $values[$this->statusCol->getColref() - 1]->getRawValue(); 61 $this->version = $values[$this->versionCol->getColref() - 1]->getRawValue(); 62 $this->user = $values[$this->userCol->getColref() - 1]->getRawValue(); 63 $this->date = $values[$this->dateCol->getColref() - 1]->getRawValue(); 64 } 65 } 66 67 public function save() 68 { 69 // drafts reference the latest version 70 if ($this->status === self::STATUS_DRAFT) { 71 //FIXME no rev yet 72 $this->setVersion($this->getVersion()); 73 } 74 75 if ($this->status === self::STATUS_PUBLISHED) { 76 $this->published = 1; 77 } 78 79 $this->updateCoreData($this->id); 80 // TODO reset publish status of older revisions 81 82 } 83 84 /** 85 * @return int 86 */ 87 public function getVersion() 88 { 89 return (int)$this->version; 90 } 91 92 /** 93 * @param int $version 94 */ 95 public function setVersion($version): void 96 { 97 $this->version = $version; 98 } 99 100 /** 101 * @return int 102 */ 103 public function getRev() 104 { 105 return $this->rev; 106 } 107 108 /** 109 * @param int $rev 110 */ 111 public function setRev($rev): void 112 { 113 $this->rev = $rev; 114 } 115 116 /** 117 * @return string 118 */ 119 public function getStatus() 120 { 121 return $this->status; 122 } 123 124 /** 125 * @param string $status 126 */ 127 public function setStatus($status): void 128 { 129 $this->status = $status; 130 } 131 132 /** 133 * @return string 134 */ 135 public function getUser() 136 { 137 return $this->user; 138 } 139 140 /** 141 * @param string $user 142 */ 143 public function setUser($user): void 144 { 145 $this->user = $user; 146 } 147 148 public function getDate() 149 { 150 return $this->date; 151 } 152 153 public function setDate($time) 154 { 155 $this->date = date('Y-m-d', $time); 156 } 157 158 /** 159 * Update publish status in the core table 160 */ 161 protected function updateCoreData($pid, $rid = 0) 162 { 163 $data = [ 164 'status' => $this->status, 165 'user' => $this->user, 166 'date' => $this->date, 167 'revision' => $this->rev, 168 'version' => $this->version, 169 ]; 170 $schema = new Schema('structpublish', 0); 171 $access = new AccessTableStructpublish($schema, $pid, 0, $rid); 172 $access->setPublished($this->published); 173 $access->saveData($data); 174 } 175 176 public function getCoreData($andFilter = '') 177 { 178 $lines = [ 179 'schema: structpublish', 180 'cols: *', 181 'filter: %pageid% = $ID$' 182 ]; 183 184 if ($andFilter) { 185 $lines[] = 'filter: ' . $andFilter; 186 } 187 188 $parser = new ConfigParser($lines); 189 $config = $parser->getConfig(); 190 $search = new SearchConfig($config, $this->sqlite); 191 $data = $search->execute(); 192 if (!empty($data)) { 193 // FIXME 194 return $data[array_key_last($data)]; 195 } 196 return []; 197 } 198 199 /** 200 * Get a property of the latest published revision associated with the current one 201 * 202 * @param string $key 203 * @return string 204 */ 205 public function getLatestPublished($key) 206 { 207 $latestPublished = $this->getCoreData('status=' . self::STATUS_PUBLISHED); 208 if (!$latestPublished) return ''; 209 210 $data = [ 211 'status' => $latestPublished[$this->statusCol->getColref() - 1]->getRawValue(), 212 'user' => $latestPublished[$this->userCol->getColref() - 1]->getRawValue(), 213 'date' => $latestPublished[$this->dateCol->getColref() - 1]->getRawValue(), 214 'revision' => $latestPublished[$this->revisionCol->getColref() - 1]->getRawValue(), 215 'version' => $latestPublished[$this->versionCol->getColref() - 1]->getRawValue(), 216 ]; 217 218 return $data[$key] ?? ''; 219 } 220} 221