-
Notifications
You must be signed in to change notification settings - Fork 2
/
mediawiki-importer.php
292 lines (229 loc) · 8.71 KB
/
mediawiki-importer.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
<?php
/*
Plugin Name: Mediawiki Importer
Plugin URI:
Description: Import Mediawiki content to WordPress.
Author: Prasath Nadarajah
Author URI:
Version: 0.1
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
if ( !defined('WP_LOAD_IMPORTERS') )
return;
// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php';
if ( !class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) )
require_once $class_wp_importer;
}
if ( class_exists( 'WP_Importer' ) ) {
class Mediawiki_Import {
private $site_url;
private $cookies = array();
private $user_agent = 'WordPress Mediawiki Importer';
private $timeout = 60;
function Mediawiki_Import() {
$this->site_url = get_option( 'mw_import_siteurl' );
$cookies = get_transient( 'mw_import_cookie' );
if( $cookies )
$this->cookie = $cookies;
}
function dispatch() {
if ( empty ( $_GET['step'] ) ) {
$step = 0;
}
else {
$this->setup(); // if no proper credentials is found redirect to login page
$step = (int) $_GET['step'];
}
$this->header();
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
$this->display_menu();
break;
case 2 :
$this->display_get_page_by_title();
break;
case 3 :
$this->get_page_by_title();
break;
}
$this->footer();
}
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import Mediawiki', 'mediawiki-importer').'</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
?>
<div class="narrow">
<form action="admin.php?import=mediawiki&step=1" method="post">
<p><?php _e( 'Howdy! This importer allows you to connect to mediawiki based sites and import content' , 'mediawiki-importer') ?></p>
<p><?php _e( 'Enter your Mediawiki username and password below:' , 'mediawiki-importer') ?></p>
<table class="form-table">
<tr>
<th scope="row"><label for="mw_username"><?php _e( 'Mediawiki Username' , 'mediawiki-importer') ?></label></th>
<td><input type="text" name="mw_username" id="mw_username" class="regular-text" value="<?php echo esc_html( get_option( 'mw_import_username' ) ); ?>"/></td>
</tr>
<tr>
<th scope="row"><label for="mw_password"><?php _e( 'Mediawiki Password' , 'mediawiki-importer') ?></label></th>
<td><input type="password" name="mw_password" id="mw_password" class="regular-text" autocomplete="off" value="<?php echo esc_html( $this->mw_import_decrypt( get_option( 'mw_import_password' ) ) ); ?>"/></td>
</tr>
<tr>
<th scope="row"><label for="mw_siteurl"><?php _e( 'Mediawiki Site Url' , 'mediawiki-importer') ?></label></th>
<td><input type="text" name="mw_siteurl" id="mw_siteurl" class="regular-text" value="<?php echo esc_html( get_option( 'mw_import_siteurl' ) ); ?>"/></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button" value="<?php esc_attr_e( 'Connect to Mediawiki site and Import' , 'mediawiki-importer') ?>" />
</p>
</form>
</div>
<?php
}
function display_menu() {
// Attempy login if redirected from greeting page
if( isset( $_POST['mw_username'] ) ) {
$lgname = sanitize_text_field( $_POST['mw_username'] );
update_option( 'mw_import_username', $lgname );
$lgpassword = sanitize_text_field( $_POST['mw_password'] );
update_option( 'mw_import_password', $this->mw_import_encrypt( $lgpassword ) );
$siteurl = sanitize_text_field( $_POST['mw_siteurl'] );
update_option( 'mw_import_siteurl', $siteurl );
$result = $this->login();
if( is_wp_error( $result ) ) {
echo '<p>Login Unsuccessful!</p>';
} else {
echo '<p>Login Successful!</p>';
}
}
?>
<p>
<a href="?import=mediawiki&step=2"><?php _e( 'Import Page by title' , 'mediawiki-importer') ?></a>
</p>
<?php
}
function display_get_page_by_title() {
?>
<div class="narrow">
<form action="admin.php?import=mediawiki&step=3" method="post">
<p><?php _e( 'Enter page title to retrieve:' , 'mediawiki-importer') ?></p>
<table class="form-table">
<tr>
<th scope="row"><label for="mw_pagetitle"><?php _e( 'Page title' , 'mediawiki-importer') ?></label></th>
<td><input type="text" name="mw_pagetitle" id="mw_pagetitle" class="regular-text" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button" value="<?php esc_attr_e( 'Import Page' , 'mediawiki-importer') ?>" />
</p>
</form>
</div>
<?php
}
function get_page_by_title() {
$page_title = sanitize_text_field( $_POST['mw_pagetitle'] );
$path = '&action=query&titles=' . urlencode( $page_title ) . '&prop=revisions&rvparse=&rvprop=content';
$url = $this->build_request_url( $path );
$response = wp_remote_get(
$url,
array (
'timeout' => $this->timeout,
'user-agent' => $this->user_agent,
'blocking' => true,
'sslverify' => false
)
);
$response_body = simplexml_load_string($response['body']);
if( empty( $response_body->query->pages->page->revisions->rev ) ) {
echo '<p>Invalid title. return to <a href=" admin.php?import=mediawiki&step=1">main menu</a></p>';
return;
}
$insert = wp_insert_post(
array(
'post_title' => $page_title,
'post_content' => $response_body->query->pages->page->revisions->rev
)
);
if ( is_wp_error( $insert ) ) {
echo '<p>Import Unsuccessful. Return to <a href=" admin.php?import=mediawiki&step=1">main menu</a></p>';
} else {
echo '<p>Import Successful. Return to <a href=" admin.php?import=mediawiki&step=1">main menu</a></p>';
}
}
// helper functions
function setup() {
}
function login() {
$lgname = get_option( 'mw_import_username' );
if( empty( $lgname ) )
return new WP_Error( 'mw_login', __( 'Empty Username', 'mediawiki-importer') );
$lgpassword = $this->mw_import_decrypt( get_option( 'mw_import_password' ) );
if( empty( $lgpassword ) )
return new WP_Error( 'mw_login', __( 'Empty Password', 'mediawiki-importer') );
$siteurl = get_option( 'mw_import_siteurl' );
if( !filter_var( $siteurl, FILTER_VALIDATE_URL ) )
return new WP_Error( 'mw_login', __( 'Invalid Site Url', 'mediawiki-importer') );
// Send the request.
$url = $siteurl . '/api.php?format=xml&action=login&lgname=' . $lgname . '&lgpassword=' . $lgpassword;
$response = wp_remote_post(
$url,
array (
'timeout' => $this->timeout,
'user-agent' => $this->user_agent,
'blocking' => true,
'sslverify' => false
)
);
if ( is_wp_error( $response ) )
return new WP_Error( 'mw_login', __( $response->errors, 'mediawiki-importer') );
// obtain the cookies returned
$cookies = $response['cookies'];
// obtain the login token returned
$lgtoken = simplexml_load_string($response['body'])->login['token'];
$url = $siteurl . '/api.php?format=xml&action=login&lgname=' . $lgname . '&lgpassword=' . $lgpassword . '&lgtoken=' . $lgtoken;
// Request with login token.
$response = wp_remote_post(
$url,
array (
'timeout' => $this->timeout,
'user-agent' => $this->user_agent,
'blocking' => true,
'cookies' => $cookies,
'sslverify' => false
)
);
if ( is_wp_error( $response ) )
return new WP_Error( 'mediawiki_login', __( $response->errors, 'mediawiki-importer') );
if( simplexml_load_string($response['body'])->login['result'] != 'Success' )
return new WP_Error( 'mediawiki_login', __( simplexml_load_string($response['body'])->login['result'], 'mediawiki-importer') );
$cookies = $response['cookies'];
set_transient( 'mw_import_cookie', $cookies, 60*60*24 );
}
function mw_import_encrypt( $data ) {
$data = serialize( $data );
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(MW_IMPORT_KEY), $data, MCRYPT_MODE_CBC, md5(md5(MW_IMPORT_KEY))));
}
function mw_import_decrypt( $data ) {
$data = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(MW_IMPORT_KEY), base64_decode($data), MCRYPT_MODE_CBC, md5(md5(MW_IMPORT_KEY))), "\0");
if ( !$data )
return false;
return @unserialize( $data );
}
function build_request_url( $path ) {
$site_url = get_option( 'mw_import_siteurl' );
$url = $site_url . '/api.php?format=xml' . $path;
return $url;
}
}
$mediawiki_import = new Mediawiki_Import();
register_importer( 'mediawiki', __( 'Mediawiki', 'mediawiki-importer' ), __( 'Import content from Mediawiki sites.', 'mediawiki-importer' ), array( $mediawiki_import, 'dispatch' ) );
}