$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>/Us', '', $text);
$text = preg_replace_callback('/
(.+)<\/a>/Us', function ($m) {
$fpath = MyUtil::getImagePath('/mediawiki/images', urldecode($m[1]));
return ''.$m[2].'';
}, $text);
$text = preg_replace_callback('/
\/(..)\/([^)
/Us', function ($m) {
return '
!['.$m[3].'](/mediawiki/images/'.$m[1].'/'.$m[2].'/'.$m[3].')
';
}, $text);
$text = preg_replace_callback('/
\/(..)\/([^\/]+)\/([^)
/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 '
!['.$m[3].'](/mediawiki/images/'.$fpath_.')
';
}, $text);
$text = preg_replace_callback('/
(.+)<\/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 ''.$m[3].'';
}, $text);
$text = preg_replace_callback('/
\/(.)\/(.)\/([^)
/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 '
!['.$m[4].'](/mediawiki/images/'.$fpath.')
';
}, $text);
$text = preg_replace('/
\s*(.*)\s*<\/p>/Us', '
\1
', $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('/
/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 '';
}, $text);
$text = preg_replace('/(.+)<\/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('/]*)>(\s)*]*)>/Us', '\2', $text);
$text = preg_replace('/<\/tr>(\s*)<\/table>/s', '
\1
', $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;
}
}