-
Notifications
You must be signed in to change notification settings - Fork 1
/
phabricator-feed.php
329 lines (257 loc) · 12.2 KB
/
phabricator-feed.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Plugin Name: Phabricator Feed Importer
* Plugin URI: https://github.com/infoaed/phabricator-feed
* Description: This imports feeds from Phabricator workflow management.
* Version: 1.1
* Author: Märt Põder
* Author URI: http://gafgaf.infoaed.ee/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* GitHub Plugin URI: https://github.com/infoaed/phabricator-feed
* GitHub Branch: master
**/
// prereq in feed importer main directory
// git clone https://github.com/phacility/libphutil.git
// also need: php-curl
require_once 'libphutil/src/__phutil_library_init__.php';
global $wpdb;
global $table_name_feed, $table_name_tags, $table_name_map;
$table_name_feed = $wpdb->prefix . 'phabricator_feed_recs';
$table_name_tags = $wpdb->prefix . 'phabricator_feed_tags';
$table_name_map = $wpdb->prefix . 'phabricator_feed_map';
function phabricator_feed_get_data() {
global $wpdb;
global $table_name_feed, $table_name_tags, $table_name_map;
$options = get_option("phabricator_feed_import");
try {
$conduit_token = $options["conduit_token"];
$conduit_uri = new PhutilURI($options['conduit_uri']);
$conduit_host = (string) $conduit_uri->setPath('/');
$conduit_uri = (string) $conduit_uri->setPath('/api/');
$conduit = new ConduitClient($conduit_uri);
$conduit->setConduitToken($conduit_token);
// "projects": [ "WMEE" ], "statuses": ["open"]
$tasks = array();
$tags = array();
$param = ['constraints' => ['projects' => [$options["source_project"]], 'statuses' => ['open']], 'attachments' => [ 'projects' => 'true', 'columns' => 'true' ]];
$tasks = $conduit->callMethodSynchronous('maniphest.search', $param);
$delete = $wpdb->query("TRUNCATE TABLE $table_name_feed");
$delete = $wpdb->query("TRUNCATE TABLE $table_name_tags");
$delete = $wpdb->query("TRUNCATE TABLE $table_name_map");
if (count($tasks) > 0) {
foreach ($tasks['data'] as $task) {
$tasks_sql[] = $wpdb->prepare("(%d,%s)", $task['id'], $task['fields']['name']);
foreach ($task['attachments']['projects']['projectPHIDs'] as $tag) {
$tasks_map_sql[] = $wpdb->prepare("(%d,%s)", $task['id'], $tag);
$project_phids[] = $tag;
}
}
$param = ['constraints' => ['phids' => $project_phids]];
$projects = $conduit->callMethodSynchronous('project.search', $param);
if (count($projects) > 0) {
foreach ($projects['data'] as $tag) {
$slug = $tag['fields']['slug'];
if (strlen($slug) == 0) $slug = str_replace(" ", "_", strtolower($tag['fields']['name']));
$tags_sql[] = $wpdb->prepare("(%s,%s,%s)", $tag['phid'], $tag['fields']['name'], $slug);
$tags[$tag['phid']] = $slug;
}
}
foreach ($tasks['data'] as $task) {
foreach ($task['attachments']['columns']['boards'] as $project_id => $tag) {
$project_slug = str_replace(" ", "_", strtolower($tags[$project_id]));
foreach ($tag['columns'] as $col) {
$tasks_map_sql[] = $wpdb->prepare("(%d,%s)", $task['id'], $col['phid']);
if (!in_array($col['id'], isset($cols) ? $cols : array())) {
$slug = $project_slug . "_" . str_replace(" ", "_", strtolower($col['name']));
$tags_sql[] = $wpdb->prepare("(%s,%s,%s)", $col['phid'], $col['name'], $slug);
$cols[] = $col['id'];
}
}
}
}
$tags_sql = implode( ",\n", $tags_sql );
$query = "INSERT INTO $table_name_tags (id, name, slug) VALUES {$tags_sql}";
$wpdb->query($query);
$tasks_sql = implode( ",\n", $tasks_sql );
$query = "INSERT INTO $table_name_feed (id, name) VALUES {$tasks_sql}";
$wpdb->query($query);
$tasks_map_sql = implode( ",\n", $tasks_map_sql );
$query = "INSERT INTO $table_name_map (rec_id, tag_id) VALUES {$tasks_map_sql}";
$wpdb->query($query);
$options["last_updated"] = current_time("timestamp");
update_option("phabricator_feed_import", $options);
add_shortcodes();
}
} catch (ConduitClientException $ex) {
error_log($ex);
}
}
function add_shortcodes() {
global $wpdb;
global $table_name_tags;
$result = $wpdb->get_results ( "
SELECT DISTINCT $table_name_tags.slug
FROM $table_name_tags
" );
if (count($result) > 0) {
foreach ( $result as $project ) {
add_shortcode("phabricator_feed_".$project->slug, function() use ($project) { return wp_feed_shortcode($project->slug); });
}
}
}
function wp_feed_shortcode($slug) {
global $wpdb;
global $table_name_feed, $table_name_tags, $table_name_map;
$options = get_option("phabricator_feed_import");
if ($options['last_updated'] + $options['update_interval'] < current_time("timestamp")) {
phabricator_feed_get_data();
}
$result = $wpdb->get_results ( "
SELECT $table_name_feed.id AS id, $table_name_feed.name, GROUP_CONCAT($table_name_tags.name) AS tag
FROM $table_name_map
INNER JOIN $table_name_tags ON $table_name_tags.id = $table_name_map.tag_id
INNER JOIN $table_name_feed ON $table_name_feed.id = $table_name_map.rec_id
WHERE $table_name_tags.slug IN ('$slug')
GROUP BY id
" );
$text = "";
if (count($result) > 0) {
$text .= "<ul class=\"". $slug. "-phabricator\">";
foreach ( $result as $page ) {
$text .= "<li><a href=\"https://phabricator.wikimedia.org/T" . $page->id . "\">" . $page->name . "</a></li>\n";
}
$text .= "</ul>";
} else {
$text .= "<ul class=\"". $slug. "-phabricator-none\">";
$text .= "</ul>";
}
return $text;
}
register_activation_hook( __FILE__, 'phabricator_feed_install' );
register_activation_hook( __FILE__, 'phabricator_feed_get_data' );
register_uninstall_hook(__FILE__, 'phabricator_feed_uninstall');
add_shortcodes();
function phabricator_feed_install() {
global $wpdb;
global $table_name_feed, $table_name_tags, $table_name_map;
$charset_collate = $wpdb->get_charset_collate();
$sql = array();
$sql[] = "CREATE TABLE $table_name_feed (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$sql[] = "CREATE TABLE $table_name_tags (
id varchar(50) NOT NULL,
name tinytext NOT NULL,
slug tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$sql[] = "CREATE TABLE $table_name_map (
tag_id varchar(50) NOT NULL,
rec_id mediumint(9) NOT NULL
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
$default_interval = 100;
add_option("phabricator_feed_import", Array("update_interval" => $default_interval, "conduit_uri" => "https://phabricator.wikimedia.org/api/", "conduit_token" => "api-token_which_starts_with_api", "last_updated" => current_time("timestamp")-$default_interval, "source_project" => "WMEE"));
}
function phabricator_feed_uninstall() {
global $wpdb;
global $table_name_feed, $table_name_tags, $table_name_map;
$table_name_feed = $wpdb->prefix . 'phabricator_feed_recs';
$table_name_tags = $wpdb->prefix . 'phabricator_feed_tags';
$table_name_map = $wpdb->prefix . 'phabricator_feed_map';
delete_option('phabricator_feed_import');
$wpdb->query("DROP TABLE IF EXISTS {$table_name_map}");
$wpdb->query("DROP TABLE IF EXISTS {$table_name_tags}");
$wpdb->query("DROP TABLE IF EXISTS {$table_name_feed}");
}
/* the rest of it is settings */
function phabricator_feed_options_init_fn(){
register_setting("phabricator_feed_import", "phabricator_feed_import", 'phabricator_feed_options_validate' );
add_settings_section('default', 'Basic settings', 'section_text_fn', "phabricator_feed_import");
add_settings_field('source_project', 'Source project', 'setting_source_project_fn', "phabricator_feed_import");
add_settings_field('update_interval', 'Update interval (minutes)', 'setting_update_interval_fn', "phabricator_feed_import");
add_settings_field('last_updated', 'Last updated', 'setting_last_updated_fn', "phabricator_feed_import");
add_settings_field('conduit_uri', 'Conduit URI', 'setting_conduit_uri_fn', "phabricator_feed_import");
add_settings_field('conduit_token', 'Conduit token', 'setting_conduit_token_fn', "phabricator_feed_import");
}
function section_text_fn() {
}
function setting_update_interval_fn() {
$options = get_option("phabricator_feed_import");
echo "<input id='update_interval' name='phabricator_feed_import[update_interval]' size='10' type='text' value='{$options['update_interval']}' />";
}
function setting_conduit_token_fn() {
$options = get_option("phabricator_feed_import");
echo "<input id='conduit_token' name='phabricator_feed_import[conduit_token]' size='40' type='text' value='{$options['conduit_token']}' />";
}
function setting_conduit_uri_fn() {
$options = get_option("phabricator_feed_import");
echo "<input id='conduit_uri' name='phabricator_feed_import[conduit_uri]' size='40' type='text' value='{$options['conduit_uri']}' />";
}
function setting_source_project_fn() {
$options = get_option("phabricator_feed_import");
echo "<input id='source_project' name='phabricator_feed_import[source_project]' size='40' type='text' value='{$options['source_project']}' />";
}
function setting_last_updated_fn() {
$options = get_option("phabricator_feed_import");
echo "<input id='last_updated' name='phabricator_feed_import[last_updated]' size='40' type='text' value='{$options['last_updated']}' />";
}
function phabricator_feed_options_validate($input) {
$new_input = array();
if( isset( $input['conduit_token'] ) )
$new_input['conduit_token'] = sanitize_text_field( $input['conduit_token'] );
if( isset( $input['conduit_uri'] ) )
$new_input['conduit_uri'] = sanitize_text_field( $input['conduit_uri'] );
if( isset( $input['source_project'] ) )
$new_input['source_project'] = sanitize_text_field( $input['source_project'] );
if( isset( $input['update_interval'] ) )
$new_input['update_interval'] = absint( $input['update_interval'] );
if( isset( $input['last_updated'] ) )
$new_input['last_updated'] = absint( $input['last_updated'] );
return $new_input;
}
function phabricator_feed_options_add_page_fn() {
add_options_page('Phabricator Feed Importer', 'Phabricator Feed', 'administrator', "phabricator_feed_import", 'phabricator_feed_options_page_fn');
}
function phabricator_feed_options_page_fn() {
global $wpdb;
global $table_name_tags;
?>
<div class="wrap">
<h1>Phabricator Feed Importer Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields("phabricator_feed_import");
do_settings_sections("phabricator_feed_import");
submit_button()
?>
</form>
<h2>Current shortcodes</h2>
<ul>
<?php
$result = $wpdb->get_results ( "
SELECT DISTINCT $table_name_tags.slug
FROM $table_name_tags
" );
if (count($result) > 0) {
foreach ( $result as $project ) {
echo "<li><code>phabricator_feed_".$project->slug."</code></li>";
}
}
?>
</ul>
</div>
<?php
}
add_action('admin_init', 'phabricator_feed_options_init_fn' );
add_action('admin_menu', 'phabricator_feed_options_add_page_fn');
function salcode_add_plugin_page_settings_link( $links ) {
$links[] = '<a href="' . admin_url( 'options-general.php?page=phabricator_feed_import' ) . '">' . __('Settings') . '</a>';
return $links;
}
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'salcode_add_plugin_page_settings_link');
?>