/* * Process input parameters */ //parse parameters $options = getopt('a:p:l:', array('amount:', 'profile:', 'locale:')); $amount = elgg_extract('amount', $options, elgg_extract('a', $options)); $profile = elgg_extract('profile', $options, elgg_extract('p', $options)); $locale = elgg_extract('locale', $options, elgg_extract('l', $options)); // var_dump($amount, $profile, $locale); //fail on missing required if (!in_array($profile, data_generator::getElggProviderMethods())) { echo "Invalid profile specified! Valid are: " . implode(', ', data_generator::getElggProviderMethods()) . "\n"; exit(1); } if (!in_array($locale, data_generator::getLocales())) { echo "Invalid locale specified! Valid are: " . implode(', ', data_generator::getLocales()) . "\n"; exit(2); } if ($amount <= 0) { echo "Invalid amount specified! It must be positive number\n"; exit(3); } /* * Environment settings */ echo "Adjusting settings...\n"; if (ini_set('memory_limit', -1) === false) { echo "Failed to change memory limit!\n"; } if (ini_get('max_execution_time') != 0 && ini_set('max_execution_time', 0) === false) { echo "Failed to change execution time limit!\n";
/** * @param int $amount * @param string $profile * @param string $locale * @throws InvalidArgumentException * @return int */ static function generate($amount, $profile, $locale) { $amount = (int) $amount; if ($amount <= 0) { throw new InvalidArgumentException("Amount must be positive integer."); } if (!in_array($profile, data_generator::getElggProviderMethods())) { throw new InvalidArgumentException("Invalid profile provided: {$profile}"); } if (!in_array($locale, data_generator::getLocales())) { throw new InvalidArgumentException("Invalid locale provided: {$locale}"); } $totalAmount = $amount; $mt = microtime(true); $time = null; $generator = self::getGenerator($locale); $success = 0; $processed = 0; while ($amount-- > 0) { $processed++; $data = $generator->{$profile}(); try { if (is_callable(array($data, 'save'))) { if ($data->save()) { $success++; } else { if (self::is_cli()) { //clear line echo "\t\t\t\t\t\t\t\t\t\r"; echo "Fail: Error while saving\n"; $messages = system_messages(NULL, "error"); echo "Msgs: " . var_export($messages['error'], true) . "\n"; } } } else { if (self::is_cli()) { //clear line echo "\t\t\t\t\t\t\t\t\t\r"; echo "Fail: Not saveable item: " . var_export($data, true) . "\n"; } } } catch (Exception $e) { if (self::is_cli()) { //clear line echo "\t\t\t\t\t\t\t\t\t\r"; echo "Fail: " . $e->getMessage() . "\n"; } //fail silently here - just count } if (self::is_cli()) { if ($time === null || time() > $time + self::$cli_info_interval) { $time = time(); echo sprintf("%.2f%% - %d items generated, %d failures in %.2fs\r", ($totalAmount - $amount) * 100 / $totalAmount, $success, $processed - $success, microtime(true) - $mt); } } } if (self::is_cli()) { //clear line echo "\t\t\t\t\t\t\t\t\t\r"; } return $success; }
<?php echo '<p>'; echo '<label>' . elgg_echo('admin:data_generator:amount') . '</label>'; echo elgg_view('input/text', array('name' => 'amount', 'placeholder' => elgg_echo('admin:data_generator:amount:placeholder'), 'value' => elgg_get_sticky_value('data_generator/run', 'amount'))); echo '</p>'; echo '<p>'; echo '<label>' . elgg_echo('admin:data_generator:profile') . '</label>'; echo elgg_view('input/dropdown', array('name' => 'profile', 'options' => data_generator::getElggProviderMethods(), 'value' => elgg_get_sticky_value('data_generator/run', 'profile'))); echo '</p>'; echo '<p>'; echo '<label>' . elgg_echo('admin:data_generator:locale') . '</label>'; echo elgg_view('input/dropdown', array('name' => 'locale', 'options' => data_generator::getLocales(), 'value' => elgg_get_sticky_value('data_generator/run', 'locale', 'en_US'))); echo '</p>'; echo elgg_view('input/submit', array('name' => 'submit', 'value' => elgg_echo('admin:data_generator:submit')));