-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelperfunctions.php
100 lines (86 loc) · 2.85 KB
/
helperfunctions.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
<?php
/**
* @author Nischay Nahata <[email protected]>
* @license GPL v2 or later
*/
function copypage( $pageName, $recursivelyCalled = true ) {
global $settings, $publicApi, $privateApi;
// Check if entire namespace is specified
if ( is_numeric( $pageName ) ) {
$result = $privateApi->listPageInNamespace( $pageName );
foreach( $result as $page ) {
copypage( (string)$page['title'] );
}
return;
}
echo "Copying over $pageName\n";
// Get Namespace
$parts = explode( ':', $pageName );
$content = $privateApi->readPage($pageName);
if ( $settings['enableTranslate'] ) {
$privateApi->setTranslateSettings( $settings['GOOGLE_TRANSLATE_PROJECT_ID'], $settings['lang_to'] );
$content = $privateApi->translateWikiText( $content );
}
if ( empty($content) ) {
// write to file that reading failed
echo "Page read error...Check if page exists and is accessible \n";
echo "logging page name in failed_pages.txt\n";
file_put_contents( 'failed_pages.txt' , $pageName . "\n", FILE_APPEND );
return;
}
if( count( $parts ) === 2 && $parts[0] === 'File') { // files are handled here
$rawFileUrl = $privateApi->getFileUrl($pageName);
echo "Downloading file " . $parts[1] . " \n";
if ( !is_dir( $settings['imagesDirectory'] ) ) {
// dir doesn't exist, make it
echo "Creating directory " . $settings['imagesDirectory'] . "\n";
mkdir( $settings['imagesDirectory'] );
}
$result = download( $rawFileUrl, $settings['imagesDirectory'] . "/" . $parts[1] );
if ( !$result ) {
echo "Download error...Check if file exists and is usable \n";
// write to file that copy failed
echo "logging in failed_pages.txt \n";
file_put_contents( 'failed_pages.txt' , $pageName . "\n", FILE_APPEND );
} else {
echo "File download successfully \n";
}
return;
}
// now copy normal page
if ($settings['create']) {
$data = $publicApi->createPage($pageName, $content);
} else {
$data = $publicApi->editPage($pageName, $content);
}
if ( $data == null ) {
// write to file that copy failed
echo "logging page name in failed_pages.txt\n";
file_put_contents( 'failed_pages.txt' , $pageName . "\n", FILE_APPEND );
}
if ($settings['copy_images']) {
// Now import images linked on the page
echo "Finding file links in $pageName ...\n";
$result = $privateApi->listImagesOnPage($pageName);
if( $result ) {
foreach( $result as $image ) {
echo "Link found to " . (string)$image['title'] . " \n";
copypage( (string)$image['title'] );
}
} else {
echo "No file links found\n";
}
} else {
echo "Skipping files\n";
}
// Now copy category members too
if( count( $parts ) === 2 && $parts[0] === 'Category' ) {
if( !$settings['recurseCategories'] && $recursivelyCalled ) {
return;
}
$result = $privateApi->listPageInCategory($pageName);
foreach( $result as $page ) {
copypage( (string)$page['title'] );
}
}
}