Beispiel #1
0
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
{
$yaml = new Dumper();
$yaml->setIndentation($indent);

return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
}
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, array &$form_state, $key = '')
 {
     // Get the old value
     $old_value = \Drupal::state()->get($key);
     // First we will show the user the content of the variable about to be edited
     $form['old_value'] = array('#type' => 'item', '#title' => t('Old value for %name', array('%name' => $key)), '#markup' => kprint_r($old_value, TRUE));
     // Store in the form the name of the state variable
     $form['state_name'] = array('#type' => 'hidden', '#value' => $key);
     // Only simple structures are allowed to be edited.
     $disabled = !$this->checkObject($old_value);
     // Set the transport format for the new value. Values:
     //  - plain
     //  - yaml
     $form['transport'] = array('#type' => 'hidden', '#value' => 'plain');
     if (is_array($old_value)) {
         $dumper = new Dumper();
         // Set Yaml\Dumper's default indentation for nested nodes/collections to
         // 2 spaces for consistency with Drupal coding standards.
         $dumper->setIndentation(2);
         // The level where you switch to inline YAML is set to PHP_INT_MAX to
         // ensure this does not occur.
         $old_value = $dumper->dump($old_value, PHP_INT_MAX);
         $form['transport']['#value'] = 'yaml';
     }
     $form['new_value'] = array('#type' => 'textarea', '#title' => t('New value'), '#default_value' => $disabled ? '' : $old_value, '#disabled' => $disabled);
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
     $form['actions']['cancel'] = array('#type' => 'link', '#title' => t('Cancel'), '#href' => 'devel/state');
     return $form;
 }
 public function getYaml($entities, $locale)
 {
     // Unflatten array
     $entitiesNested = array();
     foreach ($entities as $entity => $spec) {
         // Legacy support: Don't count *.ss as namespace
         $entity = preg_replace('/\\.ss\\./', '___ss.', $entity);
         $parts = explode('.', $entity);
         $currLevel =& $entitiesNested;
         while ($part = array_shift($parts)) {
             $part = str_replace('___ss', '.ss', $part);
             if (!isset($currLevel[$part])) {
                 $currLevel[$part] = array();
             }
             $currLevel =& $currLevel[$part];
         }
         $currLevel = $spec[0];
     }
     // Write YAML
     $dumper = new Dumper();
     $dumper->setIndentation(2);
     // TODO Dumper can't handle YAML comments, so the context information is currently discarded
     $result = $dumper->dump(array($locale => $entitiesNested), 99);
     return $result;
 }
Beispiel #4
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $config_name = '')
 {
     $data = $this->config($config_name)->get();
     if ($data === FALSE) {
         drupal_set_message(t('Config !name does not exist in the system.', array('!name' => $config_name)), 'error');
         return;
     }
     if (empty($data)) {
         drupal_set_message(t('Config !name exists but has no data.', array('!name' => $config_name)), 'warning');
         return;
     }
     $dumper = new Dumper();
     // Set Yaml\Dumper's default indentation for nested nodes/collections to
     // 2 spaces for consistency with Drupal coding standards.
     $dumper->setIndentation(2);
     // The level where you switch to inline YAML is set to PHP_INT_MAX to
     // ensure this does not occur.
     $output = $dumper->dump($data, PHP_INT_MAX);
     $form['name'] = array('#type' => 'value', '#value' => $config_name);
     $form['value'] = array('#type' => 'item', '#title' => t('Old value for %variable', array('%variable' => $config_name)), '#markup' => dpr($output, TRUE));
     $form['new'] = array('#type' => 'textarea', '#title' => t('New value'), '#default_value' => $output);
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
     $form['actions']['cancel'] = array('#type' => 'link', '#title' => t('Cancel'), '#href' => 'devel/config');
     return $form;
 }
Beispiel #5
0
 /**
  * @param mixed $value
  *
  * @return string
  */
 public function dumpToString($value)
 {
     $dumper = new Dumper();
     $dumper->setIndentation(2);
     $yamlString = $dumper->dump($value, 15);
     return $yamlString;
 }
Beispiel #6
0
 public function asString()
 {
     $jsondom = new \FluentDOM\Serializer\Json($this->_document);
     $dumper = new Dumper();
     $dumper->setIndentation(2);
     $yaml = $dumper->dump(json_decode(json_encode($jsondom), JSON_OBJECT_AS_ARRAY), 10, 0);
     return (string) $yaml;
 }
Beispiel #7
0
 /**
  * Dumps a PHP value to a YAML string.
  *
  * The dump method, when supplied with an array, will do its best
  * to convert the array into friendly YAML.
  *
  * @param mixed $input                  The PHP value
  * @param int   $inline                 The level where you switch to inline YAML
  * @param int   $indent                 The amount of spaces to use for indentation of nested nodes
  * @param bool  $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  * @param bool  $objectSupport          true if object support is enabled, false otherwise
  *
  * @return string A YAML string representing the original PHP value
  */
 public static function dump($input, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
 {
     if ($indent < 1) {
         throw new \InvalidArgumentException('The indentation must be greater than zero.');
     }
     $yaml = new Dumper();
     $yaml->setIndentation($indent);
     return $yaml->dump($input, $inline, 0, $exceptionOnInvalidType, $objectSupport);
 }
Beispiel #8
0
 /**
  * Dumps a PHP array to a YAML string.
  *
  * The dump method, when supplied with an array, will do its best
  * to convert the array into friendly YAML.
  *
  * @param array $array                  PHP array
  * @param int   $inline                 The level where you switch to inline YAML
  * @param int   $indent                 The amount of spaces to use for indentation of nested nodes.
  * @param bool  $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  * @param int   $flags                  A bit field of DUMP_* constants to customize the dumped YAML string
  *
  * @return string A YAML string representing the original PHP array
  */
 public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
 {
     if (is_bool($flags)) {
         @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
         $flags = (int) $flags;
     }
     $yaml = new Dumper();
     $yaml->setIndentation($indent);
     return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
 }
Beispiel #9
0
 /**
  * {@inheritdoc}
  */
 public static function encode($data)
 {
     try {
         $yaml = new Dumper();
         $yaml->setIndentation(2);
         return $yaml->dump($data, PHP_INT_MAX, 0, TRUE, FALSE);
     } catch (\Exception $e) {
         throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
     }
 }
 public function arrayToYaml($data)
 {
     if (is_string($data)) {
         return trim($data);
     } else {
         // Convert data to YAML
         $dumper = new Dumper();
         $dumper->setIndentation(2);
         return trim($dumper->dump($data, PHP_INT_MAX));
     }
 }
Beispiel #11
0
 public function getData()
 {
     if (TRUE === $this->getIsHierarchical()) {
         $yaml = new Dumper();
         $yaml->setIndentation(2);
         return $yaml->dump($this->_prepareHierarchicalCollection(), 5, 0, FALSE, TRUE);
     } else {
         $configSet = $this->_prepareFlatCollection();
         return $this->_generateYaml($configSet);
     }
 }
Beispiel #12
0
 protected static function yamlDecode($file, $aliases = [])
 {
     $code = "alias:\n";
     $aliases['path'] = dirname($file);
     $yaml = new Dumper();
     $yaml->setIndentation(2);
     foreach ($aliases as $key => $value) {
         $code .= '  - &' . $key . "\n" . $yaml->dump($value, PHP_INT_MAX, 4, TRUE, FALSE) . "\n";
     }
     return Yaml::decode($code . file_get_contents($file));
 }
 /**
  * Export translations in to the given file.
  *
  * @param string $file
  * @param array  $translations
  *
  * @return bool
  */
 public function export($file, $translations)
 {
     $bytes = false;
     if (pathinfo($file, PATHINFO_EXTENSION) === 'yml') {
         $ymlDumper = new Dumper();
         $ymlDumper->setIndentation(0);
         $ymlContent = '';
         $ymlContent .= $ymlDumper->dump($translations, 10);
         $bytes = file_put_contents($file, $ymlContent);
     }
     return $bytes !== false;
 }
Beispiel #14
0
 /**
  * Generate a YAML file from an array
  *
  * @param array  $data
  * @param string $outputFile
  *
  * @throws \InvalidArgumentException
  */
 public static function generateYaml(array $data, $outputFile)
 {
     $outputFileDir = dirname($outputFile);
     if (!is_dir($outputFileDir)) {
         throw new \InvalidArgumentException("{$outputFileDir} is not a valid directory (" . __FUNCTION__ . ')');
     }
     $dumper = new YamlDumper();
     $dumper->setIndentation(4);
     $yaml = $dumper->dump($data, 3);
     file_put_contents($outputFile, $yaml);
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function export($file, $translations)
 {
     if ($this->createTree) {
         $result = $this->createMultiArray($translations);
         $translations = $this->flattenArray($result);
     }
     $ymlDumper = new Dumper();
     $ymlDumper->setIndentation(2);
     $ymlContent = $ymlDumper->dump($translations, 10);
     $bytes = file_put_contents($file, $ymlContent);
     return $bytes !== false;
 }
 public function getFrontMatter()
 {
     $yaml = new Dumper();
     $yaml->setIndentation(2);
     $out[] = '---';
     if (!empty($this->getTitle())) {
         $out[] = 'title: ' . $this->getTitle();
     }
     if (!empty($this->front_matter)) {
         $out[] = $yaml->dump($this->front_matter, 3, 0, false, false);
     }
     $out[] = '---';
     return implode($out, PHP_EOL);
 }
 /**
  * @param string $filename
  * @param array  $data
  */
 protected function _write($filename, array $data)
 {
     // Prepare data
     if (true === $this->getIsHierarchical()) {
         $yaml = new Dumper();
         $yaml->setIndentation(2);
         $content = $yaml->dump($data, 5, 0, false, true);
     } else {
         $content = $this->generateYaml($data);
     }
     // Write data to file
     $tmpDirectory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::VAR_DIR);
     $tmpDirectory->writeFile($filename, $content);
     $this->getOutput()->writeln(sprintf('<info>Wrote: %s settings to file %s</info>', count($data), $tmpDirectory->getAbsolutePath($filename)));
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $indentation = 4;
     if ($input->getOption('app-only')) {
         $message = "This is the configuration defined in the app/ directory (not processed):\n";
         $config = $this->getServiceManager()->get('ApplicationConfig');
     } else {
         $message = "This is the configuration in use for your current setup (merged and processed):\n";
         $config = $this->getServiceManager()->get('Config');
     }
     $files = array();
     $contents = array();
     if ($files['php'] = $input->getOption('write-php')) {
         $contents['php'] = "<?php\n\nreturn " . var_export($config, true) . ";\n\n?>\n";
     }
     if ($files['yaml'] = $input->getOption('write-yaml')) {
         $dumper = new Dumper();
         $dumper->setIndentation($indentation);
         $contents['yaml'] = $dumper->dump($config, 6, 0, false, false);
     }
     if (empty($contents)) {
         $dumper = new Dumper();
         $dumper->setIndentation($indentation);
         $output->writeln($message);
         foreach ($config as $rootKey => $subConfig) {
             $output->writeln('<info>' . $rootKey . '</info>:');
             $output->writeln($dumper->dump($subConfig, 6, $indentation, false, false));
         }
         return;
     }
     foreach ($files as $format => $file) {
         $output->write('Saving configuration in <info>' . strtoupper($format) . '</info> format...');
         if ($fileExists = file_exists($file)) {
             if (!isset($dialog)) {
                 $dialog = $this->getHelperSet()->get('dialog');
             }
             if (!$dialog->askConfirmation($output, " <question>File \"" . $file . "\" already exists. Proceed anyway?</question> ", false)) {
                 continue;
             }
         }
         file_put_contents($file, $contents[$format]);
         if (!$fileExists) {
             $output->writeln(' OK.');
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public static function create(array $twig_directories)
 {
     $file_system = new Filesystem();
     $twig_loader = new Twig_Loader_Filesystem($twig_directories);
     $twig = new TwigEnvironment($twig_loader);
     $yaml_dumper = new Dumper();
     $yaml_dumper->setIndentation(2);
     return new static($file_system, $twig, $yaml_dumper);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->users = [];
     $this->filesystem = new Filesystem();
     $this->titleFilter = new TitleFilter();
     $displayIndex = $input->getOption('indexes');
     $displayAuthor = $input->getOption('display-author');
     $maxHops = (int) $input->getOption('max-pages');
     // Maximum number of pages we go through
     $revMaxHops = (int) $input->getOption('max-revs');
     // Maximum number of revisions per page we go through
     $listMissed = $input->getOption('missed');
     $counter = 0;
     // Increment the number of pages we are going through
     $redirects = [];
     $pages = [];
     $urlParts = [];
     $missedIndexes = [];
     $urlsWithContent = [];
     $moreThanHundredRevs = [];
     $translations = [];
     $sanity_redirs = [];
     $directlyOnRoot = [];
     $rev_count = [];
     // So we can know what’s the average
     // Pages we have to make sure aren’t duplicate on the CMS prior
     // to the final migration.
     $temporary_acceptable_duplicates = [];
     //$temporary_acceptable_duplicates[] = 'css/selectors/pseudo-classes/:lang'; // DONE
     if ($listMissed === true) {
         $output->writeln('We are going to try to give you XML indexes to use for --retry=..., we will therefore limit the revision loops to one.');
         $missed_file = DATA_DIR . '/missed.yml';
         if (realpath($missed_file) === false) {
             throw new Exception(sprintf('Could not find missed file at %s', $missed_file));
         }
         $missedFileContents = file_get_contents($missed_file);
         $parser = new Yaml\Parser();
         try {
             $missed = $parser->parse($missedFileContents);
         } catch (Exception $e) {
             throw new Exception(sprintf('Could not get file %s contents to be parsed as YAML. Is it in YAML format?', $missed_file), null, $e);
         }
         if (!isset($missed['missed'])) {
             throw new Exception('Please ensure missed.yml has a list of titles under a "missed:" top level key');
         }
         $revMaxHops = 1;
         $this->missed = $missed['missed'];
     }
     /**
      * Last minute redirects. Order matters.
      */
     $redirects['after'] = 'css/selectors/pseudo-elements/after';
     $redirects['tutorials/What_is_CSS'] = 'tutorials/learning_what_css_is';
     $redirects['html/attributes/type type (a, link, embed)'] = 'html/attributes/type';
     /* -------------------- Author --------------------
      *
      * Author array of MediaWikiContributor objects with $this->users[$uid],
      * where $uid is MediaWiki user_id.
      *
      * You may have to increase memory_limit value,
      * but we’ll load this only once.
      **/
     $users_file = DATA_DIR . '/users.json';
     $users_loop = json_decode(file_get_contents($users_file), 1);
     foreach ($users_loop as &$u) {
         $uid = (int) $u['user_id'];
         $this->users[$uid] = new MediaWikiContributor($u);
         unset($u);
         // Dont fill too much memory, if that helps.
     }
     /* -------------------- /Author -------------------- **/
     /* -------------------- XML source -------------------- **/
     $file = DATA_DIR . '/dumps/main_full.xml';
     $streamer = XmlStringStreamer::createStringWalkerParser($file);
     /* -------------------- /XML source -------------------- **/
     while ($node = $streamer->getNode()) {
         if ($maxHops > 0 && $maxHops === $counter) {
             $output->writeln(sprintf('Reached desired maximum of %d loops', $maxHops) . PHP_EOL . PHP_EOL);
             break;
         }
         $pageNode = new SimpleXMLElement($node);
         if (isset($pageNode->title)) {
             $wikiDocument = new MediaWikiDocument($pageNode);
             $persistable = new GitCommitFileRevision($wikiDocument, 'out/content/', '.md');
             $title = $wikiDocument->getTitle();
             $normalized_location = $wikiDocument->getName();
             $file_path = $this->titleFilter->filter($persistable->getName());
             $redirect_to = $this->titleFilter->filter($wikiDocument->getRedirect());
             // False if not a redirect, string if it is
             $is_translation = $wikiDocument->isTranslation();
             $language_code = $wikiDocument->getLanguageCode();
             $language_name = $wikiDocument->getLanguageName();
             $revs = $wikiDocument->getRevisions()->count();
             $output->writeln(sprintf('"%s":', $title));
             if ($displayIndex === true) {
                 $output->writeln(sprintf('  - index: %d', $counter));
             }
             $output->writeln(sprintf('  - normalized: %s', $normalized_location));
             $output->writeln(sprintf('  - file: %s', $file_path));
             if ($wikiDocument->hasRedirect() === true) {
                 $output->writeln(sprintf('  - redirect_to: %s', $redirect_to));
             } else {
                 $urlsWithContent[] = $title;
                 foreach (explode('/', $normalized_location) as $urlDepth => $urlPart) {
                     $urlPartKey = strtolower($urlPart);
                     $urlParts[$urlPartKey] = $urlPart;
                     $urlPartsAll[$urlPartKey][] = $urlPart;
                 }
             }
             if ($is_translation === true) {
                 $output->writeln(sprintf('  - lang: %s (%s)', $language_code, $language_name));
             }
             if ($listMissed === true && in_array($normalized_location, $this->missed)) {
                 $missedIndexes[$counter] = $title;
             }
             $output->writeln(sprintf('  - revs: %d', $revs));
             $output->writeln(sprintf('  - revisions:'));
             $revList = $wikiDocument->getRevisions();
             $revLast = $wikiDocument->getLatest();
             $revCounter = 0;
             /* ----------- REVISION --------------- **/
             for ($revList->rewind(); $revList->valid(); $revList->next()) {
                 if ($revMaxHops > 0 && $revMaxHops === $revCounter) {
                     $output->writeln(sprintf('    - stop: Reached maximum %d revisions', $revMaxHops) . PHP_EOL . PHP_EOL);
                     break;
                 }
                 $wikiRevision = $revList->current();
                 $revision_id = $wikiRevision->getId();
                 /* -------------------- Author -------------------- **/
                 // An edge case where MediaWiki may give author as user_id 0, even though we dont have it
                 // so we’ll give the first user instead.
                 $contributor_id = $wikiRevision->getContributorId() === 0 ? 1 : $wikiRevision->getContributorId();
                 if (isset($this->users[$contributor_id])) {
                     $contributor = clone $this->users[$contributor_id];
                     // We want a copy, because its specific to here only anyway.
                     $wikiRevision->setContributor($contributor, false);
                 } else {
                     // In case we didn’t find data for $this->users[$contributor_id]
                     $contributor = clone $this->users[1];
                     // We want a copy, because its specific to here only anyway.
                     $wikiRevision->setContributor($contributor, false);
                 }
                 /* -------------------- /Author -------------------- **/
                 $output->writeln(sprintf('    - id: %d', $revision_id));
                 if ($displayIndex === true) {
                     $output->writeln(sprintf('      index: %d', $revCounter));
                 }
                 $persistArgs = $persistable->setRevision($wikiRevision)->getArgs();
                 foreach ($persistArgs as $argKey => $argVal) {
                     if ($argKey === 'message') {
                         $argVal = trim(mb_strimwidth($argVal, strpos($argVal, ': ') + 2, 100));
                     }
                     if ($argKey === 'message' && empty($argVal)) {
                         // Lets not pollute report with empty messages
                         continue;
                     }
                     if ($displayAuthor === false && $argKey === 'author') {
                         continue;
                     }
                     $output->writeln(sprintf('      %s: %s', $argKey, $argVal));
                 }
                 if ($revLast->getId() === $wikiRevision->getId() && $wikiDocument->hasRedirect()) {
                     $output->writeln('      is_last_and_has_redirect: True');
                 }
                 ++$revCounter;
             }
             /* ----------- REVISION --------------- */
             $rev_count[] = $revs;
             // Which pages are directly on /wiki/foo. Are there some we
             // should move elsewhere such as the glossary items?
             if (count(explode('/', $title)) == 1 && $wikiDocument->hasRedirect() === false) {
                 $directlyOnRoot[] = $title;
             }
             if ($revs > 99) {
                 $moreThanHundredRevs[] = sprintf('%s (%d)', $title, $revs);
             }
             if ($is_translation === true && $wikiDocument->hasRedirect() === false) {
                 $translations[] = $title;
             }
             // The ones with invalid URL characters that shouldn’t be part of
             // a page name because they may confuse with their natural use (:,(,),!,?)
             if ($title !== $normalized_location && $wikiDocument->hasRedirect() === false) {
                 $sanity_redirs[$title] = $normalized_location;
             }
             // We have a number of pages, some of them had been
             // deleted or erased with a redirect left behind.
             //
             // Since we want to write to files all pages that currently
             // has content into a filesystem, we have to generate a file
             // name that can be stored into a filesystem. We therefore have
             // to normalize the names.
             //
             // We don’t want to have two entries with the same name.
             //
             // If a redirect (i.e. an empty file) exist, let’s set keep it
             // separate from the pages that still has content.
             //
             // Sanity check;
             // 1. Get list of redirects
             // 2. Get list of pages
             //
             // If we have a page duplicate, throw an exception!
             if ($wikiDocument->hasRedirect() === true) {
                 // Pages we know are redirects within MediaWiki, we won’t
                 // pass them within the $pages aray because they would be
                 // empty content with only a redirect anyway.
                 if ($normalized_location !== $redirect_to) {
                     $redirects[str_replace('_', ' ', $normalized_location)] = $redirect_to;
                 }
             } elseif (!in_array($normalized_location, array_keys($pages))) {
                 // Pages we know has content, lets count them!
                 if ($wikiDocument->hasRedirect() === false) {
                     $pages[$normalized_location] = $title;
                 }
             } elseif (in_array($title, $temporary_acceptable_duplicates)) {
                 // Lets not throw, we got that covered.
             } else {
                 // Hopefully we should never encounter this.
                 $previous = $pages[$normalized_location];
                 $duplicatePagesExceptionText = 'We have duplicate entry for %s it ' . 'would be stored in %s which would override content of %s';
                 throw new Exception(sprintf($duplicatePagesExceptionText, $title, $file_path, $previous));
             }
             $output->writeln(PHP_EOL . PHP_EOL);
             ++$counter;
         }
     }
     /*
      * Work some numbers on number of edits
      *
      * - Average
      * - Median
      */
     $total_edits = 0;
     sort($rev_count);
     $edit_average = array_sum($rev_count) / $counter;
     // Calculate median
     $value_in_middle = floor(($counter - 1) / 2);
     if ($counter % 2) {
         // odd number, middle is the median
         $edit_median = $rev_count[$value_in_middle];
     } else {
         // even number, calculate avg of 2 medians
         $low = $rev_count[$value_in_middle];
         $high = $rev_count[$value_in_middle + 1];
         $edit_median = ($low + $high) / 2;
     }
     $numbers = array('Numbers:');
     $numbers[] = sprintf('  - "iterations": %d', $counter);
     $numbers[] = sprintf('  - "content pages": %d', count($pages));
     $numbers[] = sprintf('  - "redirects": %d', count($redirects));
     $numbers[] = sprintf('  - "translated": %d', count($translations));
     $numbers[] = sprintf('  - "not in a directory": %d', count($directlyOnRoot));
     $numbers[] = sprintf('  - "redirects for URL sanity": %d', count($sanity_redirs));
     $numbers[] = sprintf('  - "edits average": %d', $edit_average);
     $numbers[] = sprintf('  - "edits median": %d', $edit_median);
     $this->filesystem->dumpFile('reports/numbers.txt', implode($numbers, PHP_EOL));
     $this->filesystem->dumpFile('reports/hundred_revs.txt', implode($moreThanHundredRevs, PHP_EOL));
     natcasesort($translations);
     $this->filesystem->dumpFile('reports/translations.txt', implode(PHP_EOL, $translations));
     natcasesort($directlyOnRoot);
     $this->filesystem->dumpFile('reports/directly_on_root.txt', implode(PHP_EOL, $directlyOnRoot));
     natcasesort($urlsWithContent);
     $this->filesystem->dumpFile('reports/url_all.txt', implode(PHP_EOL, $urlsWithContent));
     natcasesort($urlParts);
     $this->filesystem->dumpFile('reports/url_parts.txt', implode(PHP_EOL, $urlParts));
     // Creating list for https://github.com/webplatform/mediawiki-conversion/issues/2
     ksort($urlPartsAll);
     $urlPartsAllOut = array('All words that exists in an URL, and the different ways they are written (needs harmonizing!):');
     foreach ($urlPartsAll as $urlPartsAllKey => $urlPartsAllRow) {
         $urlPartsAllEntryUnique = array_unique($urlPartsAllRow);
         if (count($urlPartsAllEntryUnique) > 1) {
             $urlPartsAllOut[] = sprintf(' - %s', implode(', ', $urlPartsAllEntryUnique));
         }
     }
     $this->filesystem->dumpFile('reports/url_parts_variants.txt', implode(PHP_EOL, $urlPartsAllOut));
     ksort($redirects, SORT_NATURAL | SORT_FLAG_CASE);
     ksort($sanity_redirs, SORT_NATURAL | SORT_FLAG_CASE);
     $nginx_redirects = [];
     $nginx_redirects[] = 'rewrite ^/wiki/((Special|Template|User).*) /disabled?r=$1 permanent;';
     $nginx_redirects[] = 'rewrite ^/w/(.*) /disabled?r=$1 permanent;';
     $nginx_redirects[] = 'rewrite ^/$ /Main_Page permanent;';
     $nginx_redirects[] = 'rewrite ^/wiki/?$ /Main_Page permanent;';
     //                             /wiki/tutorials/canvas/canvas_tutorial
     //$nginx_redirects[] = 'rewrite ^/wiki/canvas/tutorial(.*)$ /wiki/tutorials/canvas$1 permanent;';
     $nginx_redirects[] = 'rewrite ^/wiki/WPD\\:Community$ /community permanent;';
     $nginx_redirects[] = 'rewrite ^/wiki/WPD\\:Contributors_Guide$ /contribute permanent;';
     $nginx_esc[':'] = '\\:';
     $nginx_esc['('] = '\\(';
     $nginx_esc[')'] = '\\)';
     $nginx_esc[','] = '\\,';
     $nginx_esc[' '] = '(\\ |_)';
     // Ordering matter, otherwise the () will be escaped and we want them here!
     $prepare_nginx_redirects = array_merge($sanity_redirs, $redirects);
     foreach ($prepare_nginx_redirects as $url => $redirect_to) {
         // NGINX Case-insensitive redirect? Its done through (?i)! Should be documented!!!
         $nginx_redirects[] = sprintf('rewrite (?i)^/wiki/%s$ /%s permanent;', str_replace(array_keys($nginx_esc), $nginx_esc, $url), $redirect_to);
     }
     $nginx_redirects[] = 'rewrite ^/wiki/(.*) /$1 permanent;';
     // Has to be the last!
     $this->filesystem->dumpFile('reports/nginx_redirects.map', implode(PHP_EOL, $nginx_redirects));
     $sanity_redirects_out = array('URLs to return new Location (from => to):');
     foreach ($sanity_redirs as $title => $sanitized) {
         $sanity_redirects_out[] = sprintf(' - "%s": "%s"', $title, $sanitized);
     }
     $this->filesystem->dumpFile('reports/sanity_redirects.txt', implode(PHP_EOL, $sanity_redirects_out));
     $redirects_out = array('Redirects (from => to):');
     foreach ($redirects as $url => $redirect_to) {
         $redirects_out[] = sprintf(' - "%s": "%s"', $url, $redirect_to);
     }
     $this->filesystem->dumpFile('reports/redirects.txt', implode(PHP_EOL, $redirects_out));
     if ($listMissed === true) {
         $yaml = new Yaml\Dumper();
         $yaml->setIndentation(2);
         try {
             $missed_out = $yaml->dump($missedIndexes, 3, 0, false, false);
         } catch (Exception $e) {
             $missed_out = sprintf('Could not create YAML out of missedIndexes array; Error was %s', $e->getMessage());
         }
         $this->filesystem->dumpFile('reports/missed_retry_argument.txt', 'app/console mediawiki:run 3 --retry=' . implode(',', array_keys($missedIndexes)));
         $this->filesystem->dumpFile('reports/missed_entries.yml', 'Missed:' . PHP_EOL . $missed_out);
         $output->writeln('Created missed_retry_argument.txt and missed_entries.yml in reports/ you can try to recover!');
     }
 }
 /**
  * Execute a batch download
  *
  * @param ProxyQueryInterface $query
  *
  * @return RedirectResponse
  */
 public function batchActionDownload(ProxyQueryInterface $queryProxy)
 {
     $flashType = 'success';
     $dumper = new Dumper();
     $dumper->setIndentation(4);
     $response = new StreamedResponse(function () use($queryProxy, &$flashType, $dumper) {
         try {
             /**
              * @var TransUnit $transUnit
              */
             foreach ($queryProxy->getQuery()->iterate() as $pos => $object) {
                 foreach ($object as $transUnit) {
                     $chunkPrefix = $transUnit->getDomain() . '__' . $transUnit->getKey() . '__' . $transUnit->getId() . '__';
                     $chunk = array();
                     /** @var TranslationInterface $translation */
                     foreach ($transUnit->getTranslations() as $translation) {
                         $chunk[$chunkPrefix . $translation->getLocale()] = $translation->getContent();
                     }
                     echo $dumper->dump($chunk, 2);
                     flush();
                 }
             }
         } catch (\PDOException $e) {
             $flashType = 'error';
             flush();
         } catch (DBALException $e) {
             $flashType = 'error';
             flush();
         }
     });
     $this->addFlash('sonata_flash_' . $flashType, 'translations.flash_batch_download_' . $flashType);
     $response->headers->set('Content-Type', 'text/x-yaml');
     $response->headers->set('Cache-Control', '');
     $response->headers->set('Transfer-Encoding', 'chunked');
     $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s'));
     $contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'translations.yml');
     $response->headers->set('Content-Disposition', $contentDisposition);
     return $response;
 }
Beispiel #22
0
 /**
  * Dumps a PHP array to a YAML string.
  *
  * The dump method, when supplied with an array, will do its best
  * to convert the array into friendly YAML.
  *
  * @param array   $array  PHP array
  * @param integer $inline The level where you switch to inline YAML
  * @param integer $indent The amount of spaces to use for indentation of nested nodes.
  *
  * @return string A YAML string representing the original PHP array
  *
  * @api
  */
 public static function dump($array, $inline = 2, $indent = 4)
 {
     $yaml = new Dumper();
     $yaml->setIndentation($indent);
     return $yaml->dump($array, $inline);
 }
$controller = new GoogleAppsGroupsController($client, $batchWrapper);
if (!file_exists('currentstate.westkingdom.org.yaml')) {
    print "about to fetch\n";
    $existing = $controller->fetch('westkingdom.org');
    $dumper = new Dumper();
    $dumper->setIndentation(2);
    $existingAsYaml = trim($dumper->dump($existing, PHP_INT_MAX));
    file_put_contents('currentstate.westkingdom.org.yaml', $existingAsYaml);
    exit(0);
}
$groupData = file_get_contents(dirname(__FILE__) . "/currentstate.westkingdom.org.yaml");
$currentState = Yaml::parse($groupData);
$groupManager = new Westkingdom\HierarchicalGroupEmail\GroupsManager($controller, $policy, $currentState);
$groupManager->update($newState);
$dumper = new Dumper();
$dumper->setIndentation(2);
/*
$pendingOperations = $batchWrapper->getSimplifiedRequests();
$pendingAsYaml = trim($dumper->dump($pendingOperations, PHP_INT_MAX));
print($pendingAsYaml);
*/
//exit(0);
$groupManager->execute();
$updatedState = $groupManager->export();
$updatedStateAsYaml = trim($dumper->dump($updatedState, PHP_INT_MAX));
print $updatedStateAsYaml;
file_put_contents('currentstate.westkingdom.org.yaml', $updatedStateAsYaml);
exit(0);
// Actually do the update
$batchWrapper->execute();
exit(0);
 /**
  * Internal method that handles writing the data array back to the YML file.
  *
  * @param array $data
  *
  * @return bool True if successful
  */
 protected function write($data)
 {
     $dumper = new Dumper();
     $dumper->setIndentation(4);
     $yaml = $dumper->dump($data, 9999);
     $file = $this->app['resources']->getPath('config/extensions/boltforms.bolt.yml');
     try {
         $response = @file_put_contents($file, $yaml);
     } catch (\Exception $e) {
         $response = null;
     }
     return $response;
 }
 /**
  * @param Request $request
  * @param $name
  * @return array
  */
 public function editAction(Request $request, $name)
 {
     $admin_pool = $this->get('sonata.admin.pool');
     $manager = $this->getManager();
     $menu = $manager->get($name);
     $form = $this->createForm(new CMType(), $menu);
     if ($request->isXmlHttpRequest()) {
         try {
             if ($attributes = $request->get('newitem')) {
                 if ($attributes['path'] != '') {
                     unset($attributes['link']);
                 }
                 $html = $this->render('FMYamlMenuBundle:Visual:_item.twig', array('item' => $attributes));
                 return new JsonResponse(array('html' => $html));
             }
         } catch (YamlMenuException $e) {
             return new JsonResponse(array('status' => 1, 'error' => $e->getMessage()));
         }
         try {
             if ($menus = $request->get('menus') && ($writeLockToken = $request->get('writeLock'))) {
                 // don't proceed if the file was edited in the meantime
                 if ($writeLock != $writeLockToken) {
                     throw new YamlMenuException($writeLock, 1);
                 } else {
                     $dumper = new YamlDumper();
                     $dumper->setIndentation(2);
                     $yaml = $dumper->dump($request->get('menus'), 9999);
                     // clean up dump a little
                     $yaml = preg_replace("~(-)(\n\\s+)~mi", "\$1 ", $yaml);
                     try {
                         $parser = new YamlParser();
                         $parser->parse($yaml);
                     } catch (ParseException $e) {
                         throw new YamlMenuException($writeLock, 2);
                     }
                     // create backup
                     $manager->backup($writeLock);
                     // save
                     $manager->set($menu);
                     //                        if (!@file_put_contents($file, $yaml)) {
                     //                            throw new YamlMenuException($writeLock, 3);
                     //                        }
                     clearstatcache(true, $file);
                     $writeLock = filemtime($file);
                     if (count($request->get('menus')) > 1) {
                         $message = Trans::__("Menus successfully saved");
                     } else {
                         $message = Trans::__("Menu successfully saved");
                     }
                     return new JsonResponse(array('writeLock' => $writeLock, 'status' => 0));
                 }
             }
         } catch (YamlMenuException $e) {
             return new JsonResponse(array('writeLock' => $e->getMessage(), 'status' => $e->getCode()));
         }
     }
     $name = $request->get('name');
     $menus = $manager->get($name);
     return $this->render("FMYamlMenuBUndle:Visual:", array('menus' => $menus, 'admin_pool' => $admin_pool));
 }
Beispiel #26
0
 /**
  * Add some awesomeness to Bolt
  *
  * @return Response|\Symfony\Component\HttpFoundation\JsonResponse
  * @throws \Exception
  */
 public function loadMenuEditor()
 {
     /**
      * check if menu.yml is writable
      */
     $file = BOLT_CONFIG_DIR . '/menu.yml';
     if (@(!is_readable($file)) || !@is_writable($file)) {
         throw new \Exception(__("The file '%s' is not writable. You will have to use your own editor to make modifications to this file.", array('%s' => $file)));
     }
     if (!($writeLock = @filemtime($file))) {
         $writeLock = 0;
     }
     /**
      * try to set symlink to localized readme
      */
     $lastLocale = $this->app['cache']->contains('extension_MenuEditor') ? $this->app['cache']->fetch('extension_MenuEditor') : 'unknown';
     $lastLocale != $this->app['locale'] && @is_writable(__DIR__) ? $this->localizeReadme() : null;
     /**
      * process xhr-post
      */
     if ('POST' == $this->app['request']->getMethod() && true === $this->app['request']->isXmlHttpRequest()) {
         /**
          * restore backup
          */
         try {
             if ($filetime = $this->app['request']->get('filetime')) {
                 if ($this->restoreBackup($filetime)) {
                     $this->app['session']->getFlashBag()->set('success', __('Backup successfully restored'));
                     return $this->app->json(array('status' => 0));
                 }
                 throw new MenuEditorException(__("Backup file could not be found"));
             }
         } catch (MenuEditorException $e) {
             return $this->app->json(array('status' => 1, 'error' => $e->getMessage()));
         }
         /**
          * save menu(s)
          */
         try {
             if ($menus = $this->app['request']->get('menus') && ($writeLockToken = $this->app['request']->get('writeLock'))) {
                 // don't proceed if the file was edited in the meantime
                 if ($writeLock != $writeLockToken) {
                     throw new MenuEditorException($writeLock, 1);
                 } else {
                     $dumper = new YamlDumper();
                     $dumper->setIndentation(2);
                     $yaml = $dumper->dump($this->app['request']->get('menus'), 9999);
                     // clean up dump a little
                     $yaml = preg_replace("~(-)(\n\\s+)~mi", "\$1 ", $yaml);
                     try {
                         $parser = new YamlParser();
                         $parser->parse($yaml);
                     } catch (ParseException $e) {
                         throw new MenuEditorException($writeLock, 2);
                     }
                     // create backup
                     if (true === $this->config['enableBackups']) {
                         $this->backup($writeLock);
                     }
                     // save
                     if (!@file_put_contents($file, $yaml)) {
                         throw new MenuEditorException($writeLock, 3);
                     }
                     clearstatcache(true, $file);
                     $writeLock = filemtime($file);
                     if (count($this->app['request']->get('menus')) > 1) {
                         $message = __("Menus successfully saved");
                     } else {
                         $message = __("Menu successfully saved");
                     }
                     $this->app['session']->getFlashBag()->set('success', $message);
                     return $this->app->json(array('writeLock' => $writeLock, 'status' => 0));
                 }
                 // broken request
                 throw new MenuEditorException($writeLock, 4);
             }
         } catch (MenuEditorException $e) {
             return $this->app->json(array('writeLock' => $e->getMessage(), 'status' => $e->getCode()));
         }
     }
     // add eMenuEditor template namespace to twig
     $this->app['twig.loader.filesystem']->addPath(__DIR__ . '/views/', 'MenuEditor');
     /**
      * load stuff
      */
     $menus = $this->app['config']->get('menu');
     $contenttypes = $this->app['config']->get('contenttypes');
     $taxonomys = $this->app['config']->get('taxonomy');
     foreach ($contenttypes as $cK => $contenttype) {
         $contenttypes[$cK]['records'] = $this->app['storage']->getContent($contenttype['name'], array());
     }
     foreach ($taxonomys as $tK => $taxonomy) {
         $taxonomys[$tK]['me_options'] = array();
         // fetch slugs
         if (isset($taxonomy['behaves_like']) && 'tags' == $taxonomy['behaves_like']) {
             $prefix = $this->app['config']->get('general/database/prefix', "bolt_");
             $taxonomytype = $tK;
             $query = "select distinct `%staxonomy`.`slug` from `%staxonomy` where `taxonomytype` = ? order by `slug` asc;";
             $query = sprintf($query, $prefix, $prefix);
             $query = $this->app['db']->executeQuery($query, array($taxonomytype));
             if ($results = $query->fetchAll()) {
                 foreach ($results as $result) {
                     $taxonomys[$tK]['me_options'][$taxonomy['singular_slug'] . '/' . $result['slug']] = $result['slug'];
                 }
             }
         }
         if (isset($taxonomy['behaves_like']) && 'grouping' == $taxonomy['behaves_like']) {
             foreach ($taxonomy['options'] as $oK => $option) {
                 $taxonomys[$tK]['me_options'][$taxonomy['singular_slug'] . '/' . $oK] = $option;
             }
         }
         if (isset($taxonomy['behaves_like']) && 'categories' == $taxonomy['behaves_like']) {
             foreach ($taxonomy['options'] as $option) {
                 $taxonomys[$tK]['me_options'][$taxonomy['singular_slug'] . '/' . $option] = $option;
             }
         }
     }
     // fetch backups
     $backups = array();
     if (true === $this->config['enableBackups']) {
         try {
             $backups = $this->backup(0, true);
         } catch (MenuEditorException $e) {
             $this->app['session']->getFlashBag()->set('warning', $e->getMessage());
         }
     }
     $body = $this->app['render']->render('@MenuEditor/base.twig', array('contenttypes' => $contenttypes, 'taxonomys' => $taxonomys, 'menus' => $menus, 'writeLock' => $writeLock, 'backups' => $backups));
     return new Response($this->injectAssets($body));
 }