Elgg  Version 4.3
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 
75  if (typeof advanceToNextWhenDone !== 'boolean') {
76  // IE doesn't support default function params
77  advanceToNextWhenDone = true;
78  }
79 
80  upgrade = $(upgrade);
81  progressbar = upgrade.find('.elgg-progressbar');
82  counter = upgrade.find('.upgrade-counter');
83  percent = upgrade.find('.upgrade-percent');
84  timer = upgrade.find('.upgrade-timer');
85  messageList = upgrade.find('.upgrade-messages');
86  errorCounter = upgrade.find('.upgrade-error-counter');
87  data = upgrade.find('.upgrade-data');
88 
89  // The total amount of items to be upgraded
90  total = data.attr('data-total');
91 
92  // Get the GUID from the element id: elgg-object-123
93  guid = upgrade.attr('id').replace('elgg-object-', '');
94 
95  // Initialize progressbar
96  $(upgrade).find('.elgg-progressbar').progressbar({
97  value: 0,
98  max: (total == UNKNOWN_COUNT) ? FAKE_TOTAL : total
99  });
100 
101  upgradeStartTime = new Date().getTime();
102  percentage = 0;
103 
104  processBatch(advanceToNextWhenDone);
105  }
106 
112  function processBatch(advanceToNextWhenDone) {
113 
114  if (typeof advanceToNextWhenDone !== 'boolean') {
115  // IE doesn't support default function params
116  advanceToNextWhenDone = true;
117  }
118 
119  var options = {
120  data: {
121  guid: guid
122  },
123  error: function(result) {
124  // Append possible errors after the progressbar
125  if (result.system_messages.error.length) {
126  // Display only the errors that haven't already been shown
127  $(result.error).each(function(key, message) {
128  if ($.inArray(message, errorMessages) === -1) {
129  var msg = '<li>' + message + '</li>';
130  messageList.append(msg);
131  messages.push(message);
132  }
133  });
134  }
135 
136  $(result.errors).each(function(key, message) {
137  var msg = '<li>' + message + '</li>';
138  messageList.append(msg);
139  messages.push(message);
140  });
141  },
142  success: function(result) {
143 
144  numSuccess = parseInt(result.numSuccess);
145  numError = parseInt(result.numErrors);
146 
147  numProcessed += (numSuccess + numError);
148 
149  // Increase success statistics
150  if (total == UNKNOWN_COUNT) {
151  counter.text(numProcessed + '/???');
152  } else {
153  counter.text(numProcessed + '/' + total);
154  }
155 
156  // Increase the progress bar
157  if (total == UNKNOWN_COUNT) {
158  fakeDone = Math.round(FAKE_TOTAL - (FAKE_TOTAL - fakeDone) / 2);
159  progressbar.progressbar({value: fakeDone});
160  } else {
161  percentage = parseInt(numProcessed * 100 / total);
162  progressbar.progressbar({value: numProcessed});
163  }
164 
165  if (numError > 0) {
166  errorCounter
167  .text(i18n.echo('upgrade:error_count', [messages.length]))
168  .css('color', 'red');
169  }
170 
171  updateCounter();
172 
173  var done;
174  if (total == UNKNOWN_COUNT || result.isComplete) {
175  done = result.isComplete;
176  } else {
177  done = numProcessed >= total;
178  }
179 
180  if (done) {
181  if (numError > 0) {
182  // Upgrade finished with errors. Give instructions on how to proceed.
183  system_messages.error(i18n.echo('upgrade:finished_with_errors'));
184  }
185 
186  if (total == UNKNOWN_COUNT) {
187  counter.text(numProcessed + '/' + numProcessed);
188  progressbar.progressbar({value: FAKE_TOTAL});
189  }
190 
191  // Increase percentage
192  percent.html('100%');
193 
194  // Reset all counters
195  numSuccess = numError = numProcessed = percentage = 0;
196  messages = [];
197 
198  if (advanceToNextWhenDone) {
199  // Get next upgrade
200  upgrade = upgrade.next();
201  } else {
202  upgrade = '';
203  }
204 
205  if (upgrade.length) {
206  // Continue to next upgrade
207  runUpgrade(advanceToNextWhenDone);
208  } else {
209  spinner.stop();
210  }
211 
212  return;
213  }
214 
215  // carry on...
216  if (total != UNKNOWN_COUNT) {
217  percentage = parseInt(numProcessed * 100 / total);
218  // Increase percentage
219  percent.html(percentage + '%');
220  }
221 
222  // Start next upgrade call
223  processBatch(advanceToNextWhenDone);
224  }
225  };
226 
227  var ajax = new Ajax(false);
228  return ajax.action('admin/upgrade', options);
229  }
230 
234  function updateCounter() {
235  var now = new Date().getTime();
236 
237  // How many milliseconds ago the last batch was started
238  var difference = (now - upgradeStartTime) / 1000;
239 
240  // How many items are waiting to be processed
241  var unProcessed = total - numProcessed;
242 
243  var timeLeft = Math.round((difference / numProcessed) * unProcessed);
244 
245  if (timeLeft < 60) {
246  var hours = '00';
247  var minutes = '00';
248  var seconds = timeLeft;
249  } else {
250  if (timeLeft < 3600) {
251  var minutes = Math.floor(timeLeft / 60);
252  var seconds = timeLeft % 60;
253  var hours = '00';
254  } else {
255  var hours = Math.floor(timeLeft / 3600);
256  var timeLeft = timeLeft % 3600;
257  var minutes = Math.floor(timeLeft / 60);
258  var seconds = timeLeft % 60;
259  }
260  }
261 
262  hours = formatDigits(hours);
263  minutes = formatDigits(minutes);
264  seconds = formatDigits(seconds);
265 
266  var value = hours + ':' + minutes + ':' + seconds;
267 
268  timer.html(value);
269  }
270 
277  function formatDigits(time) {
278  time = Math.floor(time);
279 
280  if (time < 1) {
281  return '00';
282  }
283 
284  if (time < 10) {
285  return '0' + time;
286  }
287 
288  return time;
289  }
290 
291  // Display the button only if there are pending upgrades
292  if ($('.elgg-item-object-elgg_upgrade').length) {
293  $('#elgg-upgrades-run').removeClass('hidden').click(run);
294  }
295 
296  $(document).on('click', '.elgg-menu-item-run-upgrade > a', runSingle);
297 
298  upgrades.each(function(key, value) {
299  // Initialize progressbar
300  $(value).find('.elgg-progressbar').progressbar();
301  });
302 });
$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');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=parseInt(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=parseInt(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);var 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(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