ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
WikiHtmlExport.php
Go to the documentation of this file.
1 <?php
2 
20 
22 use ilFileUtils;
25 
31 {
32  public const MODE_DEFAULT = "html";
33  public const MODE_COMMENTS = "html_comments";
34  public const MODE_USER = "user_html";
35  public const MODE_USER_COMMENTS = "user_html_comments";
36  protected \ILIAS\components\Export\HTML\ExportCollector $collector;
37  protected \ILIAS\Export\HTML\ExternalDomainService $html_export;
38  protected \ILIAS\components\Export\HTML\Util $export_util;
39 
40  protected \ilDBInterface $db;
41  protected \ilObjUser $user;
42  protected \ilLanguage $lng;
43  protected \ilTabsGUI $tabs;
44  protected \ilObjWiki $wiki;
45  protected string $mode = self::MODE_DEFAULT;
46  protected \ilLogger $log;
47  protected \ilCOPageHTMLExport $co_page_html_export;
48  protected string $export_dir;
49  protected \ILIAS\GlobalScreen\Services $global_screen;
50  protected \ilGlobalTemplateInterface $main_tpl;
51  protected \ilWikiUserHTMLExport $user_html_exp;
52  protected \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
53 
54  // has global context been initialized?
55  protected static $context_init = false;
56 
57  public function __construct(\ilObjWiki $a_wiki)
58  {
59  global $DIC;
60 
61  $this->db = $DIC->database();
62  $this->user = $DIC->user();
63  $this->lng = $DIC->language();
64  $this->tabs = $DIC->tabs();
65  $this->wiki = $a_wiki;
66  $this->log = \ilLoggerFactory::getLogger('wiki');
67  $this->global_screen = $DIC->globalScreen();
68  $this->main_tpl = $DIC->ui()->mainTemplate();
69  $this->content_style_domain = $DIC
70  ->contentStyle()
71  ->domain()
72  ->styleForRefId($a_wiki->getRefId());
73  $this->html_export = $DIC->export()->domain()->html();
74  }
75 
76  public function setMode(
77  string $a_val
78  ): void {
79  $this->mode = $a_val;
80  }
81 
82  public function getMode(): string
83  {
84  return $this->mode;
85  }
86 
92  public function buildExportFile(bool $print_version = false): ExportCollector
93  {
94  $global_screen = $this->global_screen;
95  $ilDB = $this->db;
96  $ilUser = $this->user;
97 
98  $this->log->debug("buildExportFile...");
99  //init the mathjax rendering for HTML export
101 
102  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
103  $this->user_html_exp = new \ilWikiUserHTMLExport($this->wiki, $ilDB, $ilUser, ($this->getMode() === self::MODE_USER_COMMENTS));
104  }
105 
106  $ascii_name = str_replace(" ", "_", ilFileUtils::getASCIIFilename($this->wiki->getTitle()));
107 
108  /*
109  \ilExport::_createExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
110  $exp_dir =
111  \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
112 
113  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
114  ilFileUtils::delDir($exp_dir, true);
115  }*/
116 
117  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
118  $subdir = $ascii_name;
119  } else {
120  $subdir = $this->wiki->getType() . "_" . $this->wiki->getId();
121  }
122 
123  if ($print_version) {
124  $subdir .= "print";
125  }
126 
127  //$this->export_dir = $exp_dir . "/" . $subdir;
128 
129 
130  $date = time();
131  $zip_file_name = (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS]))
132  ? $ascii_name . ".zip"
133  : $date . "__" . IL_INST_ID . "__" . $this->wiki->getType() . "_" . $this->wiki->getId() . ".zip";
134 
135  $this->collector = $this->html_export->collector($this->wiki->getId(), $this->getMode());
136  $this->collector->init($zip_file_name);
137 
138  $this->export_util = new \ILIAS\components\Export\HTML\Util("", "", $this->collector);
139  $this->co_page_html_export = new \ilCOPageHTMLExport("", null, 0, $this->collector);
140 
141  $this->export_util->exportSystemStyle(
142  [
143  "icon_wiki.svg"
144  ]
145  );
146  $eff_style_id = $this->content_style_domain->getEffectiveStyleId();
147  $this->export_util->exportCOPageFiles($eff_style_id, "wiki");
148  $this->co_page_html_export->setContentStyleId($eff_style_id);
149 
150  // export pages
151  $this->log->debug("export pages");
152  if (!self::$context_init) {
153  $global_screen->tool()->context()->current()->addAdditionalData(
155  true
156  );
157  self::$context_init = true;
158  }
159  if ($print_version) {
160  $this->exportHTMLPagesPrint();
161  } else {
162  $this->exportHTMLPages();
163  }
164  //$this->exportUserImages();
165 
166  $this->export_util->exportResourceFiles();
167 
168 
169  // zip everything
170  /*
171  if (true) {
172  // zip it all
173  $zip_file = \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki") .
174  "/" . $zip_file_name;
175  $this->log->debug("zip: " . $zip_file);
176  //var_dump($zip_file);
177  //exit;
178  $this->log->debug("zip, export dir: " . $this->export_dir);
179  $this->log->debug("zip, export file: " . $zip_file);
180  ilFileUtils::zip($this->export_dir, $zip_file);
181  ilFileUtils::delDir($this->export_dir);
182  }*/
183  return $this->collector;
184  }
185 
191  public function exportHTMLPages(): void
192  {
193  global $DIC;
194 
195  $pages = \ilWikiPage::getAllWikiPages($this->wiki->getId());
196 
197  $cnt = 0;
198 
199  foreach ($pages as $page) {
200  $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
201  $this->co_page_html_export->getPreparedMainTemplate($tpl);
202  $this->log->debug("page: " . $page["id"]);
203  if (\ilWikiPage::_exists("wpg", $page["id"])) {
204  $this->log->debug("export page");
205  $this->exportPageHTML($page["id"], $tpl);
206  $this->log->debug("collect page elements");
207  $this->co_page_html_export->collectPageElements("wpg:pg", $page["id"]);
208  }
209 
210  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
211  $cnt++;
212  $this->log->debug("update status: " . $cnt);
213  $this->user_html_exp->updateStatus((int) (50 / count($pages) * $cnt), \ilWikiUserHTMLExport::RUNNING);
214  }
215  }
216  $this->co_page_html_export->exportPageElements(
217  function (int $total, int $cnt): void {
218  $this->updateUserHTMLStatusForPageElements($total, $cnt);
219  }
220  );
221  }
222 
226  public function exportHTMLPagesPrint(): void
227  {
228  // collect page elements
229  $pages = \ilWikiPage::getAllWikiPages($this->wiki->getId());
230  foreach ($pages as $page) {
231  if (\ilWikiPage::_exists("wpg", $page["id"])) {
232  $this->co_page_html_export->collectPageElements("wpg:pg", $page["id"]);
233  }
234  }
235  $this->co_page_html_export->exportPageElements();
236 
237  // render print view
238  $wiki_gui = new \ilObjWikiGUI([], $this->wiki->getRefId(), true);
239  $print_view = $wiki_gui->getPrintView(true);
240  $print_view->setOffline(true);
241  $html = $print_view->renderPrintView();
242  $this->collector->addString($html, "index.html");
243  }
244 
248  /*protected function exportUserImages(): void
249  {
250  if (in_array($this->getMode(), [self::MODE_COMMENTS, self::MODE_USER_COMMENTS])) {
251  $user_export = new \ILIAS\Notes\Export\UserImageExporter();
252  $user_export->exportUserImagesForRepObjId($this->export_dir, $this->wiki->getId());
253  }
254  }*/
255 
260  int $a_total,
261  int $a_cnt
262  ): void {
263  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
264  $this->user_html_exp->updateStatus((int) (50 + (50 / $a_total * $a_cnt)), \ilWikiUserHTMLExport::RUNNING);
265  }
266  }
267 
268 
273  public function exportPageHTML(
274  int $a_page_id,
276  ): void {
277  $this->log->debug("Export page:" . $a_page_id);
278  $lng = $this->lng;
279  $ilTabs = $this->tabs;
280 
281  $ilTabs->clearTargets();
282 
283  //$this->tpl->loadStandardTemplate();
284  $file = "wpg_" . $a_page_id . ".html";
285  // return if file is already existing
286  /*
287  if (is_file($file)) {
288  $this->log->debug("file already exists");
289  return;
290  }*/
291 
292  // page
293  $this->log->debug("init page gui");
294  $wpg_gui = new \ilWikiPageGUI(
295  $a_page_id,
296  0,
297  $this->wiki->getRefId()
298  );
299  $wpg_gui->setOutputMode("offline");
300  $page_content = $wpg_gui->showPage();
301 
302  // export template: page content
303  $this->log->debug("init page gui-" . $this->getMode() . "-");
304  $ep_tpl = new \ilTemplate(
305  "tpl.export_page.html",
306  true,
307  true,
308  "components/ILIAS/Wiki"
309  );
310  $ep_tpl->setVariable("PAGE_CONTENT", $page_content);
311 
312  $comments = (in_array($this->getMode(), [self::MODE_USER_COMMENTS, self::MODE_COMMENTS]))
313  ? $wpg_gui->getCommentsHTMLExport()
314  : "";
315  $ep_tpl->setVariable("COMMENTS", $comments);
316 
317  // export template: right content
318  $bl = new \ilWikiImportantPagesBlockGUI();
319  $tpl->setRightContent($bl->getHTML(true));
320 
321 
322  $this->log->debug("set title");
323  $tpl->setTitle($this->wiki->getTitle());
324  $tpl->setTitleIcon(
325  \ilUtil::getImagePath("standard/icon_wiki.svg"),
326  $lng->txt("obj_wiki")
327  );
328 
329  $tpl->setContent($ep_tpl->get());
330  $content = $tpl->printToString();
331 
332  // open file
333  /*$this->log->debug("write file: " . $file);
334  if (!($fp = fopen($file, 'wb+'))) {
335  $this->log->error("Could not open " . $file . " for writing.");
336  throw new \ilWikiExportException("Could not open \"" . $file . "\" for writing.");
337  }*/
338 
339  // set file permissions
340  /*$this->log->debug("set permissions");
341  chmod($file, 0770);*/
342 
343  // write xml data into the file
344  //fwrite($fp, $content);
345  $this->collector->addString($content, $file);
346 
347  // close file
348  //fclose($fp);
349 
350  if ($this->wiki->getStartPage() === $wpg_gui->getPageObject()->getTitle()) {
351  $this->collector->addString($content, "index.html");
352  }
353  }
354 
358  public function getUserExportFile(): string
359  {
360  $exp_dir =
361  \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
362  $this->log->debug("dir: " . $exp_dir);
363  if (!is_dir($exp_dir)) {
364  return "";
365  }
366  foreach (new \DirectoryIterator($exp_dir) as $fileInfo) {
367  $this->log->debug("file: " . $fileInfo->getFilename());
368  if (pathinfo($fileInfo->getFilename(), PATHINFO_EXTENSION) === "zip") {
369  $this->log->debug("return: " . $exp_dir . "/" . $fileInfo->getFilename());
370  return $exp_dir . "/" . $fileInfo->getFilename();
371  }
372  }
373  return "";
374  }
375 
376  public function deliverLatest(): void
377  {
378  $fm = $this->html_export->fileManager();
379  $latest = $fm->getLatestOfObjectIdAndType($this->wiki->getId(), $this->getMode());
380  if ($latest) {
381  $fm->deliver($latest);
382  }
383  }
384 
385  public function getLatest(): ?ExportFile
386  {
387  return $this->html_export->fileManager()->getLatestOfObjectIdAndType($this->wiki->getId(), $this->getMode());
388  }
389 }
updateUserHTMLStatusForPageElements(int $a_total, int $a_cnt)
Export user images.
const IL_INST_ID
Definition: constants.php:40
static getLogger(string $a_component_id)
Get component logger.
setTitle(string $a_title, bool $hidden=false)
Sets title in standard template.
Wiki HTML exporter class.
ilWikiUserHTMLExport $user_html_exp
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object
setTitleIcon(string $a_icon_path, string $a_icon_desc="")
set title icon
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS components Export HTML ExportCollector $collector
static getASCIIFilename(string $a_filename)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ILIAS components Export HTML Util $export_util
setRightContent(string $a_html)
Sets content of right column.
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getUserExportFile()
Get user export file.
global $DIC
Definition: shib_login.php:26
ILIAS Style Content Object ObjectFacade $content_style_domain
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
$comments
printToString()
Use this method to get the finally rendered page as string.
ILIAS GlobalScreen Services $global_screen
clearTargets()
clear all targets
exportPageHTML(int $a_page_id, \ilGlobalPageTemplate $tpl)
Export page html.
const PURPOSE_EXPORT
static getInstance()
ilCOPageHTMLExport $co_page_html_export
ILIAS Export HTML ExternalDomainService $html_export
exportHTMLPagesPrint()
Export all pages as one print version.
ilGlobalTemplateInterface $main_tpl
exportHTMLPages()
Export all pages.
static getAllWikiPages(int $a_wiki_id, string $lang="-")
buildExportFile(bool $print_version=false)
Build export file.
setContent(string $a_html)
Sets content for standard template.