Skip to content

Commit c4b0f2b

Browse files
authored
Merge pull request #10 from circuscode/feature/twitter-dsgvo-export
Feature/twitter dsgvo export
2 parents 29dfb01 + 0b3068d commit c4b0f2b

File tree

4 files changed

+81
-47
lines changed

4 files changed

+81
-47
lines changed

mathilda.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function mathilda_activate () {
9494
add_option('mathilda_import_finish', "0");
9595
add_option('mathilda_import_subprocess_running', "0");
9696
add_option('mathilda_load_process_running',"0");
97+
add_option('mathilda_import_filesize_max',"409600");
9798

9899
/* Create Mathilda Tables */
99100

@@ -206,6 +207,7 @@ function mathilda_delete () {
206207
delete_option('mathilda_import_finish');
207208
delete_option('mathilda_import_subprocess_running');
208209
delete_option('mathilda_load_process_running');
210+
delete_option('mathilda_import_filesize_max');
209211

210212
/* Delete Tables */
211213

mathilda_import.php

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ function mathilda_import_tool() {
4646
$twitter_import_path = mathilda_get_import_directory();
4747

4848
/*
49-
Arrays
49+
Variables & Arrays
5050
*/
5151

5252
$list_import_files_draft=array();
5353
$number_of_files_draft=0;
5454
$list_import_files=array();
5555
$number_of_files=0;
56+
$max_file_size=0;
57+
$filesize_max_threshold=get_option('mathilda_import_filesize_max');
58+
$filesize_max_string=$filesize_max_threshold;
59+
$filesize_max_string=$filesize_max_string/1024;
5660

5761
/*
5862
Read Files @ Import Directory
@@ -105,6 +109,22 @@ function mathilda_import_tool() {
105109

106110
array_multisort($list_import_files);
107111

112+
/*
113+
Get Max File Size
114+
*/
115+
116+
for($i=0; $i<$number_of_files; $i++)
117+
{
118+
119+
$filename_with_path=$twitter_import_path . '/' . $list_import_files[$i][0];
120+
$filesize_this=filesize($filename_with_path);
121+
122+
if($filesize_this>$max_file_size) {
123+
$max_file_size=$filesize_this;
124+
}
125+
126+
}
127+
108128
/*
109129
Check Number of Files
110130
*/
@@ -116,10 +136,24 @@ function mathilda_import_tool() {
116136
echo 'Please follow the instructions below.</p>';
117137
echo '<p><strong>Required Steps</strong></p>';
118138
echo '<p>1. Download your tweet archive from Twitter (Profile/Settings).<br/>';
119-
echo '2. Upload all files from /data/js/tweets to /wp-content/uploads/mathilda-import.<br/>';
120-
echo '3. Run this import script again.</p>';
139+
echo '2. Split the file data/tweets.js into smaller files (<'.$filesize_max_string.' KB) with a local app.<br/>';
140+
echo '3. Upload all files to the folder wp-content/uploads/mathilda-import.</br>';
141+
echo '4. Run this import script again.</p>';
121142
echo '<p>&nbsp;<br/><a class="button" href="'.admin_url().'tools.php?page=mathilda-tools-menu">Close</a></p>';
122143
return;
144+
145+
}
146+
147+
if ($max_file_size>$filesize_max_threshold) {
148+
149+
echo '<p><strong>Error</strong></p>';
150+
echo '<p>One or more files in the import folder are larger as '.$filesize_max_string.' KB.<br/>';
151+
echo 'Unfortunately the import process is limited to files not larger as 400 KB.<br/>';
152+
echo 'You must split the affected files in smaller files with a local app.<br/>';
153+
echo 'After that run this import script again.</p>';
154+
echo '<p>&nbsp;<br/><a class="button" href="'.admin_url().'tools.php?page=mathilda-tools-menu">Close</a></p>';
155+
return;
156+
123157
}
124158

125159
if ($number_of_files>0) {
@@ -327,15 +361,15 @@ function mathilda_import_file( $file ) {
327361
$hashtag_index_e=false;
328362
$hashtags_yes_or_no="FALSE";
329363

330-
if(array_key_exists('0', $items['entities']['hashtags']))
364+
if(array_key_exists('0', $items['tweet']['entities']['hashtags']))
331365
{
332-
foreach($items['entities']['hashtags'] as $hashtags)
366+
foreach($items['tweet']['entities']['hashtags'] as $hashtags)
333367
{
334368
$hashtag_text=$hashtags['text'];
335369
$hashtag_index_s=$hashtags['indices'][0];
336370
$hashtag_index_e=$hashtags['indices'][1];
337371
$hashtags_yes_or_no="TRUE";
338-
$hashtag_cache[]=array($hashtag_text,$hashtag_index_s,$hashtag_index_e,$items['id_str']);
372+
$hashtag_cache[]=array($hashtag_text,$hashtag_index_s,$hashtag_index_e,$items['tweet']['id_str']);
339373
$num_hashtags=$num_hashtags+1;
340374
}
341375
}
@@ -349,17 +383,17 @@ function mathilda_import_file( $file ) {
349383
$mention_index_end=false;
350384
$mentions_yes_or_no="FALSE";
351385

352-
if(array_key_exists('0', $items['entities']['user_mentions']))
386+
if(array_key_exists('0', $items['tweet']['entities']['user_mentions']))
353387
{
354-
foreach($items['entities']['user_mentions'] as $mentions)
388+
foreach($items['tweet']['entities']['user_mentions'] as $mentions)
355389
{
356390
$mention_useridstr=$mentions['id_str'];
357391
$mention_screenname=$mentions['screen_name'];
358392
$mention_fullname=$mentions['name'];
359393
$mention_index_start=$mentions['indices'][0];
360394
$mention_index_end=$mentions['indices'][1];
361395
$mentions_yes_or_no="TRUE";
362-
$mention_cache[]=array($mention_useridstr,$mention_screenname,$mention_fullname,$mention_index_start,$mention_index_end,$items['id_str']);
396+
$mention_cache[]=array($mention_useridstr,$mention_screenname,$mention_fullname,$mention_index_start,$mention_index_end,$items['tweet']['id_str']);
363397
$num_mentions=$num_mentions+1;
364398
}
365399
}
@@ -373,17 +407,17 @@ function mathilda_import_file( $file ) {
373407
$url_index_end=false;
374408
$urls_yes_or_no="FALSE";
375409

376-
if(array_key_exists('0', $items['entities']['urls']))
410+
if(array_key_exists('0', $items['tweet']['entities']['urls']))
377411
{
378-
foreach($items['entities']['urls'] as $urls)
412+
foreach($items['tweet']['entities']['urls'] as $urls)
379413
{
380414
$url_tco=$urls['url'];
381415
$url_extended=$urls['expanded_url'];
382416
$url_display=$urls['display_url'];
383417
$url_index_start=$urls['indices'][0];
384418
$url_index_end=$urls['indices'][1];
385419
$urls_yes_or_no="TRUE";
386-
$url_cache[]=array($url_tco,$url_extended,$url_display,$url_index_start,$url_index_end,$items['id_str']);
420+
$url_cache[]=array($url_tco,$url_extended,$url_display,$url_index_start,$url_index_end,$items['tweet']['id_str']);
387421
$num_urls=$num_urls+1;
388422
}
389423
}
@@ -404,9 +438,9 @@ function mathilda_import_file( $file ) {
404438
$index_end=false;
405439
$media_yes_or_no="FALSE";
406440

407-
if(isset($items['entities']['media']))
441+
if(isset($items['tweet']['entities']['media']))
408442
{
409-
foreach($items['entities']['media'] as $images)
443+
foreach($items['tweet']['entities']['media'] as $images)
410444
{
411445

412446
$media_idstr=$images['id_str'];
@@ -416,23 +450,9 @@ function mathilda_import_file( $file ) {
416450
$media_displayurl=$images['display_url'];
417451
$media_extendedurl=$images['expanded_url'];
418452

419-
/*
420-
Twitter Export JSON does not contain the size-array (small, medium, large, xxx)
421-
Search Largest Value Pair
422-
*/
423-
424-
$select_size=0;
425-
for ($x=0; $x<4; $x++)
426-
{
427-
if($images['sizes'][$select_size]['w']<$images['sizes'][($x+1)]['w'])
428-
{
429-
$select_size=($x+1);
430-
}
431-
}
432-
433-
$media_size_w=$images['sizes'][$select_size]['w'];
434-
$media_size_h=$images['sizes'][$select_size]['h'];
435-
$media_size_resize=$images['sizes'][$select_size]['resize'];
453+
$media_size_w=$images['sizes']['large']['w'];
454+
$media_size_h=$images['sizes']['large']['h'];
455+
$media_size_resize=$images['sizes']['large']['resize'];
436456
$media_type='photo';
437457
$index_start=$images['indices'][0];
438458
$index_end=$images['indices'][1];
@@ -457,7 +477,7 @@ function mathilda_import_file( $file ) {
457477

458478
// Update Media Array
459479

460-
$media_cache[]=array($media_idstr,$media_mediaurl,$media_mediaurlhttps,$media_url,$media_displayurl,$media_extendedurl, $media_size_w, $media_size_h, $media_size_resize,$media_type, $index_start, $index_end,$items['id_str'],$filename,$loaded);
480+
$media_cache[]=array($media_idstr,$media_mediaurl,$media_mediaurlhttps,$media_url,$media_displayurl,$media_extendedurl, $media_size_w, $media_size_h, $media_size_resize,$media_type, $index_start, $index_end,$items['tweet']['id_str'],$filename,$loaded);
461481
$media_yes_or_no="TRUE";
462482
$num_media=$num_media+1;
463483
}
@@ -470,16 +490,19 @@ function mathilda_import_file( $file ) {
470490
$tweet_retweet="FALSE";
471491
$tweet_quote="FALSE";
472492

473-
if(isset($items['retweeted_status'])) {
474-
$tweet_retweet="TRUE";
493+
// Identify Retweets
494+
$is_retweet = strpos($items['tweet']['full_text'], 'RT ');
495+
if($is_retweet===0) {
496+
$tweet_retweet="TRUE";
475497
}
476-
if(isset($items['truncated'])) {
477-
if($items['truncated']=='true') {
498+
499+
if(isset($items['tweet']['truncated'])) {
500+
if($items['tweet']['truncated']=='true') {
478501
$tweet_truncate="TRUE";
479502
}
480503
}
481-
if(isset($items['in_reply_to_status_id'])) {
482-
if($items['in_reply_to_status_id']!=null) {
504+
if(isset($items['tweet']['in_reply_to_user_id'])) {
505+
if($items['tweet']['in_reply_to_user_id']!=null) {
483506
$tweet_reply="TRUE";
484507
}
485508
}
@@ -490,9 +513,9 @@ function mathilda_import_file( $file ) {
490513
}
491514

492515
$tweet_cache[]=array($num_tweets,
493-
$items['id_str'],
494-
$items['text'],
495-
$items['created_at'],
516+
$items['tweet']['id_str'],
517+
$items['tweet']['full_text'],
518+
$items['tweet']['created_at'],
496519
$hashtags_yes_or_no,
497520
$mentions_yes_or_no,
498521
$media_yes_or_no,
@@ -518,10 +541,8 @@ function mathilda_import_file( $file ) {
518541

519542
// Convert Date
520543

521-
$tweet_cache[$i][3]=str_replace ( '-' , '' , $tweet_cache[$i][3] );
522-
$tweet_cache[$i][3]=str_replace ( ' ' , '' , $tweet_cache[$i][3] );
523-
$tweet_cache[$i][3]=str_replace ( ':' , '' , $tweet_cache[$i][3] );
524-
$tweet_cache[$i][3]=str_replace ( '+0000' , '' , $tweet_cache[$i][3] );
544+
$tweet_date=strtotime($tweet_cache[$i][3]);
545+
$tweet_cache[$i][3]=date('YmdHis', $tweet_date);
525546

526547
// Update Tweets
527548

mathilda_tools.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ function mathilda_tools_close() {
435435
*/
436436

437437
function mathilda_handbook() {
438+
439+
$filesize_max_threshold=get_option('mathilda_import_filesize_max');
440+
$filesize_max_string=$filesize_max_threshold;
441+
$filesize_max_string=$filesize_max_string/1024;
438442

439443
echo '<h1 class="mathilda_tools_headline">Mathilda Handbook</h1>';
440444
echo '<p class="mathilda_tools_description">Get it working!<br/>&nbsp;</p>';
@@ -454,8 +458,9 @@ function mathilda_handbook() {
454458

455459
echo '<h2>How to import your complete twitter history?</h2>
456460
1. Download your tweet archive from Twitter (Profile/Settings/Your Data).<br/>
457-
2. Upload all files from the folder data/js/tweets to the folder wp-content/uploads/mathilda-import.</br>
458-
3. Run the import.</p>';
461+
2. Split the file data/tweets.js into smaller files (<'.$filesize_max_string.' KB) with a local app.<br/>
462+
3. Upload all files to the folder wp-content/uploads/mathilda-import.</br>
463+
4. Run the import.</p>';
459464

460465
echo '<h2>Helpful Resources</h2>';
461466
echo mathilda_helpful_resources();

mathilda_update.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ function mathilda_update () {
188188
update_option('mathilda_plugin_version', "13");
189189
}
190190

191+
/* Update Process Version 0.11 */
192+
if($mathilda_previous_version==13) {
193+
update_option('mathilda_plugin_version', "14");
194+
add_option('mathilda_import_filesize_max',"409600");
195+
}
196+
191197
}
192198
add_action( 'plugins_loaded', 'mathilda_update' );
193199

0 commit comments

Comments
 (0)