-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.php
193 lines (170 loc) · 5.9 KB
/
parser.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace glotstats;
function parse( $locale = false, $directory = false, $view = 'top', $exclude = array() ) {
if ( empty( $locale ) || empty( $directory ) ) {
return false;
}
if ( ! defined( 'GLOTSTATS_DEBUG' ) ) {
$url = 'https://translate.wordpress.org/locale/' . $locale . '/default/stats/' . $directory;
$file = wp_remote_get( $url );
if ( is_wp_error( $file ) ) {
return __( 'An error occurred while fetching data from the WordPress directory', 'glotpress-stats' );
}
$file = wp_remote_retrieve_body( $file );
} else {
echo '<h2>[debugging]</h2>';
$file = file_get_contents( plugin_dir_path( __FILE__ ) . '/demo_data/top_' . $directory . '_es.html' );
}
$file = str_replace( '&', '&', $file );
$start = strpos( $file, '<tbody' );
$end = strpos( $file, '</tbody>' );
$file = substr( $file, $start, ( $end - $start ) + 8 );
$xml = new \SimpleXMLElement( $file );
$input = array();
foreach ( $xml as $item ) {
$input[] = array(
'title' => (string) $item->th->a,
'installs' => (int) $item->th['data-sort-value'],
'link' => (string) $item->th->a['href'],
'slug' => (string) preg_replace('/^.*\/([^\/]+)\/?$/', '$1', $item->th->a['href']),
'percent' => (int) $item->td[0]['data-sort-value'],
'language_link' => (string) $item->td[0]->a['href'],
'translated' => (int) $item->td[1]['data-sort-value'],
//'translated_link' => (string) $item->td[1]->a['href'],
'untranslated' => (int) $item->td[2]['data-sort-value'],
'untranslated_link_themes' => (string) $item->td[2]->a['href'],
'untranslated_link_plugins' => (string) $item->th->a['href'] . 'stable/' . $locale . '/default/?filters[status]=untranslated',
'fuzzy' => (int) $item->td[3]['data-sort-value'],
//'fuzzy_link' => (string) $item->td[3]->a['href'],
'waiting' => (int) $item->td[4]['data-sort-value'],
//'waiting_link' => (string) $item->td[4]->a['href'],
);
}
$base_url = 'https://translate.wordpress.org';
$count = count( $input );
if ( 'top' === $view ) {
?>
<div class="stats-table">
<table id="stats-table">
<thead>
<tr>
<th style="width: 60px;">#</th>
<th><?php esc_html_e( 'Name', 'glotpress-stats' ); ?></th>
<th><?php esc_html_e( 'Installs', 'glotpress-stats' ); ?></th>
<th><?php esc_html_e( 'Untranslated', 'glotpress-stats' ); ?></th>
</tr>
</thead>
<tbody>
<?php
} elseif ( 'tasks' === $view ) {
echo '<pre>';
}
$clean = true;
$remaining = array(
'top100' => array(
'projects' => 0,
'strings' => 0,
),
'top200' => array(
'projects' => 0,
'strings' => 0,
),
'total' => array(
'projects' => 0,
'strings' => 0,
),
);
$finished_top = 0;
$printed_tasks = 0;
for ( $i = 0; $i < $count; $i++ ) {
$row = $input[ $i ];
if ( in_array( $row['slug'], $exclude ) ) {
continue;
}
if ( 0 === $row['untranslated'] ) {
if ( $clean ) {
$finished_top = $i + 1;
}
} else {
if ( $i <= 100 ) {
$remaining['top100']['projects']++;
$remaining['top100']['strings'] += $row['untranslated'];
}
if ( $i <= 200 ) {
$remaining['top200']['projects']++;
$remaining['top200']['strings'] += $row['untranslated'];
}
$remaining['total']['projects']++;
$remaining['total']['strings'] += $row['untranslated'];
$untranslated_link = $row[ 'untranslated_link_' . $directory ];
if ( $clean ) {
$clean = false;
}
if ( 'top' === $view ) {
echo '<tr data-slug="' . $row['slug'] .'">' . "\n";
echo '<th>' . esc_html( $i + 1 ) . '</th>';
echo '<th><a href="' . esc_url( $base_url . $row['language_link'] ) . '">' . esc_html( $row['title'] ) . '</a></td>';
echo '<td>' . number_format( $row['installs'], 0, '', '.' ) . '</td>';
echo '<td><a href="' . esc_url( $base_url . $untranslated_link ) . '" rel="nofollow">' . esc_html( $row['untranslated'] ) . '</a></td>';
echo '</tr>' . "\n";
} elseif ( 'tasks' === $view ) {
if ( $printed_tasks < 3 ) {
echo esc_html( '*' . $row['title'] . '* (' . number_format( $row['installs'], 0, '', '.' ) . '+ instalaciones)' ) . "\n";
echo esc_html( $row['untranslated'] . ' cadenas sin traducir' ) . "\n";
echo esc_html( $base_url . $untranslated_link ) . "\n\n";
}
}
$printed_tasks++;
}
}
if ( 'top' === $view ) {
echo '</tbody></table></div>';
} elseif ( 'tasks' === $view ) {
echo '</pre>';
}
echo '<div id="glotpress-stats-overview">';
echo '<h2>' . esc_html( __( 'Overview', 'glotpress-stats' ) ) . '</h2>';
echo '<p>Top' . esc_html( $finished_top ) . ' 💯<br>';
if ( $remaining['top100'] > 0 ) {
echo esc_html(
sprintf(
/* translators: 1. Remaining projects, 2. Remaining strings, 3. Top plugins/themes */
__( '%1$s projects remaining ( %2$s strings) to complete Top %3$s', 'glotpress-stats' ),
$remaining['top100']['projects'],
$remaining['top100']['strings'],
100
)
) . '<br>';
}
if ( $remaining['top200'] > 0 ) {
echo esc_html(
sprintf(
/* translators: 1. Remaining projects, 2. Remaining strings, 3. Top plugins/themes */
__( '%1$s projects remaining ( %2$s strings) to complete Top %3$s', 'glotpress-stats' ),
$remaining['top200']['projects'],
$remaining['top200']['strings'],
200
)
) . '<br>';
}
if ( $printed_tasks > 0 ) {
echo esc_html(
sprintf(
/* translators: 1. Remaining projects, 2. Remaining strings, 3. Top plugins/themes */
__( '%1$s projects remaining ( %2$s strings) to complete Top %3$s', 'glotpress-stats' ),
$remaining['total']['projects'],
$remaining['total']['strings'],
$count
)
);
} else {
echo esc_html(
sprintf(
/* translators: Top plugins/themes */
__( 'All projects in Top %1$s have been translated!', 'glotpress-stats' ),
$count
)
);
}
echo '</p></div>';
}