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

/mod/lightboxgallery/restorelib.php

https://github.com/nadavkav/RTL-BIDI-Hebrew-Moodle-Plugins
PHP | 243 lines | 199 code | 44 blank | 0 comment | 45 complexity | 7b25b92ebddaf8b6f8883fe031610e1b MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0
  1. <?php
  2. function lightboxgallery_restore_mods($mod, $restore) {
  3. $status = true;
  4. if ($data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id)) {
  5. $info = $data->info;
  6. $gallery->course = $restore->course_id;
  7. $gallery->folder = backup_todb($info['MOD']['#']['FOLDER']['0']['#']);
  8. $gallery->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  9. $gallery->description = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
  10. $gallery->perpage = backup_todb($info['MOD']['#']['PERPAGE']['0']['#']);
  11. $gallery->comments = backup_todb($info['MOD']['#']['COMMENTS']['0']['#']);
  12. $gallery->public = backup_todb($info['MOD']['#']['PUBLIC']['0']['#']);
  13. $gallery->rss = backup_todb($info['MOD']['#']['RSS']['0']['#']);
  14. $gallery->autoresize = backup_todb($info['MOD']['#']['AUTORESIZE']['0']['#']);
  15. $gallery->resize = backup_todb($info['MOD']['#']['RESIZE']['0']['#']);
  16. $gallery->extinfo = backup_todb($info['MOD']['#']['EXTINFO']['0']['#']);
  17. $gallery->timemodified = time();
  18. $newid = insert_record('lightboxgallery', $gallery);
  19. if (! defined('RESTORE_SILENTLY')) {
  20. echo('<li>' . get_string('modulename', 'lightboxgallery') . ' "' . format_string(stripslashes($gallery->name), true) . '"</li>');
  21. }
  22. backup_flush(300);
  23. if ($newid) {
  24. backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
  25. $status = lightboxgallery_restore_files($newid, $restore);
  26. if ($status) {
  27. $status = lightboxgallery_restore_metadata($newid, $info, $restore);
  28. }
  29. } else {
  30. $status = false;
  31. }
  32. } else {
  33. $status = false;
  34. }
  35. return $status;
  36. }
  37. function lightboxgallery_restore_metadata($galleryid, $info, $restore) {
  38. $status = true;
  39. if (isset($info['MOD']['#']['IMAGEMETAS']['0']['#']['IMAGEMETA'])) {
  40. $imagemetas = $info['MOD']['#']['IMAGEMETAS']['0']['#']['IMAGEMETA'];
  41. } else {
  42. $imagemetas = array();
  43. }
  44. for ($i = 0; $i < sizeof($imagemetas); $i++) {
  45. $sub_info = $imagemetas[$i];
  46. $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
  47. $record = new object;
  48. $record->gallery = $galleryid;
  49. $record->image = backup_todb($sub_info['#']['IMAGE']['0']['#']);
  50. $record->metatype = backup_todb($sub_info['#']['METATYPE']['0']['#']);
  51. $record->description = backup_todb($sub_info['#']['DESCRIPTION']['0']['#']);
  52. $newid = insert_record('lightboxgallery_image_meta', $record);
  53. if (($i + 1) % 50 == 0) {
  54. if (!defined('RESTORE_SILENTLY')) {
  55. echo('.');
  56. if (($i + 1) % 1000 == 0) {
  57. echo('<br />');
  58. }
  59. }
  60. backup_flush(300);
  61. }
  62. if ($newid) {
  63. backup_putid($restore->backup_unique_code, 'lightboxgallery_image_meta', $oldid, $newid);
  64. } else {
  65. $status = false;
  66. }
  67. }
  68. return $status;
  69. }
  70. function lightboxgallery_restore_files($gallery, $restore) {
  71. global $CFG;
  72. $status = true;
  73. if (is_numeric($gallery)) {
  74. $gallery = get_record('lightboxgallery', 'id', $gallery);
  75. }
  76. $newpath = $CFG->dataroot . '/' . $gallery->course . '/' . $gallery->folder;
  77. $status = check_dir_exists($newpath, true, true);
  78. if ($status) {
  79. $tmppath = $CFG->dataroot . '/temp/backup/' . $restore->backup_unique_code . '/' . $gallery->folder;
  80. if (is_dir($tmppath)) {
  81. $status = backup_copy_file($tmppath, $newpath);
  82. }
  83. }
  84. return $status;
  85. }
  86. function lightboxgallery_decode_content_links($content, $restore) {
  87. global $CFG;
  88. $result = $content;
  89. $searchstring = '/\$@(GALLERYINDEX)\*([0-9]+)@\$/';
  90. preg_match_all($searchstring, $content, $foundset);
  91. if ($foundset[0]) {
  92. foreach ($foundset[2] as $old_id) {
  93. $rec = backup_getid($restore->backup_unique_code, 'course', $old_id);
  94. $searchstring = '/\$@(GALLERYINDEX)\*('.$old_id.')@\$/';
  95. if (!empty($rec->new_id)) {
  96. $result = preg_replace($searchstring, $CFG->wwwroot.'/mod/lightboxgallery/index.php?id='.$rec->new_id, $result);
  97. } else {
  98. $result = preg_replace($searchstring, $restore->original_wwwroot.'/mod/lightboxgallery/index.php?id='.$old_id, $result);
  99. }
  100. }
  101. }
  102. $searchstring = '/\$@(GALLERYVIEWBYID)\*([0-9]+)@\$/';
  103. preg_match_all($searchstring, $result, $foundset);
  104. if ($foundset[0]) {
  105. foreach($foundset[2] as $old_id) {
  106. $rec = backup_getid($restore->backup_unique_code, 'course_modules', $old_id);
  107. $searchstring = '/\$@(GALLERYVIEWBYID)\*('.$old_id.')@\$/';
  108. if (!empty($rec->new_id)) {
  109. $result = preg_replace($searchstring, $CFG->wwwroot.'/mod/lightboxgallery/view.php?id='.$rec->new_id, $result);
  110. } else {
  111. $result = preg_replace($searchstring, $restore->original_wwwroot.'/mod/lightboxgallery/view.php?id='.$old_id, $result);
  112. }
  113. }
  114. }
  115. $searchstring = '/\$@(GALLERYVIEWBYL)\*([0-9]+)@\$/';
  116. preg_match_all($searchstring, $result, $foundset);
  117. if ($foundset[0]) {
  118. foreach($foundset[2] as $old_id) {
  119. $rec = backup_getid($restore->backup_unique_code, 'lightboxgallery', $old_id);
  120. $searchstring = '/\$@(GALLERYVIEWBYL)\*('.$old_id.')@\$/';
  121. if (!empty($rec->new_id)) {
  122. $result = preg_replace($searchstring, $CFG->wwwroot.'/mod/lightboxgallery/view.php?l='.$rec->new_id, $result);
  123. } else {
  124. $result = preg_replace($searchstring, $restore->original_wwwroot.'/mod/lightboxgallery/view.php?l='.$old_id, $result);
  125. }
  126. }
  127. }
  128. return $result;
  129. }
  130. function lightboxgallery_decode_content_links_caller($restore) {
  131. global $CFG;
  132. $status = true;
  133. if ($galleries = get_records_sql("SELECT l.id, l.description FROM {$CFG->prefix}lightboxgallery l WHERE l.course = $restore->course_id")) {
  134. $i = 0;
  135. foreach ($galleries as $gallery) {
  136. $i++;
  137. $content = $gallery->description;
  138. $result = restore_decode_content_links_worker($content, $restore);
  139. if ($content != $result) {
  140. $gallery->description = addslashes($result);
  141. $status = update_record('lightboxgallery', $gallery);
  142. if (debugging()) {
  143. if (!defined('RESTORE_SILENTLY')) {
  144. echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
  145. }
  146. }
  147. }
  148. if (($i + 1) % 50 == 0) {
  149. if (!defined('RESTORE_SILENTLY')) {
  150. echo('.');
  151. if (($i + 1) % 1000 == 0) {
  152. echo('<br />');
  153. }
  154. }
  155. backup_flush(300);
  156. }
  157. }
  158. }
  159. return $status;
  160. }
  161. function lightboxgallery_restore_logs($restore, $log) {
  162. $status = false;
  163. switch ($log->action) {
  164. case 'view':
  165. case 'comment':
  166. case 'addimage':
  167. case 'editimage':
  168. if ($log->cmid) {
  169. if ($mod = backup_getid($restore->backup_unique_code, $log->module, $log->info)) {
  170. $log->url = 'view.php?id=' . $log->cmid;
  171. $log->info = $mod->new_id;
  172. $status = true;
  173. }
  174. }
  175. break;
  176. case 'search':
  177. if ($log->cmid) {
  178. if ($mod = backup_getid($restore->backup_unique_code, $log->module, $log->info)) {
  179. $log->url = 'search.php?id=' . $log->course . '&l=' . $log->cmid;
  180. $log->info = $mod->new_id;
  181. $status = true;
  182. }
  183. }
  184. break;
  185. case 'view all':
  186. $log->url = 'index.php?id=' . $log->course;
  187. $status = true;
  188. break;
  189. default:
  190. if (!defined('RESTORE_SILENTLY')) {
  191. echo('action ('.$log->module.'-'.$log->action.') unknown. Not restored<br />');
  192. }
  193. }
  194. if ($status) {
  195. $status = $log;
  196. }
  197. return $status;
  198. }
  199. ?>