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