PageRenderTime 53ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/resource/restorelib.php

https://bitbucket.org/ceu/moodle_demo
PHP | 389 lines | 250 code | 46 blank | 93 comment | 67 complexity | 043fbee3156e0dd3ecfc51321484cf41 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php //$Id: restorelib.php,v 1.26 2007/04/22 22:07:03 stronk7 Exp $
  2. //This php script contains all the stuff to backup/restore
  3. //resource mods
  4. //This is the "graphical" structure of the resource mod:
  5. //
  6. // resource
  7. // (CL,pk->id,files)
  8. //
  9. // Meaning: pk->primary key field of the table
  10. // fk->foreign key to link with parent
  11. // nt->nested field (recursive data)
  12. // CL->course level info
  13. // UL->user level info
  14. // files->table may have files)
  15. //
  16. //-----------------------------------------------------------
  17. //This function executes all the restore procedure about this mod
  18. function resource_restore_mods($mod,$restore) {
  19. global $CFG;
  20. $status = true;
  21. //Get record from backup_ids
  22. $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  23. if ($data) {
  24. //Now get completed xmlized object
  25. $info = $data->info;
  26. //traverse_xmlize($info); //Debug
  27. //print_object ($GLOBALS['traverse_array']); //Debug
  28. //$GLOBALS['traverse_array']=""; //Debug
  29. //Now, build the RESOURCE record structure
  30. $resource->course = $restore->course_id;
  31. $resource->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  32. $resource->type = $info['MOD']['#']['TYPE']['0']['#'];
  33. $resource->reference = backup_todb($info['MOD']['#']['REFERENCE']['0']['#']);
  34. $resource->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
  35. $resource->alltext = backup_todb($info['MOD']['#']['ALLTEXT']['0']['#']);
  36. $resource->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
  37. $resource->options = backup_todb($info['MOD']['#']['OPTIONS']['0']['#']);
  38. $resource->timemodified = $info['MOD']['#']['TIMEMODIFIED']['0']['#'];
  39. //To mantain compatibility, in 1.4 the type and alltext meaning has changed and
  40. //two new fields have arrived (popup and options). We have to modify somethigs
  41. //if the popup field isn't present in the backup file to be upwards compatible.
  42. if (! isset($info['MOD']['#']['POPUP']['0']['#'])) { //It's a pre-14 backup file
  43. //Move alltext to popup in 3 and 5 resource types
  44. if ($resource->type == 3 || $resource->type == 5) {
  45. $resource->popup = $resource->alltext;
  46. $resource->alltext = '';
  47. }
  48. //Move reference to alltext for references
  49. if ($resource->type == 1) {
  50. $resource->popup = '';
  51. $resource->alltext = $resource->reference;
  52. $resource->reference = '';
  53. }
  54. //Reencode the type field to its new values and fill the options field as needed
  55. //Array 1-9 of new types
  56. $types = array ('','html','file','file','text','file',
  57. 'html','file','text','directory');
  58. //Array 1-9 of corresponding options
  59. $options = array ('','','frame','','0','',
  60. '','','3','');
  61. //Make the conversion
  62. $oldtype = $resource->type;
  63. $resource->type = $types[$oldtype];
  64. $resource->options = $options[$oldtype];
  65. }
  66. if ($resource->type == 'reference') { // Obsolete type of resource
  67. $resource->type == 'html';
  68. $resource->alltext = $resource->reference;
  69. $resource->reference = '';
  70. }
  71. //The structure is equal to the db, so insert the resource
  72. $newid = insert_record ("resource",$resource);
  73. //Do some output
  74. if (!defined('RESTORE_SILENTLY')) {
  75. echo "<li>".get_string("modulename","resource")." \"".format_string(stripslashes($resource->name),true)."\"</li>";
  76. }
  77. backup_flush(300);
  78. if ($newid) {
  79. //We have the newid, update backup_ids
  80. backup_putid($restore->backup_unique_code,$mod->modtype,
  81. $mod->id, $newid);
  82. // restore any associated files...
  83. if ($resource->type == 'file' || $resource->type == 'directory' || $resource->type == 'ims') {
  84. resource_restore_files($mod->id,$newid,$resource,$restore);
  85. }
  86. } else {
  87. $status = false;
  88. }
  89. } else {
  90. $status = false;
  91. }
  92. return $status;
  93. }
  94. //Return a content decoded to support interactivities linking. Every module
  95. //should have its own. They are called automatically from
  96. //resource_decode_content_links_caller() function in each module
  97. //in the restore process
  98. function resource_decode_content_links ($content,$restore) {
  99. global $CFG;
  100. $result = $content;
  101. //Link to the list of resources
  102. $searchstring='/\$@(RESOURCEINDEX)\*([0-9]+)@\$/';
  103. //We look for it
  104. preg_match_all($searchstring,$content,$foundset);
  105. //If found, then we are going to look for its new id (in backup tables)
  106. if ($foundset[0]) {
  107. //print_object($foundset); //Debug
  108. //Iterate over foundset[2]. They are the old_ids
  109. foreach($foundset[2] as $old_id) {
  110. //We get the needed variables here (course id)
  111. $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
  112. //Personalize the searchstring
  113. $searchstring='/\$@(RESOURCEINDEX)\*('.$old_id.')@\$/';
  114. //If it is a link to this course, update the link to its new location
  115. if (!empty($rec->new_id)) {
  116. //Now replace it
  117. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/resource/index.php?id='.$rec->new_id,$result);
  118. } else {
  119. //It's a foreign link so leave it as original
  120. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/resource/index.php?id='.$old_id,$result);
  121. }
  122. }
  123. }
  124. //Link to resource view by moduleid
  125. $searchstring='/\$@(RESOURCEVIEWBYID)\*([0-9]+)@\$/';
  126. //We look for it
  127. preg_match_all($searchstring,$result,$foundset);
  128. //If found, then we are going to look for its new id (in backup tables)
  129. if ($foundset[0]) {
  130. //print_object($foundset); //Debug
  131. //Iterate over foundset[2]. They are the old_ids
  132. foreach($foundset[2] as $old_id) {
  133. //We get the needed variables here (course_modules id)
  134. $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
  135. //Personalize the searchstring
  136. $searchstring='/\$@(RESOURCEVIEWBYID)\*('.$old_id.')@\$/';
  137. //If it is a link to this course, update the link to its new location
  138. if (!empty($rec->new_id)) {
  139. //Now replace it
  140. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/resource/view.php?id='.$rec->new_id,$result);
  141. } else {
  142. //It's a foreign link so leave it as original
  143. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/resource/view.php?id='.$old_id,$result);
  144. }
  145. }
  146. }
  147. //Link to resource view by resourceid
  148. $searchstring='/\$@(RESOURCEVIEWBYR)\*([0-9]+)@\$/';
  149. //We look for it
  150. preg_match_all($searchstring,$result,$foundset);
  151. //If found, then we are going to look for its new id (in backup tables)
  152. if ($foundset[0]) {
  153. //print_object($foundset); //Debug
  154. //Iterate over foundset[2]. They are the old_ids
  155. foreach($foundset[2] as $old_id) {
  156. //We get the needed variables here (forum id)
  157. $rec = backup_getid($restore->backup_unique_code,"resource",$old_id);
  158. //Personalize the searchstring
  159. $searchstring='/\$@(RESOURCEVIEWBYR)\*('.$old_id.')@\$/';
  160. //If it is a link to this course, update the link to its new location
  161. if($rec->new_id) {
  162. //Now replace it
  163. $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/resource/view.php?r='.$rec->new_id,$result);
  164. } else {
  165. //It's a foreign link so leave it as original
  166. $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/resource/view.php?r='.$old_id,$result);
  167. }
  168. }
  169. }
  170. return $result;
  171. }
  172. //This function makes all the necessary calls to xxxx_decode_content_links()
  173. //function in each module, passing them the desired contents to be decoded
  174. //from backup format to destination site/course in order to mantain inter-activities
  175. //working in the backup/restore process. It's called from restore_decode_content_links()
  176. //function in restore process
  177. function resource_decode_content_links_caller($restore) {
  178. global $CFG;
  179. $status = true;
  180. if ($resources = get_records_sql ("SELECT r.id, r.alltext, r.summary, r.reference
  181. FROM {$CFG->prefix}resource r
  182. WHERE r.course = $restore->course_id")) {
  183. $i = 0; //Counter to send some output to the browser to avoid timeouts
  184. foreach ($resources as $resource) {
  185. //Increment counter
  186. $i++;
  187. $content1 = $resource->alltext;
  188. $content2 = $resource->summary;
  189. $content3 = $resource->reference;
  190. $result1 = restore_decode_content_links_worker($content1,$restore);
  191. $result2 = restore_decode_content_links_worker($content2,$restore);
  192. $result3 = restore_decode_content_links_worker($content3,$restore);
  193. if ($result1 != $content1 || $result2 != $content2 || $result3 != $content3) {
  194. //Update record
  195. $resource->alltext = addslashes($result1);
  196. $resource->summary = addslashes($result2);
  197. $resource->reference = addslashes($result3);
  198. $status = update_record("resource",$resource);
  199. if (debugging()) {
  200. if (!defined('RESTORE_SILENTLY')) {
  201. echo '<br /><hr />'.s($content1).'<br />changed to<br />'.s($result1).'<hr /><br />';
  202. echo '<br /><hr />'.s($content2).'<br />changed to<br />'.s($result2).'<hr /><br />';
  203. echo '<br /><hr />'.s($content3).'<br />changed to<br />'.s($result3).'<hr /><br />';
  204. }
  205. }
  206. }
  207. //Do some output
  208. if (($i+1) % 5 == 0) {
  209. if (!defined('RESTORE_SILENTLY')) {
  210. echo ".";
  211. if (($i+1) % 100 == 0) {
  212. echo "<br />";
  213. }
  214. }
  215. backup_flush(300);
  216. }
  217. }
  218. }
  219. return $status;
  220. }
  221. //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
  222. //some texts in the module
  223. function resource_restore_wiki2markdown ($restore) {
  224. global $CFG;
  225. $status = true;
  226. //Convert resource->alltext
  227. if ($records = get_records_sql ("SELECT r.id, r.alltext, r.options
  228. FROM {$CFG->prefix}resource r,
  229. {$CFG->prefix}backup_ids b
  230. WHERE r.course = $restore->course_id AND
  231. options = ".FORMAT_WIKI. " AND
  232. b.backup_code = $restore->backup_unique_code AND
  233. b.table_name = 'resource' AND
  234. b.new_id = r.id")) {
  235. foreach ($records as $record) {
  236. //Rebuild wiki links
  237. $record->alltext = restore_decode_wiki_content($record->alltext, $restore);
  238. //Convert to Markdown
  239. $wtm = new WikiToMarkdown();
  240. $record->alltext = $wtm->convert($record->alltext, $restore->course_id);
  241. $record->options = FORMAT_MARKDOWN;
  242. $status = update_record('resource', addslashes_object($record));
  243. //Do some output
  244. $i++;
  245. if (($i+1) % 1 == 0) {
  246. if (!defined('RESTORE_SILENTLY')) {
  247. echo ".";
  248. if (($i+1) % 20 == 0) {
  249. echo "<br />";
  250. }
  251. }
  252. backup_flush(300);
  253. }
  254. }
  255. }
  256. return $status;
  257. }
  258. //This function returns a log record with all the necessay transformations
  259. //done. It's used by restore_log_module() to restore modules log.
  260. function resource_restore_logs($restore,$log) {
  261. $status = false;
  262. //Depending of the action, we recode different things
  263. switch ($log->action) {
  264. case "add":
  265. if ($log->cmid) {
  266. //Get the new_id of the module (to recode the info field)
  267. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  268. if ($mod) {
  269. $log->url = "view.php?id=".$log->cmid;
  270. $log->info = $mod->new_id;
  271. $status = true;
  272. }
  273. }
  274. break;
  275. case "update":
  276. if ($log->cmid) {
  277. //Get the new_id of the module (to recode the info field)
  278. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  279. if ($mod) {
  280. $log->url = "view.php?id=".$log->cmid;
  281. $log->info = $mod->new_id;
  282. $status = true;
  283. }
  284. }
  285. break;
  286. case "view":
  287. if ($log->cmid) {
  288. //Get the new_id of the module (to recode the info field)
  289. $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
  290. if ($mod) {
  291. $log->url = "view.php?id=".$log->cmid;
  292. $log->info = $mod->new_id;
  293. $status = true;
  294. }
  295. }
  296. break;
  297. case "view all":
  298. $log->url = "index.php?id=".$log->course;
  299. $status = true;
  300. break;
  301. default:
  302. if (!defined('RESTORE_SILENTLY')) {
  303. echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
  304. }
  305. break;
  306. }
  307. if ($status) {
  308. $status = $log;
  309. }
  310. return $status;
  311. }
  312. function resource_restore_files($oldid,$newid,$resource,$restore) {
  313. global $CFG;
  314. $status = true;
  315. $status = check_dir_exists($CFG->dataroot."/".$restore->course_id,true);
  316. // we need to do anything referenced by $resource->reference and anything in moddata/resource/instance
  317. // do referenced files/dirs first.
  318. $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.'/course_files/'.$resource->reference;
  319. if (file_exists($temp_path)) { // ok, it was backed up, restore it.
  320. $new_path = $CFG->dataroot.'/'.$restore->course_id.'/'.$resource->reference;
  321. // if this is somewhere deeply nested we need to do all the structure stuff first.....
  322. $bits = explode('/',$resource->reference);
  323. $newbit = '';
  324. for ($i = 0; $i< count($bits)-1; $i++) {
  325. $newbit .= $bits[$i].'/';
  326. $status = $status && check_dir_exists($CFG->dataroot.'/'.$restore->course_id.'/'.$newbit,true);
  327. }
  328. $status = $status && backup_copy_file($temp_path,$new_path);
  329. }
  330. // and now for moddata.
  331. $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
  332. "/moddata/resource/".$oldid;
  333. if (file_exists($temp_path)) { // there's something to back up, restore it.
  334. $new_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
  335. $status = $status && check_dir_exists($new_path,true);
  336. $new_path .= '/resource';
  337. $status = $status && check_dir_exists($new_path,true);
  338. $new_path .= '/'.$newid;
  339. $status = $status && backup_copy_file($temp_path,$new_path);
  340. }
  341. return $status;
  342. }
  343. ?>