forked from nischayn22/Sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncAction.php
106 lines (90 loc) · 2.84 KB
/
SyncAction.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
<?php
/**
* Sync's a page with other wikis
*
* @ingroup Actions
*/
use Nischayn22\MediaWikiApi;
class SyncAction extends FormAction {
public function getName() {
return 'sync';
}
protected function getDescription() {
return '';
}
public function onSubmit( $data ) {
global $wgSyncWikis, $wgUser;
$title = $this->getTitle()->getFullText();
$wikiPage = new WikiPage( $this->getTitle() );
if ( class_exists( 'CommentStreams' ) ) {
$comments = CommentStreams::singleton()->getComments( $this->getTitle()->getArticleID(), $wgUser );
$child_comments = array();
foreach( $comments as &$comment ) {
if ( array_key_exists( 'children', $comment ) ) {
foreach( $comment['children'] as &$child_comment) {
$child_comment['parent_comment_title'] = WikiPage::newFromId( $child_comment['parentid'] )->getTitle()->getFullText();
}
$child_comments = array_merge( $child_comments, $comment['children'] );
unset( $comment['children'] );
}
}
$comments = array_merge( $comments, $child_comments );
// Generate Export
$exporter = new WikiExporter( wfGetDB( DB_MASTER ) );
$comments_export = new DumpStringOutput;
$exporter->setOutputSink( $comments_export );
$exporter->openStream();
foreach ( $comments as $comment_data ) {
$exporter->pageByTitle( Title::newFromText( $comment_data['comment_page_title'] ) );
}
$exporter->closeStream();
}
foreach ( $wgSyncWikis as $wgSyncWiki ) {
$syncWiki = new MediaWikiApi( $wgSyncWiki['api_path'] );
if ( $syncWiki->login( $wgSyncWiki['username'], $wgSyncWiki['password'] ) ) {
if ( class_exists( 'CommentStreams' ) ) {
$syncWiki->importXML( $comments_export->__toString() );
}
if ( $wgSyncWiki['translate'] ) {
$autoTranslate = new AutoTranslate( $wgSyncWiki['translate_to'] );
$title = $autoTranslate->translateTitle( $wikiPage->getId() );
$content = $autoTranslate->translate( $wikiPage->getId() );
} else {
$revision = $wikiPage->getRevision();
$content = ContentHandler::getContentText( $revision->getContent( Revision::RAW ) );
}
$syncWiki->editPage( $title, $content );
}
}
return true;
}
protected function checkCanExecute( User $user ) {
// Must be logged in
if ( !in_array( 'sysop', $user->getEffectiveGroups()) ) {
throw new PermissionsError( 'sysop' );
}
}
protected function usesOOUI() {
return true;
}
protected function getFormFields() {
return [
'intro' => [
'type' => 'info',
'vertical-label' => true,
'raw' => true,
'default' => "Confirm Sync (this may take a while)?"
]
];
}
protected function alterForm( HTMLForm $form ) {
$form->setSubmitText( 'Confirm' );
$form->setTokenSalt( 'sync' );
}
public function onSuccess() {
$this->getOutput()->addHtml( "Page has been synced!" );
}
public function doesWrites() {
return false;
}
}