739e6af0d65901cec3fe739bb75a16ce116c80b0
[moodle.git] / restorelib.php
1 <?PHP // $Id: restorelib.php,v 1.1 2006/03/12 18:39:59 skodak Exp $
2     //This php script contains all the stuff to backup/restore
3     //book mods
5     //This is the "graphical" structure of the book mod:
6     //
7     //                       book
8     //                     (CL,pk->id)
9     //                        |
10     //                        |
11     //                        |
12     //                     book_chapters
13     //               (CL,pk->id, fk->bookid)
14     //
15     // Meaning: pk->primary key field of the table
16     //          fk->foreign key to link with parent
17     //          nt->nested field (recursive data)
18     //          CL->course level info
19     //          UL->user level info
20     //          files->table may have files)
21     //
22     //-----------------------------------------------------------
25     //This function executes all the restore procedure about this mod
26     function book_restore_mods($mod,$restore) {
28         global $CFG;
30         $status = true;
32         //Get record from backup_ids
33         $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
35         if ($data) {
36             //Now get completed xmlized object
37             $info = $data->info;
38             //traverse_xmlize($info);                                                                     //Debug
39             //print_object ($GLOBALS['traverse_array']);                                                  //Debug
40             //$GLOBALS['traverse_array']="";                                                              //Debug
42             //Now, build the BOOK record structure
43             $book->course = $restore->course_id;
44             $book->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
45             $book->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
46             $book->numbering = backup_todb($info['MOD']['#']['NUMBERING']['0']['#']);
47             $book->disableprinting = backup_todb($info['MOD']['#']['DISABLEPRINTING']['0']['#']);
48             $book->customtitles = backup_todb($info['MOD']['#']['CUSTOMTITLES']['0']['#']);
49             $book->timecreated = $info['MOD']['#']['TIMECREATED']['0']['#'];
50             $book->timemodified = $info['MOD']['#']['TIMEMODIFIED']['0']['#'];
52             //The structure is equal to the db, so insert the book
53             $newid = insert_record ('book',$book);
55             //Do some output
56             if (!defined('RESTORE_SILENTLY')) {
57                 echo '<ul><li>'.get_string('modulename','book').' "'.$book->name.'"<br>';
58             }
59             backup_flush(300);
61             if ($newid) {
62                 //We have the newid, update backup_ids
63                 backup_putid($restore->backup_unique_code,$mod->modtype,
64                              $mod->id, $newid);
65                 //now restore chapters
66                 $status = book_chapters_restore($mod->id,$newid,$info,$restore);
68             } else {
69                 $status = false;
70             }
71             //Finalize ul
72             if (!defined('RESTORE_SILENTLY')) {
73                 echo "</ul>";
74             }
76         } else {
77             $status = false;
78         }
80         return $status;
81     }
83     //This function restores the book_chapters
84     function book_chapters_restore($old_book_id, $new_book_id,$info,$restore) {
86         global $CFG;
88         $status = true;
90         //Get the chapters array
91         $chapters = $info['MOD']['#']['CHAPTERS']['0']['#']['CHAPTER'];
93         //Iterate over chapters
94         for($i = 0; $i < sizeof($chapters); $i++) {
95             $sub_info = $chapters[$i];
96             //traverse_xmlize($sub_info);                                                                 //Debug
97             //print_object ($GLOBALS['traverse_array']);                                                  //Debug
98             //$GLOBALS['traverse_array']="";                                                              //Debug
100             //We'll need this later!!
101             $old_id = backup_todb($sub_info['#']['ID']['0']['#']);
103             //Now, build the ASSIGNMENT_CHAPTERS record structure
104             $chapter->bookid = $new_book_id;
105             $chapter->pagenum = backup_todb($sub_info['#']['PAGENUM']['0']['#']);
106             $chapter->subchapter = backup_todb($sub_info['#']['SUBCHAPTER']['0']['#']);
107             $chapter->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
108             $chapter->content = backup_todb($sub_info['#']['CONTENT']['0']['#']);
109             $chapter->hidden = backup_todb($sub_info['#']['HIDDEN']['0']['#']);
110             $chapter->timecreated = backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
111             $chapter->timemodified = backup_todb($sub_info['#']['TIMEMODIFIED']['0']['#']);
112             $chapter->importsrc = backup_todb($sub_info['#']['IMPORTSRC']['0']['#']);
114             //The structure is equal to the db, so insert the book_chapters
115             $newid = insert_record ('book_chapters',$chapter);
117             //Do some output
118             if (($i+1) % 50 == 0) {
119                 if (!defined('RESTORE_SILENTLY')) {
120                     echo '.';
121                     if (($i+1) % 1000 == 0) {
122                         echo '<br>';
123                     }
124                 }
125                 backup_flush(300);
126             }
128             if ($newid) {
129                 //We have the newid, update backup_ids
130                 backup_putid($restore->backup_unique_code,'book_chapters',$old_id,
131                              $newid);
132             } else {
133                 $status = false;
134             }
135         }
136         return $status;
137     }
139     //This function returns a log record with all the necessay transformations
140     //done. It's used by restore_log_module() to restore modules log.
141     function book_restore_logs($restore,$log) {
143         $status = false;
145         //Depending of the action, we recode different things
146         switch ($log->action) {
147             case "update":
148             case "view": //TO DO ... verify!!!
149                if ($log->cmid) {
150                     //Get the new_id of the chapter (to recode the url field)
151                     $ch = backup_getid($restore->backup_unique_code,"book_chapters",$log->info);
152                     if ($pag) {
153                         $log->url = "view.php?id=".$log->cmid."&chapterid=".$ch->new_id;
154                         $log->info = $ch->new_id;
155                         $status = true;
156                     }
157                 }
158                 break;
159             case "view all":
160                 if ($log->cmid) {
161                     //Get the new_id of the module (to recode the info field)
162                     $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
163                     if ($mod) {
164                         $log->url = "view.php?id=".$log->cmid;
165                         $log->info = $mod->new_id;
166                         $status = true;
167                     }
168                 }
169                 break;
170             case "export":
171                 if ($log->cmid) {
172                     //Get the new_id of the module (to recode the info field)
173                     $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
174                     if ($mod) {
175                         $log->url = "export.php?id=".$log->cmid;
176                         $log->info = $mod->new_id;
177                         $status = true;
178                     }
179                 }
180                 break;
181             case "print":
182                 if ($log->cmid) {
183                     //Get the new_id of the module (to recode the info field)
184                     $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
185                     if ($mod) {
186                         $log->url = "print.php?id=".$log->cmid;
187                         $log->info = $mod->new_id;
188                         $status = true;
189                     }
190                 }
191                 break;
192             default:
193                 if (!defined('RESTORE_SILENTLY')) {
194                     echo "action (".$log->module."-".$log->action.") unknow. Not restored<br>";                 //Debug
195                 }
196                 break;
197         }
199         if ($status) {
200             $status = $log;
201         }
202         return $status;
203     }
205     //Return a content decoded to support interactivities linking. Every module
206     //should have its own. They are called automatically from
207     //book_decode_content_links_caller() function in each module
208     //in the restore process
209     function book_decode_content_links ($content,$restore) {
211         global $CFG;
213         $result = $content;
215         //Link to the list of books
217         $searchstring='/\$@(BOOKINDEX)\*([0-9]+)@\$/';
218         //We look for it
219         preg_match_all($searchstring,$content,$foundset);
220         //If found, then we are going to look for its new id (in backup tables)
221         if ($foundset[0]) {
222             //print_object($foundset);                                     //Debug
223             //Iterate over foundset[2]. They are the old_ids
224             foreach($foundset[2] as $old_id) {
225                 //We get the needed variables here (course id)
226                 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
227                 //Personalize the searchstring
228                 $searchstring='/\$@(BOOKINDEX)\*('.$old_id.')@\$/';
229                 //If it is a link to this course, update the link to its new location
230                 if($rec->new_id) {
231                     //Now replace it
232                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/book/index.php?id='.$rec->new_id,$result);
233                 } else {
234                     //It's a foreign link so leave it as original
235                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/book/index.php?id='.$old_id,$result);
236                 }
237             }
238         }
241         //Links to specific chapters of books
243         $searchstring='/\$@(BOOKCHAPTER)\*([0-9]+)\*([0-9]+)@\$/';
244         //We look for it
245         preg_match_all($searchstring,$result,$foundset);
246         //If found, then we are going to look for its new id (in backup tables)
247         if ($foundset[0]) {
248             //print_object($foundset);                                     //Debug
249             //Iterate over foundset[2] and foundset[3]. They are the old_ids
250             foreach($foundset[2] as $key => $old_id) {
251                 $old_id2 = $foundset[3][$key];
252                 //We get the needed variables here (discussion id and post id)
253                 $rec = backup_getid($restore->backup_unique_code,'course_modules',$old_id);
254                 $rec2 = backup_getid($restore->backup_unique_code,'book_chapters',$old_id2);
255                 //Personalize the searchstring
256                 $searchstring='/\$@(BOOKCHAPTER)\*('.$old_id.')\*('.$old_id2.')@\$/';
257                 //If it is a link to this course, update the link to its new location
258                 if($rec->new_id && $rec2->new_id) {
259                     //Now replace it
260                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/book/view.php?id='.$rec->new_id.'&chapterid='.$rec2->new_id,$result);
261                 } else {
262                     //It's a foreign link so leave it as original
263                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/book/view.php?id='.$old_id.'&chapterid='.$old_id2,$result);
264                 }
265             }
266         }
269         //Links to first chapters of books
271         $searchstring='/\$@(BOOKSTART)\*([0-9]+)@\$/';
272         //We look for it
273         preg_match_all($searchstring,$result,$foundset);
274         //If found, then we are going to look for its new id (in backup tables)
275         if ($foundset[0]) {
276             //print_object($foundset);                                     //Debug
277             //Iterate over foundset[2]. They are the old_ids
278             foreach($foundset[2] as $old_id) {
279                 //We get the needed variables here (course_modules id)
280                 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
281                 //Personalize the searchstring
282                 $searchstring='/\$@(BOOKSTART)\*('.$old_id.')@\$/';
283                 //If it is a link to this course, update the link to its new location
284                 if($rec->new_id) {
285                     //Now replace it
286                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/book/view.php?id='.$rec->new_id,$result);
287                 } else {
288                     //It's a foreign link so leave it as original
289                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/book/view.php?id='.$old_id,$result);
290                 }
291             }
292         }
294         return $result;
295     }
297     //This function makes all the necessary calls to xxxx_decode_content_links()
298     //function in each module, passing them the desired contents to be decoded
299     //from backup format to destination site/course in order to mantain inter-activities
300     //working in the backup/restore process. It's called from restore_decode_content_links()
301     //function in restore process
302     function book_decode_content_links_caller($restore) {
303         global $CFG;
304         $status = true;
306         //Decode every BOOK (summary) in the coure
307         if ($books = get_records_sql ("SELECT b.id, b.summary
308                                    FROM {$CFG->prefix}book b
309                                    WHERE b.course = $restore->course_id")) {
310             //Iterate over each book->summary
311             $i = 0;   //Counter to send some output to the browser to avoid timeouts
312             foreach ($books as $book) {
313                 //Increment counter
314                 $i++;
315                 $content = $book->summary;
316                 $result = restore_decode_content_links_worker($content,$restore);
317                 if ($result != $content) {
318                     //Update record
319                     $book->summary = addslashes($result);
320                     $status = update_record('book',$book);
321                     if ($CFG->debug>7) {
322                         if (!defined('RESTORE_SILENTLY')) {
323                             echo '<br /><hr />'.htmlentities($content).'<br />changed to<br />'.htmlentities($result).'<hr /><br />';
324                         }
325                     }
326                 }
327                 //Do some output
328                 if (($i+1) % 5 == 0) {
329                     if (!defined('RESTORE_SILENTLY')) {
330                         echo '.';
331                         if (($i+1) % 100 == 0) {
332                             echo '<br />';
333                         }
334                     }
335                     backup_flush(300);
336                 }
337             }
338         }
340         //Decode every CHAPTER in the course
341         if ($chapters = get_records_sql ("SELECT ch.id, ch.content
342                                    FROM {$CFG->prefix}book b,
343                                         {$CFG->prefix}book_chapters ch
344                                    WHERE b.course = $restore->course_id AND
345                                          ch.bookid = b.id")) {
346             //Iterate over each chapter->content
347             $i = 0;   //Counter to send some output to the browser to avoid timeouts
348             foreach ($chapters as $chapter) {
349                 //Increment counter
350                 $i++;
351                 $content = $chapter->content;
352                 $result = restore_decode_content_links_worker($content,$restore);
353                 if ($result != $content) {
354                     //Update record
355                     $chapter->content = addslashes($result);
356                     $status = update_record('book_chapters',$chapter);
357                     if ($CFG->debug>7) {
358                         if (!defined('RESTORE_SILENTLY')) {
359                             echo '<br /><hr />'.htmlentities($content).'<br />changed to<br />'.htmlentities($result).'<hr /><br />';
360                         }
361                     }
362                 }
363                 //Do some output
364                 if (($i+1) % 5 == 0) {
365                     if (!defined('RESTORE_SILENTLY')) {
366                         echo '.';
367                         if (($i+1) % 100 == 0) {
368                             echo '<br />';
369                         }
370                     }
371                     backup_flush(300);
372                 }
373             }
374         }
376         return $status;
377     }
380 ?>