/mod/data/db/upgrade.php

https://github.com/raymanuk/moodle · PHP · 140 lines · 67 code · 17 blank · 56 comment · 10 complexity · 093b45b14577a62d749b10be289089b4 MD5 · raw file

  1. <?php
  2. // This file keeps track of upgrades to
  3. // the data module
  4. //
  5. // Sometimes, changes between versions involve
  6. // alterations to database structures and other
  7. // major things that may break installations.
  8. //
  9. // The upgrade function in this file will attempt
  10. // to perform all the necessary actions to upgrade
  11. // your older installation to the current version.
  12. //
  13. // If there's something it cannot do itself, it
  14. // will tell you what you need to do.
  15. //
  16. // The commands in here will all be database-neutral,
  17. // using the methods of database_manager class
  18. //
  19. // Please do not forget to use upgrade_set_timeout()
  20. // before any action that may take longer time to finish.
  21. function xmldb_data_upgrade($oldversion) {
  22. global $CFG, $DB, $OUTPUT;
  23. $dbman = $DB->get_manager();
  24. // Moodle v2.2.0 release upgrade line
  25. // Put any upgrade step following this
  26. // Moodle v2.3.0 release upgrade line
  27. // Put any upgrade step following this
  28. if ($oldversion < 2012112901) {
  29. // Check if there is a directory containing any old presets.
  30. $olddatadir = $CFG->dataroot . '/data';
  31. $oldpresetdir = "$olddatadir/preset";
  32. if (file_exists($oldpresetdir)) {
  33. // Get directory contents.
  34. $userfolders = new DirectoryIterator($oldpresetdir);
  35. // Store the system context, these are site wide presets.
  36. $context = context_system::instance();
  37. // Create file storage object.
  38. $fs = get_file_storage();
  39. // Create array of accepted files.
  40. $arracceptedfilenames = array('singletemplate.html', 'listtemplateheader.html', 'listtemplate.html',
  41. 'listtemplatefooter.html', 'addtemplate.html', 'rsstemplate.html',
  42. 'rsstitletemplate.html', 'csstemplate.css', 'jstemplate.js',
  43. 'asearchtemplate.html', 'preset.xml');
  44. // Loop through all the folders, they should represent userids.
  45. foreach ($userfolders as $userfolder) {
  46. // If it is a file, skip it.
  47. if ($userfolder->isFile()) {
  48. continue;
  49. }
  50. // The folder name should represent the user id.
  51. $userid = $userfolder->getFilename();
  52. // Skip if it is not numeric.
  53. if (!is_numeric($userid)) {
  54. continue;
  55. }
  56. // Skip if the number does not correspond to a user (does not matter if user was deleted).
  57. if (!$DB->record_exists('user', array('id' => $userid))) {
  58. continue;
  59. }
  60. // Open this folder.
  61. $presetfolders = new DirectoryIterator("$oldpresetdir/$userid");
  62. foreach ($presetfolders as $presetfolder) {
  63. // If it is a file, skip it.
  64. if ($presetfolder->isFile()) {
  65. continue;
  66. }
  67. // Save the name of the preset.
  68. $presetname = $presetfolder->getFilename();
  69. // Get the files in this preset folder.
  70. $presetfiles = new DirectoryIterator("$oldpresetdir/$userid/$presetname");
  71. // Now we want to get the contents of the presets.
  72. foreach ($presetfiles as $file) {
  73. // If it is not a file, skip it.
  74. if (!$file->isFile()) {
  75. continue;
  76. }
  77. // Set the filename.
  78. $filename = $file->getFilename();
  79. // If it is not in the array of accepted file names skip it.
  80. if (!in_array($filename, $arracceptedfilenames)) {
  81. continue;
  82. }
  83. // Store the full file path.
  84. $fullfilepath = "$oldpresetdir/$userid/$presetname/$filename";
  85. // Create file record.
  86. $filerecord = array('contextid' => $context->id,
  87. 'component' => 'mod_data',
  88. 'filearea' => 'site_presets',
  89. 'itemid' => 0,
  90. 'filename' => $filename,
  91. 'userid' => $userid);
  92. // Check to ensure it does not already exists in the file directory.
  93. if (!$fs->file_exists($context->id, 'mod_data', 'site_presets', 0, '/' . $presetfolder . '/', $filename)) {
  94. $filerecord['filepath'] = '/' . $presetfolder . '/';
  95. } else {
  96. $filerecord['filepath'] = '/' . $presetfolder . '_' . $userid . '_old/';
  97. }
  98. $fs->create_file_from_pathname($filerecord, $fullfilepath);
  99. // Remove the file.
  100. @unlink($fullfilepath);
  101. }
  102. // Remove the preset directory.
  103. @rmdir("$oldpresetdir/$userid/$presetname");
  104. }
  105. // Remove the user directory.
  106. @rmdir("$oldpresetdir/$userid");
  107. }
  108. // Remove the final directories.
  109. @rmdir("$oldpresetdir");
  110. @rmdir("$olddatadir");
  111. }
  112. upgrade_mod_savepoint(true, 2012112901, 'data');
  113. }
  114. // Moodle v2.4.0 release upgrade line
  115. // Put any upgrade step following this
  116. // Moodle v2.5.0 release upgrade line.
  117. // Put any upgrade step following this.
  118. // Moodle v2.6.0 release upgrade line.
  119. // Put any upgrade step following this.
  120. // Moodle v2.7.0 release upgrade line.
  121. // Put any upgrade step following this.
  122. return true;
  123. }