Alters Skin::makeLinkObj() to bypass using linkcache
authorJulian Ostrow <jostrow@users.mediawiki.org>
Sun, 18 Jul 2004 03:23:11 +0000 (03:23 +0000)
committerJulian Ostrow <jostrow@users.mediawiki.org>
Sun, 18 Jul 2004 03:23:11 +0000 (03:23 +0000)
Adds function OutputPage::parseLinkHolders(), which works in conjunction with the modified Skin::makeLinkObj()

includes/OutputPage.php
includes/Skin.php

index 5e17bbf..fb66267 100644 (file)
@@ -345,6 +345,8 @@ class OutputPage {
 
 
                $this->sendCacheControl();
+               
+               $this->mBodytext = $this->parseLinkHolders();
 
                header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
                header( "Content-language: {$wgLanguageCode}" );
@@ -754,5 +756,91 @@ class OutputPage {
                # $ret .= "<!--[if gte IE 5.5000]><script type='text/javascript' src='$fix'></script><![endif]-->";
                return $ret;
        }
+       
+       # Parse <tmp=> link placeholders to avoid using linkcache
+       # $wgInternalLinks populated in Skin::makeLinkObj()
+       function parseLinkHolders()
+       {
+               global $wgUser, $wgInternalLinks;
+               
+               $fname = 'OutputPage::parseLinkHolders';
+               wfProfileIn( $fname );
+               
+               # Get placeholders from body
+               preg_match_all( "/<tmp=(.*?)>/", $this->mBodytext, $tmpLinks );
+               
+               if ( !empty( $tmpLinks[0] ) ) {
+                       $dbr =& wfGetDB( DB_READ );
+                       $sk = $wgUser->getSkin();
+                       
+                       # Index the DB key along with other information
+                       foreach ( $tmpLinks[1] as $key => $val ) {
+                               $wgInternalLinks['dbkey'][$key] = $wgInternalLinks['obj'][$key]->getDBkey();
+                       }
+                       
+                       # Sort according to namespace
+                       asort($wgInternalLinks['ns']);
+                       
+                       # Generate query
+                       foreach ( $wgInternalLinks['ns'] as $key => $val ) {
+                               if ( !isset( $current ) ) {
+                                       $current = $val;
+                                       $query =  "SELECT cur_title, LENGTH(cur_text) AS cur_len, cur_is_redirect FROM cur ";
+                                       $query .= "WHERE (cur_namespace=$val AND cur_title IN(";
+                               } elseif ( $current != $val ) {
+                                       $current = $val;
+                                       $query .= ")) OR (cur_namespace=$val AND cur_title IN(";
+                               } else {
+                                       $query .= ", ";
+                               }
+                               
+                               $query .= $dbr->addQuotes( $wgInternalLinks['dbkey'][$key] );
+                       }
+
+                       $query .= "))";
+                       
+                       $res = $dbr->query( $query );
+                       
+                       # Fetch data and pass to appropriate make*LinkObj()
+                       # Index search and replace strings to substitute placeholders for actual links
+                       while ( $s = $dbr->fetchRow($res) ) {
+                               $i = array_search($s['cur_title'], $wgInternalLinks['dbkey']);
+                               $threshold = $wgUser->getOption('stubthreshold');
+                               if ( $threshold > 0 ) {
+                                       $size = $s['cur_len'];
+                                       if ( $s['cur_is_redirect'] || ( $wgInternalLinks['ns'][$i] != 0 ) ) {
+                                               $size = $threshold * 2; # Really big
+                                       }
+                               } else {
+                                       $size = 1;
+                               }
+                               
+                               if ( $size < $threshold ) {
+                                       $wgInternalLinks['replace'][$i] = $sk->makeStubLinkObj( $wgInternalLinks['obj'][$i],
+                                               $wgInternalLinks['text'][$i], $wgInternalLinks['query'][$i], '',
+                                               $wgInternalLinks['prefix'][$i]);
+                               } else {
+                                       $wgInternalLinks['replace'][$i] = $sk->makeKnownLinkObj( $wgInternalLinks['obj'][$i],
+                                               $wgInternalLinks['text'][$i], $wgInternalLinks['query'][$i], '',
+                                               $wgInternalLinks['prefix'][$i]);
+                               }
+                       }
+                       
+                       # Finish populating search and replace arrays for broken links
+                       foreach ( $wgInternalLinks['ns'] as $key => $val ) {
+                               $search[]  = $tmpLinks[0][$key];
+                               $replace[] = ( empty ( $wgInternalLinks['replace'][$key] ) ) ?
+                                       $sk->makeBrokenLinkObj( $wgInternalLinks['obj'][$key],
+                                               $wgInternalLinks['text'][$key], $wgInternalLinks['query'][$key], '',
+                                               $wgInternalLinks['prefix'][$key])
+                                       : $wgInternalLinks['replace'][$key];
+                       }
+                       
+                       $out = str_replace( $search, $replace, $this->mBodytext );
+               }
+               
+               wfProfileOut( $fname );
+               return ( $out );
+       }
 }
 ?>
index 5c16a87..bf4efbb 100644 (file)
@@ -1440,6 +1440,7 @@ class Skin {
        function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' )
        {
                global $wgOut, $wgUser;
+               global $wgInternalLinks;
                $fname = 'Skin::makeLinkObj';
 
                if ( $nt->isExternal() ) {
@@ -1462,35 +1463,21 @@ class Skin {
                                ( Namespace::getImage() == $nt->getNamespace() ) ) {
                        $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
                } else {
-                       $aid = $nt->getArticleID() ;
-                       if ( 0 == $aid ) {
-                               $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
-                       } else {
-                               $threshold = $wgUser->getOption('stubthreshold') ;
-                               if ( $threshold > 0 ) {
-                                       $s = $dbr->getArray( 'cur',
-                                               array( 'LENGTH(cur_text) AS x', 'cur_namespace', 'cur_is_redirect' ),
-                                               array( 'cur_id' => $aid ),
-                                               $fname
-                                       );
-
-                                       if ( $s !== false ) {
-                                               $size = $s->x;
-                                               if ( $s->cur_is_redirect OR $s->cur_namespace != 0 ) {
-                                                       $size = $threshold*2 ; # Really big
-                                               }
-                                       } else {
-                                               $size = $threshold*2 ; # Really big
-                                       }
-                               } else {
-                                       $size = 1 ;
-                               }
-                               if ( $size < $threshold ) {
-                                       $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
-                               } else {
-                                       $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
+                       $inside = '';
+                       if ( '' != $trail ) {
+                               if ( preg_match( $this->linktrail, $trail, $m ) ) {
+                                       $inside = $m[1];
+                                       $trail = $m[2];
                                }
                        }
+                       
+                       # Allows wiki to bypass using linkcache, see OutputPage::parseLinkHolders()
+                       $wgInternalLinks['obj'][] = $nt;
+                       $wgInternalLinks['text'][] = $text . $inside;
+                       $wgInternalLinks['query'][] = $query;
+                       $wgInternalLinks['prefix'][] = $prefix;
+                       $wgInternalLinks['ns'][] = $nt->getNamespace();
+                       $retVal = "<tmp=" . $nt->getDBkey() . ">{$trail}";
                }
                return $retVal;
        }