Skip to content

Commit 033ca92

Browse files
authored
Merge pull request #314 from abgreeve/backup-api
[docs] Migrate Backup and Restore API pages.
2 parents f444689 + bd363d4 commit 033ca92

File tree

5 files changed

+187
-1
lines changed

5 files changed

+187
-1
lines changed

data/migratedPages.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Automated_Manipulation_of_Strings_2.0:
4040
Automated_code_review:
4141
- filePath: "/general/development/tools/cibot.md"
4242
slug: "/general/development/tools/cibot"
43+
Backup_API:
44+
- filePath: "/docs/apis/subsystems/backup/index.md"
45+
slug: "/docs/apis/subsystems/backup/"
4346
Behat_integration:
4447
- filePath: "/general/development/tools/behat/index.md"
4548
slug: "/general/development/tools/behat/"
@@ -1464,6 +1467,9 @@ Repository_API:
14641467
Repository_plugins:
14651468
- filePath: "/docs/apis/plugintypes/repository/index"
14661469
slug: "/docs/apis/plugintypes/repository"
1470+
Restore_API:
1471+
- filePath: "/docs/apis/subsystems/backup/restore.md"
1472+
slug: "/docs/apis/subsystems/backup/restore"
14671473
Roadmap:
14681474
- filePath: "/general/community/roadmap.md"
14691475
slug: "/general/community/roadmap"

docs/apis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The [Availability API](https://docs.moodle.org/dev/Availability_API) controls ac
7474

7575
### Backup API (backup)
7676

77-
The [Backup API](https://docs.moodle.org/dev/Backup_API) defines exactly how to convert course data into XML for backup purposes, and the [Restore API](https://docs.moodle.org/dev/Restore_API) describes how to convert it back the other way.
77+
The [Backup API](./apis/subsystems/backup/index.md) defines exactly how to convert course data into XML for backup purposes, and the [Restore API](./apis/subsystems/backup/restore.md) describes how to convert it back the other way.
7878

7979
### Cache API (cache)
8080

docs/apis/subsystems/backup/index.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: Backup API
3+
tags:
4+
- Subsystem
5+
- API
6+
- Backup
7+
---
8+
The Backup API provides a way to include your plugin's in the course backup. See [Restore API](./restore.md) for the part that takes care of restoring data.
9+
10+
## Overview
11+
12+
This page provides just a quick reference to the backup machinery in Moodle 2.0 and higher. There is a detailed documentation available at [Backup 2.0](https://docs.moodle.org/dev/Backup_2.0) page - see especially the tutorial for plugin authors at [Backup 2.0 for developers](https://docs.moodle.org/dev/Backup_2.0_for_developers) page.
13+
14+
Moodle creates backups of courses or their parts by executing so called *backup plan*. The backup plan consists of a set of *backup tasks* and finally each backup task consists of one or more *backup steps*. You as the developer of a plugin will have to implement one backup task that deals with your plugin data. Most plugins have their backup tasks consisting of a single backup step - the one that creates the XML file with data from your plugin's tables. Some activities may need two or more steps to finish their backup task though (for example the backup task of the Quiz module consists of three steps as it needs to write not just the Quiz setting itself but also the questions used by that particular quiz).
15+
16+
There are subtle differences in how the backup code is organised in various supported plugin types (activity modules, blocks, themes, course reports).
17+
18+
## API for activity modules
19+
20+
### Files
21+
22+
The files that implement the backup support in your activity module must be located in the subdirectory backup/moodle2/ in your plugin's folder. So, if you are developing the activity module called *foobar* then the backup files will be located in mod/foobar/backup/moodle2/ folder.
23+
24+
The following two files are supposed to exist in that location (replace *foobar* with the name of your module):
25+
26+
- **backup_foobar_activity_task.class.php** <br />
27+
Provides the activity task class
28+
- **backup_foobar_stepslib.php** <br />
29+
Provides all the backup steps classes
30+
31+
If your module declares its own backup setting (apart from the ones common for all activity modules provided by the core), you will also want to create the backup_foobar_settingslib.php file to provide the setting classes. However most modules do not need this feature.
32+
33+
### Backup task class
34+
35+
The file backup_foobar_activity_task.class.php must provide a single class called **backup_foobar_activity_task**. All activity tasks extend the backup_activity_task class.
36+
37+
There are three methods that your class must define.
38+
39+
- **protected function define_my_settings()** <br />
40+
If your module declares own backup settings defined in the file backup_foobar_settingslib.php, add them here. Most modules just leave the method body empty.
41+
- **protected function define_my_steps()** <br />
42+
This method typically consists of one or more `$this->add_step()` calls. This is the place where you define the task as a sequence of steps to execute.
43+
- **static public function encode_content_links($content)** <br />
44+
The current instance of the activity may be referenced from other places in the course by URLs like `http://my.moodle.site/mod/foobar/view.php?id=42` Obviously, such URLs are not valid any more once the course is restored elsewhere. For this reason the backup file does not store the original URLs but encodes them into a transportable form. During the restore, the reverse process is applied and the encoded URLs are replaced with the new ones valid for the target site.
45+
46+
### Backup structure step class
47+
48+
The classes that represent the backup steps added in define_my_steps() are implemented in the file backup_foobar_stepslib.php. Most plugins define just a single step in the class called **backup_foobar_activity_structure_step** that extends the backup_activity_structure_step class. This class defines the structure step - that is the step where the structure of your plugin's instance data is described and linked with the data sources.
49+
50+
You have to implement a single method `protected function define_structure()` in this class class. There are three main things that the method must do.
51+
52+
- Create a set of backup_nested_element instances that describe the required data of your plugin
53+
- Connect these instances into a hierarchy using their `add_child()` method
54+
- Set data sources for the elements, using their methods like `set_source_table()` or `set_source_sql()`
55+
56+
The method must return the root backup_nested_element instance processed by the `prepare_activity_structure()` method (which just wraps your structures with a common envelope).
57+
58+
## API for blocks
59+
60+
### Files
61+
62+
The files that implement the backup support in your block must be located in the subdirectory backup/moodle2/ in your plugin's folder. So, if you are developing the block called *foobar* then the backup files will be located in blocks/foobar/backup/moodle2/ folder.
63+
64+
At least the file backup_foobar_block_task.class.php is supposed to exist in that location (replace *foobar* with the name of your block).
65+
66+
If your block defines its own database tables, data from which must be included in the backup, you will want to create a file backup_foobar_stepslib.php, too. Additionally, if your block declares its own backup setting, you will also want to create the backup_foobar_settingslib.php file to provide the setting classes. However most blocks do not need this feature.
67+
68+
### Backup task class
69+
70+
The file backup_foobar_block_task.class.php must provide a single class called **backup_foobar_block_task**. All block tasks extend the backup_block_task class.
71+
72+
There are five methods that your class must define.
73+
74+
- **protected function define_my_settings()** <br />
75+
If your block declares own backup settings defined in the file backup_foobar_settingslib.php, add them here. Most blocks just leave the method body empty.
76+
- **protected function define_my_steps()** <br />
77+
Blocks that do not have their own database tables usually leave this method empty. Otherwise this method consists of one or more `$this->add_step()` calls where you define the task as a sequence of steps to execute.
78+
- **public function get_fileareas()** <br />
79+
Returns the array of file area names within the block context.
80+
- **public function get_configdata_encoded_attributes()** <br />
81+
Instead of using their own tables, blocks usually use the configuration tables to hold their data (see the instance_config_save() of the block class). This method returns the array of all config elements that must be processed before they are stored in the backup. This is typically used when the stored config elements holds links to embedded media. Most blocks just return empty array here.
82+
- **static public function encode_content_links($content)** <br />
83+
If the current instance of the block may be referenced from other places in the course by URLs, it must be encoded into a transportable form. Most blocks just return unmodified $content parameter.
84+
85+
## API for admin tools
86+
87+
The files that implement the backup support in your plugin must be located in the subdirectory *backup/moodle2/* in your plugin's folder. So, if you are developing *tool_foobar* then the backup files will be located in *admin/tool/foobar/backup/moodle2/*.
88+
89+
### Task class
90+
91+
The file backup_tool_foobar_plugin.class.php must provide a single class called *backup_tool_foobar_task* extending *backup_tool_plugin*.
92+
93+
Here is a minimalistic task:
94+
95+
```php
96+
require_once($CFG->dirroot . '/backup/moodle2/backup_tool_plugin.class.php');
97+
98+
class backup_tool_foobar_plugin extends backup_tool_plugin {
99+
100+
protected function define_course_plugin_structure() {
101+
$this->step->log('Yay, backup!', backup::LOG_DEBUG);
102+
return $plugin;
103+
}
104+
105+
}
106+
```
107+
108+
## API for themes
109+
110+
See [Backup 2.0 theme data](https://docs.moodle.org/dev/Backup_2.0_theme_data)
111+
112+
## API for reports
113+
114+
See [Backup 2.0 course report data](https://docs.moodle.org/dev/Backup_2.0_course_report_data)
115+
116+
## See also
117+
118+
- [Restore API](./restore.md)
119+
- [Core APIs](../../../apis.md)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Restore API
3+
tags:
4+
- Subsystem
5+
- API
6+
- Backup
7+
---
8+
The Restore API provides a way to restore your plugin's data from a backup file created in Moodle 2.0 or later. For the information on how backup files are created, see [Backup API](./index.md). For the information on how to support restoring data from backup files created in Moodle 1.x, see [Backup conversion API](https://docs.moodle.org/dev/Backup_conversion_API).
9+
10+
## Overview
11+
12+
This page provides just a quick reference to the restore machinery in Moodle 2.0 and higher. There is a detailed documentation available at [Backup 2.0](https://docs.moodle.org/dev/Backup_2.0) page - see especially the tutorial for plugin authors at [Restore 2.0 for developers](https://docs.moodle.org/dev/Restore_2.0_for_developers) page.
13+
14+
Moodle restores data from course backups by executing so called *restore plan*. The restore plan consists of a set of *restore tasks* and finally each restore task consists of one or more *restore steps*. You as the developer of a plugin will have to implement one restore task that deals with your plugin data. Most plugins have their restore tasks consisting of a single restore step - the one that parses the plugin XML file and puts the data into its tables.
15+
16+
## API for activity modules
17+
18+
### Files
19+
20+
The files that implement the restore support in your activity module must be located in the subdirectory `backup/moodle2/` in your plugin's folder (yes, it's the same folder where the backup related files are located). So, if you are developing the activity module called *foobar* then the restore files will be located in `mod/foobar/backup/moodle2/` folder.
21+
22+
The following two files are supposed to exist in that location (replace *foobar* with the name of your module):
23+
24+
- **restore_foobar_activity_task.class.php** <br />
25+
Provides the activity task class
26+
- **restore_foobar_stepslib.php** <br />
27+
Provides all the restore steps classes
28+
29+
(to be continued)
30+
31+
## API for admin tools
32+
33+
The files that implement the backup support in your plugin must be located in the subdirectory `backup/moodle2/` in your plugin's folder. So, if you are developing `tool_foobar` then the backup files will be located in `admin/tool/foobar/backup/moodle2/`.
34+
35+
### Task class
36+
37+
The file `backup_tool_foobar_plugin.class.php` must provide a single class called `restore_tool_foobar_task` extending `restore_tool_plugin`.
38+
39+
Here is a minimalistic task:
40+
41+
```php
42+
require_once($CFG->dirroot . '/backup/moodle2/restore_tool_plugin.class.php');
43+
44+
class restore_tool_foobar_plugin extends restore_tool_plugin {
45+
46+
protected function define_course_plugin_structure() {
47+
$paths = array();
48+
$this->step->log('Yay, restore!', backup::LOG_DEBUG);
49+
return $paths;
50+
}
51+
52+
}
53+
```
54+
55+
## See also
56+
57+
- [Core APIs](../../../apis.md)
58+
- [Backup API](../backup)

project-words.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ columnsortorder
103103
competencyframeworks
104104
completionstatus
105105
componentlibrary
106+
configdata
106107
contextcourse
107108
contextcoursecat
108109
contextid
@@ -248,6 +249,7 @@ safeoverride
248249
scormreport
249250
selfcompletion
250251
sesskey
252+
settingslib
251253
setuplib
252254
shortanswer
253255
showdebugging
@@ -258,6 +260,7 @@ siteidentifier
258260
sitepolicynotagreed
259261
smallmessage
260262
splitview
263+
stepslib
261264
strftimedate
262265
strikethrough
263266
subplugin

0 commit comments

Comments
 (0)