Index: branches/iwtransclusion/phase3/includes/Interwiki.php |
— | — | @@ -170,14 +170,14 @@ |
171 | 171 | * @return Boolean: whether everything was there |
172 | 172 | */ |
173 | 173 | protected static function loadFromArray( $mc ) { |
174 | | - if( isset( $mc['iw_url'] ) && isset( $mc['iw_api'] ) && isset( $mc['iw_wikiid'] ) |
175 | | - && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) { |
| 174 | + if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) { |
176 | 175 | $iw = new Interwiki(); |
177 | 176 | $iw->mURL = $mc['iw_url']; |
178 | | - $iw->mAPI = $mc['iw_api']; |
179 | | - $iw->mWikiID = $mc['iw_wikiid']; |
180 | 177 | $iw->mLocal = $mc['iw_local']; |
181 | 178 | $iw->mTrans = $mc['iw_trans']; |
| 179 | + $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : ''; |
| 180 | + $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : ''; |
| 181 | + |
182 | 182 | return $iw; |
183 | 183 | } |
184 | 184 | return false; |
— | — | @@ -256,4 +256,187 @@ |
257 | 257 | $msg = wfMsgForContent( $key ); |
258 | 258 | return wfEmptyMsg( $key, $msg ) ? '' : $msg; |
259 | 259 | } |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | + /** |
| 264 | + * Transclude an interwiki link. |
| 265 | + * TODO: separate in interwikiTranscludeFromDB & interwikiTranscludeFromAPI according to the iw type |
| 266 | + */ |
| 267 | + public static function interwikiTransclude( $title ) { |
| 268 | + |
| 269 | + global $wgEnableScaryTranscluding; |
| 270 | + |
| 271 | + if ( !$wgEnableScaryTranscluding ) { |
| 272 | + return wfMsg('scarytranscludedisabled'); |
| 273 | + } |
| 274 | + |
| 275 | + // If we have a wikiID, we will use it to get an access to the remote database |
| 276 | + // if not, we will use the API URL to retrieve the data through a HTTP Get |
| 277 | + |
| 278 | + $wikiID = $title->getTransWikiID( ); |
| 279 | + $transAPI = $title->getTransAPI( ); |
| 280 | + |
| 281 | + if ( $wikiID !== '') { |
| 282 | + |
| 283 | + $finalText = self::fetchTemplateFromDB( $wikiID, $title->getNamespace(), $title->getDBkey()); |
| 284 | + $subTemplates = self::fetchSubTemplatesListFromDB( $wikiID, $title->getNamespace(), $title->getDBkey()); |
| 285 | + |
| 286 | + foreach ($subTemplates as $template) { |
| 287 | + $listSubTemplates.=$template['namespace'].':'.$template['title']."\n"; |
| 288 | + $list2.="<h2>".$template['title']."</h2>\n<pre>".self::fetchTemplateFromDB( $wikiID, $template['namespace'], $template['title'])."</pre>"; |
| 289 | + } |
| 290 | + |
| 291 | + } else if( $transAPI !== '' ) { |
| 292 | + |
| 293 | + $url1 = $transAPI."?action=query&prop=revisions&titles=$fullTitle&rvprop=content&format=json"; |
| 294 | + |
| 295 | + if ( strlen( $url1 ) > 255 ) { |
| 296 | + return wfMsg( 'Interwiki-transclusion-url-too-long' ); |
| 297 | + } |
| 298 | + |
| 299 | + $text = self::fetchTemplateHTTPMaybeFromCache( $url1 ); |
| 300 | + |
| 301 | + $fullTitle = $title->getNsText().':'.$title->getText(); |
| 302 | + |
| 303 | + $url2 = $transAPI."?action=parse&text={{".$fullTitle."}}&prop=templates&format=json"; |
| 304 | + |
| 305 | + $get = Http::get( $url2 ); |
| 306 | + $myArray = FormatJson::decode($get, true); |
| 307 | + |
| 308 | + if ( ! empty( $myArray['parse'] )) { |
| 309 | + $templates = $myArray['parse']['templates']; |
| 310 | + } |
| 311 | + |
| 312 | + |
| 313 | + // TODO: The templates are retrieved one by one. |
| 314 | + // We should split the templates in two groups: up-to-date and out-of-date |
| 315 | + // Only the second group would be retrieved through the API or DB request |
| 316 | + for ($i = 0 ; $i < count( $templates ) ; $i++) { |
| 317 | + $newTitle = $templates[$i]['*']; |
| 318 | + |
| 319 | + $url = $transAPI."?action=query&prop=revisions&titles=$newTitle&rvprop=content&format=json"; |
| 320 | + |
| 321 | + $listSubTemplates.= $newTitle."\n"; |
| 322 | + $list2.="<h2>".$newTitle."</h2>\n<pre>".self::fetchTemplateHTTPMaybeFromCache( $url )."</pre>"; |
| 323 | + |
| 324 | + } |
| 325 | + |
| 326 | + $finalText = "$url1\n$url2\n$text"; |
| 327 | + |
| 328 | + } else { |
| 329 | + return wfMsg( 'Interwiki-transclusion-failed' ); |
| 330 | + } |
| 331 | + |
| 332 | + return "<h2>$fullTitle</h2><pre>$finalText</pre> List of templates: <pre>".$listSubTemplates.'</pre>' . $list2; |
| 333 | + } |
| 334 | + |
| 335 | + public static function fetchTemplateFromDB ( $wikiID, $namespace, $DBkey ) { |
| 336 | + |
| 337 | + try { |
| 338 | + $dbr = wfGetDb( DB_SLAVE, array(), $wikiID ); |
| 339 | + } catch (Exception $e) { |
| 340 | + return wfMsg( 'Failed-to-connect-the-distant-DB' ); |
| 341 | + } |
| 342 | + |
| 343 | + $fields = array('old_text', 'page_id'); |
| 344 | + $res = $dbr->select( |
| 345 | + array( 'page', 'revision', 'text' ), |
| 346 | + $fields, |
| 347 | + array( 'rev_id=page_latest', |
| 348 | + 'page_namespace' => $namespace, |
| 349 | + 'page_title' => $DBkey, |
| 350 | + 'page_id=rev_page', |
| 351 | + 'rev_text_id=old_id'), |
| 352 | + null, |
| 353 | + array( 'LIMIT' => 1 ) |
| 354 | + ); |
| 355 | + |
| 356 | + $obj = $dbr->resultObject( $res ); |
| 357 | + |
| 358 | + if ( $obj ) { |
| 359 | + $row = $obj->fetchObject(); |
| 360 | + $obj->free(); |
| 361 | + |
| 362 | + if( $row ) { |
| 363 | + $res = new Revision( $row ); |
| 364 | + $articleID = $res->mTextRow->page_id; |
| 365 | + $text = $articleID."\n".$res->mTextRow->old_text; |
| 366 | + } |
| 367 | + } |
| 368 | + |
| 369 | + return $text; |
| 370 | + } |
| 371 | + |
| 372 | + public static function fetchSubTemplatesListFromDB ( $wikiID, $namespace, $DBkey ) { |
| 373 | + |
| 374 | + try { |
| 375 | + $dbr = wfGetDb( DB_SLAVE, array(), $wikiID ); |
| 376 | + } catch (Exception $e) { |
| 377 | + return wfMsg( 'Failed-to-connect-the-distant-DB' ); |
| 378 | + } |
| 379 | + |
| 380 | + $fields = array('tl_namespace', 'tl_title'); |
| 381 | + $res = $dbr->select( |
| 382 | + array( 'page', 'templatelinks' ), |
| 383 | + $fields, |
| 384 | + array( 'page_namespace' => $namespace, |
| 385 | + 'page_title' => $DBkey, |
| 386 | + 'tl_from=page_id' ) |
| 387 | + ); |
| 388 | + |
| 389 | + $obj = $dbr->resultObject( $res ); |
| 390 | + |
| 391 | + if ( $obj ) { |
| 392 | + |
| 393 | + $listTemplates = array(); |
| 394 | + |
| 395 | + while ($row = $obj->fetchObject() ) { |
| 396 | + $listTemplates[] = array( 'namespace' => $row->tl_namespace, 'title' => $row->tl_title ); |
| 397 | + } |
| 398 | + $obj->free(); |
| 399 | + } |
| 400 | + |
| 401 | + return $listTemplates; |
| 402 | + } |
| 403 | + |
| 404 | + public static function fetchTemplateHTTPMaybeFromCache( $url ) { |
| 405 | + global $wgTranscludeCacheExpiry; |
| 406 | + $dbr = wfGetDB( DB_SLAVE ); |
| 407 | + $tsCond = $dbr->timestamp( time() - $wgTranscludeCacheExpiry ); |
| 408 | + $obj = $dbr->selectRow( 'transcache', array('tc_time', 'tc_contents' ), |
| 409 | + array( 'tc_url' => $url, "tc_time >= " . $dbr->addQuotes( $tsCond ) ) ); |
| 410 | + |
| 411 | + if ( $obj ) { |
| 412 | + return $obj->tc_contents; |
| 413 | + } |
| 414 | + |
| 415 | + $get = Http::get( $url ); |
| 416 | + |
| 417 | + $content = FormatJson::decode( $get, true ); |
| 418 | + |
| 419 | + if ( ! empty($content['query']['pages']) ) { |
| 420 | + |
| 421 | + $page = array_pop( $content['query']['pages'] ); |
| 422 | + $text = $page['revisions'][0]['*']; |
| 423 | + |
| 424 | + } else { |
| 425 | + |
| 426 | + return wfMsg( 'scarytranscludefailed', $url ); |
| 427 | + |
| 428 | + } |
| 429 | + |
| 430 | + $dbw = wfGetDB( DB_MASTER ); |
| 431 | + $dbw->replace( 'transcache', array('tc_url'), array( |
| 432 | + 'tc_url' => $url, |
| 433 | + 'tc_time' => $dbw->timestamp( time() ), |
| 434 | + 'tc_contents' => $text) |
| 435 | + ); |
| 436 | + |
| 437 | + return $text; |
| 438 | + } |
| 439 | + |
| 440 | + |
| 441 | + |
| 442 | + |
260 | 443 | } |
Index: branches/iwtransclusion/phase3/includes/parser/Parser.php |
— | — | @@ -3166,16 +3166,12 @@ |
3167 | 3167 | } |
3168 | 3168 | } elseif ( $title->isTrans() ) { |
3169 | 3169 | // TODO: Work by Peter17 in progress |
3170 | | - # Interwiki transclusion |
3171 | | - //if ( $this->ot['html'] && !$forceRawInterwiki ) { |
3172 | | - // $text = $this->interwikiTransclude( $title, 'render' ); |
3173 | | - // $isHTML = true; |
3174 | | - //} else { |
3175 | | - $text = $this->interwikiTransclude( $title ); |
| 3170 | + |
| 3171 | + $text = Interwiki::interwikiTransclude( $title ); |
3176 | 3172 | # Preprocess it like a template |
3177 | 3173 | $text = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION ); |
3178 | 3174 | $isChildObj = true; |
3179 | | - //} |
| 3175 | + |
3180 | 3176 | $found = true; |
3181 | 3177 | } |
3182 | 3178 | |
— | — | @@ -3386,116 +3382,6 @@ |
3387 | 3383 | |
3388 | 3384 | |
3389 | 3385 | /** |
3390 | | - * Transclude an interwiki link. |
3391 | | - * TODO: separate in interwikiTranscludeFromDB & interwikiTranscludeFromAPI according to the iw type |
3392 | | - */ |
3393 | | - function interwikiTransclude( $title ) { |
3394 | | - |
3395 | | - global $wgEnableScaryTranscluding; |
3396 | | - |
3397 | | - if ( !$wgEnableScaryTranscluding ) { |
3398 | | - return wfMsg('scarytranscludedisabled'); |
3399 | | - } |
3400 | | - |
3401 | | - $fullTitle = $title->getNsText().':'.$title->getText(); |
3402 | | - |
3403 | | - $url1 = $title->getTransAPI( )."?action=query&prop=revisions&titles=$fullTitle&rvprop=content&format=json"; |
3404 | | - |
3405 | | - if ( strlen( $url1 ) > 255 ) { |
3406 | | - return wfMsg( 'scarytranscludetoolong' ); |
3407 | | - } |
3408 | | - |
3409 | | - $text = $this->fetchTemplateMaybeFromCache( $url1 ); |
3410 | | - |
3411 | | - $url2 = $title->getTransAPI( )."?action=parse&text={{".$fullTitle."}}&prop=templates&format=json"; |
3412 | | - |
3413 | | - $get = Http::get( $url2 ); |
3414 | | - $myArray = FormatJson::decode($get, true); |
3415 | | - |
3416 | | - if ( ! empty( $myArray['parse'] )) { |
3417 | | - $templates = $myArray['parse']['templates']; |
3418 | | - } |
3419 | | - |
3420 | | - |
3421 | | - // TODO: The templates are retrieved one by one. |
3422 | | - // We should split the templates in two groups: up-to-date and out-of-date |
3423 | | - // Only the second group would be retrieved through the API or DB request |
3424 | | - for ($i = 0 ; $i < count( $templates ) ; $i++) { |
3425 | | - $newTitle = $templates[$i]['*']; |
3426 | | - |
3427 | | - $url = $title->getTransAPI( )."?action=query&prop=revisions&titles=$newTitle&rvprop=content&format=json"; |
3428 | | - |
3429 | | - $listSubTemplates.= $newTitle."\n"; |
3430 | | - $list2.="<h2>".$newTitle."</h2>\n<pre>".$this->fetchTemplateMaybeFromCache( $url )."</pre>"; |
3431 | | - |
3432 | | - } |
3433 | | - |
3434 | | - return "<h2>$fullTitle</h2><pre>$url1\n$url2\n$text</pre> List of templates: <pre>".$listSubTemplates.'</pre>' . $list2; |
3435 | | - } |
3436 | | - |
3437 | | - |
3438 | | - function fetchTemplateMaybeFromCache( $url ) { |
3439 | | - global $wgTranscludeCacheExpiry; |
3440 | | - $dbr = wfGetDB( DB_SLAVE ); |
3441 | | - $tsCond = $dbr->timestamp( time() - $wgTranscludeCacheExpiry ); |
3442 | | - $obj = $dbr->selectRow( 'transcache', array('tc_time', 'tc_contents' ), |
3443 | | - array( 'tc_url' => $url, "tc_time >= " . $dbr->addQuotes( $tsCond ) ) ); |
3444 | | - |
3445 | | - if ( $obj ) { |
3446 | | - return $obj->tc_contents; |
3447 | | - } |
3448 | | - |
3449 | | - $get = Http::get( $url ); |
3450 | | - |
3451 | | - $content = FormatJson::decode( $get, true ); |
3452 | | - |
3453 | | - if ( ! empty($content['query']['pages']) ) { |
3454 | | - |
3455 | | - $page = array_pop( $content['query']['pages'] ); |
3456 | | - $text = $page['revisions'][0]['*']; |
3457 | | - |
3458 | | - } else { |
3459 | | - |
3460 | | - return wfMsg( 'scarytranscludefailed', $url ); |
3461 | | - |
3462 | | - } |
3463 | | - |
3464 | | - $dbw = wfGetDB( DB_MASTER ); |
3465 | | - $dbw->replace( 'transcache', array('tc_url'), array( |
3466 | | - 'tc_url' => $url, |
3467 | | - 'tc_time' => $dbw->timestamp( time() ), |
3468 | | - 'tc_contents' => $text) |
3469 | | - ); |
3470 | | - |
3471 | | - return $text; |
3472 | | - } |
3473 | | - |
3474 | | - function fetchScaryTemplateMaybeFromCache( $url ) { |
3475 | | - global $wgTranscludeCacheExpiry; |
3476 | | - $dbr = wfGetDB( DB_SLAVE ); |
3477 | | - $tsCond = $dbr->timestamp( time() - $wgTranscludeCacheExpiry ); |
3478 | | - $obj = $dbr->selectRow( 'transcache', array('tc_time', 'tc_contents' ), |
3479 | | - array( 'tc_url' => $url, "tc_time >= " . $dbr->addQuotes( $tsCond ) ) ); |
3480 | | - if ( $obj ) { |
3481 | | - return $obj->tc_contents; |
3482 | | - } |
3483 | | - |
3484 | | - $text = Http::get( $url ); |
3485 | | - if ( !$text ) { |
3486 | | - return wfMsg( 'scarytranscludefailed', $url ); |
3487 | | - } |
3488 | | - |
3489 | | - $dbw = wfGetDB( DB_MASTER ); |
3490 | | - $dbw->replace( 'transcache', array('tc_url'), array( |
3491 | | - 'tc_url' => $url, |
3492 | | - 'tc_time' => $dbw->timestamp( time() ), |
3493 | | - 'tc_contents' => $text) |
3494 | | - ); |
3495 | | - return $text; |
3496 | | - } |
3497 | | - |
3498 | | - |
3499 | | - /** |
3500 | 3386 | * Triple brace replacement -- used for template arguments |
3501 | 3387 | * @private |
3502 | 3388 | */ |