6
6
7
7
/* ----- Settings ----- */
8
8
9
- define ('DOKU_INC ' , realpath (dirname (__FILE__ ). '/../../../ ' ). '/ ' );
10
- define ( ' DISCUSSION_NS ' , 'discussion ' ) ;
9
+ define ('DOKU_INC ' , realpath (dirname (__FILE__ ) . '/../../../ ' ) . '/ ' );
10
+ const DISCUSSION_NS = 'discussion ' ;
11
11
12
12
/* ----- Main ----- */
13
13
14
14
// 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
+ }
17
18
18
- require_once (DOKU_INC . 'inc/init.php ' );
19
+ require_once (DOKU_INC . 'inc/init.php ' );
19
20
20
21
$ files = getDiscussionPages ();
21
- $ n = 0 ;
22
+ $ n = 0 ;
22
23
23
24
foreach ($ files as $ file ) {
24
25
if (convertDiscussionPage ($ file )) {
25
- echo $ file ['id ' ]. '<br /> ' ;
26
+ echo $ file ['id ' ] . '<br /> ' ;
26
27
$ n ++;
27
28
}
28
29
}
29
30
30
31
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. ' ;
33
34
} else {
34
35
echo 'No discussion pages found. ' ;
35
36
}
39
40
/**
40
41
* returns a list of all discussion pages in the wiki
41
42
*/
42
- function getDiscussionPages () {
43
+ function getDiscussionPages ()
44
+ {
43
45
global $ conf ;
44
46
45
- $ data = array () ;
46
- search ($ data , $ conf ['datadir ' ], 'search_discussionpages ' , array () );
47
+ $ data = [] ;
48
+ search ($ data , $ conf ['datadir ' ], 'search_discussionpages ' , [] );
47
49
return $ data ;
48
50
}
49
51
50
52
/**
51
53
* function for the search callback
52
54
*/
53
- function search_discussionpages (&$ data , $ base , $ file , $ type , $ lvl , $ opts ) {
55
+ function search_discussionpages (&$ data , $ base , $ file , $ type , $ lvl , $ opts )
56
+ {
54
57
global $ conf ;
55
58
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
+ }
59
69
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
+ ] ;
66
76
return true ;
67
77
}
68
78
69
79
/**
70
80
* this converts individual discussion pages to .comment meta files
71
81
*/
72
- function convertDiscussionPage ($ file ) {
82
+ function convertDiscussionPage ($ file )
83
+ {
73
84
74
85
// read the old file
75
86
$ data = io_readFile ($ file ['old ' ], false );
76
87
77
88
// handle file with no comments yet
78
89
if (trim ($ data ) == '' ) {
79
- io_saveFile ($ file ['new ' ], serialize (array ( 'status ' => 1 , 'number ' => 0 ) ));
90
+ io_saveFile ($ file ['new ' ], serialize ([ 'status ' => 1 , 'number ' => 0 ] ));
80
91
@unlink ($ file ['old ' ]);
81
92
return true ;
82
93
}
@@ -85,57 +96,62 @@ function convertDiscussionPage($file) {
85
96
$ old = explode ('---- ' , $ data );
86
97
87
98
// merge with possibly already existing (newer) comments
88
- $ comments = array () ;
89
- if (@file_exists ($ file ['new ' ]))
99
+ $ comments = [] ;
100
+ if (@file_exists ($ file ['new ' ])) {
90
101
$ comments = unserialize (io_readFile ($ file ['old ' ], false ));
102
+ }
91
103
92
104
// set general info
93
- if (!isset ($ comments ['status ' ])) $ comments ['status ' ] = 1 ;
105
+ if (!isset ($ comments ['status ' ])) {
106
+ $ comments ['status ' ] = 1 ;
107
+ }
94
108
$ comments ['number ' ] += count ($ old );
95
109
96
110
foreach ($ old as $ comment ) {
97
111
98
112
// prepare comment data
99
113
if (strpos ($ comment , '<sub> ' ) !== false ) {
100
- $ in = '<sub> ' ;
114
+ $ in = '<sub> ' ;
101
115
$ out = ':</sub> ' ;
102
116
} else {
103
- $ in = '// ' ;
117
+ $ in = '// ' ;
104
118
$ out = ': // ' ;
105
119
}
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 );
108
122
109
123
// skip empty comments
110
124
if (!$ raw ) {
111
125
$ comments ['number ' ]--;
112
126
continue ;
113
127
}
114
128
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 , '' );
117
131
$ date = strtotime ($ strd );
118
- if ($ date == -1 ) $ date = time ();
132
+ if ($ date == -1 ) {
133
+ $ date = time ();
134
+ }
119
135
if ($ mail ) {
120
- list ($ mail ) = array_pad (explode (' | ' , $ mail , 2 ),2 , '' );
136
+ list ($ mail ) = array_pad (explode (' | ' , $ mail , 2 ), 2 , '' );
121
137
$ mail = substr (strrchr ($ mail , '> ' ), 1 );
122
138
}
123
- $ cid = md5 ($ name. $ date );
139
+ $ cid = md5 ($ name . $ date );
124
140
125
141
// render comment
126
142
$ xhtml = p_render ('xhtml ' , p_get_instructions ($ raw ), $ info );
127
143
128
144
// 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
+ ] ;
139
155
}
140
156
141
157
// save the new file
@@ -146,4 +162,3 @@ function convertDiscussionPage($file) {
146
162
147
163
return true ;
148
164
}
149
- // vim:ts=4:sw=4:et:enc=utf-8:
0 commit comments