ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
WikiHtmlExport.php
Go to the documentation of this file.
1 <?php
2 
20 
22 use ilFileUtils;
23 
29 {
30  public const MODE_DEFAULT = "html";
31  public const MODE_COMMENTS = "html_comments";
32  public const MODE_USER = "user_html";
33  public const MODE_USER_COMMENTS = "user_html_comments";
34  protected \ILIAS\Services\Export\HTML\Util $export_util;
35 
36  protected \ilDBInterface $db;
37  protected \ilObjUser $user;
38  protected \ilLanguage $lng;
39  protected \ilTabsGUI $tabs;
40  protected \ilObjWiki $wiki;
41  protected string $mode = self::MODE_DEFAULT;
42  protected \ilLogger $log;
43  protected \ilCOPageHTMLExport $co_page_html_export;
44  protected string $export_dir;
45  protected \ILIAS\GlobalScreen\Services $global_screen;
46  protected \ilGlobalTemplateInterface $main_tpl;
47  protected \ilWikiUserHTMLExport $user_html_exp;
48  protected \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
49 
50  // has global context been initialized?
51  protected static $context_init = false;
52 
53  public function __construct(\ilObjWiki $a_wiki)
54  {
55  global $DIC;
56 
57  $this->db = $DIC->database();
58  $this->user = $DIC->user();
59  $this->lng = $DIC->language();
60  $this->tabs = $DIC->tabs();
61  $this->wiki = $a_wiki;
62  $this->log = \ilLoggerFactory::getLogger('wiki');
63  $this->global_screen = $DIC->globalScreen();
64  $this->main_tpl = $DIC->ui()->mainTemplate();
65  $this->content_style_domain = $DIC
66  ->contentStyle()
67  ->domain()
68  ->styleForRefId($a_wiki->getRefId());
69  }
70 
71  public function setMode(
72  string $a_val
73  ): void {
74  $this->mode = $a_val;
75  }
76 
77  public function getMode(): string
78  {
79  return $this->mode;
80  }
81 
87  public function buildExportFile(bool $print_version = false): string
88  {
89  $global_screen = $this->global_screen;
90  $ilDB = $this->db;
92 
93  $this->log->debug("buildExportFile...");
94  //init the mathjax rendering for HTML export
96 
97  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
98  $this->user_html_exp = new \ilWikiUserHTMLExport($this->wiki, $ilDB, $ilUser, ($this->getMode() === self::MODE_USER_COMMENTS));
99  }
100 
101  $ascii_name = str_replace(" ", "_", ilFileUtils::getASCIIFilename($this->wiki->getTitle()));
102 
103  // create export file
104  \ilExport::_createExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
105  $exp_dir =
106  \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
107 
108  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
109  ilFileUtils::delDir($exp_dir, true);
110  }
111 
112  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
113  $subdir = $ascii_name;
114  } else {
115  $subdir = $this->wiki->getType() . "_" . $this->wiki->getId();
116  }
117 
118  if ($print_version) {
119  $subdir .= "print";
120  }
121 
122  $this->export_dir = $exp_dir . "/" . $subdir;
123 
124  $this->export_util = new \ILIAS\Services\Export\HTML\Util($exp_dir, $subdir);
125 
126  // initialize temporary target directory
127  ilFileUtils::delDir($this->export_dir);
128  ilFileUtils::makeDir($this->export_dir);
129 
130  $this->log->debug("export directory: " . $this->export_dir);
131 
132 
133  $this->export_util->exportSystemStyle();
134  $eff_style_id = $this->content_style_domain->getEffectiveStyleId();
135  $this->export_util->exportCOPageFiles($eff_style_id, "wiki");
136  $this->co_page_html_export = new \ilCOPageHTMLExport($this->export_dir);
137  $this->co_page_html_export->setContentStyleId($eff_style_id);
138 
139  // export pages
140  $this->log->debug("export pages");
141  if (!self::$context_init) {
142  $global_screen->tool()->context()->current()->addAdditionalData(
144  true
145  );
146  self::$context_init = true;
147  }
148  if ($print_version) {
149  $this->exportHTMLPagesPrint();
150  } else {
151  $this->exportHTMLPages();
152  }
153  $this->exportUserImages();
154 
155  $this->export_util->exportResourceFiles();
156 
157  $date = time();
158  $zip_file_name = (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS]))
159  ? $ascii_name . ".zip"
160  : $date . "__" . IL_INST_ID . "__" . $this->wiki->getType() . "_" . $this->wiki->getId() . ".zip";
161 
162  // zip everything
163  if (true) {
164  // zip it all
165  $zip_file = \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki") .
166  "/" . $zip_file_name;
167  $this->log->debug("zip: " . $zip_file);
168  //var_dump($zip_file);
169  //exit;
170  $this->log->debug("zip, export dir: " . $this->export_dir);
171  $this->log->debug("zip, export file: " . $zip_file);
172  ilFileUtils::zip($this->export_dir, $zip_file);
173  ilFileUtils::delDir($this->export_dir);
174  }
175  return $zip_file;
176  }
177 
183  public function exportHTMLPages(): void
184  {
185  global $DIC;
186 
187  $pages = \ilWikiPage::getAllWikiPages($this->wiki->getId());
188 
189  $cnt = 0;
190 
191  foreach ($pages as $page) {
192  $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
193  $this->co_page_html_export->getPreparedMainTemplate($tpl);
194  $this->log->debug("page: " . $page["id"]);
195  if (\ilWikiPage::_exists("wpg", $page["id"])) {
196  $this->log->debug("export page");
197  $this->exportPageHTML($page["id"], $tpl);
198  $this->log->debug("collect page elements");
199  $this->co_page_html_export->collectPageElements("wpg:pg", $page["id"]);
200  }
201 
202  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
203  $cnt++;
204  $this->log->debug("update status: " . $cnt);
205  $this->user_html_exp->updateStatus((int) (50 / count($pages) * $cnt), \ilWikiUserHTMLExport::RUNNING);
206  }
207  }
208  $this->co_page_html_export->exportPageElements(
209  function (int $total, int $cnt): void {
210  $this->updateUserHTMLStatusForPageElements($total, $cnt);
211  }
212  );
213  }
214 
218  public function exportHTMLPagesPrint(): void
219  {
220  // collect page elements
221  $pages = \ilWikiPage::getAllWikiPages($this->wiki->getId());
222  foreach ($pages as $page) {
223  if (\ilWikiPage::_exists("wpg", $page["id"])) {
224  $this->co_page_html_export->collectPageElements("wpg:pg", $page["id"]);
225  }
226  }
227  $this->co_page_html_export->exportPageElements();
228 
229  // render print view
230  $wiki_gui = new \ilObjWikiGUI([], $this->wiki->getRefId(), true);
231  $print_view = $wiki_gui->getPrintView(true);
232  $print_view->setOffline(true);
233  $html = $print_view->renderPrintView();
234  file_put_contents($this->export_dir . "/index.html", $html);
235  }
236 
240  protected function exportUserImages(): void
241  {
242  if (in_array($this->getMode(), [self::MODE_COMMENTS, self::MODE_USER_COMMENTS])) {
243  $user_export = new \ILIAS\Notes\Export\UserImageExporter();
244  $user_export->exportUserImagesForRepObjId($this->export_dir, $this->wiki->getId());
245  }
246  }
247 
252  int $a_total,
253  int $a_cnt
254  ): void {
255  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
256  $this->user_html_exp->updateStatus(50 + (50 / $a_total * $a_cnt), \ilWikiUserHTMLExport::RUNNING);
257  }
258  }
259 
260 
265  public function exportPageHTML(
266  int $a_page_id,
268  ): void {
269  $this->log->debug("Export page:" . $a_page_id);
270  $lng = $this->lng;
271  $ilTabs = $this->tabs;
272 
273  $ilTabs->clearTargets();
274 
275  //$this->tpl->loadStandardTemplate();
276  $file = $this->export_dir . "/wpg_" . $a_page_id . ".html";
277  // return if file is already existing
278  if (is_file($file)) {
279  $this->log->debug("file already exists");
280  return;
281  }
282 
283  // page
284  $this->log->debug("init page gui");
285  $wpg_gui = new \ilWikiPageGUI($a_page_id);
286  $wpg_gui->setOutputMode("offline");
287  $page_content = $wpg_gui->showPage();
288 
289  // export template: page content
290  $this->log->debug("init page gui-" . $this->getMode() . "-");
291  $ep_tpl = new \ilTemplate(
292  "tpl.export_page.html",
293  true,
294  true,
295  "Modules/Wiki"
296  );
297  $ep_tpl->setVariable("PAGE_CONTENT", $page_content);
298 
299  $comments = (in_array($this->getMode(), [self::MODE_USER_COMMENTS, self::MODE_COMMENTS]))
300  ? $wpg_gui->getCommentsHTMLExport()
301  : "";
302  $ep_tpl->setVariable("COMMENTS", $comments);
303 
304  // export template: right content
305  $bl = new \ilWikiImportantPagesBlockGUI();
306  $tpl->setRightContent($bl->getHTML(true));
307 
308 
309  $this->log->debug("set title");
310  $tpl->setTitle($this->wiki->getTitle());
311  $tpl->setTitleIcon(
312  \ilUtil::getImagePath("icon_wiki.svg"),
313  $lng->txt("obj_wiki")
314  );
315 
316  $tpl->setContent($ep_tpl->get());
317  $content = $tpl->printToString();
318 
319  // open file
320  $this->log->debug("write file: " . $file);
321  if (!($fp = fopen($file, 'wb+'))) {
322  $this->log->error("Could not open " . $file . " for writing.");
323  throw new \ilWikiExportException("Could not open \"" . $file . "\" for writing.");
324  }
325 
326  // set file permissions
327  $this->log->debug("set permissions");
328  chmod($file, 0770);
329 
330  // write xml data into the file
331  fwrite($fp, $content);
332 
333  // close file
334  fclose($fp);
335 
336  if ($this->wiki->getStartPage() === $wpg_gui->getPageObject()->getTitle()) {
337  copy($file, $this->export_dir . "/index.html");
338  }
339  }
340 
344  public function getUserExportFile(): string
345  {
346  $exp_dir =
347  \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
348  $this->log->debug("dir: " . $exp_dir);
349  if (!is_dir($exp_dir)) {
350  return "";
351  }
352  foreach (new \DirectoryIterator($exp_dir) as $fileInfo) {
353  $this->log->debug("file: " . $fileInfo->getFilename());
354  if (pathinfo($fileInfo->getFilename(), PATHINFO_EXTENSION) === "zip") {
355  $this->log->debug("return: " . $exp_dir . "/" . $fileInfo->getFilename());
356  return $exp_dir . "/" . $fileInfo->getFilename();
357  }
358  }
359  return "";
360  }
361 }
ILIAS Services Export HTML Util $export_util
updateUserHTMLStatusForPageElements(int $a_total, int $a_cnt)
Callback for updating the export status during elements export (media objects, files, ...)
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.
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
Wiki HTML exporter class.
static getAllWikiPages(int $a_wiki_id)
ilWikiUserHTMLExport $user_html_exp
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...
static getASCIIFilename(string $a_filename)
global $DIC
Definition: feed.php:28
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.
static _createExportDirectory(int $a_obj_id, string $a_export_type="xml", string $a_obj_type="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
getUserExportFile()
Get user export file.
ILIAS Style Content Object ObjectFacade $content_style_domain
$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()
Singleton: get instance for use in ILIAS requests with a config loaded from the settings.
ilCOPageHTMLExport $co_page_html_export
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
$ilUser
Definition: imgupload.php:34
exportUserImages()
Export user images.
exportHTMLPagesPrint()
Export all pages as one print version.
ilGlobalTemplateInterface $main_tpl
exportHTMLPages()
Export all pages.
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
buildExportFile(bool $print_version=false)
Build export file.
setContent(string $a_html)
Sets content for standard template.
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...