차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
tech:mediawiki2dokuwiki [2013/07/25 13:31] – V_L | tech:mediawiki2dokuwiki [2016/07/12 00:56] (현재) – 바깥 편집 127.0.0.1 | ||
---|---|---|---|
줄 1: | 줄 1: | ||
+ | {{tag> | ||
+ | ======Mediawiki2dokuwiki====== | ||
+ | |||
+ | <file php m2d.php> | ||
+ | |||
+ | <?php | ||
+ | /** | ||
+ | * MediaWiki2DokuWiki importer. | ||
+ | * Copyright (C) 2011-2013 | ||
+ | * | ||
+ | * This program is free software: you can redistribute it and/or modify | ||
+ | * it under the terms of the GNU General Public License as published by | ||
+ | * the Free Software Foundation, either version 3 of the License, or | ||
+ | * (at your option) any later version. | ||
+ | * | ||
+ | * This program is distributed in the hope that it will be useful, | ||
+ | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
+ | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
+ | * GNU General Public License for more details. | ||
+ | * | ||
+ | * You should have received a copy of the GNU General Public License | ||
+ | * along with this program. | ||
+ | * | ||
+ | * @package | ||
+ | * @author | ||
+ | * @copyright Copyright (C) 2011-2013 Andrei Nicholson | ||
+ | * @link https:// | ||
+ | */ | ||
+ | |||
+ | /** | ||
+ | * Convert syntaxes. | ||
+ | * | ||
+ | * Regular expressions originally by Johannes Buchner | ||
+ | * < | ||
+ | * | ||
+ | * Changes by Frederik Tilkin: | ||
+ | * | ||
+ | * <ul> | ||
+ | * < | ||
+ | * < | ||
+ | | ||
+ | * < | ||
+ | | ||
+ | * </ul> | ||
+ | * | ||
+ | * @author Andrei Nicholson | ||
+ | * @author Johannes Buchner | ||
+ | * @author Frederik Tilkin | ||
+ | * @since | ||
+ | */ | ||
+ | class MediaWiki2DokuWiki_MediaWiki_SyntaxConverter | ||
+ | { | ||
+ | /** Original MediaWiki record. */ | ||
+ | private $record = ''; | ||
+ | |||
+ | /** Stored code blocks to prevent further conversions. */ | ||
+ | private $codeBlock = array(); | ||
+ | |||
+ | /** What string should never occur in user content? */ | ||
+ | private $placeholder = ''; | ||
+ | |||
+ | /** | ||
+ | * Constructor. | ||
+ | * | ||
+ | * @param string $record MediaWiki record. | ||
+ | */ | ||
+ | public function __construct($record) | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert page syntax from MediaWiki to DokuWiki. | ||
+ | * | ||
+ | * @return string DokuWiki page. | ||
+ | * @author Johannes Buchner < | ||
+ | * @author Frederik Tilkin | ||
+ | */ | ||
+ | public function convert() | ||
+ | { | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | $record = $this-> | ||
+ | |||
+ | if (count($this-> | ||
+ | $record = $this-> | ||
+ | } | ||
+ | |||
+ | return $record; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Double forward slashes are not italic. There is no double slash syntax | ||
+ | * rule in MediaWiki. This conversion must happen before the conversion of | ||
+ | * italic markup. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertDoubleSlash($record) | ||
+ | { | ||
+ | $patterns = array( | ||
+ | '/ | ||
+ | ); | ||
+ | return preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Code blocks. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertCodeBlocks($record) | ||
+ | { | ||
+ | $patterns = array( | ||
+ | // Change the ones that have been replaced in a link [] BACK to | ||
+ | // normal (do it twice in case | ||
+ | // [http:// | ||
+ | '/ | ||
+ | '/ | ||
+ | |||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | |||
+ | return preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Replace content in PRE tag with placeholder. This is done so no more | ||
+ | * conversions are performed with the contents. The last thing this class | ||
+ | * will do is replace those placeholders with their original content. | ||
+ | * | ||
+ | * @param string $code Contents of PRE tag. | ||
+ | * | ||
+ | * @return string CODE tag with placeholder in content. | ||
+ | */ | ||
+ | private function storeCodeBlock($code) | ||
+ | { | ||
+ | $this-> | ||
+ | |||
+ | $replace = $this-> | ||
+ | |||
+ | return "< | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Replace PRE tag placeholders back with their original content. | ||
+ | * | ||
+ | * @param string $record Converted record. | ||
+ | * | ||
+ | * @return string Record with placeholders removed. | ||
+ | */ | ||
+ | private function replaceStoredCodeBlocks($record) | ||
+ | { | ||
+ | for ($i = 0, $numBlocks = count($this-> | ||
+ | $record = str_replace( | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | return $record; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert images and files. | ||
+ | * | ||
+ | * @param string $record Converted record. | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertImagesFiles($record) | ||
+ | { | ||
+ | $numMatches = preg_match_all( | ||
+ | '/ | ||
+ | $record, | ||
+ | $matches | ||
+ | ); | ||
+ | |||
+ | if ($numMatches ==== 0 || $numMatches ==== false) { | ||
+ | return $record; | ||
+ | } | ||
+ | |||
+ | for ($i = 0; $i < $numMatches; | ||
+ | $converted = $this-> | ||
+ | |||
+ | // Replace the full tag, [[File: | ||
+ | // with the DokuWiki equivalent. | ||
+ | $record = str_replace($matches[0][$i], | ||
+ | } | ||
+ | |||
+ | return $record; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Process a MediaWiki image tag. | ||
+ | * | ||
+ | * @param string $detail Filename and options, ie. | ||
+ | | ||
+ | * | ||
+ | * @return string DokuWiki version of tag. | ||
+ | */ | ||
+ | private function convertImage($detail) | ||
+ | { | ||
+ | $parts = explode(' | ||
+ | $numParts = count($parts); | ||
+ | |||
+ | // Image link. | ||
+ | if ($numParts ===== 2 && substr($parts[1], | ||
+ | return ' | ||
+ | } | ||
+ | |||
+ | $converted = ' | ||
+ | $leftAlign = ''; | ||
+ | $rightAlign = ''; | ||
+ | $imageSize = ''; | ||
+ | $caption = ''; | ||
+ | |||
+ | if ($numParts > 1) { | ||
+ | $imageFilename = array_shift($parts); | ||
+ | |||
+ | foreach ($parts as $part) { | ||
+ | if ($part ===== ' | ||
+ | $leftAlign = ' '; | ||
+ | continue; | ||
+ | } else if ($part ===== ' | ||
+ | $rightAlign = ' '; | ||
+ | continue; | ||
+ | } else if ($part ===== ' | ||
+ | $leftAlign = $rightAlign = ' '; | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | if (substr($part, | ||
+ | preg_match('/ | ||
+ | |||
+ | if (count($matches) > 0) { | ||
+ | if ($matches[1] ===== '' | ||
+ | $imageSize = $matches[3]; | ||
+ | } else { | ||
+ | $imageSize = $matches[2] . ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | continue; | ||
+ | } | ||
+ | |||
+ | $caption = $part; | ||
+ | } | ||
+ | |||
+ | $converted .= $leftAlign . ' | ||
+ | |||
+ | if ($imageSize != '' | ||
+ | $converted .= '?' | ||
+ | } | ||
+ | |||
+ | if ($caption != '' | ||
+ | $converted .= ' | ||
+ | } | ||
+ | } else { | ||
+ | $converted .= " | ||
+ | } | ||
+ | |||
+ | $converted .= ' | ||
+ | |||
+ | return $converted; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert talks. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertTalks($record) | ||
+ | { | ||
+ | $patterns = array( | ||
+ | '/^[ ]*:/' | ||
+ | '/>:/' | ||
+ | '/>>:/' | ||
+ | '/>>>:/' | ||
+ | '/>>>>:/' | ||
+ | '/>>>>>:/' | ||
+ | '/>>>>>>:/' | ||
+ | ); | ||
+ | |||
+ | return preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert bold and italic. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertBoldItalic($record) | ||
+ | { | ||
+ | $patterns = array( | ||
+ | "/''''' | ||
+ | "/'''/" | ||
+ | "/''/" | ||
+ | |||
+ | // Changes by Reiner Rottmann: - fixed erroneous interpretation | ||
+ | // of combined bold and italic text. | ||
+ | ' | ||
+ | ); | ||
+ | |||
+ | return preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert [link] => [[link]]. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertLink($record) | ||
+ | { | ||
+ | $patterns = array('/ | ||
+ | |||
+ | return preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert [url text] => [url|text]. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertUrlText($record) | ||
+ | { | ||
+ | $patterns = array( | ||
+ | '/ | ||
+ | ); | ||
+ | |||
+ | return preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert lists. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertList($record) | ||
+ | { | ||
+ | $patterns = array( | ||
+ | '/^\* / | ||
+ | '/ | ||
+ | '/ | ||
+ | '/ | ||
+ | '/^# / | ||
+ | '/ | ||
+ | '/ | ||
+ | '/ | ||
+ | ); | ||
+ | |||
+ | return preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Convert headings. Syntax between MediaWiki and DokuWiki is completely | ||
+ | * opposite: the largest heading in MediaWiki is two equal marks while in | ||
+ | * DokuWiki it's six equal marks. This creates a problem since the first | ||
+ | * replaced string of two marks will be caught by the last search string | ||
+ | * also of two marks, resulting in eight total equal marks. | ||
+ | * | ||
+ | * @param string $record | ||
+ | * | ||
+ | * @return string | ||
+ | */ | ||
+ | private function convertHeadings($record) | ||
+ | { | ||
+ | $patterns = array( | ||
+ | '/ | ||
+ | '/ | ||
+ | '/ | ||
+ | '/ | ||
+ | '/ | ||
+ | ); | ||
+ | |||
+ | // Insert a unique string to the replacement so that it won't be | ||
+ | // caught in a search later. | ||
+ | // @todo A lambda function can be used when PHP 5.4 is required. | ||
+ | array_walk( | ||
+ | $patterns, | ||
+ | create_function( | ||
+ | '& | ||
+ | '$v = "' | ||
+ | ) | ||
+ | ); | ||
+ | |||
+ | $convertedRecord = preg_replace( | ||
+ | array_keys($patterns), | ||
+ | array_values($patterns), | ||
+ | $record | ||
+ | ); | ||
+ | |||
+ | // No headings were found. | ||
+ | if ($convertedRecord ===== $record) { | ||
+ | return $record; | ||
+ | } | ||
+ | |||
+ | // Strip out the unique strings. | ||
+ | return str_replace($this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ^ 누구나 수정하실 수 있습니다. [[http:// | ||
+ | |||
+ | |||