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