-
Notifications
You must be signed in to change notification settings - Fork 1
/
populate-extras-descriptions.php
199 lines (143 loc) · 4.41 KB
/
populate-extras-descriptions.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
194
195
196
197
198
199
<?php
// skip the first x repos?
$skip = 0;
$current_page = 1;
$api_url = 'https://api.github.com/orgs/habari-extras/repos';
// UPDATE YOUR USERNAME AND PASSWORD HERE
$api_update_url = 'https://username:[email protected]/repos/habari-extras/';
// make sure our repos directory exists
if ( !is_dir( 'habari-extras' ) ) {
mkdir( 'habari-extras' );
}
// move into the directory
chdir( 'habari-extras' );
// set the total number of pages to 1 for now - it'll get updated in the loop
$pages = 1;
$i = 0;
for ( $page = 1; $page <= $pages; $page++ ) {
echo 'Getting page ' . $page . "\n";
$result = get_page( $api_url . '?page=' . $page );
echo 'Got ' . count( $result['items'] ) . ' repos' . "\n";
// make sure we go all the way
$pages = $result['last_page'];
foreach ( $result['items'] as $repo ) {
$i++;
if ( $i < $skip ) {
continue;
}
echo 'Processing ' . $repo->name . "\n";
if ( strpos( $repo->description, 'A mirror of the ' ) === 0 ) {
// they already have a non-default description, so let's just skip over it now that we've made one pass thru
}
if ( !is_dir( $repo->name ) ) {
echo 'Cloning ' . $repo->clone_url . "\n";
// clone the repo locally
exec( 'git clone ' . $repo->clone_url );
}
else {
echo 'Pulling' . "\n";
chdir( $repo->name );
// update it
exec( 'git pull' );
chdir( '../' );
}
// look for a plugin xml file in the new cloned copy
$xml_files = glob( $repo->name . '/{*.plugin.xml,theme.xml}', GLOB_BRACE );
if ( count( $xml_files ) == 0 ) {
echo 'No XML file found for ' . $repo->name . "\n";
continue;
}
// get the xml contents
$xml = file_get_contents( $xml_files[0] );
// read it in with SimpleXML, which is horrible, but gets the job done
$s = new SimpleXMLElement( $xml );
// make sure it has a description
if ( isset( $s->description ) && !empty( $s->description ) ) {
$description = (string)$s->description;
// prefix it
$description = strtoupper( (string)$s['type'] ) . ': ' . $description;
}
else {
echo 'No description in XML for ' . $repo->name . "\n";
continue;
}
// does the repo lack a URL or have a generic one?
if ( !isset( $repo->homepage ) || empty( $repo->homepage ) || $repo->homepage == 'http://habariproject.org' ) {
// then look for that in the XML, too
if ( isset( $s->url ) && !empty( $s->url ) ) {
$url = (string)$s->url;
}
else {
$url = null;
echo 'No URL for ' . $repo->name . "\n";
// we don't continue for this one
}
}
else {
$url = null;
}
// is there actually anything to change?
if ( ( $description != $repo->description ) || ( $url != null && $url != $repo->homepage ) ) {
// update the repo's description and (maybe) URL
update_repo( $repo->name, $description, $url );
}
}
}
function get_page ( $url ) {
$content = file_get_contents( $url );
if ( $content === false ) {
die('Unable to get repo list' );
}
$items = json_decode( $content );
// parse out the next and last page links
foreach ( $http_response_header as $header ) {
if ( strpos( $header, 'Link: ' ) === 0 ) {
preg_match_all( '/page=(\d+)/', $header, $matches );
$next_page = $matches[1][0];
$last_page = $matches[1][1];
echo $last_page . ' total pages' . "\n";
}
}
if ( !isset( $next_page ) || !isset( $last_page ) ) {
echo 'No pages!';
print_r($http_response_header);
echo $items;
die();
}
return array(
'items' => $items,
'next_page' => $next_page,
'last_page' => $last_page,
);
}
function update_repo ( $name, $description, $homepage = null ) {
global $api_update_url;
$url = $api_update_url . $name;
$fields = array(
'name' => $name,
'description' => $description,
);
if ( $homepage != null ) {
$fields['homepage'] = $homepage;
}
$options = array(
'http' => array(
'method' => 'PATCH',
'content' => json_encode( $fields ),
'header' => array(
'Content-Type: application/x-www-form-urlencoded ',
),
),
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$json = json_decode( $result );
// did it fail?
if ( $json->description != $description || ( $homepage != null && $json->homepage != $homepage ) ) {
echo 'Failed updating ' . $name . "\n";
}
else {
echo 'Updated ' . $name . "\n";
}
}
?>