Elgg  Version 5.1
upgrader.js
Go to the documentation of this file.
1 
7 define(['jquery', 'elgg/Ajax', 'elgg/spinner', 'elgg/popup', 'elgg/system_messages', 'elgg/i18n', 'jquery-ui/widgets/progressbar'], function($, Ajax, spinner, popup, system_messages, i18n) {
8 
9  var UNKNOWN_COUNT = -1;
10  var upgrades = $('.elgg-item-object-elgg_upgrade');
11  var upgrade;
12  var guid;
13  var progressbar;
14  var upgradeStartTime;
15  var timer;
16  var counter;
17  var percent;
18  var errorCounter;
19  var errorMessages = [];
20  var numSuccess;
21  var numError;
22  var numProcessed = 0;
23  var total;
24  var messages = [];
25  var messageList;
26  var percentage = 0;
27 
28  // used to fake the progressbar if we don't know the total
29  var FAKE_TOTAL = 1000000;
30  var fakeDone = 0;
31 
37  function run(event) {
38  event.preventDefault();
39 
40  // Replace button with spinner when upgrade starts
41  $('#elgg-upgrades-run').addClass('hidden');
42  spinner.start();
43 
44  upgrade = upgrades.first();
45  runUpgrade();
46 
47  return false;
48  }
49 
53  function runSingle() {
54  var guid = $(this).data().guid;
55 
56  if (!$('#elgg-object-' + guid).length) {
57  return;
58  }
59 
60  spinner.start();
61  upgrade = $('#elgg-object-' + guid);
62 
63  runUpgrade(false);
64 
65  popup.close();
66  }
67 
73  function runUpgrade(advanceToNextWhenDone) {
74  if (typeof advanceToNextWhenDone !== 'boolean') {
75  advanceToNextWhenDone = true;
76  }
77 
78  upgrade = $(upgrade);
79  progressbar = upgrade.find('.elgg-progressbar');
80  counter = upgrade.find('.upgrade-counter');
81  percent = upgrade.find('.upgrade-percent');
82  timer = upgrade.find('.upgrade-timer');
83  messageList = upgrade.find('.upgrade-messages');
84  errorCounter = upgrade.find('.upgrade-error-counter');
85  var data = upgrade.find('.upgrade-data');
86 
87  // The total amount of items to be upgraded
88  total = data.attr('data-total');
89 
90  // Get the GUID from the element id: elgg-object-123
91  guid = upgrade.attr('id').replace('elgg-object-', '');
92 
93  // Initialize progressbar
94  $(upgrade).find('.elgg-progressbar').progressbar({
95  value: 0,
96  max: (total == UNKNOWN_COUNT) ? FAKE_TOTAL : total
97  });
98 
99  upgradeStartTime = new Date().getTime();
100  percentage = 0;
101 
102  processBatch(advanceToNextWhenDone);
103  }
104 
110  function processBatch(advanceToNextWhenDone) {
111 
112  if (typeof advanceToNextWhenDone !== 'boolean') {
113  // IE doesn't support default function params
114  advanceToNextWhenDone = true;
115  }
116 
117  var options = {
118  data: {
119  guid: guid
120  },
121  error: function(result) {
122  // Append possible errors after the progressbar
123  if (result.system_messages.error.length) {
124  // Display only the errors that haven't already been shown
125  $(result.error).each(function(key, message) {
126  if ($.inArray(message, errorMessages) === -1) {
127  var msg = '<li>' + message + '</li>';
128  messageList.append(msg);
129  messages.push(message);
130  }
131  });
132  }
133 
134  $(result.errors).each(function(key, message) {
135  var msg = '<li>' + message + '</li>';
136  messageList.append(msg);
137  messages.push(message);
138  });
139  },
140  success: function(result) {
141 
142  numSuccess = parseInt(result.numSuccess);
143  numError = parseInt(result.numErrors);
144 
145  numProcessed += (numSuccess + numError);
146 
147  // Increase success statistics
148  if (total == UNKNOWN_COUNT) {
149  counter.text(numProcessed + '/???');
150  } else {
151  counter.text(numProcessed + '/' + total);
152  }
153 
154  // Increase the progress bar
155  if (total == UNKNOWN_COUNT) {
156  fakeDone = Math.round(FAKE_TOTAL - (FAKE_TOTAL - fakeDone) / 2);
157  progressbar.progressbar({value: fakeDone});
158  } else {
159  percentage = Math.round(numProcessed * 100 / total);
160  progressbar.progressbar({value: numProcessed});
161  }
162 
163  if (numError > 0) {
164  errorCounter
165  .text(i18n.echo('upgrade:error_count', [messages.length]))
166  .css('color', 'red');
167  }
168 
169  updateCounter();
170 
171  var done;
172  if (total == UNKNOWN_COUNT || result.isComplete) {
173  done = result.isComplete;
174  } else {
175  done = numProcessed >= total;
176  }
177 
178  if (done) {
179  if (numError > 0) {
180  // Upgrade finished with errors. Give instructions on how to proceed.
181  system_messages.error(i18n.echo('upgrade:finished_with_errors'));
182  }
183 
184  if (total == UNKNOWN_COUNT) {
185  counter.text(numProcessed + '/' + numProcessed);
186  progressbar.progressbar({value: FAKE_TOTAL});
187  }
188 
189  // Increase percentage
190  percent.html('100%');
191 
192  // Reset all counters
193  numSuccess = numError = numProcessed = percentage = 0;
194  messages = [];
195 
196  if (advanceToNextWhenDone) {
197  // Get next upgrade
198  upgrade = upgrade.next();
199  } else {
200  upgrade = '';
201  }
202 
203  if (upgrade.length) {
204  // Continue to next upgrade
205  runUpgrade(advanceToNextWhenDone);
206  } else {
207  spinner.stop();
208  }
209 
210  return;
211  }
212 
213  // carry on...
214  if (total != UNKNOWN_COUNT) {
215  percentage = Math.round(numProcessed * 100 / total);
216  // Increase percentage
217  percent.html(percentage + '%');
218  }
219 
220  // Start next upgrade call
221  processBatch(advanceToNextWhenDone);
222  }
223  };
224 
225  var ajax = new Ajax(false);
226  return ajax.action('admin/upgrade', options);
227  }
228 
232  function updateCounter() {
233  var now = new Date().getTime();
234 
235  // How many milliseconds ago the last batch was started
236  var difference = (now - upgradeStartTime) / 1000;
237 
238  // How many items are waiting to be processed
239  var unProcessed = total - numProcessed;
240 
241  var timeLeft = Math.round((difference / numProcessed) * unProcessed);
242 
243  if (timeLeft < 60) {
244  var hours = '00';
245  var minutes = '00';
246  var seconds = timeLeft;
247  } else {
248  if (timeLeft < 3600) {
249  var minutes = Math.floor(timeLeft / 60);
250  var seconds = timeLeft % 60;
251  var hours = '00';
252  } else {
253  var hours = Math.floor(timeLeft / 3600);
254  timeLeft = timeLeft % 3600;
255  var minutes = Math.floor(timeLeft / 60);
256  var seconds = timeLeft % 60;
257  }
258  }
259 
260  hours = formatDigits(hours);
261  minutes = formatDigits(minutes);
262  seconds = formatDigits(seconds);
263 
264  var value = hours + ':' + minutes + ':' + seconds;
265 
266  timer.html(value);
267  }
268 
276  function formatDigits(time) {
277  time = Math.floor(parseInt(time));
278 
279  if (time < 1) {
280  return '00';
281  }
282 
283  if (time < 10) {
284  return '0' + time;
285  }
286 
287  return time;
288  }
289 
290  // Display the button only if there are pending upgrades
291  if ($('.elgg-item-object-elgg_upgrade').length) {
292  $('#elgg-upgrades-run').removeClass('hidden').click(run);
293  }
294 
295  $(document).on('click', '.elgg-menu-item-run-upgrade > a', runSingle);
296 
297  upgrades.each(function(key, value) {
298  // Initialize progressbar
299  $(value).find('.elgg-progressbar').progressbar();
300  });
301 });
$data value
Definition: default.php:27
$result message
and give any other recipients of the Program a copy of this License along with the Program You may charge a fee for the physical act of transferring a and you may at your option offer warranty protection in exchange for a fee You may modify your copy or copies of the Program or any portion of thus forming a work based on the and copy and distribute such modifications or work under the terms of Section provided that you also meet all of these that in whole or in part contains or is derived from the Program or any part to be licensed as a whole at no charge to all third parties under the terms of this License c If the modified program normally reads commands interactively when run
Definition: LICENSE.txt:140
$result error
define(['jquery', 'elgg/Ajax', 'elgg/spinner', 'elgg/popup', 'elgg/system_messages', 'elgg/i18n', 'jquery-ui/widgets/progressbar'], function($, Ajax, spinner, popup, system_messages, i18n){var UNKNOWN_COUNT=-1;var upgrades=$('.elgg-item-object-elgg_upgrade');var upgrade;var guid;var progressbar;var upgradeStartTime;var timer;var counter;var percent;var errorCounter;var errorMessages=[];var numSuccess;var numError;var numProcessed=0;var total;var messages=[];var messageList;var percentage=0;var FAKE_TOTAL=1000000;var fakeDone=0;function run(event){event.preventDefault();$('#elgg-upgrades-run').addClass('hidden');spinner.start();upgrade=upgrades.first();runUpgrade();return false;}function runSingle(){var guid=$(this).data().guid;if(!$('#elgg-object-'+guid).length){return;}spinner.start();upgrade=$('#elgg-object-'+guid);runUpgrade(false);popup.close();}function runUpgrade(advanceToNextWhenDone){if(typeof advanceToNextWhenDone!== 'boolean'){advanceToNextWhenDone=true;}upgrade=$(upgrade);progressbar=upgrade.find('.elgg-progressbar');counter=upgrade.find('.upgrade-counter');percent=upgrade.find('.upgrade-percent');timer=upgrade.find('.upgrade-timer');messageList=upgrade.find('.upgrade-messages');errorCounter=upgrade.find('.upgrade-error-counter');var data=upgrade.find('.upgrade-data');total=data.attr('data-total');guid=upgrade.attr('id').replace('elgg-object-', '');$(upgrade).find('.elgg-progressbar').progressbar({value:0, max:(total==UNKNOWN_COUNT)?FAKE_TOTAL:total});upgradeStartTime=new Date().getTime();percentage=0;processBatch(advanceToNextWhenDone);}function processBatch(advanceToNextWhenDone){if(typeof advanceToNextWhenDone!== 'boolean'){advanceToNextWhenDone=true;}var options={data:{guid:guid}, error:function(result){if(result.system_messages.error.length){$(result.error).each(function(key, message){if($.inArray(message, errorMessages)===-1){var msg= '< li >'+message+ '</li >';messageList.append(msg);messages.push(message);}});}$(result.errors).each(function(key, message){var msg= '< li >'+message+ '</li >';messageList.append(msg);messages.push(message);});}, success:function(result){numSuccess=parseInt(result.numSuccess);numError=parseInt(result.numErrors);numProcessed+=(numSuccess+numError);if(total==UNKNOWN_COUNT){counter.text(numProcessed+ '/?^);}else{counter.text(numProcessed+ '/'+total);}if(total==UNKNOWN_COUNT){fakeDone=Math.round(FAKE_TOTAL-(FAKE_TOTAL-fakeDone)/2);progressbar.progressbar({value:fakeDone});}else{percentage=Math.round(numProcessed *100/total);progressbar.progressbar({value:numProcessed});}if(numError > 0){errorCounter.text(i18n.echo('upgrade:error_count', [messages.length])).css('color', 'red');}updateCounter();var done;if(total==UNKNOWN_COUNT||result.isComplete){done=result.isComplete;}else{done=numProcessed >=total;}if(done){if(numError > 0){system_messages.error(i18n.echo('upgrade:finished_with_errors'));}if(total==UNKNOWN_COUNT){counter.text(numProcessed+ '/'+numProcessed);progressbar.progressbar({value:FAKE_TOTAL});}percent.html('100%');numSuccess=numError=numProcessed=percentage=0;messages=[];if(advanceToNextWhenDone){upgrade=upgrade.next();}else{upgrade= '';}if(upgrade.length){runUpgrade(advanceToNextWhenDone);}else{spinner.stop();}return;}if(total!=UNKNOWN_COUNT){percentage=Math.round(numProcessed *100/total);percent.html(percentage+ '%');}processBatch(advanceToNextWhenDone);}};var ajax=new Ajax(false);return ajax.action('admin/upgrade', options);}function updateCounter(){var now=new Date().getTime();var difference=(now-upgradeStartTime)/1000;var unProcessed=total-numProcessed;var timeLeft=Math.round((difference/numProcessed)*unProcessed);if(timeLeft< 60){var hours= '00';var minutes= '00';var seconds=timeLeft;}else{if(timeLeft< 3600){var minutes=Math.floor(timeLeft/60);var seconds=timeLeft%60;var hours= '00';}else{var hours=Math.floor(timeLeft/3600);timeLeft=timeLeft%3600;var minutes=Math.floor(timeLeft/60);var seconds=timeLeft%60;}}hours=formatDigits(hours);minutes=formatDigits(minutes);seconds=formatDigits(seconds);var value=hours+ ':'+minutes+ ':'+seconds;timer.html(value);}function formatDigits(time){time=Math.floor(parseInt(time));if(time< 1){return '00';}if(time< 10){return '0'+time;}return time;}if($('.elgg-item-object-elgg_upgrade').length){$('#elgg-upgrades-run').removeClass('hidden').click(run);}$(document).on('click', '.elgg-menu-item-run-upgrade > a', runSingle);upgrades.each(function(key, value){$(value).find('.elgg-progressbar').progressbar();});})
Javascript that takes care of running batch upgrades.
Bundled plugins(the contents of the"/mod"directory) are available only under the GPLv2 license.The remainder of the project is available under either MIT or GPLv2.Both licenses can be found below.More info and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed on
Definition: LICENSE.txt:96