2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the moodle format implementation of the content writer.
20 * @package core_privacy
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_privacy\local\request
;
26 defined('MOODLE_INTERNAL') ||
die();
29 * The moodle_content_writer is the default Moodle implementation of a content writer.
31 * It exports data to a rich tree structure using Moodle's context system,
32 * and produces a single zip file with all content.
34 * Objects of data are stored as JSON.
36 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class moodle_content_writer
implements content_writer
{
41 * @var string The base path on disk for this instance.
43 protected $path = null;
46 * @var \context The current context of the writer.
48 protected $context = null;
51 * @var \stored_file[] The list of files to be exported.
53 protected $files = [];
56 * Constructor for the content writer.
58 * Note: The writer factory must be passed.
60 * @param writer $writer The factory.
62 public function __construct(writer
$writer) {
63 $this->path
= make_request_directory();
67 * Set the context for the current item being processed.
69 * @param \context $context The context to use
71 public function set_context(\context
$context) : content_writer
{
72 $this->context
= $context;
78 * Export the supplied data within the current context, at the supplied subcontext.
80 * @param array $subcontext The location within the current context that this data belongs.
81 * @param \stdClass $data The data to be exported
82 * @return content_writer
84 public function export_data(array $subcontext, \stdClass
$data) : content_writer
{
85 $path = $this->get_path($subcontext, 'data.json');
87 $this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE
));
93 * Export metadata about the supplied subcontext.
95 * Metadata consists of a key/value pair and a description of the value.
97 * @param array $subcontext The location within the current context that this data belongs.
98 * @param string $key The metadata name.
99 * @param string $value The metadata value.
100 * @param string $description The description of the value.
101 * @return content_writer
103 public function export_metadata(array $subcontext, string $key, $value, string $description) : content_writer
{
104 $path = $this->get_full_path($subcontext, 'metadata.json');
106 if (file_exists($path)) {
107 $data = json_decode(file_get_contents($path));
112 $data->$key = (object) [
114 'description' => $description,
117 $path = $this->get_path($subcontext, 'metadata.json');
118 $this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE
));
124 * Export a piece of related data.
126 * @param array $subcontext The location within the current context that this data belongs.
127 * @param string $name The name of the file to be exported.
128 * @param \stdClass $data The related data to export.
129 * @return content_writer
131 public function export_related_data(array $subcontext, $name, $data) : content_writer
{
132 $path = $this->get_path($subcontext, "{$name}.json");
134 $this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE
));
140 * Export a piece of data in a custom format.
142 * @param array $subcontext The location within the current context that this data belongs.
143 * @param string $filename The name of the file to be exported.
144 * @param string $filecontent The content to be exported.
146 public function export_custom_file(array $subcontext, $filename, $filecontent) : content_writer
{
147 $filename = clean_param($filename, PARAM_FILE
);
148 $path = $this->get_path($subcontext, $filename);
149 $this->write_data($path, $filecontent);
155 * Prepare a text area by processing pluginfile URLs within it.
157 * @param array $subcontext The location within the current context that this data belongs.
158 * @param string $component The name of the component that the files belong to.
159 * @param string $filearea The filearea within that component.
160 * @param string $itemid Which item those files belong to.
161 * @param string $text The text to be processed
162 * @return string The processed string
164 public function rewrite_pluginfile_urls(array $subcontext, $component, $filearea, $itemid, $text) : string {
165 return str_replace('@@PLUGINFILE@@/', $this->get_files_target_path($component, $filearea, $itemid).'/', $text);
169 * Export all files within the specified component, filearea, itemid combination.
171 * @param array $subcontext The location within the current context that this data belongs.
172 * @param string $component The name of the component that the files belong to.
173 * @param string $filearea The filearea within that component.
174 * @param string $itemid Which item those files belong to.
176 public function export_area_files(array $subcontext, $component, $filearea, $itemid) : content_writer
{
177 $fs = get_file_storage();
178 $files = $fs->get_area_files($this->context
->id
, $component, $filearea, $itemid);
179 foreach ($files as $file) {
180 $this->export_file($subcontext, $file);
187 * Export the specified file in the target location.
189 * @param array $subcontext The location within the current context that this data belongs.
190 * @param \stored_file $file The file to be exported.
192 public function export_file(array $subcontext, \stored_file
$file) : content_writer
{
193 if (!$file->is_directory()) {
194 $pathitems = array_merge(
196 [$this->get_files_target_path($file->get_component(), $file->get_filearea(), $file->get_itemid())],
197 [$file->get_filepath()]
199 $path = $this->get_path($pathitems, $file->get_filename());
200 check_dir_exists(dirname($path), true, true);
201 $this->files
[$path] = $file;
208 * Export the specified user preference.
210 * @param string $component The name of the component.
211 * @param string $key The name of th key to be exported.
212 * @param string $value The value of the preference
213 * @param string $description A description of the value
214 * @return content_writer
216 public function export_user_preference(string $component, string $key, string $value, string $description) : content_writer
{
218 get_string('userpreferences'),
220 $fullpath = $this->get_full_path($subcontext, "{$component}.json");
221 $path = $this->get_path($subcontext, "{$component}.json");
223 if (file_exists($fullpath)) {
224 $data = json_decode(file_get_contents($fullpath));
229 $data->$key = (object) [
231 'description' => $description,
233 $this->write_data($path, json_encode($data, JSON_UNESCAPED_UNICODE
));
239 * Determine the path for the current context.
241 * @return array The context path.
243 protected function get_context_path() : Array {
245 $contexts = array_reverse($this->context
->get_parent_contexts(true));
246 foreach ($contexts as $context) {
247 $name = $context->get_context_name();
249 $path[] = clean_param("{$name} {$id}", PARAM_FILE
);
256 * Get the relative file path within the current context, and subcontext, using the specified filename.
258 * @param string[] $subcontext The location within the current context to export this data.
259 * @param string $name The intended filename, including any extensions.
260 * @return string The fully-qualfiied file path.
262 protected function get_path(array $subcontext, string $name) : string {
263 // Combine the context path, and the subcontext data.
265 $this->get_context_path(),
269 // Join the directory together with the name.
270 $filepath = implode(DIRECTORY_SEPARATOR
, $path) . DIRECTORY_SEPARATOR
. $name;
272 return preg_replace('@' . DIRECTORY_SEPARATOR
. '+@', DIRECTORY_SEPARATOR
, $filepath);
276 * Get the fully-qualified file path within the current context, and subcontext, using the specified filename.
278 * @param string[] $subcontext The location within the current context to export this data.
279 * @param string $name The intended filename, including any extensions.
280 * @return string The fully-qualfiied file path.
282 protected function get_full_path(array $subcontext, string $name) : string {
285 [$this->get_path($subcontext, $name)]
288 // Join the directory together with the name.
289 $filepath = implode(DIRECTORY_SEPARATOR
, $path);
291 return preg_replace('@' . DIRECTORY_SEPARATOR
. '+@', DIRECTORY_SEPARATOR
, $filepath);
295 * Get a path within a subcontext where exported files should be written to.
297 * @param string $component The name of the component that the files belong to.
298 * @param string $filearea The filearea within that component.
299 * @param string $itemid Which item those files belong to.
300 * @return string The path
302 protected function get_files_target_path($component, $filearea, $itemid) : string {
304 // We do not need to include the component because we organise things by context.
305 $parts = ['_files', $filearea];
307 if (!empty($itemid)) {
311 return implode(DIRECTORY_SEPARATOR
, $parts);
315 * Write the data to the specified path.
317 * @param string $path The path to export the data at.
318 * @param string $data The data to be exported.
320 protected function write_data(string $path, string $data) {
321 $targetpath = $this->path
. DIRECTORY_SEPARATOR
. $path;
322 check_dir_exists(dirname($targetpath), true, true);
323 file_put_contents($targetpath, $data);
324 $this->files
[$path] = $targetpath;
328 * Perform any required finalisation steps and return the location of the finalised export.
332 public function finalise_content() : string {
333 $exportfile = make_request_directory() . '/export.zip';
335 $fp = get_file_packer();
336 $fp->archive_to_pathname($this->files
, $exportfile);
338 // Reset the writer to prevent any further writes.