forked from nischayn22/Sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sync.hooks.php
143 lines (132 loc) · 5.19 KB
/
Sync.hooks.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
<?php
use Nischayn22\MediaWikiApi;
class SyncHooks {
public static function onTranslationsApproved( $pageId, $target_lang ) {
global $wgSyncWikis;
$title = '';
foreach ( $wgSyncWikis as $wgSyncWiki ) {
if ( $wgSyncWiki['translate_to'] != $target_lang ) {
continue;
}
$syncWiki = new MediaWikiApi( $wgSyncWiki['api_path'] );
if ( $syncWiki->login( $wgSyncWiki['username'], $wgSyncWiki['password'] ) ) {
if ( $wgSyncWiki['translate'] ) {
$autoTranslate = new AutoTranslate( $target_lang );
$title = $autoTranslate->translateTitle( $pageId );
$content = $autoTranslate->translate( $pageId );
} else {
$revision = $wikiPage->getRevision();
$content = ContentHandler::getContentText( $revision->getContent( Revision::RAW ) );
$title = Title::newFromId( $pageId )->getFullText();
}
$syncWiki->editPage( $title, $content );
}
}
return true;
}
public static function onPageContentSaveComplete( &$wikiPage, &$user, $content, $summary, $isMinor, $isWatch, $section, &$flags, $revision, &$status, $baseRevId, $undidRevId ) {
global $wgSyncWikis;
$title = '';
foreach ( $wgSyncWikis as $wgSyncWiki ) {
if ( !$wgSyncWiki['live_edit'] ) {
continue;
}
$syncWiki = new MediaWikiApi( $wgSyncWiki['api_path'] );
if ( $syncWiki->login( $wgSyncWiki['username'], $wgSyncWiki['password'] ) ) {
if ( $wgSyncWiki['translate'] ) {
$autoTranslate = new AutoTranslate( $wgSyncWiki['translate_to'] );
$title = $autoTranslate->translateTitle( $wikiPage->getId() );
$syncWiki->editPage( $title, $autoTranslate->translate( $wikiPage->getId() ) );
} else {
$title = $wikiPage->getTitle()->getFullText();
$syncWiki->editPage( $title, $content->getNativeData() );
}
}
}
return true;
}
public static function onPageContentInsertComplete( &$wikiPage, User &$user, $content, $summary, $isMinor, $isWatch, $section, &$flags, Revision $revision ) {
global $wgSyncWikis, $wgSyncGoogleTranslateProjectId;
$title = $wikiPage->getTitle()->getFullText();
foreach ( $wgSyncWikis as $wgSyncWiki ) {
if ( !$wgSyncWiki['live_create'] ) {
continue;
}
$syncWiki = new MediaWikiApi( $wgSyncWiki['api_path'] );
if ( $syncWiki->login( $wgSyncWiki['username'], $wgSyncWiki['password'] ) ) {
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;
}
public static function onTitleMoveComplete( Title &$title, Title &$newTitle, User $user, $oldid, $newid, $reason, Revision $revision ) {
global $wgSyncWikis;
foreach ( $wgSyncWikis as $wgSyncWiki ) {
if ( !$wgSyncWiki['live_move'] ) {
continue;
}
$syncWiki = new MediaWikiApi( $wgSyncWiki['api_path'] );
if ( $syncWiki->login( $wgSyncWiki['username'], $wgSyncWiki['password'] ) ) {
$rev_id = $title->getLatestRevID();
$old_revision = Revision::newFromId( $rev_id );
$title = $title->getFullText();
if ( $wgSyncWiki['translate'] ) {
$autoTranslate = new AutoTranslate( $wgSyncWiki['translate_to'] );
$title = $autoTranslate->translateTitle( $old_revision->getPage() );
$content = $autoTranslate->translate( $old_revision->getPage() );
} else {
$content = ContentHandler::getContentText( $old_revision->getContent( Revision::RAW ) );
}
$syncWiki->editPage( $title->getPrefixedText(), $content );
$rev_id = $newTitle->getLatestRevID();
$new_revision = Revision::newFromId( $rev_id );
$title = $newTitle->getFullText();
if ( $wgSyncWiki['translate'] ) {
$autoTranslate = new AutoTranslate( $wgSyncWiki['translate_to'] );
$title = $autoTranslate->translateTitle( $new_revision->getPage() );
$content = $autoTranslate->translate( $new_revision->getPage() );
} else {
$content = ContentHandler::getContentText( $new_revision->getContent( Revision::RAW ) );
}
$syncWiki->editPage( $title, $content );
}
}
return true;
}
public static function onArticleDeleteComplete( &$article, User &$user, $reason, $id, $content, LogEntry $logEntry ) {
global $wgSyncWikis;
foreach ( $wgSyncWikis as $wgSyncWiki ) {
if ( !$wgSyncWiki['live_delete'] ) {
continue;
}
$syncWiki = new MediaWikiApi( $wgSyncWiki['api_path'] );
if ( $syncWiki->login( $wgSyncWiki['username'], $wgSyncWiki['password'] ) ) {
$syncWiki->deleteById( $id );
}
}
return true;
}
public static function onSkinTemplateNavigation( SkinTemplate &$skinTemplate, array &$links ) {
global $wgUser;
if ( !in_array( 'sysop', $wgUser->getEffectiveGroups()) ) {
return true;
}
$request = $skinTemplate->getRequest();
$action = $request->getText( 'action' );
$links['actions']['sync'] = array(
'class' => ( $action == 'sync') ? 'selected' : false,
'text' => "Sync",
'href' => $skinTemplate->makeArticleUrlDetails(
$skinTemplate->getTitle()->getFullText(), 'action=sync' )['href']
);
return true;
}
}