Skip to content

Commit ab996e0

Browse files
committed
convert: reformatting
1 parent da3a880 commit ab996e0

File tree

1 file changed

+62
-47
lines changed

1 file changed

+62
-47
lines changed

convert.php

+62-47
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@
66

77
/* ----- Settings ----- */
88

9-
define('DOKU_INC', realpath(dirname(__FILE__).'/../../../').'/');
10-
define('DISCUSSION_NS', 'discussion');
9+
define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../') . '/');
10+
const DISCUSSION_NS = 'discussion';
1111

1212
/* ----- Main ----- */
1313

1414
// conversion script should only be run once
15-
if (@file_exists(dirname(__FILE__).'/convert_completed'))
16-
die('Conversion already completed.');
15+
if (@file_exists(dirname(__FILE__) . '/convert_completed')) {
16+
die('Conversion already completed.');
17+
}
1718

18-
require_once(DOKU_INC.'inc/init.php');
19+
require_once(DOKU_INC . 'inc/init.php');
1920

2021
$files = getDiscussionPages();
21-
$n = 0;
22+
$n = 0;
2223

2324
foreach ($files as $file) {
2425
if (convertDiscussionPage($file)) {
25-
echo $file['id'].'<br />';
26+
echo $file['id'] . '<br />';
2627
$n++;
2728
}
2829
}
2930

3031
if ($n > 0) {
31-
io_saveFile(dirname(__FILE__).'/convert_completed', '');
32-
echo '<br />Successfully converted '.$n.' discussion pages to new comments meta files.';
32+
io_saveFile(dirname(__FILE__) . '/convert_completed', '');
33+
echo '<br />Successfully converted ' . $n . ' discussion pages to new comments meta files.';
3334
} else {
3435
echo 'No discussion pages found.';
3536
}
@@ -39,44 +40,54 @@
3940
/**
4041
* returns a list of all discussion pages in the wiki
4142
*/
42-
function getDiscussionPages() {
43+
function getDiscussionPages()
44+
{
4345
global $conf;
4446

45-
$data = array();
46-
search($data, $conf['datadir'], 'search_discussionpages', array());
47+
$data = [];
48+
search($data, $conf['datadir'], 'search_discussionpages', []);
4749
return $data;
4850
}
4951

5052
/**
5153
* function for the search callback
5254
*/
53-
function search_discussionpages(&$data, $base, $file, $type, $lvl, $opts) {
55+
function search_discussionpages(&$data, $base, $file, $type, $lvl, $opts)
56+
{
5457
global $conf;
5558

56-
if ($type == 'd') return true; // recurse into directories
57-
if (!preg_match('#'.preg_quote('/'.DISCUSSION_NS.'/', '#').'#u', $file)) return false;
58-
if (!preg_match('#\.txt$#', $file)) return false;
59+
// recurse into directories
60+
if ($type == 'd') {
61+
return true;
62+
}
63+
if (!preg_match('#' . preg_quote('/' . DISCUSSION_NS . '/', '#') . '#u', $file)) {
64+
return false;
65+
}
66+
if (!preg_match('#\.txt$#', $file)) {
67+
return false;
68+
}
5969

60-
$id = pathID(str_replace(DISCUSSION_NS.'/', '', $file));
61-
$data[] = array(
62-
'id' => $id,
63-
'old' => $conf['datadir'].$file,
64-
'new' => metaFN($id, '.comments')
65-
);
70+
$id = pathID(str_replace(DISCUSSION_NS . '/', '', $file));
71+
$data[] = [
72+
'id' => $id,
73+
'old' => $conf['datadir'] . $file,
74+
'new' => metaFN($id, '.comments')
75+
];
6676
return true;
6777
}
6878

6979
/**
7080
* this converts individual discussion pages to .comment meta files
7181
*/
72-
function convertDiscussionPage($file) {
82+
function convertDiscussionPage($file)
83+
{
7384

7485
// read the old file
7586
$data = io_readFile($file['old'], false);
7687

7788
// handle file with no comments yet
7889
if (trim($data) == '') {
79-
io_saveFile($file['new'], serialize(array('status' => 1, 'number' => 0)));
90+
io_saveFile($file['new'], serialize(['status' => 1, 'number' => 0]));
8091
@unlink($file['old']);
8192
return true;
8293
}
@@ -85,57 +96,62 @@ function convertDiscussionPage($file) {
8596
$old = explode('----', $data);
8697

8798
// merge with possibly already existing (newer) comments
88-
$comments = array();
89-
if (@file_exists($file['new']))
99+
$comments = [];
100+
if (@file_exists($file['new'])) {
90101
$comments = unserialize(io_readFile($file['old'], false));
102+
}
91103

92104
// set general info
93-
if (!isset($comments['status'])) $comments['status'] = 1;
105+
if (!isset($comments['status'])) {
106+
$comments['status'] = 1;
107+
}
94108
$comments['number'] += count($old);
95109

96110
foreach ($old as $comment) {
97111

98112
// prepare comment data
99113
if (strpos($comment, '<sub>') !== false) {
100-
$in = '<sub>';
114+
$in = '<sub>';
101115
$out = ':</sub>';
102116
} else {
103-
$in = '//';
117+
$in = '//';
104118
$out = ': //';
105119
}
106-
list($meta, $raw) = array_pad(explode($out, $comment, 2),2, '');
107-
$raw = trim($raw);
120+
list($meta, $raw) = array_pad(explode($out, $comment, 2), 2, '');
121+
$raw = trim($raw);
108122

109123
// skip empty comments
110124
if (!$raw) {
111125
$comments['number']--;
112126
continue;
113127
}
114128

115-
list($mail, $meta) = array_pad(explode($in, $meta, 2),2, '');
116-
list($name, $strd) = array_pad(explode(', ', $meta, 2),2, '');
129+
list($mail, $meta) = array_pad(explode($in, $meta, 2), 2, '');
130+
list($name, $strd) = array_pad(explode(', ', $meta, 2), 2, '');
117131
$date = strtotime($strd);
118-
if ($date == -1) $date = time();
132+
if ($date == -1) {
133+
$date = time();
134+
}
119135
if ($mail) {
120-
list($mail) = array_pad(explode(' |', $mail, 2),2, '');
136+
list($mail) = array_pad(explode(' |', $mail, 2), 2, '');
121137
$mail = substr(strrchr($mail, '>'), 1);
122138
}
123-
$cid = md5($name.$date);
139+
$cid = md5($name . $date);
124140

125141
// render comment
126142
$xhtml = p_render('xhtml', p_get_instructions($raw), $info);
127143

128144
// fill in the converted comment
129-
$comments['comments'][$cid] = array(
130-
'user' => array(
131-
'name' => hsc($name),
132-
'mail' => hsc($mail)),
133-
'date' => array('created' => $date),
134-
'show' => true,
135-
'raw' => $raw,
136-
'xhtml' => $xhtml,
137-
'replies' => array()
138-
);
145+
$comments['comments'][$cid] = [
146+
'user' => [
147+
'name' => hsc($name),
148+
'mail' => hsc($mail)],
149+
'date' => ['created' => $date],
150+
'show' => true,
151+
'raw' => $raw,
152+
'xhtml' => $xhtml,
153+
'replies' => []
154+
];
139155
}
140156

141157
// save the new file
@@ -146,4 +162,3 @@ function convertDiscussionPage($file) {
146162

147163
return true;
148164
}
149-
// vim:ts=4:sw=4:et:enc=utf-8:

0 commit comments

Comments
 (0)