r68170 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68169‎ | r68170 | r68171 >
Date:13:43, 17 June 2010
Author:peter17
Status:ok
Tags:
Comment:
First version of my code, for interwiki transclusion
Modified paths:
  • /branches/iwtransclusion/phase3/includes/Interwiki.php (modified) (history)
  • /branches/iwtransclusion/phase3/includes/Title.php (modified) (history)
  • /branches/iwtransclusion/phase3/includes/parser/Parser.php (modified) (history)

Diff [purge]

Index: branches/iwtransclusion/phase3/includes/Interwiki.php
@@ -15,11 +15,12 @@
1616 protected static $smCache = array();
1717 const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
1818
19 - protected $mPrefix, $mURL, $mLocal, $mTrans;
 19+ protected $mPrefix, $mURL, $mAPI, $mLocal, $mTrans;
2020
21 - public function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 ) {
 21+ public function __construct( $prefix = null, $url = '', $api = '', $local = 0, $trans = 0 ) {
2222 $this->mPrefix = $prefix;
2323 $this->mURL = $url;
 24+ $this->mAPI = $api;
2425 $this->mLocal = $local;
2526 $this->mTrans = $trans;
2627 }
@@ -153,7 +154,7 @@
154155 __METHOD__ ) );
155156 $iw = Interwiki::loadFromArray( $row );
156157 if ( $iw ) {
157 - $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
 158+ $mc = array( 'iw_url' => $iw->mURL, 'iw_api' => $iw->mAPI, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
158159 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
159160 return $iw;
160161 }
@@ -168,9 +169,10 @@
169170 * @return Boolean: whether everything was there
170171 */
171172 protected static function loadFromArray( $mc ) {
172 - if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
 173+ if( isset( $mc['iw_url'] ) && isset( $mc['iw_api'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
173174 $iw = new Interwiki();
174175 $iw->mURL = $mc['iw_url'];
 176+ $iw->mAPI = $mc['iw_api'];
175177 $iw->mLocal = $mc['iw_local'];
176178 $iw->mTrans = $mc['iw_trans'];
177179 return $iw;
@@ -193,6 +195,17 @@
194196 }
195197
196198 /**
 199+ * Get the API URL for this wiki
 200+ *
 201+ * @return String: the URL
 202+ */
 203+ public function getAPI( ) {
 204+ $url = $this->mAPI;
 205+
 206+ return $url;
 207+ }
 208+
 209+ /**
197210 * Is this a local link from a sister project, or is
198211 * it something outside, like Google
199212 *
Index: branches/iwtransclusion/phase3/includes/parser/Parser.php
@@ -2846,7 +2846,6 @@
28472847 $found = false; # $text has been filled
28482848 $nowiki = false; # wiki markup in $text should be escaped
28492849 $isHTML = false; # $text is HTML, armour it against wikitext transformation
2850 - $forceRawInterwiki = false; # Force interwiki transclusion to be done in raw mode not rendered
28512850 $isChildObj = false; # $text is a DOM node needing expansion in a child frame
28522851 $isLocalObj = false; # $text is a DOM node needing expansion in the current frame
28532852
@@ -2917,12 +2916,6 @@
29182917 $mwMsg = MagicWord::get( 'msg' );
29192918 $mwMsg->matchStartAndRemove( $part1 );
29202919 }
2921 -
2922 - # Check for RAW:
2923 - $mwRaw = MagicWord::get( 'raw' );
2924 - if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
2925 - $forceRawInterwiki = true;
2926 - }
29272920 }
29282921 wfProfileOut( __METHOD__.'-modifiers' );
29292922
@@ -3056,16 +3049,17 @@
30573050 $found = true;
30583051 }
30593052 } elseif ( $title->isTrans() ) {
 3053+ // TODO: Work by Peter17 in progress
30603054 # Interwiki transclusion
3061 - if ( $this->ot['html'] && !$forceRawInterwiki ) {
3062 - $text = $this->interwikiTransclude( $title, 'render' );
3063 - $isHTML = true;
3064 - } else {
 3055+ //if ( $this->ot['html'] && !$forceRawInterwiki ) {
 3056+ // $text = $this->interwikiTransclude( $title, 'render' );
 3057+ // $isHTML = true;
 3058+ //} else {
30653059 $text = $this->interwikiTransclude( $title, 'raw' );
30663060 # Preprocess it like a template
30673061 $text = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
30683062 $isChildObj = true;
3069 - }
 3063+ //}
30703064 $found = true;
30713065 }
30723066
@@ -3274,24 +3268,91 @@
32753269 'deps' => $deps );
32763270 }
32773271
 3272+
32783273 /**
32793274 * Transclude an interwiki link.
 3275+ * TODO: separate in interwikiTranscludeFromDB & interwikiTranscludeFromAPI according to the iw type
32803276 */
32813277 function interwikiTransclude( $title, $action ) {
 3278+
32823279 global $wgEnableScaryTranscluding;
32833280
32843281 if ( !$wgEnableScaryTranscluding ) {
32853282 return wfMsg('scarytranscludedisabled');
32863283 }
32873284
3288 - $url = $title->getFullUrl( "action=$action" );
 3285+ $url1 = $title->getTransAPI( )."?action=query&prop=revisions&titles=$titles&rvprop=content&format=json";
32893286
3290 - if ( strlen( $url ) > 255 ) {
 3287+ if ( strlen( $url1 ) > 255 ) {
32913288 return wfMsg( 'scarytranscludetoolong' );
32923289 }
3293 - return $this->fetchScaryTemplateMaybeFromCache( $url );
 3290+
 3291+ $text = $this->fetchTemplateMaybeFromCache( $url1 );
 3292+
 3293+ $titles = $title->getNsText().':'.$title->getText();
 3294+ $url2 = $title->getTransAPI( )."?action=parse&text={{".$titles."}}&prop=templates&format=json";
 3295+
 3296+ $get = Http::get( $url2 );
 3297+ $myArray = FormatJson::decode($get, true);
 3298+
 3299+ if ( ! empty( $myArray['parse'] )) {
 3300+ $templates = $myArray['parse']['templates'];
 3301+ }
 3302+
 3303+
 3304+ // TODO: The templates are retrieved one by one.
 3305+ // We should split the templates in two groups: up-to-date and out-of-date
 3306+ // Only the second group would be retrieved through the API or DB request
 3307+ for ($i = 0 ; $i < count( $templates ) ; $i++) {
 3308+ $newTitle = $templates[$i]['*'];
 3309+
 3310+ $url = $title->getTransAPI( )."?action=query&prop=revisions&titles=$newTitle&rvprop=content&format=json";
 3311+
 3312+ $listSubTemplates.= $newTitle."\n";
 3313+ $list2.="<h2>".$newTitle."</h2>\n<pre>".$this->fetchTemplateMaybeFromCache( $url )."</pre>";
 3314+
 3315+ }
 3316+
 3317+ return "<h2>$titles</h2><pre>$text</pre> List of templates: <pre>".$listSubTemplates.'</pre>' . $list2;
32943318 }
 3319+
 3320+
 3321+ function fetchTemplateMaybeFromCache( $url ) {
 3322+ global $wgTranscludeCacheExpiry;
 3323+ $dbr = wfGetDB( DB_SLAVE );
 3324+ $tsCond = $dbr->timestamp( time() - $wgTranscludeCacheExpiry );
 3325+ $obj = $dbr->selectRow( 'transcache', array('tc_time', 'tc_contents' ),
 3326+ array( 'tc_url' => $url, "tc_time >= " . $dbr->addQuotes( $tsCond ) ) );
32953327
 3328+ if ( $obj ) {
 3329+ return $obj->tc_contents;
 3330+ }
 3331+
 3332+ $get = Http::get( $url );
 3333+
 3334+ $content = FormatJson::decode( $get, true );
 3335+
 3336+ if ( ! empty($content['query']['pages']) ) {
 3337+
 3338+ $page = array_pop( $content['query']['pages'] );
 3339+ $text = $page['revisions'][0]['*'];
 3340+
 3341+ } else {
 3342+
 3343+ return wfMsg( 'scarytranscludefailed', $url );
 3344+
 3345+ }
 3346+
 3347+ $dbw = wfGetDB( DB_MASTER );
 3348+ $dbw->replace( 'transcache', array('tc_url'), array(
 3349+ 'tc_url' => $url,
 3350+ 'tc_time' => $dbw->timestamp( time() ),
 3351+ 'tc_contents' => $text)
 3352+ );
 3353+
 3354+ return $text;
 3355+ }
 3356+
32963357 function fetchScaryTemplateMaybeFromCache( $url ) {
32973358 global $wgTranscludeCacheExpiry;
32983359 $dbr = wfGetDB( DB_SLAVE );
Index: branches/iwtransclusion/phase3/includes/Title.php
@@ -513,7 +513,20 @@
514514
515515 return Interwiki::fetch( $this->mInterwiki )->isTranscludable();
516516 }
 517+
 518+ /**
 519+ * Returns the API URL of the distant wiki
 520+ * which owns the object.
 521+ *
 522+ * @return \type{\string} the API URL
 523+ */
 524+ public function getTransAPI() {
 525+ if ( $this->mInterwiki == '' )
 526+ return false;
517527
 528+ return Interwiki::fetch( $this->mInterwiki )->getAPI();
 529+ }
 530+
518531 /**
519532 * Escape a text fragment, say from a link, for a URL
520533 *

Follow-up revisions

RevisionCommit summaryAuthorDate
r69542Add iw_api and iw_wikiid fields to the interwiki table, plus rudimentary supp...catrope11:55, 19 July 2010
r87104Merge of r86170, r68448, r69480reedy00:19, 29 April 2011
r92985Merge r87104, which itself is a merge of r68170, r68448, r69480reedy17:19, 24 July 2011

Status & tagging log