1<?php 2 3use dokuwiki\plugin\structpublish\meta\Constants; 4use dokuwiki\plugin\structpublish\meta\Revision; 5 6class action_plugin_structpublish_publish extends DokuWiki_Action_Plugin 7{ 8 /** @var \helper_plugin_structpublish_db */ 9 protected $dbHelper; 10 11 /** @inheritDoc */ 12 public function register(Doku_Event_Handler $controller) 13 { 14 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleApprove'); 15 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handlePublish'); 16 } 17 18 /** 19 * Handle the publish button and version field 20 * 21 * @param Doku_Event $event 22 * @return void 23 */ 24 public function handlePublish(Doku_Event $event) 25 { 26 if ($event->data != 'show') { 27 return; 28 } 29 30 $this->dbHelper = plugin_load('helper', 'structpublish_db'); 31 32 global $INPUT; 33 $in = $INPUT->arr('structpublish'); 34 if (!$in || !isset($in[Constants::ACTION_PUBLISH])) { 35 return; 36 } 37 38 if (checkSecurityToken()) { 39 $this->saveRevision(Constants::STATUS_PUBLISHED, $INPUT->str('version')); 40 $this->updateSchemaData(); 41 } 42 } 43 44 /** 45 * Handle the approve button 46 * 47 * @param Doku_Event $event 48 * @return void 49 */ 50 public function handleApprove(Doku_Event $event) 51 { 52 if ($event->data != 'show') { 53 return; 54 } 55 56 $this->dbHelper = plugin_load('helper', 'structpublish_db'); 57 58 global $INPUT; 59 $in = $INPUT->arr('structpublish'); 60 if (!$in || !isset($in[Constants::ACTION_APPROVE])) { 61 return; 62 } 63 64 if (checkSecurityToken()) { 65 $this->saveRevision(Constants::STATUS_APPROVED); 66 } 67 } 68 69 /** 70 * Save publish data 71 * 72 * @param string $status 73 * @return void 74 */ 75 protected function saveRevision($status, $newversion = '') 76 { 77 global $ID; 78 global $INFO; 79 80 if ( 81 $status === Constants::STATUS_PUBLISHED && 82 !$this->dbHelper->checkAccess($ID, [Constants::ACTION_PUBLISH]) 83 ) { 84 throw new \Exception('User may not publish'); 85 } 86 87 if ( 88 $status === Constants::STATUS_APPROVED && 89 !$this->dbHelper->checkAccess($ID, [Constants::ACTION_APPROVE]) 90 ) { 91 throw new \Exception('User may not approve'); 92 } 93 94 // FIXME prevent bumping an already published revision 95 $sqlite = $this->dbHelper->getDB(); 96 $revision = new Revision($sqlite, $ID, $INFO['currentrev']); 97 98 if ($status === Constants::STATUS_PUBLISHED) { 99 $revision->setVersion($newversion); 100 } 101 $revision->setUser($_SERVER['REMOTE_USER']); 102 $revision->setStatus($status); 103 $revision->setTimestamp(time()); 104 $revision->save(); 105 } 106 107 /** 108 * Set "published" status in all assigned schemas 109 * 110 * @return void 111 */ 112 protected function updateSchemaData() 113 { 114 global $ID; 115 global $INFO; 116 117 $schemaAssignments = \dokuwiki\plugin\struct\meta\Assignments::getInstance(); 118 $tables = $schemaAssignments->getPageAssignments($ID); 119 120 if (empty($tables)) { 121 return; 122 } 123 124 $sqlite = $this->dbHelper->getDB(); 125 126 foreach ($tables as $table) { 127 // TODO unpublish earlier revisions 128 $sqlite->query("UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?", 129 [$ID, $INFO['currentrev']]); 130 $sqlite->query("UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?", 131 [$ID, $INFO['currentrev']]); 132 } 133 } 134} 135