MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / wiki / backuplib.php
blob120c3daea4e6383554cf2cf78dee86a88e0dc509
1 <?PHP //$Id$
2 //This php script contains all the stuff to backup/restore
3 //wiki mods
5 //This is the "graphical" structure of the wiki mod:
6 //
7 // wiki
8 // (CL,pk->id)
9 //
10 // wiki_entries
11 // (pk->id, fk->wikiid)
13 // wiki_pages
14 // (pk->pagename,version,wiki, fk->wiki)
16 // Meaning: pk->primary key field of the table
17 // fk->foreign key to link with parent
18 // nt->nested field (recursive data)
19 // CL->course level info
20 // UL->user level info
21 // files->table may have files)
23 //-----------------------------------------------------------
25 //This function executes all the backup procedure about this mod
26 function wiki_backup_mods($bf,$preferences) {
27 global $CFG;
29 $status = true;
31 ////Iterate over wiki table
32 if ($wikis = get_records ("wiki","course", $preferences->backup_course,"id")) {
33 foreach ($wikis as $wiki) {
34 if (backup_mod_selected($preferences,'wiki',$wiki->id)) {
35 wiki_backup_one_mod($bf,$preferences,$wiki);
40 return $status;
43 function wiki_backup_one_mod($bf,$preferences,$wiki) {
45 $status = true;
47 if (is_numeric($wiki)) {
48 $wiki = get_record('wiki','id',$wiki);
51 //Start mod
52 fwrite ($bf,start_tag("MOD",3,true));
53 //Print assignment data
54 fwrite ($bf,full_tag("ID",4,false,$wiki->id));
55 fwrite ($bf,full_tag("MODTYPE",4,false,"wiki"));
56 fwrite ($bf,full_tag("NAME",4,false,$wiki->name));
57 fwrite ($bf,full_tag("SUMMARY",4,false,$wiki->summary));
58 fwrite ($bf,full_tag("PAGENAME",4,false,$wiki->wtype));
59 fwrite ($bf,full_tag("WTYPE",4,false,$wiki->wtype));
60 fwrite ($bf,full_tag("EWIKIPRINTTITLE",4,false,$wiki->ewikiprinttitle));
61 fwrite ($bf,full_tag("HTMLMODE",4,false,$wiki->htmlmode));
62 fwrite ($bf,full_tag("EWIKIACCEPTBINARY",4,false,$wiki->ewikiacceptbinary));
63 fwrite ($bf,full_tag("DISABLECAMELCASE",4,false,$wiki->disablecamelcase));
64 fwrite ($bf,full_tag("SETPAGEFLAGS",4,false,$wiki->setpageflags));
65 fwrite ($bf,full_tag("STRIPPAGES",4,false,$wiki->strippages));
66 fwrite ($bf,full_tag("REMOVEPAGES",4,false,$wiki->removepages));
67 fwrite ($bf,full_tag("REVERTCHANGES",4,false,$wiki->revertchanges));
68 fwrite ($bf,full_tag("INITIALCONTENT",4,false,$wiki->initialcontent));
69 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$wiki->timemodified));
71 //backup entries and pages
72 if (backup_userdata_selected($preferences,'wiki',$wiki->id)) {
73 $status = backup_wiki_entries($bf,$preferences,$wiki->id, $preferences->mods["wiki"]->userinfo);
74 $status = backup_wiki_files_instance($bf,$preferences,$wiki->id);
77 //End mod
78 fwrite ($bf,end_tag("MOD",3,true));
80 return $status;
83 function wiki_check_backup_mods_instances($instance,$backup_unique_code) {
84 $info[$instance->id.'0'][0] = $instance->name;
85 $info[$instance->id.'0'][1] = '';
86 // wiki_check_backup_mods ignores userdata, so we do too.
87 return $info;
90 ////Return an array of info (name,value)
91 function wiki_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
92 if (!empty($instances) && is_array($instances) && count($instances)) {
93 $info = array();
94 foreach ($instances as $id => $instance) {
95 $info += wiki_check_backup_mods_instances($instance,$backup_unique_code);
97 return $info;
99 //First the course data
100 $info[0][0] = get_string("modulenameplural","wiki");
101 $info[0][1] = count_records("wiki", "course", "$course");
102 return $info;
105 //Backup wiki_entries contents (executed from wiki_backup_mods)
106 function backup_wiki_entries ($bf,$preferences,$wiki, $userinfo) {
108 global $CFG;
110 $status = true;
112 $wiki_entries = get_records("wiki_entries","wikiid",$wiki,"id");
113 //If there are entries
114 if ($wiki_entries) {
115 //Write start tag
116 $status =fwrite ($bf,start_tag("ENTRIES",4,true));
117 //Iterate over each entry
118 foreach ($wiki_entries as $wik_ent) {
119 //Entry start
120 $status =fwrite ($bf,start_tag("ENTRY",5,true));
122 fwrite ($bf,full_tag("ID",6,false,$wik_ent->id));
123 fwrite ($bf,full_tag("GROUPID",6,false,$wik_ent->groupid));
124 fwrite ($bf,full_tag("USERID",6,false,$wik_ent->userid));
125 fwrite ($bf,full_tag("PAGENAME",6,false,$wik_ent->pagename));
126 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$wik_ent->timemodified));
128 //Now save entry pages
129 $status = backup_wiki_pages($bf,$preferences,$wik_ent->id);
131 //Entry end
132 $status =fwrite ($bf,end_tag("ENTRY",5,true));
134 //Write end tag
135 $status =fwrite ($bf,end_tag("ENTRIES",4,true));
137 return $status;
140 //Write wiki_pages contents
141 function backup_wiki_pages ($bf,$preferences,$entryid) {
143 global $CFG;
145 $status = true;
147 $pages = get_records("wiki_pages","wiki",$entryid);
148 if ($pages) {
149 //Start tag
150 $status =fwrite ($bf,start_tag("PAGES",6,true));
151 //Iterate over each page
152 foreach ($pages as $page) {
153 $status =fwrite ($bf,start_tag("PAGE",7,true));
155 fwrite ($bf,full_tag("ID",8,false,$page->id));
156 fwrite ($bf,full_tag("PAGENAME",8,false,$page->pagename));
157 fwrite ($bf,full_tag("VERSION",8,false,$page->version));
158 fwrite ($bf,full_tag("FLAGS",8,false,$page->flags));
159 fwrite ($bf,full_tag("CONTENT",8,false,$page->content));
160 fwrite ($bf,full_tag("AUTHOR",8,false,$page->author));
161 fwrite ($bf,full_tag("USERID",8,false,$page->userid));
162 fwrite ($bf,full_tag("CREATED",8,false,$page->created));
163 fwrite ($bf,full_tag("LASTMODIFIED",8,false,$page->lastmodified));
164 fwrite ($bf,full_tag("REFS",8,false,str_replace("\n","$@LINEFEED@$",$page->refs)));
165 fwrite ($bf,full_tag("META",8,false,$page->meta));
166 fwrite ($bf,full_tag("HITS",8,false,$page->hits));
168 $status =fwrite ($bf,end_tag("PAGE",7,true));
170 $status =fwrite ($bf,end_tag("PAGES",6,true));
172 return $status;
175 function backup_wiki_files_instance($bf,$preferences,$instanceid) {
177 global $CFG;
179 $status = true;
181 //First we check to moddata exists and create it as necessary
182 //in temp/backup/$backup_code dir
183 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
184 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/wiki/",true);
185 //Now copy the forum dir
186 if ($status) {
187 //Only if it exists !! Thanks to Daniel Miksik.
188 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/wiki/".$instanceid)) {
189 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/wiki/".$instanceid,
190 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/wiki/".$instanceid);
194 return $status;
197 //Backup wiki binary files
198 function backup_wiki_files($bf,$preferences) {
200 global $CFG;
202 $status = true;
204 //First we check to moddata exists and create it as necessary
205 //in temp/backup/$backup_code dir
206 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
207 //Now copy the forum dir
208 if ($status) {
209 //Only if it exists !! Thanks to Daniel Miksik.
210 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/wiki")) {
211 $handle = opendir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/wiki");
212 while (false!==($item = readdir($handle))) {
213 if ($item != '.' && $item != '..' && is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/wiki/".$item)
214 && array_key_exists($item,$preferences->mods['wiki']->instances)
215 && !empty($preferences->mods['wiki']->instances[$item]->backup)) {
216 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/wiki/".$item,
217 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/wiki/",$item);
223 return $status;
227 //Return a content encoded to support interactivities linking. Every module
228 //should have its own. They are called automatically from the backup procedure.
229 function wiki_encode_content_links ($content,$preferences) {
231 global $CFG;
233 $base = preg_quote($CFG->wwwroot,"/");
235 //Link to the list of wikis
236 $buscar="/(".$base."\/mod\/wiki\/index.php\?id\=)([0-9]+)/";
237 $result= preg_replace($buscar,'$@WIKIINDEX*$2@$',$content);
239 //Link to wiki view by moduleid
240 $buscar="/(".$base."\/mod\/wiki\/view.php\?id\=)([0-9]+)/";
241 $result= preg_replace($buscar,'$@WIKIVIEWBYID*$2@$',$result);
243 return $result;