upgrades.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Provides functions for site upgrades performed through XHR
  3. */
  4. elgg.provide('elgg.upgrades');
  5. // The already displayed messages are saved here
  6. elgg.upgrades.errorMessages = [];
  7. elgg.upgrades.init = function () {
  8. $('#upgrade-run').click(elgg.upgrades.run);
  9. };
  10. /**
  11. * Initializes the XHR upgrade feature
  12. *
  13. * @param {Object} e Event object
  14. */
  15. elgg.upgrades.run = function(e) {
  16. e.preventDefault();
  17. // The total amount of items to be upgraded
  18. var total = $('#upgrade-total').text();
  19. // Initialize progressbar
  20. $('.elgg-progressbar').progressbar({
  21. value: 0,
  22. max: total
  23. });
  24. // Replace button with spinner when upgrade starts
  25. $('#upgrade-run').addClass('hidden');
  26. $('#upgrade-spinner').removeClass('hidden');
  27. // Start upgrade from offset 0
  28. elgg.upgrades.upgradeBatch(0);
  29. };
  30. /**
  31. * Fires the ajax action to upgrade a batch of items.
  32. *
  33. * @param {Number} offset The next upgrade offset
  34. */
  35. elgg.upgrades.upgradeBatch = function(offset) {
  36. var options = {
  37. data: {
  38. offset: offset
  39. },
  40. dataType: 'json'
  41. };
  42. options.data = elgg.security.addToken(options.data);
  43. var upgradeCount = $('#upgrade-count');
  44. var action = $('#upgrade-run').attr('href');
  45. options.success = function(json) {
  46. // Append possible errors after the progressbar
  47. if (json.system_messages.error.length) {
  48. // Display only the errors that haven't already been shown
  49. $(json.system_messages.error).each(function(key, message) {
  50. if (jQuery.inArray(message, elgg.upgrades.errorMessages) === -1) {
  51. var msg = '<li class="elgg-message elgg-state-error">' + message + '</li>';
  52. $('#upgrade-messages').append(msg);
  53. // Add this error to the displayed errors
  54. elgg.upgrades.errorMessages.push(message);
  55. }
  56. });
  57. }
  58. // Increase success statistics
  59. var numSuccess = $('#upgrade-success-count');
  60. var successCount = parseInt(numSuccess.text()) + json.output.numSuccess;
  61. numSuccess.text(successCount);
  62. // Increase error statistics
  63. var numErrors = $('#upgrade-error-count');
  64. var errorCount = parseInt(numErrors.text()) + json.output.numErrors;
  65. numErrors.text(errorCount);
  66. // Increase total amount of processed items
  67. var numProcessed = successCount + errorCount;
  68. upgradeCount.text(numProcessed);
  69. // Increase the progress bar
  70. $('.elgg-progressbar').progressbar({ value: numProcessed });
  71. var total = $('#upgrade-total').text();
  72. var percent = 100;
  73. if (numProcessed < total) {
  74. percent = parseInt(numProcessed * 100 / total);
  75. /**
  76. * Start next upgrade call. Offset is the total amount of erros so far.
  77. * This prevents faulty items from causing the same error again.
  78. */
  79. elgg.upgrades.upgradeBatch(errorCount);
  80. } else {
  81. $('#upgrade-spinner').addClass('hidden');
  82. if (errorCount > 0) {
  83. // Upgrade finished with errors. Give instructions on how to proceed.
  84. elgg.register_error(elgg.echo('upgrade:finished_with_errors'));
  85. } else {
  86. // Upgrade is finished. Make one more call to mark it complete.
  87. elgg.action(action, {'upgrade_completed': 1});
  88. elgg.system_message(elgg.echo('upgrade:finished'));
  89. }
  90. }
  91. // Increase percentage
  92. $('#upgrade-counter').text(percent + '%');
  93. };
  94. // We use post() instead of action() so we can catch error messages
  95. // and display them manually underneath the upgrade view.
  96. return elgg.post(action, options);
  97. };
  98. elgg.register_hook_handler('init', 'system', elgg.upgrades.init);