Elgg  Version 3.0
upgrader.js
Go to the documentation of this file.
1 
10 define(function(require) {
11  var $ = require('jquery');
12  var elgg = require('elgg');
13  var spinner = require('elgg/spinner');
14  var popup = require('elgg/popup');
15 
16  var UNKNOWN_COUNT = -1;
17  var upgrades = $('.elgg-item-object-elgg_upgrade');
18  var upgrade;
19  var guid;
20  var progressbar;
21  var upgradeStartTime;
22  var timer;
23  var counter;
24  var percent;
25  var errorCounter;
26  var errorMessages = [];
27  var numSuccess;
28  var numError;
29  var numProcessed = 0;
30  var total;
31  var messages = [];
32  var messageList;
33  var percentage = 0;
34 
35  // used to fake the progressbar if we don't know the total
36  var FAKE_TOTAL = 1000000;
37  var fakeDone = 0;
38 
45  function init() {
46  // Display the button only if there are pending upgrades
47  if ($('.elgg-item-object-elgg_upgrade').length) {
48  $('#elgg-upgrades-run').removeClass('hidden').click(run);
49  }
50 
51  $(document).on('click', '.elgg-menu-item-run-upgrade > a', runSingle);
52 
53  upgrades.each(function(key, value) {
54  // Initialize progressbar
55  $(value).find('.elgg-progressbar').progressbar();
56  });
57  }
58 
64  function run(e) {
65  e.preventDefault();
66 
67  // Replace button with spinner when upgrade starts
68  $('#elgg-upgrades-run').addClass('hidden');
69  spinner.start();
70 
71  upgrade = upgrades.first();
72  runUpgrade();
73 
74  return false;
75  }
76 
82  function runSingle(e) {
83  var guid = $(this).data().guid;
84 
85  if (!$('#elgg-object-' + guid).length) {
86  return;
87  }
88 
89  spinner.start();
90  upgrade = $('#elgg-object-' + guid);
91 
92  runUpgrade(false);
93 
94  popup.close();
95  }
96 
102  function runUpgrade(advanceToNextWhenDone) {
103 
104  if (typeof advanceToNextWhenDone !== 'boolean') {
105  // IE doesn't support default function params
106  advanceToNextWhenDone = true;
107  }
108 
109  upgrade = $(upgrade);
110  progressbar = upgrade.find('.elgg-progressbar');
111  counter = upgrade.find('.upgrade-counter');
112  percent = upgrade.find('.upgrade-percent');
113  timer = upgrade.find('.upgrade-timer');
114  messageList = upgrade.find('.upgrade-messages');
115  errorCounter = upgrade.find('.upgrade-error-counter');
116  data = upgrade.find('.upgrade-data');
117 
118  // The total amount of items to be upgraded
119  total = data.attr('data-total');
120 
121  // Get the GUID from the element id: elgg-object-123
122  guid = upgrade.attr('id').replace('elgg-object-', '');
123 
124  // Initialize progressbar
125  $(upgrade).find('.elgg-progressbar').progressbar({
126  value: 0,
127  max: (total == UNKNOWN_COUNT) ? FAKE_TOTAL : total
128  });
129 
130  upgradeStartTime = new Date().getTime();
131  percentage = 0;
132 
133  processBatch(advanceToNextWhenDone);
134  }
135 
141  function processBatch(advanceToNextWhenDone) {
142 
143  if (typeof advanceToNextWhenDone !== 'boolean') {
144  // IE doesn't support default function params
145  advanceToNextWhenDone = true;
146  }
147 
148  var options = {
149  data: {guid: guid},
150  dataType: 'json'
151  };
152 
153  options.data = elgg.security.addToken(options.data);
154 
155  options.success = function(json) {
156  // Append possible errors after the progressbar
157  if (json.system_messages.error.length) {
158  // Display only the errors that haven't already been shown
159  $(json.system_messages.error).each(function(key, message) {
160  if ($.inArray(message, errorMessages) === -1) {
161  var msg = '<li>' + message + '</li>';
162  messageList.append(msg);
163  messages.push(message);
164  }
165  });
166  }
167 
168  $(json.output.errors).each(function(key, message) {
169  var msg = '<li>' + message + '</li>';
170  messageList.append(msg);
171  messages.push(message);
172  });
173 
174  numSuccess = parseInt(json.output.numSuccess);
175  numError = parseInt(json.output.numErrors);
176 
177  numProcessed += (numSuccess + numError);
178 
179  // Increase success statistics
180  if (total == UNKNOWN_COUNT) {
181  counter.text(numProcessed + '/???');
182  } else {
183  counter.text(numProcessed + '/' + total);
184  }
185 
186  // Increase the progress bar
187  if (total == UNKNOWN_COUNT) {
188  fakeDone = Math.round(FAKE_TOTAL - (FAKE_TOTAL - fakeDone) / 2);
189  progressbar.progressbar({value: fakeDone});
190  } else {
191  percentage = parseInt(numProcessed * 100 / total);
192  progressbar.progressbar({value: numProcessed});
193  }
194 
195  if (numError > 0) {
196  errorCounter
197  .text(elgg.echo('upgrade:error_count', [messages.length]))
198  .css('color', 'red');
199  }
200 
201  updateCounter();
202 
203  var done;
204  if (total == UNKNOWN_COUNT) {
205  done = json.output.isComplete;
206  } else {
207  done = numProcessed >= total;
208  }
209 
210  if (done) {
211  if (numError > 0) {
212  // Upgrade finished with errors. Give instructions on how to proceed.
213  elgg.register_error(elgg.echo('upgrade:finished_with_errors'));
214  }
215 
216  if (total == UNKNOWN_COUNT) {
217  counter.text(numProcessed + '/' + numProcessed);
218  progressbar.progressbar({value: FAKE_TOTAL});
219  }
220 
221  // Increase percentage
222  percent.html('100%');
223 
224  // Reset all counters
225  numSuccess = numError = numProcessed = percentage = 0;
226  messages = [];
227 
228  if (advanceToNextWhenDone) {
229  // Get next upgrade
230  upgrade = upgrade.next();
231  } else {
232  upgrade = '';
233  }
234 
235  if (upgrade.length) {
236  // Continue to next upgrade
237  runUpgrade(advanceToNextWhenDone);
238  } else {
239  spinner.stop();
240  $('#upgrade-finished').removeClass('hidden');
241  }
242 
243  return;
244  }
245 
246  // carry on...
247  if (total != UNKNOWN_COUNT) {
248  percentage = parseInt(numProcessed * 100 / total);
249  // Increase percentage
250  percent.html(percentage + '%');
251  }
252 
253  // Start next upgrade call
254  processBatch(advanceToNextWhenDone);
255  };
256 
257  // We use post() instead of action() so we can catch error messages
258  // and display them manually underneath the upgrade view.
259  return elgg.post('action/admin/upgrade', options);
260  }
261 
265  function updateCounter() {
266  var now = new Date().getTime();
267 
268  // How many milliseconds ago the last batch was started
269  var difference = (now - upgradeStartTime) / 1000;
270 
271  // How many items are waiting to be processed
272  var unProcessed = total - numProcessed;
273 
274  var timeLeft = Math.round((difference / numProcessed) * unProcessed);
275 
276  if (timeLeft < 60) {
277  var hours = '00';
278  var minutes = '00';
279  var seconds = timeLeft;
280  } else {
281  if (timeLeft < 3600) {
282  var minutes = Math.floor(timeLeft / 60);
283  var seconds = timeLeft % 60;
284  var hours = '00';
285  } else {
286  var hours = Math.floor(timeLeft / 3600);
287  var timeLeft = timeLeft % 3600;
288  var minutes = Math.floor(timeLeft / 60);
289  var seconds = timeLeft % 60;
290  }
291  }
292 
293  hours = formatDigits(hours);
294  minutes = formatDigits(minutes);
295  seconds = formatDigits(seconds);
296 
297  var value = hours + ':' + minutes + ':' + seconds;
298 
299  timer.html(value);
300  }
301 
308  function formatDigits(time) {
309  time = Math.floor(time);
310 
311  if (time < 1) {
312  return '00';
313  }
314 
315  if (time < 10) {
316  return '0' + time;
317  }
318 
319  return time;
320  }
321 
322  init();
323 });
$data value
Definition: default.php:27
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 message
elgg require
Throw an error if the required package isn&#39;t present.
Definition: elgglib.js:164
define(function(require){var $=require('jquery');var elgg=require('elgg');var spinner=require('elgg/spinner');var popup=require('elgg/popup');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 init(){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();});}function run(e){e.preventDefault();$('#elgg-upgrades-run').addClass('hidden');spinner.start();upgrade=upgrades.first();runUpgrade();return false;}function runSingle(e){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}, dataType: 'json'};options.data=elgg.security.addToken(options.data);options.success=function(json){if(json.system_messages.error.length){$(json.system_messages.error).each(function(key, message){if($.inArray(message, errorMessages)===-1){var msg= '< li >'+message+ '</li >';messageList.append(msg);messages.push(message);}});}$(json.output.errors).each(function(key, message){var msg= '< li >'+message+ '</li >';messageList.append(msg);messages.push(message);});numSuccess=parseInt(json.output.numSuccess);numError=parseInt(json.output.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(elgg.echo('upgrade:error_count', [messages.length])).css('color', 'red');}updateCounter();var done;if(total==UNKNOWN_COUNT){done=json.output.isComplete;}else{done=numProcessed >=total;}if(done){if(numError > 0){elgg.register_error(elgg.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();$('#upgrade-finished').removeClass('hidden');}return;}if(total!=UNKNOWN_COUNT){percentage=parseInt(numProcessed *100/total);percent.html(percentage+ '%');}processBatch(advanceToNextWhenDone);};return elgg.post('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;}init();})
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
var elgg
Definition: elgglib.js:4