r82229 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82228‎ | r82229 | r82230 >
Date:10:22, 16 February 2011
Author:reedy
Status:ok
Tags:
Comment:
Partial revert to r82202. Readd the loop, keep a couple of the improvements that the rev brought
Modified paths:
  • /trunk/extensions/DoubleWiki/DoubleWiki_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DoubleWiki/DoubleWiki_body.php
@@ -15,8 +15,6 @@
1616 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717 # http://www.gnu.org/copyleft/gpl.html
1818
19 -
20 -
2119 class DoubleWiki {
2220
2321 /**
@@ -55,7 +53,7 @@
5654 * @param $text
5755 */
5856 function addMatchedText( &$out, &$text ) {
59 - global $wgContLang, $wgRequest, $wgLang, $wgMemc, $wgDoubleWikiCacheTime;
 57+ global $wgContLang, $wgContLanguageCode, $wgRequest, $wgLang, $wgMemc, $wgDoubleWikiCacheTime;
6058
6159 $match_request = $wgRequest->getText( 'match' );
6260 if ( $match_request === '' ) {
@@ -65,55 +63,63 @@
6664
6765 $langLinks = $out->getLanguageLinks();
6866
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();
7170
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] );
9973
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 );
10776
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' ) ) );
11185
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 );
115103
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+ }
117122 }
 123+ break;
118124 }
119125 }
120126 return true;

Follow-up revisions

RevisionCommit summaryAuthorDate
r82281Fix for r82229, extra title construction that didnt work anywaydemon19:31, 16 February 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r82202Cannot fathom out why the loop was even needed...reedy22:32, 15 February 2011

Status & tagging log