PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/book/restorelib.php

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