MDL-35305 wiki: backup and restore files
[moodle.git] / mod / wiki / backup / moodle2 / backup_wiki_stepslib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * @package    mod_wiki
20  * @subpackage backup-moodle2
21  * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 /**
26  * Define all the backup steps that will be used by the backup_wiki_activity_task
27  */
29 /**
30  * Define the complete wiki structure for backup, with file and id annotations
31  */
32 class backup_wiki_activity_structure_step extends backup_activity_structure_step {
34     protected function define_structure() {
36         // To know if we are including userinfo
37         $userinfo = $this->get_setting_value('userinfo');
39         // Define each element separated
40         $wiki = new backup_nested_element('wiki', array('id'), array('name', 'intro', 'introformat', 'timecreated', 'timemodified', 'firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend'));
42         $subwikis = new backup_nested_element('subwikis');
44         $subwiki = new backup_nested_element('subwiki', array('id'), array('groupid', 'userid'));
46         $pages = new backup_nested_element('pages');
48         $page = new backup_nested_element('page', array('id'), array('title', 'cachedcontent', 'timecreated', 'timemodified', 'timerendered', 'userid', 'pageviews', 'readonly'));
50         $synonyms = new backup_nested_element('synonyms');
52         $synonym = new backup_nested_element('synonym', array('id'), array('pageid', 'pagesynonym'));
54         $links = new backup_nested_element('links');
56         $link = new backup_nested_element('link', array('id'), array('frompageid', 'topageid', 'tomissingpage'));
58         $versions = new backup_nested_element('versions');
60         $version = new backup_nested_element('version', array('id'), array('content', 'contentformat', 'version', 'timecreated', 'userid'));
62         $tags = new backup_nested_element('tags');
64         $tag = new backup_nested_element('tag', array('id'), array('name', 'rawname'));
66         // Build the tree
67         $wiki->add_child($subwikis);
68         $subwikis->add_child($subwiki);
70         $subwiki->add_child($pages);
71         $pages->add_child($page);
73         $subwiki->add_child($synonyms);
74         $synonyms->add_child($synonym);
76         $subwiki->add_child($links);
77         $links->add_child($link);
79         $page->add_child($versions);
80         $versions->add_child($version);
82         $page->add_child($tags);
83         $tags->add_child($tag);
85         // Define sources
86         $wiki->set_source_table('wiki', array('id' => backup::VAR_ACTIVITYID));
88         // All these source definitions only happen if we are including user info
89         if ($userinfo) {
90             $subwiki->set_source_sql('
91                 SELECT *
92                   FROM {wiki_subwikis}
93                  WHERE wikiid = ?', array(backup::VAR_PARENTID));
95             $page->set_source_table('wiki_pages', array('subwikiid' => backup::VAR_PARENTID));
97             $synonym->set_source_table('wiki_synonyms', array('subwikiid' => backup::VAR_PARENTID));
99             $link->set_source_table('wiki_links', array('subwikiid' => backup::VAR_PARENTID));
101             $version->set_source_table('wiki_versions', array('pageid' => backup::VAR_PARENTID));
103             $tag->set_source_sql('SELECT t.id, t.name, t.rawname
104                                     FROM {tag} t
105                                     JOIN {tag_instance} ti ON ti.tagid = t.id
106                                    WHERE ti.itemtype = ?
107                                      AND ti.itemid = ?', array(
108                                          backup_helper::is_sqlparam('wiki_pages'),
109                                          backup::VAR_PARENTID));
110         }
112         // Define id annotations
113         $subwiki->annotate_ids('group', 'groupid');
115         $subwiki->annotate_ids('user', 'userid');
117         $page->annotate_ids('user', 'userid');
119         $version->annotate_ids('user', 'userid');
121         // Define file annotations
122         $wiki->annotate_files('mod_wiki', 'intro', null); // This file area hasn't itemid
123         $subwiki->annotate_files('mod_wiki', 'attachments', 'id'); // This file area hasn't itemid
125         // Return the root element (wiki), wrapped into standard activity structure
126         return $this->prepare_activity_structure($wiki);
127     }