ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
WikiHtmlExport.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 namespace ILIAS\Wiki\Export;
6 
13 {
17  protected $db;
18 
22  protected $user;
23 
27  protected $lng;
28 
32  protected $tabs;
33 
37  protected $wiki;
38 
39  const MODE_DEFAULT = "html";
40  const MODE_COMMENTS = "html_comments";
41  const MODE_USER = "user_html";
42  const MODE_USER_COMMENTS = "user_html_comments";
43 
47  protected $mode = self::MODE_DEFAULT;
48 
52  protected $log;
53 
58 
62  protected $export_dir;
63 
67  protected $global_screen;
68 
72  protected $main_tpl;
73 
77  protected $user_html_exp;
78 
84  public function __construct(\ilObjWiki $a_wiki)
85  {
86  global $DIC;
87 
88  $this->db = $DIC->database();
89  $this->user = $DIC->user();
90  $this->lng = $DIC->language();
91  $this->tabs = $DIC->tabs();
92  $this->wiki = $a_wiki;
93  $this->log = \ilLoggerFactory::getLogger('wiki');
94  $this->global_screen = $DIC->globalScreen();
95  $this->main_tpl = $DIC->ui()->mainTemplate();
96  }
97 
103  public function setMode(string $a_val)
104  {
105  $this->mode = $a_val;
106  }
107 
113  public function getMode() : string
114  {
115  return $this->mode;
116  }
117 
125  public function buildExportFile()
126  {
128  $ilDB = $this->db;
130 
131  $this->log->debug("buildExportFile...");
132  //init the mathjax rendering for HTML export
133  include_once './Services/MathJax/classes/class.ilMathJax.php';
135 
136  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
137  include_once("./Modules/Wiki/classes/class.ilWikiUserHTMLExport.php");
138  $this->user_html_exp = new \ilWikiUserHTMLExport($this->wiki, $ilDB, $ilUser, ($this->getMode() == self::MODE_USER_COMMENTS));
139  }
140 
141  $ascii_name = str_replace(" ", "_", \ilUtil::getASCIIFilename($this->wiki->getTitle()));
142 
143  // create export file
144  include_once("./Services/Export/classes/class.ilExport.php");
145  \ilExport::_createExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
146  $exp_dir =
147  \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
148 
149  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
150  \ilUtil::delDir($exp_dir, true);
151  }
152 
153  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
154  $subdir = $ascii_name;
155  } else {
156  $subdir = $this->wiki->getType() . "_" . $this->wiki->getId();
157  }
158  $this->export_dir = $exp_dir . "/" . $subdir;
159 
160  $this->export_util = new \ILIAS\Services\Export\HTML\Util($exp_dir, $subdir);
161 
162  // initialize temporary target directory
163  \ilUtil::delDir($this->export_dir);
164  \ilUtil::makeDir($this->export_dir);
165 
166  $this->log->debug("export directory: " . $this->export_dir);
167 
168 
169  $this->export_util->exportSystemStyle();
170  $this->export_util->exportCOPageFiles($this->wiki->getStyleSheetId(), "wiki");
171 
172  $this->co_page_html_export = new \ilCOPageHTMLExport($this->export_dir);
173  $this->co_page_html_export->setContentStyleId(\ilObjStyleSheet::getEffectiveContentStyleId(
174  $this->wiki->getStyleSheetId(),
175  "wiki"
176  ));
177 
178  // export pages
179  $this->log->debug("export pages");
180  $global_screen->tool()->context()->current()->addAdditionalData(\ilHTMLExportViewLayoutProvider::HTML_EXPORT_RENDERING, true);
181  $this->exportHTMLPages();
182  $this->exportUserImages();
183 
184  $this->export_util->exportResourceFiles($global_screen, $this->export_dir);
185 
186  $date = time();
187  $zip_file_name = (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS]))
188  ? $ascii_name . ".zip"
189  : $date . "__" . IL_INST_ID . "__" . $this->wiki->getType() . "_" . $this->wiki->getId() . ".zip";
190 
191  // zip everything
192  if (true) {
193  // zip it all
194  $zip_file = \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki") .
195  "/" . $zip_file_name;
196  $this->log->debug("zip: " . $zip_file);
197  //var_dump($zip_file);
198  //exit;
199  $this->log->debug("zip, export dir: " . $this->export_dir);
200  $this->log->debug("zip, export file: " . $zip_file);
201  \ilUtil::zip($this->export_dir, $zip_file);
202  \ilUtil::delDir($this->export_dir);
203  }
204  return $zip_file;
205  }
206 
212  public function exportHTMLPages()
213  {
214  global $DIC;
215 
216  $pages = \ilWikiPage::getAllWikiPages($this->wiki->getId());
217 
218  $cnt = 0;
219 
220  foreach ($pages as $page) {
221  $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
222  $this->co_page_html_export->getPreparedMainTemplate($tpl);
223  $this->log->debug("page: " . $page["id"]);
224  if (\ilWikiPage::_exists("wpg", $page["id"])) {
225  $this->log->debug("export page");
226  $this->exportPageHTML($page["id"], $tpl);
227  $this->log->debug("collect page elements");
228  $this->co_page_html_export->collectPageElements("wpg:pg", $page["id"]);
229  }
230 
231  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
232  $cnt++;
233  $this->log->debug("update status: " . $cnt);
234  $this->user_html_exp->updateStatus((int) (50 / count($pages) * $cnt), \ilWikiUserHTMLExport::RUNNING);
235  }
236  }
237  $this->co_page_html_export->exportPageElements($this->updateUserHTMLStatusForPageElements);
238  }
239 
243  protected function exportUserImages()
244  {
245  if (in_array($this->getMode(), [self::MODE_COMMENTS, self::MODE_USER_COMMENTS])) {
246  $user_export = new \ILIAS\Notes\Export\UserImageExporter();
247  $user_export->exportUserImagesForRepObjId($this->export_dir, $this->wiki->getId());
248  }
249  }
250 
256  public function updateUserHTMLStatusForPageElements($a_total, $a_cnt)
257  {
258  if (in_array($this->getMode(), [self::MODE_USER, self::MODE_USER_COMMENTS])) {
259  $this->user_html_exp->updateStatus((int) 50 + (50 / count($a_total) * $a_cnt), \ilWikiUserHTMLExport::RUNNING);
260  }
261  }
262 
263 
270  public function exportPageHTML($a_page_id, \ilGlobalPageTemplate $tpl)
271  {
272  $this->log->debug("Export page:" . $a_page_id);
273  $lng = $this->lng;
274  $ilTabs = $this->tabs;
275 
276  $ilTabs->clearTargets();
277 
278 
279 
280  //$this->tpl->loadStandardTemplate();
281  $file = $this->export_dir . "/wpg_" . $a_page_id . ".html";
282  // return if file is already existing
283  if (@is_file($file)) {
284  $this->log->debug("file already exists");
285  return;
286  }
287 
288  // page
289  $this->log->debug("init page gui");
290  include_once("./Modules/Wiki/classes/class.ilWikiPageGUI.php");
291  $wpg_gui = new \ilWikiPageGUI($a_page_id);
292  $wpg_gui->setOutputMode("offline");
293  $page_content = $wpg_gui->showPage();
294 
295  // export template: page content
296  $this->log->debug("init page gui-" . $this->getMode() . "-");
297  $ep_tpl = new \ilTemplate(
298  "tpl.export_page.html",
299  true,
300  true,
301  "Modules/Wiki"
302  );
303  $ep_tpl->setVariable("PAGE_CONTENT", $page_content);
304 
305  $comments = (in_array($this->getMode(), [self::MODE_USER_COMMENTS, self::MODE_COMMENTS]))
306  ? $wpg_gui->getCommentsHTMLExport()
307  : "";
308  $ep_tpl->setVariable("COMMENTS", $comments);
309 
310  // export template: right content
311  include_once("./Modules/Wiki/classes/class.ilWikiImportantPagesBlockGUI.php");
312  $bl = new \ilWikiImportantPagesBlockGUI();
313  $tpl->setRightContent($bl->getHTML(true));
314 
315 
316  $this->log->debug("set title");
317  $tpl->setTitle($this->wiki->getTitle());
318  $tpl->setTitleIcon(
319  \ilUtil::getImagePath("icon_wiki.svg"),
320  $lng->txt("obj_wiki")
321  );
322 
323  $tpl->setContent($ep_tpl->get());
324  $content = $tpl->printToString();
325 
326  // open file
327  $this->log->debug("write file: " . $file);
328  if (!($fp = @fopen($file, "w+"))) {
329  $this->log->error("Could not open " . $file . " for writing.");
330  include_once("./Modules/Wiki/exceptions/class.ilWikiExportException.php");
331  throw new \ilWikiExportException("Could not open \"" . $file . "\" for writing.");
332  }
333 
334  // set file permissions
335  $this->log->debug("set permissions");
336  chmod($file, 0770);
337 
338  // write xml data into the file
339  fwrite($fp, $content);
340 
341  // close file
342  fclose($fp);
343 
344  if ($this->wiki->getStartPage() == $wpg_gui->getPageObject()->getTitle()) {
345  copy($file, $this->export_dir . "/index.html");
346  }
347  }
348 
354  public function getUserExportFile()
355  {
356  include_once("./Services/Export/classes/class.ilExport.php");
357  $exp_dir =
358  \ilExport::_getExportDirectory($this->wiki->getId(), $this->getMode(), "wiki");
359  $this->log->debug("dir: " . $exp_dir);
360  if (!is_dir($exp_dir)) {
361  return "";
362  }
363  foreach (new \DirectoryIterator($exp_dir) as $fileInfo) {
364  $this->log->debug("file: " . $fileInfo->getFilename());
365  if (pathinfo($fileInfo->getFilename(), PATHINFO_EXTENSION) == "zip") {
366  $this->log->debug("return: " . $exp_dir . "/" . $fileInfo->getFilename());
367  return $exp_dir . "/" . $fileInfo->getFilename();
368  }
369  }
370  return "";
371  }
372 }
static _createExportDirectory($a_obj_id, $a_export_type="xml", $a_obj_type="")
static _exists($a_parent_type, $a_id, $a_lang="", $a_no_cache=false)
Checks whether page exists.
const IL_INST_ID
Definition: constants.php:38
buildExportFile()
Build export file.
setTitleIcon($a_icon_path, $a_icon_desc="")
setMode(string $a_val)
Set mode.
Wiki HTML exporter class.
__construct(\ilObjWiki $a_wiki)
Constructor.
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
user()
Definition: user.php:4
updateUserHTMLStatusForPageElements($a_total, $a_cnt)
Callback for updating the export status during elements export (media objects, files, ...)
Class ilGlobalPageTemplate.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
Class ilObjWiki.
static getEffectiveContentStyleId($a_style_id, $a_type="")
Get effective Style Id.
global $DIC
Definition: goto.php:24
getUserExportFile()
Get user export file.
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
const PURPOSE_EXPORT
static getAllWikiPages($a_wiki_id)
Get all pages of wiki.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
setTitle($a_title, $hidden=false)
static getInstance()
Singleton: get instance.
global $ilDB
exportUserImages()
Export user images.
exportPageHTML($a_page_id, \ilGlobalPageTemplate $tpl)
Export page html.
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
static getLogger($a_component_id)
Get component logger.
exportHTMLPages()
Export all pages.
$ilUser
Definition: imgupload.php:18
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively