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