| Index: trunk/extensions/DoubleWiki/DoubleWiki_body.php |
| — | — | @@ -15,8 +15,6 @@ |
| 16 | 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | 17 | # http://www.gnu.org/copyleft/gpl.html |
| 18 | 18 | |
| 19 | | - |
| 20 | | - |
| 21 | 19 | class DoubleWiki { |
| 22 | 20 | |
| 23 | 21 | /** |
| — | — | @@ -55,7 +53,7 @@ |
| 56 | 54 | * @param $text |
| 57 | 55 | */ |
| 58 | 56 | function addMatchedText( &$out, &$text ) { |
| 59 | | - global $wgContLang, $wgRequest, $wgLang, $wgMemc, $wgDoubleWikiCacheTime; |
| | 57 | + global $wgContLang, $wgContLanguageCode, $wgRequest, $wgLang, $wgMemc, $wgDoubleWikiCacheTime; |
| 60 | 58 | |
| 61 | 59 | $match_request = $wgRequest->getText( 'match' ); |
| 62 | 60 | if ( $match_request === '' ) { |
| — | — | @@ -65,55 +63,63 @@ |
| 66 | 64 | |
| 67 | 65 | $langLinks = $out->getLanguageLinks(); |
| 68 | 66 | |
| 69 | | - if ( isset( $langLinks[$match_request] ) ) { |
| 70 | | - $nt = Title::newFromText( $langLinks[$match_request] ); |
| | 67 | + foreach( $langLinks as $l) { |
| | 68 | + $nt = Title::newFromText( $l ); |
| | 69 | + $iw = $nt->getInterwiki(); |
| 71 | 70 | |
| 72 | | - $key = wfMemcKey( 'doublewiki', $wgLang->getCode(), $nt->getPrefixedDbKey() ); |
| 73 | | - $cachedText = $wgMemc->get( $key ); |
| 74 | | - if( $cachedText ) { |
| 75 | | - $text = $cachedText; |
| 76 | | - } else { |
| 77 | | - $url = $nt->getFullURL(); |
| 78 | | - $myURL = $out->getTitle()->getLocalURL(); |
| 79 | | - $languageName = $wgContLang->getLanguageName( $nt->getInterwiki() ); |
| 80 | | - $myLanguage = $wgLang->getLanguageName( $wgContLang->getCode() ); |
| 81 | | - $translation = Http::get( wfAppendQuery( $url, array( 'action' => 'render' ) ) ); |
| 82 | | - if ( $translation !== null ) { |
| 83 | | - /** |
| 84 | | - * first find all links that have no 'class' parameter. |
| 85 | | - * these links are local so we add '?match=xx' to their url, |
| 86 | | - * unless it already contains a '?' |
| 87 | | - */ |
| 88 | | - $translation = preg_replace( |
| 89 | | - "/<a href=\"http:\/\/([^\"\?]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i", |
| 90 | | - "<a href=\"http://\\1?match={$wgContLanguageCode}\"\\2>", $translation ); |
| 91 | | - // now add class='extiw' to these links |
| 92 | | - $translation = preg_replace( |
| 93 | | - "/<a href=\"http:\/\/([^\"]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i", |
| 94 | | - "<a href=\"http://\\1\" class=\"extiw\"\\3>", $translation ); |
| 95 | | - // use class='extiw' for images too |
| 96 | | - $translation = preg_replace( |
| 97 | | - "/<a href=\"http:\/\/([^\"]*)\"([^\>]*)class=\"image\"([^\>]*)\>/i", |
| 98 | | - "<a href=\"http://\\1\"\\2class=\"extiw\"\\3>", $translation ); |
| | 71 | + if ( $iw === $match_request ) { |
| | 72 | + $nt = Title::newFromText( $langLinks[$match_request] ); |
| 99 | 73 | |
| 100 | | - // add prefixes to internal links, in order to prevent duplicates |
| 101 | | - $translation = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#l_\\1\"", |
| 102 | | - $translation ); |
| 103 | | - $translation = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"l_\\1\"", |
| 104 | | - $translation ); |
| 105 | | - $text = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#r_\\1\"", $text ); |
| 106 | | - $text = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"r_\\1\"", $text ); |
| | 74 | + $key = wfMemcKey( 'doublewiki', $wgLang->getCode(), $nt->getPrefixedDbKey() ); |
| | 75 | + $cachedText = $wgMemc->get( $key ); |
| 107 | 76 | |
| 108 | | - // add ?match= to local links of the local wiki |
| 109 | | - $text = preg_replace( "/<a href=\"\/([^\"\?]*)\"/i", |
| 110 | | - "<a href=\"/\\1?match={$match_request}\"", $text ); |
| | 77 | + if( $cachedText ) { |
| | 78 | + $text = $cachedText; |
| | 79 | + } else { |
| | 80 | + $url = $nt->getFullURL(); |
| | 81 | + $myURL = $out->getTitle()->getLocalURL(); |
| | 82 | + $languageName = $wgContLang->getLanguageName( $iw ); |
| | 83 | + $myLanguage = $wgLang->getLanguageName( $wgContLang->getCode() ); |
| | 84 | + $translation = Http::get( wfAppendQuery( $url, array( 'action' => 'render' ) ) ); |
| 111 | 85 | |
| 112 | | - // do the job |
| 113 | | - $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode, |
| 114 | | - $translation, $languageName, $url, $match_request ); |
| | 86 | + if ( $translation !== null ) { |
| | 87 | + /** |
| | 88 | + * first find all links that have no 'class' parameter. |
| | 89 | + * these links are local so we add '?match=xx' to their url, |
| | 90 | + * unless it already contains a '?' |
| | 91 | + */ |
| | 92 | + $translation = preg_replace( |
| | 93 | + "/<a href=\"http:\/\/([^\"\?]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i", |
| | 94 | + "<a href=\"http://\\1?match={$wgContLanguageCode}\"\\2>", $translation ); |
| | 95 | + // now add class='extiw' to these links |
| | 96 | + $translation = preg_replace( |
| | 97 | + "/<a href=\"http:\/\/([^\"]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i", |
| | 98 | + "<a href=\"http://\\1\" class=\"extiw\"\\3>", $translation ); |
| | 99 | + // use class='extiw' for images too |
| | 100 | + $translation = preg_replace( |
| | 101 | + "/<a href=\"http:\/\/([^\"]*)\"([^\>]*)class=\"image\"([^\>]*)\>/i", |
| | 102 | + "<a href=\"http://\\1\"\\2class=\"extiw\"\\3>", $translation ); |
| 115 | 103 | |
| 116 | | - $wgMemc->set( $key, $text, $wgDoubleWikiCacheTime ); |
| | 104 | + // add prefixes to internal links, in order to prevent duplicates |
| | 105 | + $translation = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#l_\\1\"", |
| | 106 | + $translation ); |
| | 107 | + $translation = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"l_\\1\"", |
| | 108 | + $translation ); |
| | 109 | + $text = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#r_\\1\"", $text ); |
| | 110 | + $text = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"r_\\1\"", $text ); |
| | 111 | + |
| | 112 | + // add ?match= to local links of the local wiki |
| | 113 | + $text = preg_replace( "/<a href=\"\/([^\"\?]*)\"/i", |
| | 114 | + "<a href=\"/\\1?match={$match_request}\"", $text ); |
| | 115 | + |
| | 116 | + // do the job |
| | 117 | + $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode, |
| | 118 | + $translation, $languageName, $url, $match_request ); |
| | 119 | + |
| | 120 | + $wgMemc->set( $key, $text, $wgDoubleWikiCacheTime ); |
| | 121 | + } |
| 117 | 122 | } |
| | 123 | + break; |
| 118 | 124 | } |
| 119 | 125 | } |
| 120 | 126 | return true; |