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