-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
263 lines (228 loc) · 7.09 KB
/
plugin.php
File metadata and controls
263 lines (228 loc) · 7.09 KB
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
<?php
/*
Plugin Name: YOURLS to PeakURL
Plugin URI: https://github.com/PeakURL/YOURLS-to-PeakURL
Description: Export YOURLS links as PeakURL-ready CSV, JSON, or XML for the PeakURL Bulk Import tool.
Version: 1.0.0
Author: PeakURL
Author URI: https://peakurl.org
*/
if ( ! defined( 'YOURLS_ABSPATH' ) ) {
die();
}
define( 'PEAKURL_YOURLS_EXPORT_SLUG', 'peakurl-export' );
define( 'PEAKURL_YOURLS_EXPORT_VERSION', '1.0.0' );
define( 'PEAKURL_YOURLS_EXPORT_NONCE_ACTION', 'peakurl_export_links' );
yourls_add_action( 'plugins_loaded', 'peakurl_yourls_register_admin_page' );
yourls_add_action( 'load-' . PEAKURL_YOURLS_EXPORT_SLUG, 'peakurl_yourls_handle_download' );
/**
* Register the plugin administration page.
*
* @return void
*/
function peakurl_yourls_register_admin_page() {
yourls_register_plugin_page(
PEAKURL_YOURLS_EXPORT_SLUG,
'PeakURL Export',
'peakurl_yourls_render_admin_page'
);
}
/**
* Handle export downloads before YOURLS renders the admin page wrapper.
*
* @return void
*/
function peakurl_yourls_handle_download() {
$action = isset( $_GET['action'] ) ? trim( (string) $_GET['action'] ) : '';
if ( 'download' !== $action ) {
return;
}
$format = isset( $_GET['format'] ) ? strtolower( trim( (string) $_GET['format'] ) ) : 'csv';
if ( ! in_array( $format, [ 'csv', 'json', 'xml' ], true ) ) {
yourls_die( 'Unsupported export format.', 'Invalid export format', 400 );
}
yourls_verify_nonce( PEAKURL_YOURLS_EXPORT_NONCE_ACTION, $_REQUEST['nonce'] ?? '' );
peakurl_yourls_stream_export( $format );
exit;
}
/**
* Render the plugin admin page.
*
* @return void
*/
function peakurl_yourls_render_admin_page() {
$stats = yourls_get_db_stats();
$total_links = isset( $stats['total_links'] ) ? (int) $stats['total_links'] : 0;
$base_url = yourls_admin_url( 'plugins.php?page=' . PEAKURL_YOURLS_EXPORT_SLUG . '&action=download' );
$csv_url = yourls_nonce_url( PEAKURL_YOURLS_EXPORT_NONCE_ACTION, $base_url . '&format=csv' );
$json_url = yourls_nonce_url( PEAKURL_YOURLS_EXPORT_NONCE_ACTION, $base_url . '&format=json' );
$xml_url = yourls_nonce_url( PEAKURL_YOURLS_EXPORT_NONCE_ACTION, $base_url . '&format=xml' );
?>
<h2>Export Links for PeakURL</h2>
<p>
Export your YOURLS links into a PeakURL-compatible import file. PeakURL's Bulk Import accepts CSV, JSON,
and XML uploads, and this plugin generates all three formats from the current YOURLS URL table.
</p>
<p>
<strong><?php echo (int) $total_links; ?></strong> links are currently available for export.
</p>
<p>
The CSV export is ready for direct upload into PeakURL and uses these columns:
<code>url,alias,title,password,expires</code>.
Password and expiry fields are included as blank columns because YOURLS core does not store them.
</p>
<ul>
<li><a class="button button-primary" href="<?php echo htmlspecialchars( $csv_url, ENT_QUOTES, 'UTF-8' ); ?>">Download CSV for PeakURL</a></li>
<li><a class="button" href="<?php echo htmlspecialchars( $json_url, ENT_QUOTES, 'UTF-8' ); ?>">Download JSON</a></li>
<li><a class="button" href="<?php echo htmlspecialchars( $xml_url, ENT_QUOTES, 'UTF-8' ); ?>">Download XML</a></li>
</ul>
<p>
In PeakURL, open <strong>Dashboard -> Bulk Import -> File Upload</strong> and upload the exported file.
</p>
<?php
}
/**
* Stream the selected export format.
*
* @param string $format Export format.
* @return void
*/
function peakurl_yourls_stream_export( $format ) {
if ( function_exists( 'set_time_limit' ) ) {
@set_time_limit( 0 );
}
yourls_no_cache_headers();
$filename = sprintf( 'peakurl-import-%s.%s', gmdate( 'Ymd-His' ), $format );
if ( 'csv' === $format ) {
yourls_content_type_header( 'text/csv' );
} elseif ( 'json' === $format ) {
yourls_content_type_header( 'application/json' );
} else {
yourls_content_type_header( 'application/xml' );
}
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
header( 'X-Content-Type-Options: nosniff' );
if ( 'csv' === $format ) {
peakurl_yourls_stream_csv();
return;
}
if ( 'json' === $format ) {
peakurl_yourls_stream_json();
return;
}
peakurl_yourls_stream_xml();
}
/**
* Stream CSV output.
*
* @return void
*/
function peakurl_yourls_stream_csv() {
$output = fopen( 'php://output', 'wb' );
if ( false === $output ) {
yourls_die( 'Could not open the export stream.', 'Export failed', 500 );
}
fputcsv( $output, [ 'url', 'alias', 'title', 'password', 'expires' ] );
peakurl_yourls_walk_records(
static function ( $record ) use ( $output ) {
fputcsv(
$output,
[
$record['url'],
$record['alias'],
$record['title'],
'',
'',
]
);
}
);
fclose( $output );
}
/**
* Stream JSON output.
*
* @return void
*/
function peakurl_yourls_stream_json() {
echo "[\n";
$first = true;
peakurl_yourls_walk_records(
static function ( $record ) use ( &$first ) {
$item = [
'destinationUrl' => $record['url'],
'alias' => $record['alias'],
'title' => $record['title'],
'password' => '',
'expiresAt' => '',
];
if ( ! $first ) {
echo ",\n";
}
echo json_encode( $item, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
$first = false;
}
);
echo "\n]\n";
}
/**
* Stream XML output.
*
* @return void
*/
function peakurl_yourls_stream_xml() {
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<urls>\n";
peakurl_yourls_walk_records(
static function ( $record ) {
echo " <url>\n";
echo ' <destinationUrl>' . peakurl_yourls_xml_escape( $record['url'] ) . "</destinationUrl>\n";
echo ' <alias>' . peakurl_yourls_xml_escape( $record['alias'] ) . "</alias>\n";
echo ' <title>' . peakurl_yourls_xml_escape( $record['title'] ) . "</title>\n";
echo " <password></password>\n";
echo " <expiresAt></expiresAt>\n";
echo " </url>\n";
}
);
echo "</urls>\n";
}
/**
* Iterate through YOURLS link rows in batches and hand them to a callback.
*
* @param callable $callback Callback to receive each normalized record.
* @return void
*/
function peakurl_yourls_walk_records( $callback ) {
$offset = 0;
$limit = 500;
$table = YOURLS_DB_TABLE_URL;
$db = yourls_get_db( 'read-peakurl_export_links' );
do {
$rows = $db->fetchObjects(
"SELECT `keyword`, `url`, `title`, `timestamp`, `clicks` FROM `$table` ORDER BY `timestamp` ASC, `keyword` ASC LIMIT $offset, $limit"
);
if ( empty( $rows ) ) {
break;
}
foreach ( $rows as $row ) {
$record = [
'alias' => isset( $row->keyword ) ? (string) $row->keyword : '',
'url' => isset( $row->url ) ? (string) $row->url : '',
'title' => isset( $row->title ) ? (string) $row->title : '',
'timestamp' => isset( $row->timestamp ) ? (string) $row->timestamp : '',
'clicks' => isset( $row->clicks ) ? (int) $row->clicks : 0,
];
$callback( $record );
}
$offset += count( $rows );
} while ( count( $rows ) === $limit );
}
/**
* Escape a value for XML element output.
*
* @param string $value Raw value.
* @return string
*/
function peakurl_yourls_xml_escape( $value ) {
return htmlspecialchars( (string) $value, ENT_XML1 | ENT_COMPAT, 'UTF-8' );
}