Advertisement
Inquisitor

Wakaba to Vichan database convertor

Aug 20th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.50 KB | None | 0 0
  1. @Echo Off
  2. SetLocal EnableDelayedExpansion
  3.  
  4. :: Wakaba[perl] to Vichan[php] MySQL database converter
  5. :: Required: perl, grep, sed
  6.  
  7. :: Before converting database, replace in input sql file all "!" to "[zxz]"
  8. :: After the end of the process do the same in reverse for restore exclamation marks in posts.
  9. :: Also, check last line in output sql and replace last symbol from "," to ";" if needed
  10. :: Import sql-file and make "Rebuild cache" in vichan adminpanel
  11.  
  12. :: Board name
  13. Set Board=b
  14. :: Limit for INSERT
  15. Set strLimit=25
  16. :: Input sql
  17. Set infile=wakaba.sql
  18. :: Output sql
  19. Set outfile=vichan.sql
  20.  
  21. Echo.>%outfile%
  22. Set i=1
  23. For /F "eol= tokens=1-3,5,7-20 delims=," %%A In (%infile%) Do (
  24.     For %%Z In (num parent timestamp ip name trip email subject password comment image size md5 width height thumbnail tn_width tn_height body body_nomarkup id thread) Do (Set "%%Z=")
  25.     For %%Z In (f_name f_type f_size f_filename f_extension f_file_id f_file f_thumb f_width f_height f_thumbwidth f_thumbheight f_file_path f_thumb_path) Do (Set "%%Z=")
  26.     Set num=%%A
  27.     Set id=!num:~1!
  28.     Set "id=!id:(=!"
  29.     Set parent=%%B
  30.     Set thread=!parent:~1!
  31.     If "!thread!"=="0" Set thread=NULL
  32.     Set timestamp=%%C
  33.     Set timestamp=!timestamp:~1!
  34.     Set ip=%%D
  35.     Set ip=!ip:~2,-1!
  36.     Call :IP ip
  37.     If Not "%%E"==" ''" (
  38.         Set "name=%%E"
  39.         Set "name='!name:~2,-1!'"
  40.     ) Else Set name=NULL
  41.     If Not "%%F"==" ''" (
  42.         Set "trip=%%F"
  43.         Set "trip='!trip:~2,-1!'"
  44.     ) Else Set trip=NULL
  45.     If Not "%%G"==" ''" (
  46.         Set "email=%%G"
  47.         Set "email='!email:~2,-1!'"
  48.         Set "email=!email:mailto:=!"
  49.     ) Else Set email=NULL
  50.     If Not "%%H"==" ''" (
  51.         Set "subject=%%H"
  52.         Set "subject='!subject:~2,-1!'"
  53.     ) Else Set subject=NULL
  54.     If Not "%%I"==" ''" (
  55.         Set "password=%%I"
  56.         Set "password='!password:~2,-1!'"
  57.     ) Else Set password=NULL
  58.     If Not "%%K"==" NULL" (
  59.         Set "image=%%K"
  60.         Set "image=!image:~2,-1!"
  61.     )
  62.     If Defined image (
  63.         Set "f_name=!image:src/=!"
  64.         Set "f_filename=!f_name!"
  65.         Set "f_file=!f_name!"
  66.         For /F "delims=" %%Z In ('Echo !f_name!^|grep -Eo "[0-9A-z]{3,4}$"') Do Set f_extension=%%Z
  67.         For /F "delims=" %%Z In ('Echo !f_name!^|grep -Eo "[0-9]+?"') Do Set f_file_id=%%Z
  68.         If /I "!f_extension!"=="jpeg" Set f_type=image\\/jpg
  69.         If /I "!f_extension!"=="jpg" Set f_type=image\\/jpg
  70.         If /I "!f_extension!"=="png" Set f_type=image\\/png
  71.         If /I "!f_extension!"=="gif" Set f_type=image\\/gif
  72.         Set "f_file_path=%Board%\\/src\\/!f_file!"
  73.         Set "thumbnail=%%P"
  74.         Set "f_thumb=!thumbnail:~2,-1!"
  75.         Set "f_thumb=!f_thumb:thumb/=!"
  76.         Set "f_thumb_path=%Board%\\/thumb\\/!f_thumb!"
  77.     )
  78.     If Not "%%L"==" NULL" (
  79.         Set "size=%%L"
  80.         Set "size=!size:~1!"
  81.         Set /A size*=1024
  82.     )
  83.     If Not "%%M"==" NULL" (
  84.         Set "md5=%%M"
  85.         Set "md5=!md5:~2,-1!"
  86.     )
  87.     If Not "%%N"==" NULL" (
  88.         Set "width=%%N"
  89.         Set "f_width=!width:~1!"
  90.     )
  91.     If Not "%%O"==" NULL" (
  92.         Set "height=%%O"
  93.         Set "f_height=!height:~1!"
  94.     )
  95.     If Not "%%Q"==" NULL" (
  96.         Set "tn_width=%%Q"
  97.         Set "f_thumbwidth=!tn_width:~2,-1!"
  98.     )
  99.     If Not "%%R"==" NULL" (
  100.         Set "tn_height=%%R"
  101.         Set "f_thumbheight=!tn_height:~2,-1!"
  102.     )
  103.     If Not "%%J"==" ''" (
  104.         Set "comment=%%J"
  105.         Set "body=!comment:~2,-1!"
  106.         Call :Body
  107.         Call :Markup
  108.     )
  109.     If !i! GTR !strLimit! Set i=1
  110.     If !i!==1 Call :Header
  111.     <nul Set /P "Echo=(!id!, !thread!, !subject!, !email!, !name!, !trip!, NULL"
  112.     <nul Set /P "Echo=, '!body!', '!body_nomarkup!'"
  113.     <nul Set /P "Echo=, !timestamp!, !timestamp!"
  114.     If Defined image (
  115.         <nul Set /P "Echo=, '[{"name":"!f_name!","type":"!f_type!","tmp_name":"\\/tmp\\/!random!","error":0,"size":!size!,"filename":"!f_filename!","extension":"!f_extension!","file_id":"!f_file_id!","file":"!f_file!","thumb":"!f_thumb!","is_an_image":true,"width":!f_width!,"height":!height!,"thumbwidth":!f_thumbwidth!,"thumbheight":!f_thumbheight!,"file_path":"!f_file_path!","thumb_path":"!f_thumb_path!"}]', 1, '!md5!'"
  116.     ) Else (
  117.         <nul Set /P "Echo=, NULL, 0, NULL"
  118.     )
  119.     <nul Set /P "Echo=, !password!, '!ip!', 0, 0, 0, NULL, NULL)"
  120.     If "!i!"=="!strLimit!" (
  121.         Echo ;
  122.     ) Else (
  123.         Echo ,
  124.     )
  125.     Set /A i+=1
  126. )>>%outfile%
  127. pause
  128.  
  129. :IP
  130. :: Вызвать перл для распаковки будет проще, чем городить костыль
  131. Set $ip=%~1
  132. Set ip=!%~1!
  133. For /F "delims=" %%A In ('perl -e "print join('.',unpack('C4',pack('N',!ip!)))"') Do Set "!$ip!=%%A!
  134. Exit /B
  135.  
  136. :Body
  137. :: Убираем неиспользуемое и заменяем на родные для vichan классы
  138. Set "body=!body:<p>=!"
  139. Set "body=!body:</p>=!"
  140. Set "body=!body:unkfunc=quote!"
  141. Set "body=!body:highlight(=highlightReply(!"
  142. Exit /B
  143.  
  144. :Markup
  145. :: Пытаемся создать версию поста с разметкой из вакабовского html-представления
  146. Set "body_nomarkup=!body:<br />=\r\n!"
  147. Set "body_nomarkup=!body_nomarkup:<strong>=[b]!"
  148. Set "body_nomarkup=!body_nomarkup:</strong>=[/b]!"
  149. Set "body_nomarkup=!body_nomarkup:<em>=[i]!"
  150. Set "body_nomarkup=!body_nomarkup:</em>=[/i]!"
  151. Set "body_nomarkup=!body_nomarkup:</em>=[/i]!"
  152. :: Ебаный sed под винду криво пишет в stdout, ненавижу временные файлы
  153. Echo "!body_nomarkup!"|sed -e :a -e "s/<[^>]*>//g;/</N;//ba">tmp.tmp
  154. For /F "eol= delims=" %%Z In (tmp.tmp) Do (Set "body_nomarkup=%%~Z")
  155. Set "body_nomarkup=!body_nomarkup:&gt;=>!"
  156. Exit /B
  157.  
  158. :Header
  159. Echo INSERT INTO `posts_%Board%` (`id`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`, `slug`) VALUES
  160. Exit /B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement