ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilWikiUserHTMLExport.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5use \ILIAS\Wiki\Export\WikiHtmlExport;
6
13{
14 const PROCESS_OTHER_USER = 0; // another user has started a running export
15 const PROCESS_STARTED = 1; // export has been started by current user
16 const PROCESS_UPTODATE = 2; // no export necessary, current export is up-to-date
17
18
19 const NOT_RUNNING = 0;
20 const RUNNING = 1;
21
22 protected $data;
23
27 protected $db;
28
32 protected $wiki;
33
37 protected $user;
38
42 protected $log;
43
47 protected $with_comments = false;
48
55 public function __construct(ilObjWiki $a_wiki, ilDBInterface $a_db, ilObjUser $a_user, $with_comments = false)
56 {
57 $this->db = $a_db;
58 $this->wiki = $a_wiki;
59 $this->user = $a_user;
60 $this->read();
61 $this->log = ilLoggerFactory::getLogger('wiki');
62 $this->with_comments = $with_comments;
63 $this->log->debug("comments: " . $this->with_comments);
64 }
65
72 protected function read()
73 {
74 $set = $this->db->query(
75 "SELECT * FROM wiki_user_html_export " .
76 " WHERE wiki_id = " . $this->db->quote($this->wiki->getId(), "integer") .
77 " AND with_comments = " . $this->db->quote($this->with_comments, "integer")
78 );
79 if (!$this->data = $this->db->fetchAssoc($set)) {
80 $this->data = array();
81 }
82 }
83
90 protected function getProcess()
91 {
92 $this->log->debug("getProcess");
93 $last_change = ilPageObject::getLastChangeByParent("wpg", $this->wiki->getId());
94 $file_exists = $this->doesFileExist();
95
96 $ilAtomQuery = $this->db->buildAtomQuery();
97 $ilAtomQuery->addTableLock('wiki_user_html_export');
98
99 $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) use ($last_change, &$ret, $file_exists) {
100 $this->log->debug("atom query start");
101
102 $this->read();
103 $ts = ilUtil::now();
104
105 if ($this->data["start_ts"] != "" &&
106 $this->data["start_ts"] > $last_change) {
107 if ($file_exists) {
109 $this->log->debug("return: " . self::PROCESS_UPTODATE);
110 return;
111 }
112 }
113
114 if (!isset($this->data["wiki_id"])) {
115 $this->log->debug("insert, wiki id: " . $this->wiki->getId() . ", user id: " . $this->user->getId() .
116 ", ts: " . $ts . ", with_comments: " . $this->with_comments);
117 $ilDB->manipulate("INSERT INTO wiki_user_html_export " .
118 "(wiki_id, usr_id, progress, start_ts, status, with_comments) VALUES (" .
119 $ilDB->quote($this->wiki->getId(), "integer") . "," .
120 $ilDB->quote($this->user->getId(), "integer") . "," .
121 $ilDB->quote(0, "integer") . "," .
122 $ilDB->quote($ts, "timestamp") . "," .
123 $ilDB->quote(self::RUNNING, "integer") . "," .
124 $ilDB->quote($this->with_comments, "integer") .
125 ")");
126 } else {
127 $this->log->debug("update, wiki id: " . $this->wiki->getId() . ", user id: " . $this->user->getId() .
128 ", ts: " . $ts . ", with_comments: " . $this->with_comments);
129 $ilDB->manipulate(
130 "UPDATE wiki_user_html_export SET " .
131 " start_ts = " . $ilDB->quote($ts, "timestamp") . "," .
132 " usr_id = " . $ilDB->quote($this->user->getId(), "integer") . "," .
133 " progress = " . $ilDB->quote(0, "integer") . "," .
134 " status = " . $ilDB->quote(self::RUNNING, "integer") .
135 " WHERE status = " . $ilDB->quote(self::NOT_RUNNING, "integer") .
136 " AND wiki_id = " . $ilDB->quote($this->wiki->getId(), "integer") .
137 " AND with_comments = " . $ilDB->quote($this->with_comments, "integer")
138 );
139 $this->read();
140 }
141
142 if ($this->data["start_ts"] == $ts && $this->data["usr_id"] == $this->user->getId()) {
143 // we started the process
145 $this->log->debug("return: " . self::PROCESS_STARTED);
146 return;
147 }
148
149 // process was already running
151 $this->log->debug("return: " . self::PROCESS_OTHER_USER);
152 });
153
154 $ilAtomQuery->run();
155
156 $this->log->debug("outer return: " . $ret);
157
158 return $ret;
159 }
160
167 public function updateStatus($a_progress, $a_status)
168 {
169 $this->db->manipulate(
170 "UPDATE wiki_user_html_export SET " .
171 " progress = " . $this->db->quote((int) $a_progress, "integer") . "," .
172 " status = " . $this->db->quote((int) $a_status, "integer") .
173 " WHERE wiki_id = " . $this->db->quote($this->wiki->getId(), "integer") .
174 " AND usr_id = " . $this->db->quote($this->user->getId(), "integer") .
175 " AND with_comments = " . $this->db->quote($this->with_comments, "integer")
176 );
177
178 $this->read();
179 }
180
187 public function getProgress()
188 {
189 $set = $this->db->query(
190 "SELECT progress, status FROM wiki_user_html_export " .
191 " WHERE wiki_id = " . $this->db->quote($this->wiki->getId(), "integer") .
192 " AND with_comments = " . $this->db->quote($this->with_comments, "integer")
193 );
194 $rec = $this->db->fetchAssoc($set);
195
196 return array("progress" => (int) $rec["progress"], "status" => (int) $rec["status"]);
197 }
198
199
206 public function initUserHTMLExport()
207 {
208 // get process, if not already running or export is up-to-date, return corresponding status
209 echo $this->getProcess();
210 exit;
211 }
212
216 public function startUserHTMLExport()
217 {
218 ignore_user_abort(true);
219 // do the export
220 $exp = new WikiHtmlExport($this->wiki);
221 if (!$this->with_comments) {
222 $exp->setMode(WikiHtmlExport::MODE_USER);
223 } else {
224 $exp->setMode(WikiHtmlExport::MODE_USER_COMMENTS);
225 }
226 $exp->buildExportFile();
227 // reset user export status
228 $this->updateStatus(100, self::NOT_RUNNING);
229 exit;
230 }
231
235 protected function doesFileExist()
236 {
237 $exp = new WikiHtmlExport($this->wiki);
238 if ($this->with_comments) {
239 $exp->setMode(WikiHtmlExport::MODE_USER_COMMENTS);
240 } else {
241 $exp->setMode(WikiHtmlExport::MODE_USER);
242 }
243 $file = $exp->getUserExportFile();
244 return is_file($file);
245 }
246
250 public function deliverFile()
251 {
252 $this->log->debug("deliver");
253
254 $exp = new WikiHtmlExport($this->wiki);
255 if ($this->with_comments) {
256 $exp->setMode(WikiHtmlExport::MODE_USER_COMMENTS);
257 } else {
258 $exp->setMode(WikiHtmlExport::MODE_USER);
259 }
260 $file = $exp->getUserExportFile();
261 $this->log->debug("file: " . $file);
262 ilUtil::deliverFile($file, pathinfo($file, PATHINFO_BASENAME));
263 }
264}
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
Wiki HTML exporter class.
static getLogger($a_component_id)
Get component logger.
Class ilObjWiki.
static getLastChangeByParent($a_parent_type, $a_parent_id, $a_lang="")
Get all pages for parent object.
static now()
Return current timestamp in Y-m-d H:i:s format.
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
Class manages user html export.
updateStatus($a_progress, $a_status)
Update status.
startUserHTMLExport()
Start user html export.
__construct(ilObjWiki $a_wiki, ilDBInterface $a_db, ilObjUser $a_user, $with_comments=false)
Construct.
initUserHTMLExport()
Init user html export.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
exit
Definition: login.php:29
$ret
Definition: parser.php:6
global $ilDB