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 setDatetime($time) 145 { 146 $this->datetime = date('Y-m-d H:i', $time); 147 } 148 149 /** 150 * Update publish status in the core table 151 */ 152 protected function updateCoreData($pid, $rid = 0) 153 { 154 $data = [ 155 'status' => $this->status, 156 'user' => $this->user, 157 'date' => $this->datetime, 158 'revision' => $this->rev, 159 'version' => $this->version, 160 ]; 161 $schema = new Schema('structpublish', 0); 162 $access = new AccessTableStructpublish($schema, $pid, 0, $rid); 163 $access->setPublished($this->published); 164 $access->saveData($data); 165 } 166 167 public function getCoreData($andFilter = '') 168 { 169 $lines = [ 170 'schema: structpublish', 171 'cols: *', 172 'filter: %pageid% = $ID$' 173 ]; 174 175 if ($andFilter) { 176 $lines[] = 'filter: ' . $andFilter; 177 } 178 179 $parser = new ConfigParser($lines); 180 $config = $parser->getConfig(); 181 $search = new SearchConfig($config, $this->sqlite); 182 $data = $search->execute(); 183 if (!empty($data)) { 184 // FIXME 185 return $data[array_key_last($data)]; 186 } 187 return []; 188 } 189 190 /** 191 * Return "latest" published revision of a given page. 192 * If $rev is specified, "latest" means relative to the $rev revision. 193 * 194 * @param int|null $rev 195 * @return Revision|null 196 */ 197 public function getLatestPublishedRevision($rev = null) 198 { 199 $andFilter = 'status=' . Constants::STATUS_PUBLISHED; 200 if ($rev) { 201 $andFilter .= ' AND revision < ' . $rev; 202 } 203 $latestPublished = $this->getCoreData($andFilter); 204 205 if (empty($latestPublished)) { 206 return null; 207 } 208 209 $published = new Revision($this->sqlite, $this->id, $latestPublished[$this->revisionCol->getColref() - 1]->getRawValue()); 210 211 $published->setStatus($latestPublished[$this->statusCol->getColref() - 1]->getRawValue()); 212 $published->setUser($latestPublished[$this->userCol->getColref() - 1]->getRawValue()); 213 $published->setDatetime($latestPublished[$this->datetimeCol->getColref() - 1]->getRawValue()); 214 $published->setVersion($latestPublished[$this->versionCol->getColref() - 1]->getRawValue()); 215 216 return $published; 217 } 218} 219