r82216 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:< r82215‎ | r82216 | r82217 >
Date:04:47, 16 February 2011
Author:demon
Status:ok
Tags:
Comment:
Fixes for DoubleWiki
* Throw rendered text in memcached for 12 hours. It's not elegant, but it sure beats making external requests and doing dozen(s) of regular expressions on it
* Use $wgContLang->getCode() rather than $wgContLanguageCode
Modified paths:
  • /trunk/extensions/DoubleWiki/DoubleWiki.php (modified) (history)
  • /trunk/extensions/DoubleWiki/DoubleWiki_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DoubleWiki/DoubleWiki_body.php
——@@ -55,7 +55,7 @@
5656 * @param $text
5757 */
5858 function addMatchedText( &$out, &$text ) {
59 - global $wgContLang, $wgRequest, $wgLang, $wgContLanguageCode;
 59+ global $wgContLang, $wgRequest, $wgLang, $wgMemc, $wgDoubleWikiCacheTime;
6060
6161 $match_request = $wgRequest->getText( 'match' );
6262 if ( $match_request === '' ) {
——@@ -68,48 +68,54 @@
6969 if ( isset( $langLinks[$match_request] ) ) {
7070 $nt = Title::newFromText( $langLinks[$match_request] );
7171
72 - $url = $nt->getFullURL();
73 - $myURL = $out->getTitle()->getLocalURL();
74 - $languageName = $wgContLang->getLanguageName( $nt->getInterwiki() );
75 - $myLanguage = $wgLang->getLanguageName( $wgContLanguageCode );
 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 );
7699
77 - $translation = Http::get( wfAppendQuery( $url, array( 'action' => 'render' ) ) );
78 - if ( $translation !== null ) {
79 - /**
80 - * first find all links that have no 'class' parameter.
81 - * these links are local so we add '?match=xx' to their url,
82 - * unless it already contains a '?'
83 - */
84 - $translation = preg_replace(
85 - "/<a href=\"http:\/\/([^\"\?]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
86 - "<a href=\"http://\\1?match={$wgContLanguageCode}\"\\2>", $translation );
87 - // now add class='extiw' to these links
88 - $translation = preg_replace(
89 - "/<a href=\"http:\/\/([^\"]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
90 - "<a href=\"http://\\1\" class=\"extiw\"\\3>", $translation );
91 - // use class='extiw' for images too
92 - $translation = preg_replace(
93 - "/<a href=\"http:\/\/([^\"]*)\"([^\>]*)class=\"image\"([^\>]*)\>/i",
94 - "<a href=\"http://\\1\"\\2class=\"extiw\"\\3>", $translation );
 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 );
95107
96 - // add prefixes to internal links, in order to prevent duplicates
97 - $translation = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#l_\\1\"",
98 - $translation );
99 - $translation = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"l_\\1\"",
100 - $translation );
101 - $text = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#r_\\1\"", $text );
102 - $text = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"r_\\1\"", $text );
 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 );
103111
104 - // add ?match= to local links of the local wiki
105 - $text = preg_replace( "/<a href=\"\/([^\"\?]*)\"/i",
106 - "<a href=\"/\\1?match={$match_request}\"", $text );
 112+ // do the job
 113+ $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode,
 114+ $translation, $languageName, $url, $match_request );
107115
108 - // do the job
109 - $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode,
110 - $translation, $languageName, $url, $match_request );
 116+ $wgMemc->set( $key, $text, $wgDoubleWikiCacheTime );
 117+ }
111118 }
112119 }
113 -
114120 return true;
115121 }
116122
Index: trunk/extensions/DoubleWiki/DoubleWiki.php
——@@ -25,6 +25,8 @@
2626 $wgExtensionMessagesFiles['DoubleWiki'] = dirname(__FILE__) . '/DoubleWiki.i18n.php';
2727 $wgAutoloadClasses['DoubleWiki'] = dirname( __FILE__ ) . "/DoubleWiki_body.php";
2828 $wgHooks['OutputPageBeforeHTML'][] = 'DoubleWiki::OutputPageBeforeHTML';
 29+// How long cached page output is stored in memcached
 30+$wgDoubleWikiCacheTime = 3600 * 12;
2931
3032 $wgExtensionCredits['other'][] = array(
3133 'path' => __FILE__,

Status & tagging log