PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/util/helper/restore_structure_parser_processor.class.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 122 lines | 57 code | 16 blank | 49 comment | 13 complexity | 580fd44edeaec93fd027750117332838 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package moodlecore
  18. * @subpackage backup-helper
  19. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. require_once($CFG->dirroot.'/backup/util/xml/parser/processors/grouped_parser_processor.class.php');
  23. /**
  24. * helper implementation of grouped_parser_processor that will
  25. * support the process of all the moodle2 backup files, with
  26. * complete specs about what to load (grouped or no), dispatching
  27. * to corresponding methods and basic decoding of contents
  28. * (NULLs and legacy file.php uses)
  29. *
  30. * TODO: Complete phpdocs
  31. */
  32. class restore_structure_parser_processor extends grouped_parser_processor {
  33. protected $courseid; // Course->id we are restoring to
  34. protected $step; // @restore_structure_step using this processor
  35. public function __construct($courseid, $step) {
  36. $this->courseid = $courseid;
  37. $this->step = $step;
  38. parent::__construct();
  39. }
  40. /**
  41. * Provide NULL and legacy file.php uses decoding
  42. */
  43. public function process_cdata($cdata) {
  44. global $CFG;
  45. if ($cdata === '$@NULL@$') { // Some cases we know we can skip complete processing
  46. return null;
  47. } else if ($cdata === '') {
  48. return '';
  49. } else if (is_numeric($cdata)) {
  50. return $cdata;
  51. } else if (strlen($cdata) < 32) { // Impossible to have one link in 32cc
  52. return $cdata; // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
  53. } else if (strpos($cdata, '$@FILEPHP@$') === false) { // No $@FILEPHP@$, nothing to convert
  54. return $cdata;
  55. }
  56. if ($CFG->slasharguments) {
  57. $slash = '/';
  58. $forcedownload = '?forcedownload=1';
  59. } else {
  60. $slash = '%2F';
  61. $forcedownload = '&amp;forcedownload=1';
  62. }
  63. // We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
  64. $basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
  65. // Decode file.php calls
  66. $search = array ("$@FILEPHP@$");
  67. $replace = array($basefileurl);
  68. $result = str_replace($search, $replace, $cdata);
  69. // Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799
  70. $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
  71. $replace = array($slash, $forcedownload);
  72. return str_replace($search, $replace, $result);
  73. }
  74. /**
  75. * Override this method so we'll be able to skip
  76. * dispatching some well-known chunks, like the
  77. * ones being 100% part of subplugins stuff. Useful
  78. * for allowing development without having all the
  79. * possible restore subplugins defined
  80. */
  81. protected function postprocess_chunk($data) {
  82. // Iterate over all the data tags, if any of them is
  83. // not 'subplugin_XXXX' or has value, then it's a valid chunk,
  84. // pass it to standard (parent) processing of chunks.
  85. foreach ($data['tags'] as $key => $value) {
  86. if (trim($value) !== '' || strpos($key, 'subplugin_') !== 0) {
  87. parent::postprocess_chunk($data);
  88. return;
  89. }
  90. }
  91. // Arrived here, all the tags correspond to sublplugins and are empty,
  92. // skip the chunk, and debug_developer notice
  93. $this->chunks--; // not counted
  94. debugging('Missing support on restore for ' . clean_param($data['path'], PARAM_PATH) .
  95. ' subplugin (' . implode(', ', array_keys($data['tags'])) .')', DEBUG_DEVELOPER);
  96. }
  97. protected function dispatch_chunk($data) {
  98. $this->step->process($data);
  99. }
  100. protected function notify_path_start($path) {
  101. // nothing to do
  102. }
  103. protected function notify_path_end($path) {
  104. // nothing to do
  105. }
  106. }