Files
dynamicbrain.neuroinf.jp/etc/fixMediaWikiDumpData.php
T
2019-08-26 22:36:33 +09:00

181 lines
8.9 KiB
PHP

<?php
define('IMAGE_DIR', '/data/www/html/uploads/mediawiki');
define('DEST_IMAGE_DIR', __DIR__.'/data/public/mediawiki/images');
$contents = json_decode(file_get_contents(__DIR__.'/data/src/mediawiki/assets/contents.json'), true);
MyUtil::makedir(DEST_IMAGE_DIR.'/thumb');
MyUtil::makedir(DEST_IMAGE_DIR.'/math');
foreach ($contents as $content) {
$data = json_decode(file_get_contents(__DIR__.'/temp/mediawiki/'.$content['id'].'.dump.json'), true);
$text = MyUtil::fixHtml($data['text'], $content['id']);
$data2 = [
'title' => $data['title'],
'text' => $text,
];
file_put_contents(__DIR__.'/data/public/mediawiki/contents/'.$content['id'].'.json', json_encode($data2, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
class MyUtil
{
public static function fixHtml($text, $revid)
{
$text = preg_replace('/<div class="magnify">.+<\/div>/Us', '', $text);
$text = preg_replace_callback('/<a href="\/modules\/mediawiki\/File:([^"]+)" class="image">(.+)<\/a>/Us', function ($m) {
$fpath = MyUtil::getImagePath('/mediawiki/images', urldecode($m[1]));
return '<a href="'.$fpath.'" class="image">'.$m[2].'</a>';
}, $text);
$text = preg_replace_callback('/<img alt="(?:[^"]*)" src="https?:\/\/dynamicbrain\.neuroinf\.jp\/uploads\/mediawiki\/(.)\/(..)\/([^"]+)" width="(\d+)" height="(\d+)" (?:class="thumbimage" )?\/>/Us', function ($m) {
return '<img alt="'.$m[3].'" src="/mediawiki/images/'.$m[1].'/'.$m[2].'/'.$m[3].'" width="'.$m[4].'" height="'.$m[5].'" class="thumbimage" />';
}, $text);
$text = preg_replace_callback('/<img alt="(?:[^"]*)" src="https?:\/\/dynamicbrain\.neuroinf\.jp\/uploads\/mediawiki\/thumb\/(.)\/(..)\/([^\/]+)\/([^"]+)" width="(\d+)" height="(\d+)" (?:class="thumbimage" )?srcset="([^"]+)" \/>/Us', function ($m) {
$bpath = 'thumb/'.$m[1];
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$bpath);
$bpath .= '/'.$m[2];
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$bpath);
$bpath_ = $bpath.'/'.$m[3];
$bpath .= '/'.urldecode($m[3]);
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$bpath);
$fpath_ = $bpath_.'/'.$m[4];
$fpath = $bpath.'/'.urldecode($m[4]);
MyUtil::fileCopy(IMAGE_DIR.'/'.$fpath, DEST_IMAGE_DIR.'/'.$fpath);
$srcset = [];
foreach (explode(', ', $m[7]) as $src) {
if (preg_match('/^https?:\/\/dynamicbrain\.neuroinf\.jp\/uploads\/mediawiki\/thumb\/(.)\/(..)\/([^\/]+)\/([^ ]+) (.+)$/Us', trim($src), $sm)) {
$sfpath_ = $bpath_.'/'.$sm[4];
$sfpath = $bpath.'/'.urldecode($sm[4]);
MyUtil::fileCopy(IMAGE_DIR.'/'.$sfpath, DEST_IMAGE_DIR.'/'.$sfpath);
$srcset[] = '/mediawiki/images/'.$sfpath_.' '.$sm[5];
}
}
return '<img alt="'.$m[3].'" src="/mediawiki/images/'.$fpath_.'" width="'.$m[5].'" height="'.$m[6].'" class="thumbimage" srcset="'.implode(', ', $srcset).'"/>';
}, $text);
$text = preg_replace_callback('/<a rel="nofollow" class="(?:[^"]+)" href="https?:\/\/dynamicbrain\.neuroinf\.jp\/modules\/xoonips\/([a-z]+\.php)\?([^"]+)">(.+)<\/a>/Us', function ($m) {
parse_str(htmlspecialchars_decode($m[2], ENT_QUOTES), $params);
$url = '';
switch ($m[1]) {
case 'detail.php':
if (isset($params['item_id'])) {
$url = 'item/'.$params['item_id'];
} elseif (isset($params['id'])) {
$url = 'item/id/'.$params['id'];
} else {
var_dump($m);
exit('Unexpected parameter found..');
}
break;
case 'itemselect.php':
if (!isset($params['op'])) {
var_dump($m);
exit('Unexpected url found..');
break;
}
switch ($params['op']) {
case 'quicksearch':
if (!isset($params['keyword'])) {
var_dump($m);
exit('Unexpected url found.. parameter keyword not found');
break;
}
if (!isset($params['search_itemtype'])) {
var_dump($m);
exit('Unexpected url found.. parameter search_itemtype not found');
break;
}
$url = 'search?keyword='.$params['keyword'].'&type='.preg_replace('/^xnp/', '', $params['search_itemtype']);
break;
default:
var_dump($m);
exit('not impremented yet..');
break;
}
break;
default:
var_dump($m);
exit('not impremented yet..');
break;
}
return '<a href="/database/'.$url.'">'.$m[3].'</a>';
}, $text);
$text = preg_replace_callback('/<img class="mwe-math-fallback-png-inline tex" alt="" src="https?:\/\/dynamicbrain.neuroinf.jp\/uploads\/mediawiki\/math\/(.)\/(.)\/(.)\/([^"]+)" \/>/Us', function ($m) {
$fpath = 'math/'.$m[1];
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$fpath);
$fpath .= '/'.$m[2];
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$fpath);
$fpath .= '/'.$m[3];
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$fpath);
$fpath .= '/'.$m[4];
MyUtil::fileCopy(IMAGE_DIR.'/'.$fpath, DEST_IMAGE_DIR.'/'.$fpath);
return '<img class="mwe-math-fallback-png-inline tex" alt="'.$m[4].'" src="/mediawiki/images/'.$fpath.'" />';
}, $text);
$text = preg_replace('/<p>\s*(.*)\s*<\/p>/Us', '<p>\1</p>', $text);
$text = preg_replace_callback('/href="(?:https?:\/\/dynamicbrain\.neuroinf\.jp)?\/modules\/mediawiki\/(?:index.php\/)?([^"]+)"/Us', function ($m) {
return 'href="/mediawiki/'.urlencode(str_replace(' ', '_', $m[1])).'"';
}, $text);
$text = preg_replace_callback('/<a rel="nofollow" class="external text" href="https?:\/\/dynamicbrain\.neuroinf\.jp\/uploads\/mediawiki\/(.)\/(..)\/([^"]+)">/Us', function ($m) {
$fpath = '/'.$m[1];
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$fpath);
$fpath .= '/'.$m[2];
MyUtil::makedir(DEST_IMAGE_DIR.'/'.$fpath);
$fpath_ = $fpath.'/'.$m[3];
$fpath .= '/'.urldecode($m[3]);
MyUtil::fileCopy(IMAGE_DIR.'/'.$fpath, DEST_IMAGE_DIR.'/'.$fpath);
return '<a class="download" href="/mediawiki/images'.$fpath_.'" download="'.urldecode($m[3]).'">';
}, $text);
$text = preg_replace('/<a rel="nofollow" class="(?:[^"]+)" href="https?:\/\/dynamicbrain\.neuroinf\.jp\/modules\/xoonips\/registeruser.php">(.+)<\/a>/Us', '\1', $text);
$text = preg_replace('/href="https?:\/\/dynamicbrain\.neuroinf\.jp\/hetero/Us', 'href="/hetero', $text);
$text = preg_replace('/href="https?:\/\/dynamicbrain\.neuroinf\.jp\/modules\/hackathon\//Us', 'href="/hackathon/', $text);
$text = preg_replace('/href="https?:\/\/dynamicbrain\.neuroinf\.jp\/conferences\//Us', 'href="/conferences/', $text);
$text = preg_replace('/href="https?:\/\/dynamicbrain\.neuroinf\.jp\/modules\/documents\/(.*)"/Us', 'href="/documents/\1"', $text);
$text = preg_replace('/https?:\/\/dynamicbrain\.neuroinf\.jp\/modules\/hackathon\//Us', 'https://dynamicbrain.neuroinf.jp/hackathon/', $text);
$text = preg_replace('/<table([^>]*)>(\s)*<tr([^>]*)>/Us', '<table\1><tbody>\2<tr\3>', $text);
$text = preg_replace('/<\/tr>(\s*)<\/table>/s', '</tr></tbody>\1</table>', $text);
$text = trim($text);
if (preg_match('/https?:\/\/dynamicbrain\.neuroinf\.jp/', $text)) {
$okids = [2614];
if (!in_array($revid, $okids)) {
echo $text.PHP_EOL;
die('dynamicbrain internal url link still found: '.$revid.PHP_EOL);
}
}
return $text;
}
public static function makedir($path)
{
if (is_dir($path)) {
return true;
}
if (false === @mkdir($path)) {
exit('Failed to create directroy: '.$path.PHP_EOL);
}
return true;
}
public static function getImagePath($basedir, $fname)
{
$hash = md5($fname);
return $basedir.'/'.substr($hash, 0, 1).'/'.substr($hash, 0, 2).'/'.$fname;
}
public static function fileCopy($from, $to)
{
system('rsync -aq '.escapeshellarg($from).' '.escapeshellarg($to), $ret);
if (0 !== $ret) {
exit('Failed to copy file: '.$from.' -> '.$to.PHP_EOL);
}
return 0 === $ret;
}
}