/** * @return ElggEntity */ protected function existingEntity($type, $subtype = ELGG_ENTITIES_ANY_VALUE) { $count = elgg_get_entities(array('type' => $type, 'subtype' => $subtype, 'count' => true)); if ($count < 1) { return false; } $offset = $this->randomNumber() % $count; $options = array('type' => $type, 'subtype' => $subtype, 'offset' => $offset, 'limit' => 1); $entity = elgg_get_entities($options); if ($entity) { return array_shift($entity); } else { if (data_generator::is_cli()) { echo "\nNo entities for ({$type}, {$subtype}) ({$count}, {$offset})"; } return false; } }
<?php elgg_make_sticky_form('data_generator/run'); $amount = get_input('amount'); $profile = get_input('profile'); $locale = get_input('locale'); try { $mt = microtime(true); $success = data_generator::generate($amount, $profile, $locale); $total = microtime(true) - $mt; system_message(elgg_echo('data_generator:action:run:success', array($success, $total))); elgg_clear_sticky_form('data_generator/run'); } catch (Exception $e) { register_error($e->getMessage()); }
/** * @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')));