r12925 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r12924‎ | r12925 | r12926 >
Date:04:41, 1 February 2006
Author:timstarling
Status:old
Tags:
Comment:
Various improvements to interwiki transclusion. Introduced {{raw:..}}, to force raw transclusion when subst: isn't in use. Referer header set in wfGetHTTP(), this could be for weak (honour-bound) detection of the external site, for statistics or license notices.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/HttpFunctions.php (modified) (history)
  • /trunk/phase3/includes/MagicWord.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/MagicWord.php
@@ -69,6 +69,7 @@
7070 'MAG_UC',
7171 'MAG_FULLPAGENAME',
7272 'MAG_FULLPAGENAMEE',
 73+ 'MAG_RAW',
7374 );
7475 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
7576 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
Index: trunk/phase3/includes/Title.php
@@ -494,7 +494,7 @@
495495 function isTrans() {
496496 global $wgTitleInterwikiCache, $wgDBname;
497497
498 - if ($this->mInterwiki == '' || !$this->isLocal())
 498+ if ($this->mInterwiki == '')
499499 return false;
500500 # Make sure key is loaded into cache
501501 $this->getInterwikiLink( $this->mInterwiki );
@@ -752,6 +752,13 @@
753753
754754 if ( $this->isExternal() ) {
755755 $url = $this->getFullURL();
 756+ if ( $query ) {
 757+ // This is currently only used for edit section links in the
 758+ // context of interwiki transclusion. In theory we should
 759+ // append the query to the end of any existing query string,
 760+ // but interwiki transclusion is already broken in that case.
 761+ $url .= "?$query";
 762+ }
756763 } else {
757764 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
758765 if ( $query == '' ) {
@@ -1344,6 +1351,13 @@
13451352 # Do another namespace split...
13461353 continue;
13471354 }
 1355+
 1356+ # If there's an initial colon after the interwiki, that also
 1357+ # resets the default namespace
 1358+ if ( $t !== '' && $t[0] == ':' ) {
 1359+ $this->mNamespace = NS_MAIN;
 1360+ $t = substr( $t, 1 );
 1361+ }
13481362 }
13491363 # If there's no recognized interwiki or namespace,
13501364 # then let the colon expression be part of the title.
Index: trunk/phase3/includes/HttpFunctions.php
@@ -9,7 +9,7 @@
1010 * if $timeout is 'default', $wgHTTPTimeout is used
1111 */
1212 function wfGetHTTP( $url, $timeout = 'default' ) {
13 - global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion;
 13+ global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle, $wgCommandLineMode;
1414
1515 # Use curl if available
1616 if ( function_exists( 'curl_init' ) ) {
@@ -25,6 +25,16 @@
2626 }
2727 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
2828 curl_setopt( $c, CURLOPT_USERAGENT, "MediaWiki/$wgVersion" );
 29+
 30+ # Set the referer to $wgTitle, even in command-line mode
 31+ # This is useful for interwiki transclusion, where the foreign
 32+ # server wants to know what the referring page is.
 33+ # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
 34+ # referring page.
 35+ if ( is_object( $wgTitle ) ) {
 36+ curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
 37+ }
 38+
2939 ob_start();
3040 curl_exec( $c );
3141 $text = ob_get_contents();
Index: trunk/phase3/includes/Parser.php
@@ -2253,9 +2253,10 @@
22542254 *
22552255 * @param string $tex The text to transform
22562256 * @param array $args Key-value pairs representing template parameters to substitute
 2257+ * @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
22572258 * @access private
22582259 */
2259 - function replaceVariables( $text, $args = array() ) {
 2260+ function replaceVariables( $text, $args = array(), $argsOnly = false ) {
22602261 # Prevent too big inclusions
22612262 if( strlen( $text ) > MAX_INCLUDE_SIZE ) {
22622263 return $text;
@@ -2268,7 +2269,9 @@
22692270 array_push( $this->mArgStack, $args );
22702271
22712272 $braceCallbacks = array();
2272 - $braceCallbacks[2] = array( &$this, 'braceSubstitution' );
 2273+ if ( !$argsOnly ) {
 2274+ $braceCallbacks[2] = array( &$this, 'braceSubstitution' );
 2275+ }
22732276 if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
22742277 $braceCallbacks[3] = array( &$this, 'argSubstitution' );
22752278 }
@@ -2356,12 +2359,16 @@
23572360 $fname = 'Parser::braceSubstitution';
23582361 wfProfileIn( $fname );
23592362
2360 - $found = false;
2361 - $nowiki = false;
2362 - $noparse = false;
2363 - $replaceHeadings = false;
2364 - $isHTML = false;
 2363+ # Flags
 2364+ $found = false; # $text has been filled
 2365+ $nowiki = false; # wiki markup in $text should be escaped
 2366+ $noparse = false; # Unsafe HTML tags should not be stripped, etc.
 2367+ $noargs = false; # Don't replace triple-brace arguments in $text
 2368+ $replaceHeadings = false; # Make the edit section links go to the template not the article
 2369+ $isHTML = false; # $text is HTML, armour it against wikitext transformation
 2370+ $forceRawInterwiki = false; # Force interwiki transclusion to be done in raw mode not rendered
23652371
 2372+ # Title object, where $text came from
23662373 $title = NULL;
23672374
23682375 $linestart = '';
@@ -2378,6 +2385,7 @@
23792386 $text = $replaceWith;
23802387 $found = true;
23812388 $noparse = true;
 2389+ $noargs = true;
23822390 }
23832391 }
23842392
@@ -2395,10 +2403,11 @@
23962404 $text = $piece['text'];
23972405 $found = true;
23982406 $noparse = true;
 2407+ $noargs = true;
23992408 }
24002409 }
24012410
2402 - # MSG, MSGNW and INT
 2411+ # MSG, MSGNW, INT and RAW
24032412 if ( !$found ) {
24042413 # Check for MSGNW:
24052414 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
@@ -2409,7 +2418,13 @@
24102419 $mwMsg =& MagicWord::get( MAG_MSG );
24112420 $mwMsg->matchStartAndRemove( $part1 );
24122421 }
2413 -
 2422+
 2423+ # Check for RAW:
 2424+ $mwRaw =& MagicWord::get( MAG_RAW );
 2425+ if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
 2426+ $forceRawInterwiki = true;
 2427+ }
 2428+
24142429 # Check if it is an internal message
24152430 $mwInt =& MagicWord::get( MAG_INT );
24162431 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
@@ -2515,12 +2530,13 @@
25162531
25172532 # Did we encounter this template already? If yes, it is in the cache
25182533 # and we need to check for loops.
2519 - if ( !$found && isset( $this->mTemplates[$part1] ) ) {
 2534+ if ( !$found && isset( $this->mTemplates[$piece['title']] ) ) {
25202535 $found = true;
25212536
25222537 # Infinite loop test
25232538 if ( isset( $this->mTemplatePath[$part1] ) ) {
25242539 $noparse = true;
 2540+ $noargs = true;
25252541 $found = true;
25262542 $text = $linestart .
25272543 '{{' . $part1 . '}}' .
@@ -2528,7 +2544,7 @@
25292545 wfDebug( "$fname: template loop broken at '$part1'\n" );
25302546 } else {
25312547 # set $text to cached message.
2532 - $text = $linestart . $this->mTemplates[$part1];
 2548+ $text = $linestart . $this->mTemplates[$piece['title']];
25332549 }
25342550 }
25352551
@@ -2553,6 +2569,7 @@
25542570 if ( is_string( $text ) ) {
25552571 $found = true;
25562572 $noparse = true;
 2573+ $noargs = true;
25572574 $isHTML = true;
25582575 $this->disableCache();
25592576 }
@@ -2571,23 +2588,26 @@
25722589 $text = '[['.$title->getPrefixedText().']]';
25732590 $found = true;
25742591 }
2575 -
2576 - # Template cache array insertion
2577 - if( $found ) {
2578 - $this->mTemplates[$part1] = $text;
2579 - $text = $linestart . $text;
2580 - }
25812592 } elseif ( $title->isTrans() ) {
25822593 // Interwiki transclusion
2583 - if ( $this->mOutputType == OT_HTML ) {
 2594+ if ( $this->mOutputType == OT_HTML && !$forceRawInterwiki ) {
25842595 $text = $this->interwikiTransclude( $title, 'render' );
25852596 $isHTML = true;
25862597 $noparse = true;
25872598 } else {
25882599 $text = $this->interwikiTransclude( $title, 'raw' );
 2600+ $replaceHeadings = true;
25892601 }
25902602 $found = true;
25912603 }
 2604+
 2605+ # Template cache array insertion
 2606+ # Use the original $piece['title'] not the mangled $part1, so that
 2607+ # modifiers such as RAW: produce separate cache entries
 2608+ if( $found ) {
 2609+ $this->mTemplates[$piece['title']] = $text;
 2610+ $text = $linestart . $text;
 2611+ }
25922612 }
25932613 }
25942614
@@ -2595,52 +2615,61 @@
25962616 # Only for HTML output
25972617 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
25982618 $text = wfEscapeWikiText( $text );
2599 - } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found && !$noparse) {
2600 - # Clean up argument array
2601 - $assocArgs = array();
2602 - $index = 1;
2603 - foreach( $args as $arg ) {
2604 - $eqpos = strpos( $arg, '=' );
2605 - if ( $eqpos === false ) {
2606 - $assocArgs[$index++] = $arg;
2607 - } else {
2608 - $name = trim( substr( $arg, 0, $eqpos ) );
2609 - $value = trim( substr( $arg, $eqpos+1 ) );
2610 - if ( $value === false ) {
2611 - $value = '';
 2619+ } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found ) {
 2620+ if ( !$noargs ) {
 2621+ # Clean up argument array
 2622+ $assocArgs = array();
 2623+ $index = 1;
 2624+ foreach( $args as $arg ) {
 2625+ $eqpos = strpos( $arg, '=' );
 2626+ if ( $eqpos === false ) {
 2627+ $assocArgs[$index++] = $arg;
 2628+ } else {
 2629+ $name = trim( substr( $arg, 0, $eqpos ) );
 2630+ $value = trim( substr( $arg, $eqpos+1 ) );
 2631+ if ( $value === false ) {
 2632+ $value = '';
 2633+ }
 2634+ if ( $name !== false ) {
 2635+ $assocArgs[$name] = $value;
 2636+ }
26122637 }
2613 - if ( $name !== false ) {
2614 - $assocArgs[$name] = $value;
2615 - }
26162638 }
 2639+
 2640+ # Add a new element to the templace recursion path
 2641+ $this->mTemplatePath[$part1] = 1;
26172642 }
26182643
2619 - # Add a new element to the templace recursion path
2620 - $this->mTemplatePath[$part1] = 1;
 2644+ if ( !$noparse ) {
 2645+ # If there are any <onlyinclude> tags, only include them
 2646+ if ( in_string( '<onlyinclude>', $text ) && in_string( '</onlyinclude>', $text ) ) {
 2647+ preg_match_all( '/<onlyinclude>(.*?)\n?<\/onlyinclude>/s', $text, $m );
 2648+ $text = '';
 2649+ foreach ($m[1] as $piece)
 2650+ $text .= $piece;
 2651+ }
 2652+ # Remove <noinclude> sections and <includeonly> tags
 2653+ $text = preg_replace( '/<noinclude>.*?<\/noinclude>/s', '', $text );
 2654+ $text = strtr( $text, array( '<includeonly>' => '' , '</includeonly>' => '' ) );
26212655
2622 - # If there are any <onlyinclude> tags, only include them
2623 - if ( in_string( '<onlyinclude>', $text ) && in_string( '</onlyinclude>', $text ) ) {
2624 - preg_match_all( '/<onlyinclude>(.*?)\n?<\/onlyinclude>/s', $text, $m );
2625 - $text = '';
2626 - foreach ($m[1] as $piece)
2627 - $text .= $piece;
2628 - }
2629 - # Remove <noinclude> sections and <includeonly> tags
2630 - $text = preg_replace( '/<noinclude>.*?<\/noinclude>/s', '', $text );
2631 - $text = strtr( $text, array( '<includeonly>' => '' , '</includeonly>' => '' ) );
 2656+ if( $this->mOutputType == OT_HTML ) {
 2657+ # Strip <nowiki>, <pre>, etc.
 2658+ $text = $this->strip( $text, $this->mStripState );
 2659+ $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'replaceVariables' ), $assocArgs );
 2660+ }
 2661+ $text = $this->replaceVariables( $text, $assocArgs );
26322662
2633 - if( $this->mOutputType == OT_HTML ) {
2634 - # Strip <nowiki>, <pre>, etc.
2635 - $text = $this->strip( $text, $this->mStripState );
2636 - $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'replaceVariables' ), $assocArgs );
 2663+ # If the template begins with a table or block-level
 2664+ # element, it should be treated as beginning a new line.
 2665+ if (!$piece['lineStart'] && preg_match('/^({\\||:|;|#|\*)/', $text)) {
 2666+ $text = "\n" . $text;
 2667+ }
 2668+ } elseif ( !$noargs ) {
 2669+ # $noparse and !$noargs
 2670+ # Just replace the arguments, not any double-brace items
 2671+ # This is used for rendered interwiki transclusion
 2672+ $text = $this->replaceVariables( $text, $assocArgs, true );
26372673 }
2638 - $text = $this->replaceVariables( $text, $assocArgs );
2639 -
2640 - # If the template begins with a table or block-level
2641 - # element, it should be treated as beginning a new line.
2642 - if (!$piece['lineStart'] && preg_match('/^({\\||:|;|#|\*)/', $text)) {
2643 - $text = "\n" . $text;
2644 - }
26452674 }
26462675 # Prune lower levels off the recursion check path
26472676 $this->mTemplatePath = $lastPathLevel;
Index: trunk/phase3/RELEASE-NOTES
@@ -258,6 +258,7 @@
259259 character before doing anything with them. This prevents certain kinds of
260260 spam filter evasion.
261261 * (bug 4783) : Fix for "{{ns:0}} does not render"
 262+* Improved support for interwiki transclusion
262263
263264 Upload:
264265 * (bug 2527) Always set destination filename when new file is selected
Index: trunk/phase3/languages/Language.php
@@ -253,10 +253,11 @@
254254 MAG_PLURAL => array( 0, 'PLURAL:' ),
255255 MAG_FULLURL => array( 0, 'FULLURL:' ),
256256 MAG_FULLURLE => array( 0, 'FULLURLE:' ),
257 - MAG_LCFIRST => array( 0, 'LCFIRST:' ),
258 - MAG_UCFIRST => array( 0, 'UCFIRST:' ),
259 - MAG_LC => array( 0, 'LC:' ),
260 - MAG_UC => array( 0, 'UC:' ),
 257+ MAG_LCFIRST => array( 0, 'LCFIRST:' ),
 258+ MAG_UCFIRST => array( 0, 'UCFIRST:' ),
 259+ MAG_LC => array( 0, 'LC:' ),
 260+ MAG_UC => array( 0, 'UC:' ),
 261+ MAG_RAW => array( 0, 'RAW:' ),
261262 );
262263
263264 if (!$wgCachedMessageArrays) {

Status & tagging log