This repository has been archived by the owner on Mar 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_text_util.php
executable file
·164 lines (148 loc) · 5.29 KB
/
user_text_util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/php
<?php
/* Данный скрипт является ответом на тестовое задание для Hearst Shkulev Digital
* Regional Network
* <https://gist.github.com/ilyachase/c454271ca3c7ecc111b47213289b48a0>.
* © 2019 Антон Карманов <[email protected]>.
*
* Скрипт протесирован на ОС GNU/Linux с использованием интерпретатора PHP 7.3.5.
* Так как в задании не указано иное, при расширении дат принято, что все даты
* относятся к XXI веку.
*
* Затраченное на выполнение задания время: 4 ч 16 мин.
*/
// Globals
/// Name of a csv formatted file with user identifiers and names.
$users_filename = 'people.csv';
/// Relative path to directory with users' files.
$txt_dir = 'texts/';
/// Relative path to directory for replaceDates output.
$txt_output_dir = 'output_texts/';
/// Delimiter used in filenames of users' files.
$filename_delim = '-';
/// Which delimiter supposed to be in file, pointed with $users_filename.
$csv_delim = ';';
/// Default action.
$action = 'countAverageLineCount';
/// Helper message.
$usage_str = "Usage: $argv[0] [comma/semicolon] [countAverageLineCount/replaceDates]" . PHP_EOL;
// Make relative paths absolute.
$cwd = getcwd();
$users_filename = "$cwd/$users_filename";
$txt_dir = "$cwd/$txt_dir";
$txt_output_dir = "$cwd/$txt_output_dir";
/** @brief Count average lines number for user's files.
* @param $files List of valid filenames to parse. Filenames must begins with
* user identifier and delimiter.
* @param $user_id Valid identifier of user. Must be integer.
* @param $dir Path to directory where files keeped.
* @param $delim Delimiter in filenames.
* @return Average number of lines for one user's file.
*/
function countUserAvgLines($files, $user_id, $dir, $delim = '-') {
$files_num = 0;
$lines_num = 0;
$result = 0.0;
foreach ($files as $filename) {
if ((int)explode($delim, $filename)[0] === $user_id) {
$files_num++;
$file = fopen($dir . $filename, 'r');
while (fgets($file)) {
$lines_num++;
}
fclose($file);
}
}
if ($files_num) $result = $lines_num / $files_num;
return $result;
}
/** @brief Count average lines number for user's files for multiple users and
* print result to stdout.
* @param $users Array in format $user_id => $user_name.
* @param $files List of valid filenames to parse. Filenames must begins with
* user identifier and delimiter.
* @param $dir Path to directory where files keeped.
* @param $delim Delimiter in filenames.
*/
function countAverageLineCount($users, $files, $dir, $delim = '-') {
foreach($users as $user_id => $username) {
echo "$username: " . countUserAvgLines($files, $user_id, $dir, $delim) . PHP_EOL;
}
}
/** @brief Change fromat of dates from dd-mm-yy to mm-dd-yyyy.
*
* yy-formatted years will be expanded to 20yy.
* @param $users Array in format $user_id => $user_name.
* @param $files List of valid filenames to process. Filenames must begins with
* user identifier and delimiter.
* @param $input_dir Path to directory where files keeped.
* @param $output_dir Path to put processed files.
* @param $delim Delimiter in filenames.
* @throw Exception When $input_dir is the same as $output_dir.
*/
function replaceDates($users, $files, $input_dir, $output_dir, $delim = '-') {
if ($input_dir == $output_dir) throw New Exception('Can not write files to input directory.');
$users_stat = array();
foreach ($users as $user_id => $username) $users_stat[$user_id] = 0;
if (!file_exists($output_dir)) mkdir($output_dir);
foreach ($files as $filename) {
$user_id = (int)explode($delim, $filename)[0];
if (!isset($users_stat[$user_id])) continue;
$file = fopen($input_dir . $filename, 'r');
$file_new = fopen($output_dir . $filename, 'w');
while ($line = fgets($file)) {
$line = preg_replace('&([0-3]\d)/([0-1]\d)/(\d{2})[^\d]&', '$2-$1-20$3', $line, -1, $counter);
$users_stat[$user_id] += $counter;
fwrite($file_new, $line);
}
fclose($file);
fclose($file_new);
}
foreach ($users as $user_id => $username) {
echo "$username: ${users_stat[$user_id]}" . PHP_EOL;
}
}
// Process command line arguments.
if ($argc > 3) {
echo $usage_str;
exit;
}
for ($i = 1; $i < $argc; $i++) {
$arg = $argv[$i];
switch ($arg) {
case 'comma':
$csv_delim = ',';
break;
case 'semicolon':
$csv_delim = ';';
break;
case 'countAverageLineCount':
$action = 'countAverageLineCount';
break;
case 'replaceDates':
$action = 'replaceDates';
break;
default:
echo $usage_str;
exit;
}
}
// Parse file with users info.
$users_file = fopen($users_filename, 'r');
if (!$users_file) exit;
while ($parsed = fgetcsv($users_file, 0, $csv_delim)) {
$users[(int)$parsed[0]] = $parsed[1];
}
fclose($users_file);
// List files in directory with users' files.
$txt_files = array_diff(scandir($txt_dir), array('..', '.'));
// Perform selected action.
switch ($action) {
case 'countAverageLineCount':
countAverageLineCount($users, $txt_files, $txt_dir, $filename_delim);
break;
case 'replaceDates':
replaceDates($users, $txt_files, $txt_dir, $txt_output_dir, $filename_delim);
break;
}
?>