Skip to content

Commit 9993cbf

Browse files
committed
better translations on errors
1 parent ea10a8e commit 9993cbf

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

Client.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Client extends \IXR_Client {
66

77
const MIN_API = 7;
88

9+
protected $filecontext = '';
10+
911
/**
1012
* Client constructor.
1113
*
@@ -21,9 +23,18 @@ public function __construct($server, $user, $pass, $timeout = 15) {
2123
}
2224

2325
/** @inheritdoc */
24-
function query() {
26+
public function query() {
2527
$ok = call_user_func_array('parent::query', func_get_args());
26-
if(!$ok) throw new SyncException($this->getErrorMessage(), $this->getErrorCode());
28+
$code = $this->getErrorCode();
29+
if($code === -32300) $code = -1 * $this->status; // use http status on transport errors
30+
if(!$ok) {
31+
// when a file context is given include it in the exception
32+
if($this->filecontext) {
33+
throw new SyncFileException($this->getErrorMessage(), $this->filecontext, $code);
34+
} else {
35+
throw new SyncException($this->getErrorMessage(), $code);
36+
}
37+
}
2738
return $ok;
2839
}
2940

@@ -54,4 +65,13 @@ protected function ensureAPIversionOk() {
5465
throw new SyncException('versionerr');
5566
}
5667
}
68+
69+
/**
70+
* Set the file ID this query is running under
71+
*
72+
* @param string $file
73+
*/
74+
public function setFileContext($file) {
75+
$this->filecontext = $file;
76+
}
5777
}

Profile.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function getSyncList() {
130130
* @param string $summary the editing summary
131131
*/
132132
public function syncFile($type, $id, $dir, $summary) {
133+
$this->client->setFileContext($id);
133134
switch($dir) {
134135
case self::DIR_PULL:
135136
$this->syncPull($type, $id, $summary);
@@ -146,6 +147,7 @@ public function syncFile($type, $id, $dir, $summary) {
146147
default:
147148
// skip
148149
}
150+
$this->client->setFileContext('');
149151
}
150152

151153
/**
@@ -154,11 +156,11 @@ public function syncFile($type, $id, $dir, $summary) {
154156
* @param int $type pages|media
155157
* @param string $id the ID of the page or media
156158
* @param string $summary the editing summary
157-
* @throws SyncException
159+
* @throws SyncFileException
158160
*/
159161
protected function syncPull($type, $id, $summary) {
160162
if($type === self::TYPE_PAGES) {
161-
if(checklock($id)) throw new SyncException('Local file is locked');
163+
if(checklock($id)) throw new SyncFileException('lockfail', $id);
162164
$this->client->query('wiki.getPage', $id);
163165
} else {
164166
$this->client->query('wiki.getAttachment', $id);
@@ -179,14 +181,14 @@ protected function syncPull($type, $id, $summary) {
179181
* @param int $type pages|media
180182
* @param string $id the ID of the page or media
181183
* @param string $summary the editing summary
182-
* @throws SyncException
184+
* @throws SyncFileException
183185
*/
184186
protected function syncPullDelete($type, $id, $summary) {
185187
if($type === self::TYPE_PAGES) {
186-
if(checklock($id)) throw new SyncException('Local file is locked');
188+
if(checklock($id)) throw new SyncFileException('lockfail', $id);
187189
saveWikiText($id, '', $summary, false);
188190
} else {
189-
if(!unlink(mediaFN($id))) throw new SyncException('File deletion failed');
191+
if(!unlink(mediaFN($id))) throw new SyncFileException('localdelfail', $id);
190192
}
191193
}
192194

@@ -196,7 +198,7 @@ protected function syncPullDelete($type, $id, $summary) {
196198
* @param int $type pages|media
197199
* @param string $id the ID of the page or media
198200
* @param string $summary the editing summary
199-
* @throws SyncException
201+
* @throws SyncFileException
200202
*/
201203
protected function syncPush($type, $id, $summary) {
202204
if($type === self::TYPE_PAGES) {
@@ -217,7 +219,7 @@ protected function syncPush($type, $id, $summary) {
217219
* @param int $type pages|media
218220
* @param string $id the ID of the page or media
219221
* @param string $summary the editing summary
220-
* @throws SyncException
222+
* @throws SyncFileException
221223
*/
222224
protected function syncPushDelete($type, $id, $summary) {
223225
if($type === self::TYPE_PAGES) {
@@ -234,7 +236,7 @@ protected function syncPushDelete($type, $id, $summary) {
234236
*
235237
* @param string $id
236238
* @param bool $state is this a lock (true) or unlock (false)
237-
* @throws SyncException
239+
* @throws SyncFileException
238240
*/
239241
protected function setRemoteLock($id, $state) {
240242
if($state) {
@@ -248,7 +250,7 @@ protected function setRemoteLock($id, $state) {
248250
$this->client->query('dokuwiki.setLocks', array('lock' => $lock, 'unlock' => $unlock));
249251
$data = $this->client->getResponse();
250252
if(count((array) $data['lockfail'])) {
251-
throw new SyncException('Locking at the remote wiki failed');
253+
throw new SyncFileException('lockfail', $id);
252254
}
253255
}
254256

SyncException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public function __construct($message = '', $code = 0, $previous = null) {
2121
$msg = $plugin->getLang($message);
2222
if($msg) $message = $msg;
2323

24+
if($code === -403) $message = $plugin->getLang('autherr');
25+
2426
parent::__construct($message, $code, $previous);
2527
}
2628
}

SyncFileException.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\sync;
4+
5+
/**
6+
* Class SyncFileException
7+
*
8+
* This exception is thrown in the context of syncing one particular file
9+
*
10+
* @package dokuwiki\plugin\sync
11+
*/
12+
class SyncFileException extends SyncException {
13+
14+
/**
15+
* SyncFileException constructor.
16+
*
17+
* @param string $message
18+
* @param int $id
19+
* @param int $code
20+
*/
21+
public function __construct($message, $id, $code = 0) {
22+
// translate error messages
23+
$plugin = plugin_load('admin', 'sync');
24+
$msg = $plugin->getLang($message);
25+
if($msg) $message = $msg;
26+
27+
if(substr($message, -1) != ':') $message .= ':';
28+
29+
// append file name
30+
$message = $message . ' ' . $id;
31+
parent::__construct($message, $code);
32+
}
33+
}

lang/en/lang.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$lang['lastsync'] = 'Last Synchronization:';
77
$lang['remotever'] = 'Remote Wiki Version:';
88

9-
$lang['xmlerr'] = 'Failed to talk to remote wiki. Make sure the remote wiki allows XMLRPC requests and that you set up the endpoint URL correctly.';
9+
$lang['autherr'] = 'The provided user is not allowed to access the XMLRPC API at the remote wiki.';
1010
$lang['loginerr'] = 'Failed to login at remote wiki. Make sure supplied credentials are valid at the remote wiki.';
1111
$lang['versionerr'] = 'The remote wiki XMLRPC API version is too old. You need to upgrade the remote wiki to use the sync plugin.';
1212

@@ -38,15 +38,7 @@
3838

3939
$lang['noconnect'] = 'Could not connect to remote wiki:';
4040
$lang['lockfail'] = 'couldn\'t lock and will skip:';
41-
$lang['pullfail'] = 'pull failed:';
42-
$lang['pullok'] = 'pull succeded:';
43-
$lang['localdelok'] = 'local delete succeeded:';
4441
$lang['localdelfail'] = 'local delete failed:';
45-
$lang['pushfail'] = 'push failed:';
46-
$lang['pushok'] = 'push succeded:';
47-
$lang['remotedelok'] = 'remote delete succeeded:';
48-
$lang['remotedelfail'] = 'remote delete failed:';
49-
$lang['skipped'] = 'skipped:';
5042

5143
$lang['js']['list'] = 'A list of files that differ between your local and the remote wiki is shown below. You need to decide which revisions you want to keep.'; #from list.txt
5244

0 commit comments

Comments
 (0)