ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilWikiUserHTMLExport.php
Go to the documentation of this file.
1<?php
2
21
28{
29 public const PROCESS_OTHER_USER = 0; // another user has started a running export
30 public const PROCESS_STARTED = 1; // export has been started by current user
31 public const PROCESS_UPTODATE = 2; // no export necessary, current export is up-to-date
32
33 public const NOT_RUNNING = 0;
34 public const RUNNING = 1;
35 protected \ILIAS\components\Export\HTML\ExportFileManager $html_export_file_manager;
36
37 protected ?array $data = null;
38 protected ilDBInterface $db;
39 protected \ilObjWiki $wiki;
40 protected ilObjUser $user;
41 protected ilLogger $log;
42 protected bool $with_comments = false;
43
44 public function __construct(
45 ilObjWiki $a_wiki,
46 ilDBInterface $a_db,
47 ilObjUser $a_user,
48 bool $with_comments = false
49 ) {
50 global $DIC;
51
52 $this->db = $a_db;
53 $this->wiki = $a_wiki;
54 $this->user = $a_user;
55 $this->read();
56 $this->log = ilLoggerFactory::getLogger('wiki');
57 $this->with_comments = $with_comments;
58 $this->log->debug("comments: " . $this->with_comments);
59 $this->html_export_file_manager = $DIC->export()->domain()->html()->fileManager();
60 }
61
62 protected function read(): void
63 {
64 $set = $this->db->query(
65 "SELECT * FROM wiki_user_html_export " .
66 " WHERE wiki_id = " . $this->db->quote($this->wiki->getId(), "integer") .
67 " AND with_comments = " . $this->db->quote($this->with_comments, "integer")
68 );
69 if (!$this->data = $this->db->fetchAssoc($set)) {
70 $this->data = array();
71 }
72 }
73
74 protected function getProcess(): int
75 {
76 $this->log->debug("getProcess");
77 $last_change = ilPageObject::getLastChangeByParent("wpg", $this->wiki->getId());
78 $latest = $this->getLatest();
79
80 $ilAtomQuery = $this->db->buildAtomQuery();
81 $ilAtomQuery->addTableLock('wiki_user_html_export');
82
83 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) use ($last_change, &$ret, $latest) {
84 $this->log->debug("atom query start");
85
86 $this->read();
87 $ts = ilUtil::now();
88
89 if (($this->data["start_ts"] ?? "") != "" &&
90 $this->data["start_ts"] > $last_change) {
91 if ($latest) {
93 $this->log->debug("return: " . self::PROCESS_UPTODATE);
94 return;
95 }
96 }
97
98 if (!isset($this->data["wiki_id"])) {
99 $this->log->debug("insert, wiki id: " . $this->wiki->getId() . ", user id: " . $this->user->getId() .
100 ", ts: " . $ts . ", with_comments: " . $this->with_comments);
101 $ilDB->manipulate("INSERT INTO wiki_user_html_export " .
102 "(wiki_id, usr_id, progress, start_ts, status, with_comments) VALUES (" .
103 $ilDB->quote($this->wiki->getId(), "integer") . "," .
104 $ilDB->quote($this->user->getId(), "integer") . "," .
105 $ilDB->quote(0, "integer") . "," .
106 $ilDB->quote($ts, "timestamp") . "," .
107 $ilDB->quote(self::RUNNING, "integer") . "," .
108 $ilDB->quote($this->with_comments, "integer") .
109 ")");
110 } else {
111 $this->log->debug("update, wiki id: " . $this->wiki->getId() . ", user id: " . $this->user->getId() .
112 ", ts: " . $ts . ", with_comments: " . $this->with_comments);
113 $ilDB->manipulate(
114 "UPDATE wiki_user_html_export SET " .
115 " start_ts = " . $ilDB->quote($ts, "timestamp") . "," .
116 " usr_id = " . $ilDB->quote($this->user->getId(), "integer") . "," .
117 " progress = " . $ilDB->quote(0, "integer") . "," .
118 " status = " . $ilDB->quote(self::RUNNING, "integer") .
119 " WHERE status = " . $ilDB->quote(self::NOT_RUNNING, "integer") .
120 " AND wiki_id = " . $ilDB->quote($this->wiki->getId(), "integer") .
121 " AND with_comments = " . $ilDB->quote($this->with_comments, "integer")
122 );
123 $this->read();
124 }
125
126 if (($this->data["start_ts"] ?? "") == $ts && $this->data["usr_id"] == $this->user->getId()) {
127 // we started the process
129 $this->log->debug("return: " . self::PROCESS_STARTED);
130 return;
131 }
132
133 // process was already running
135 $this->log->debug("return: " . self::PROCESS_OTHER_USER);
136 });
137
138 $ilAtomQuery->run();
139
140 $this->log->debug("outer return: " . $ret);
141
142 return $ret;
143 }
144
145 public function updateStatus(
146 int $a_progress,
147 int $a_status
148 ): void {
149 $this->db->manipulate(
150 "UPDATE wiki_user_html_export SET " .
151 " progress = " . $this->db->quote($a_progress, "integer") . "," .
152 " status = " . $this->db->quote($a_status, "integer") .
153 " WHERE wiki_id = " . $this->db->quote($this->wiki->getId(), "integer") .
154 " AND usr_id = " . $this->db->quote($this->user->getId(), "integer") .
155 " AND with_comments = " . $this->db->quote($this->with_comments, "integer")
156 );
157
158 $this->read();
159 }
160
161 public function getProgress(): array
162 {
163 $set = $this->db->query(
164 "SELECT progress, status FROM wiki_user_html_export " .
165 " WHERE wiki_id = " . $this->db->quote($this->wiki->getId(), "integer") .
166 " AND with_comments = " . $this->db->quote($this->with_comments, "integer")
167 );
168 $rec = $this->db->fetchAssoc($set);
169
170 return array("progress" => (int) $rec["progress"], "status" => (int) $rec["status"]);
171 }
172
173 public function initUserHTMLExport(): void
174 {
175 // get process, if not already running or export is up-to-date, return corresponding status
176 echo $this->getProcess();
177 exit;
178 }
179
180 public function startUserHTMLExport(): void
181 {
182 ignore_user_abort(true);
183 // do the export
184 $exp = new WikiHtmlExport($this->wiki);
185 if (!$this->with_comments) {
186 $exp->setMode(WikiHtmlExport::MODE_USER);
187 } else {
188 $exp->setMode(WikiHtmlExport::MODE_USER_COMMENTS);
189 }
190 $exp = $exp->buildExportFile();
191 // reset user export status
192 $this->updateStatus(100, self::NOT_RUNNING);
193 exit;
194 }
195
196 protected function getLatest(): ?ExportFile
197 {
198 $exp = new WikiHtmlExport($this->wiki);
199 if ($this->with_comments) {
200 $exp->setMode(WikiHtmlExport::MODE_USER_COMMENTS);
201 } else {
202 $exp->setMode(WikiHtmlExport::MODE_USER);
203 }
204 return $exp->getLatest();
205 }
206
207 public function deliverFile(): void
208 {
209 $this->log->debug("deliver");
210
211 $exp = new WikiHtmlExport($this->wiki);
212 if ($this->with_comments) {
213 $exp->setMode(WikiHtmlExport::MODE_USER_COMMENTS);
214 } else {
215 $exp->setMode(WikiHtmlExport::MODE_USER);
216 }
217 $exp->deliverLatest();
218 }
219}
Wiki HTML exporter class.
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getLastChangeByParent(string $a_parent_type, int $a_parent_id, string $a_lang="")
Get all pages for parent object.
static now()
Return current timestamp in Y-m-d H:i:s format.
Class manages user html export.
updateStatus(int $a_progress, int $a_status)
__construct(ilObjWiki $a_wiki, ilDBInterface $a_db, ilObjUser $a_user, bool $with_comments=false)
ILIAS components Export HTML ExportFileManager $html_export_file_manager
exit
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26