Skip to content

Instantly share code, notes, and snippets.

@WinterSilence
Last active July 7, 2022 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WinterSilence/a45cfd0b6b470be364404b077ec5737f to your computer and use it in GitHub Desktop.
Save WinterSilence/a45cfd0b6b470be364404b077ec5737f to your computer and use it in GitHub Desktop.
Find outdated documentation translations for Yii 2
<?php
/**
* @param int $pr Identifier of Pull Request
* @param string $repoDir Base directory of repository
* @param string $package "{owner}/{repo}"
* @param string $docDir Repository directory of documentation
* @return array Files to update
*/
function findOutdatedDocTranslations($pr, $repoDir, $package = 'yiisoft/yii2', $docDir = 'docs')
{
$context = \stream_context_create(['http' => ['header' => 'Accept: application/vnd.github.v3+json']]);
$json = \file_get_contents("https://api.github.com/repos/{$package}/pulls/{$pr}/files?per_page=500", $context);
$files = \json_decode($json, true);
$files = \array_column($files, 'filename');
unset($context, $json);
$repoDir = \realpath($repoDir) . '/';
$docPatterns = [];
$translatedDocs = [];
foreach ($files as $file) {
$pattern = '/^' . $docDir . '\/(?<dir>\w+)(-(?<lang>[-a-zA-Z]+))*\/(?<file>[-\w]+\.md)$/';
if (\preg_match($pattern, $file, $matches) === 1) {
if ($matches['lang'] === '') {
$docPatterns[] = $repoDir . $docDir . '/' . $matches['dir'] . '-*/' . $matches['file'];
} else {
$translatedDocs[] = $file;
}
}
}
if ($docPatterns === []) {
return [];
}
$repoDirLength = \strlen($repoDir);
$oldDocs = [];
foreach ($docPatterns as $pattern) {
$files = \glob($pattern, \GLOB_NOSORT);
foreach ($files as $file) {
$path =\substr($file, $repoDirLength);
if (!\in_array($path, $translatedDocs, true)) {
$oldDocs[] = $file;
}
}
}
return $oldDocs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment