From b64fae638363395e47d89b066b6144b900bbf792 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Wed, 22 Oct 2003 23:56:49 +0000 Subject: [PATCH] Optimisation --- includes/LinkCache.php | 16 ++-- includes/OutputPage.php | 28 +++--- includes/Skin.php | 203 ++++++++++++++++++++++++++-------------- includes/Title.php | 56 ++++++----- 4 files changed, 191 insertions(+), 112 deletions(-) diff --git a/includes/LinkCache.php b/includes/LinkCache.php index fe35d60907..25b84ac4bc 100644 --- a/includes/LinkCache.php +++ b/includes/LinkCache.php @@ -74,6 +74,12 @@ class LinkCache { function addLink( $title ) { + return $this->addLinkObj( Title::newFromDBkey( $title ) ); + } + + function addLinkObj( &$nt ) + { + $title = $nt->getDBkey(); if ( $this->isBadLink( $title ) ) { return 0; } $id = $this->getGoodLinkID( $title ); if ( 0 != $id ) { return $id; } @@ -82,19 +88,17 @@ class LinkCache { $fname = "LinkCache::addLink-checkdatabase"; wfProfileIn( $fname ); - $nt = Title::newFromDBkey( $title ); $ns = $nt->getNamespace(); - $t = $nt->getDBkey(); - if ( "" == $t ) { - wfProfileOut( $fname); + if ( "" == $title ) { + wfProfileOut( $fname ); return 0; } $id = $wgMemc->get( $key = "$wgDBname:lc:title:$title" ); if( $id === FALSE ) { $sql = "SELECT HIGH_PRIORITY cur_id FROM cur WHERE cur_namespace=" . - "{$ns} AND cur_title='" . wfStrencode( $t ) . "'"; + "{$ns} AND cur_title='" . wfStrencode( $title ) . "'"; $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" ); if ( 0 == wfNumRows( $res ) ) { @@ -111,7 +115,7 @@ class LinkCache { return $id; } - function preFill( $fromtitle ) + function preFill( &$fromtitle ) { $fname = "LinkCache::preFill"; wfProfileIn( $fname ); diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 1bff75d927..5d8982d85d 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -551,11 +551,11 @@ class OutputPage { # $t = $t->getText() ; $t = $x->l_from ; $y = explode ( ":" , $t , 2 ) ; - if ( count ( $y ) == 2 && $y[0] == $cat ) - { + if ( count ( $y ) == 2 && $y[0] == $cat ) { array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ; + } else { + array_push ( $articles , $sk->makeLink ( $t ) ) ; } - else array_push ( $articles , $sk->makeLink ( $t ) ) ; } wfFreeResult ( $res ) ; @@ -921,7 +921,6 @@ $t[] = "" ; wfProfileOut( "$fname-setup" ); foreach ( $a as $line ) { - wfProfileIn( "$fname-loop" ); if ( preg_match( $e1, $line, $m ) ) { # page with alternate text $text = $m[2]; @@ -935,7 +934,7 @@ $t[] = "" ; else { # Invalid form; output directly $s .= "[[" . $line ; - wfProfileOut( "$fname-loop" ); + wfProfileOut( "$fname-loop1" ); continue; } if(substr($m[1],0,1)=="/") { # subpage @@ -957,7 +956,7 @@ $t[] = "" ; } else { # no subpage $link = $m[1]; } - + if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z\\x80-\\xff]+):(.*)\$/", $link, $m ) ) { $pre = strtolower( $m[1] ); $suf = trim($m[2]); @@ -982,15 +981,16 @@ $t[] = "" ; $s .= $sk->makeMediaLink( $name, wfImageUrl( $name ), $text ); $s .= $trail; - } else if ( isset($wgUseCategoryMagic) && $wgUseCategoryMagic && $pre == wfMsg ( "category" ) ) { - $l = $sk->makeLink ( $pre.":".ucfirst($m[2]) , ucfirst ( $m[2] ) ) ; - array_push ( $this->mCategoryLinks , $l ) ; - $s .= $trail ; + } else if ( isset($wgUseCategoryMagic) && $wgUseCategoryMagic && $pre == wfMsg ( "category" ) ) { + $l = $sk->makeLink ( $pre.":".ucfirst( $m[2] ), ucfirst ( $m[2] ) ) ; + array_push ( $this->mCategoryLinks , $l ) ; + $s .= $trail ; } else { $l = $wgLang->getLanguageName( $pre ); - if ( "" == $l or !$wgInterwikiMagic or - Namespace::isTalk( $wgTitle->getNamespace() ) ) { - if ( "" == $text ) { $text = $link; } + if ( "" == $l or !$wgInterwikiMagic or Namespace::isTalk( $wgTitle->getNamespace() ) ) { + if ( "" == $text ) { + $text = $link; + } $s .= $sk->makeLink( $link, $text, "", $trail ); } else if ( $pre != $wgLanguageCode ) { array_push( $this->mLanguageLinks, "$pre:$suf" ); @@ -1002,9 +1002,9 @@ $t[] = "" ; # $s .= "{$text}{$trail}"; } else { if ( "" == $text ) { $text = $link; } + # Hotspot: $s .= $sk->makeLink( $link, $text, "", $trail ); } - wfProfileOut( "$fname-loop" ); } wfProfileOut( $fname ); return $s; diff --git a/includes/Skin.php b/includes/Skin.php index dad6d940ba..99400a8f97 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -204,17 +204,43 @@ class Skin { $link = str_replace( "_", " ", $link ); $link = wfEscapeHTML( $link ); - if ( $wgOut->isPrintable() ) { $r = " class='printable'"; } - else if ( $broken == "stub" ) { $r = " class='stub'"; } - else if ( $broken == "yes" ) { $r = " class='new'"; } - else { $r = " class='internal'"; } + if ( $wgOut->isPrintable() ) { + $r = " class='printable'"; + } else if ( $broken == "stub" ) { + $r = " class='stub'"; + } else if ( $broken == "yes" ) { + $r = " class='new'"; + } else { + $r = " class='internal'"; + } if ( 1 == $wgUser->getOption( "hover" ) ) { $r .= " title=\"{$link}\""; } return $r; } + + function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) + { + global $wgUser, $wgOut; + $link = $nt->getEscapedText(); + + if ( $wgOut->isPrintable() ) { + $r = " class='printable'"; + } else if ( $broken == "stub" ) { + $r = " class='stub'"; + } else if ( $broken == "yes" ) { + $r = " class='new'"; + } else { + $r = " class='internal'"; + } + if ( 1 == $wgUser->getOption( "hover" ) ) { + $r .= ' title ="' . $nt->getEscapedText() . '"'; + } + return $r; + } + function getLogo() { global $wgLogo; @@ -455,7 +481,7 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) $c++; if ($cmakeLink($growinglink,$link); + $getlink=$this->makeLink( $growingLink, $link); if(preg_match("/class='new'/i",$getlink)) { break; } # this is a hack, but it saves time if ($c>1) { $sub .= " | "; @@ -701,7 +727,8 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) if ($nstext = $wgLang->getNsText($tns) ) { # add namespace if necessary $link = $nstext . ":" . $link ; } - $s .= $this->makeLink($link, $text ); + + $s .= $this->makeLink( $link, $text ); } elseif( $wgTitle->getNamespace() != Namespace::getSpecial() ) { # we just throw in a "New page" text to tell the user that he's in edit mode, # and to avoid messing with the separator that is prepended to the next item @@ -1111,12 +1138,28 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) # Note: This function MUST call getArticleID() on the link, # otherwise the cache won't get updated properly. See LINKCACHE.DOC. # - function makeLink( $title, $text= "", $query = "", $trail = "" ) - { - global $wgOut, $wgUser; + function makeLink( $title, $text = "", $query = "", $trail = "" ) { + return $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail ); + } - $nt = Title::newFromText( $title ); + function makeKnownLink( $title, $text = "", $query = "", $trail = "" ) { + return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail ); + } + function makeBrokenLink( $title, $text = "", $query = "", $trail = "" ) { + return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail ); + } + + function makeStubLink( $title, $text = "", $query = "", $trail = "" ) { + return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail ); + } + + # Pass a title object, not a title string + function makeLinkObj( &$nt, $text= "", $query = "", $trail = "" ) + { + global $wgOut, $wgUser; + $fname = "Skin::makeLinkObj"; + wfProfileIn( $fname ); if ( $nt->isExternal() ) { $u = $nt->getFullURL(); if ( "" == $text ) { $text = $nt->getPrefixedText(); } @@ -1129,43 +1172,53 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) $trail = $m[2]; } } - return "{$text}{$inside}{$trail}"; - } - if ( 0 == $nt->getNamespace() && "" == $nt->getText() ) { - return $this->makeKnownLink( $title, $text, $query, $trail ); - } - if ( ( -1 == $nt->getNamespace() ) || + $retVal = "{$text}{$inside}{$trail}"; + } elseif ( 0 == $nt->getNamespace() && "" == $nt->getText() ) { + $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail ); + } elseif ( ( -1 == $nt->getNamespace() ) || ( Namespace::getImage() == $nt->getNamespace() ) ) { - return $this->makeKnownLink( $title, $text, $query, $trail ); - } - $aid = $nt->getArticleID() ; - if ( 0 == $aid ) { - return $this->makeBrokenLink( $title, $text, $query, $trail ); + $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail ); } else { - $threshold = $wgUser->getOption("stubthreshold") ; - if ( $threshold > 0 ) { - $res = wfQuery ( "SELECT HIGH_PRIORITY length(cur_text) AS x, cur_namespace, cur_is_redirect FROM cur WHERE cur_id='{$aid}'", DB_READ ) ; - - if ( wfNumRows( $res ) > 0 ) { - $s = wfFetchObject( $res ); - $size = $s->x; - if ( $s->cur_is_redirect OR $s->cur_namespace != 0 ) + $aid = $nt->getArticleID() ; + if ( 0 == $aid ) { + $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail ); + } else { + $threshold = $wgUser->getOption("stubthreshold") ; + if ( $threshold > 0 ) { + $res = wfQuery ( "SELECT HIGH_PRIORITY length(cur_text) AS x, cur_namespace, cur_is_redirect FROM cur WHERE cur_id='{$aid}'", DB_READ ) ; + + if ( wfNumRows( $res ) > 0 ) { + $s = wfFetchObject( $res ); + $size = $s->x; + if ( $s->cur_is_redirect OR $s->cur_namespace != 0 ) { + $size = $threshold*2 ; # Really big + } + wfFreeResult( $res ); + } else { $size = $threshold*2 ; # Really big - wfFreeResult( $res ); - } else $size = $threshold*2 ; # Really big - } else $size = 1 ; - - if ( $size < $threshold ) - return $this->makeStubLink( $title, $text, $query, $trail ); - return $this->makeKnownLink( $title, $text, $query, $trail ); + } + } else { + $size = 1 ; + } + if ( $size < $threshold ) { + $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail ); + } else { + $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail ); + } + } } + wfProfileOut( $fname ); + return $retVal; } - function makeKnownLink( $title, $text = "", $query = "", $trail = "" ) + # Pass a title object, not a title string + function makeKnownLinkObj( &$nt, $text = "", $query = "", $trail = "" ) { global $wgOut, $wgTitle; - $nt = Title::newFromText( $title ); + $fname = "Skin::makeKnownLinkObj"; + wfProfileIn( $fname ); + $link = $nt->getPrefixedURL(); if ( "" == $link ) { @@ -1178,7 +1231,7 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) $u .= "#" . wfEscapeHTML( $nt->getFragment() ); } if ( "" == $text ) { $text = $nt->getPrefixedText(); } - $style = $this->getInternalLinkAttributes( $link, $text ); + $style = $this->getInternalLinkAttributesObj( $nt, $text ); $inside = ""; if ( "" != $trail ) { @@ -1188,14 +1241,18 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) } } $r = "{$text}{$inside}{$trail}"; + wfProfileOut( $fname ); return $r; } - - function makeBrokenLink( $title, $text = "", $query = "", $trail = "" ) + + # Pass a title object, not a title string + function makeBrokenLinkObj( &$nt, $text = "", $query = "", $trail = "" ) { global $wgOut, $wgUser; + + $fname = "Skin::makeBrokenLinkObj"; + wfProfileIn( $fname ); - $nt = Title::newFromText( $title ); $link = $nt->getPrefixedURL(); if ( "" == $query ) { $q = "action=edit"; } @@ -1203,7 +1260,7 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) $u = wfLocalUrlE( $link, $q ); if ( "" == $text ) { $text = $nt->getPrefixedText(); } - $style = $this->getInternalLinkAttributes( $link, $text, "yes" ); + $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" ); $inside = ""; if ( "" != $trail ) { @@ -1218,36 +1275,38 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) } else { $s = "{$text}{$inside}?{$trail}"; } + + wfProfileOut( $fname ); return $s; } + + # Pass a title object, not a title string + function makeStubLinkObj( &$nt, $text = "", $query = "", $trail = "" ) + { + global $wgOut, $wgUser; - function makeStubLink( $title, $text = "", $query = "", $trail = "" ) - { - global $wgOut, $wgUser; - - $nt = Title::newFromText( $title ); - $link = $nt->getPrefixedURL(); + $link = $nt->getPrefixedURL(); - $u = wfLocalUrlE( $link, $query ); + $u = wfLocalUrlE( $link, $query ); - if ( "" == $text ) { $text = $nt->getPrefixedText(); } - $style = $this->getInternalLinkAttributes( $link, $text, "stub" ); + if ( "" == $text ) { $text = $nt->getPrefixedText(); } + $style = $this->getInternalLinkAttributesObj( $nt, $text, "stub" ); - $inside = ""; - if ( "" != $trail ) { - if ( preg_match( wfMsg("linktrail"), $trail, $m ) ) { - $inside = $m[1]; - $trail = $m[2]; - } - } - if ( $wgOut->isPrintable() || - ( 1 == $wgUser->getOption( "highlightbroken" ) ) ) { - $s = "{$text}{$inside}{$trail}"; - } else { - $s = "{$text}{$inside}!{$trail}"; - } - return $s; - } + $inside = ""; + if ( "" != $trail ) { + if ( preg_match( wfMsg("linktrail"), $trail, $m ) ) { + $inside = $m[1]; + $trail = $m[2]; + } + } + if ( $wgOut->isPrintable() || + ( 1 == $wgUser->getOption( "highlightbroken" ) ) ) { + $s = "{$text}{$inside}{$trail}"; + } else { + $s = "{$text}{$inside}!{$trail}"; + } + return $s; + } function fnamePart( $url ) { @@ -1372,8 +1431,9 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) if ( 0 == $u ) { $ul = $this->makeKnownLink( $wgLang->specialPage( "Contributions" ), $ut, "target=" . $ut ); - } else { $ul = $this->makeLink( $wgLang->getNsText( - Namespace::getUser() ) . ":{$ut}", $ut ); } + } else { + $ul = $this->makeLink( $wgLang->getNsText( + Namespace::getUser() ) . ":{$ut}", $ut ); } $s = "
  • "; if ( $oid ) { @@ -1584,8 +1644,9 @@ if ( isset ( $wgUseApproval ) && $wgUseApproval ) if ( 0 == $u ) { $ul = $this->makeKnownLink( $wgLang->specialPage( "Contributions" ), $ut, "target=" . $ut ); - } else { $ul = $this->makeLink( $wgLang->getNsText( - Namespace::getUser() ) . ":{$ut}", $ut ); } + } else { + $ul = $this->makeLink( $wgLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut ); + } $utns=$wgLang->getNsText(Namespace::getTalk(Namespace::getUser())); $talkname=$wgLang->getNsText(Namespace::getTalk(0)); # use the shorter name diff --git a/includes/Title.php b/includes/Title.php index e5cd9b9eb9..bbefb9bc42 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -5,6 +5,7 @@ class Title { /* private */ var $mTextform, $mUrlform, $mDbkeyform; /* private */ var $mNamespace, $mInterwiki, $mFragment; /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded; + /* private */ var $mPrefixedText; /* private */ function Title() { @@ -14,6 +15,7 @@ class Title { $this->mNamespace = 0; $this->mRestrictionsLoaded = false; $this->mRestrictions = array(); + $this->mPrefixedText = false; } # Static factory methods @@ -187,9 +189,12 @@ class Title { function getPrefixedText() { - $s = $this->prefix( $this->mTextform ); - $s = str_replace( "_", " ", $s ); - return $s; + if ( !$this->mPrefixedText ) { + $s = $this->prefix( $this->mTextform ); + $s = str_replace( "_", " ", $s ); + $this->mPrefixedText = $s; + } + return $this->mPrefixedText; } function getPrefixedURL() @@ -233,6 +238,12 @@ class Title { return $s; } + + # For the title field in tags + function getEscapedText() + { + return wfEscapeHTML( $this->getPrefixedText() ); + } function isExternal() { return ( "" != $this->mInterwiki ); } @@ -312,8 +323,7 @@ class Title { global $wgLinkCache; if ( -1 != $this->mArticleID ) { return $this->mArticleID; } - $this->mArticleID = $wgLinkCache->addLink( - $this->getPrefixedDBkey() ); + $this->mArticleID = $wgLinkCache->addLinkObj( $this ); return $this->mArticleID; } @@ -346,9 +356,7 @@ class Title { # and uses undersocres, but not otherwise munged. This function # removes illegal characters, splits off the winterwiki and # namespace prefixes, sets the other forms, and canonicalizes - # everything. This one function is really at the core of - # Wiki--don't mess with it unless you're really sure you know - # what you're doing. + # everything. # /* private */ function secureAndSplit() { @@ -356,16 +364,27 @@ class Title { $fname = "Title::secureAndSplit"; wfProfileIn( $fname ); - $validNamespaces = $wgLang->getNamespaces(); - unset( $validNamespaces[0] ); + static $imgpre = false; + static $rxTc = false; + + # Initialisation + if ( $imgpre === false ) { + $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":"; + $rxTc = "/[^" . Title::legalChars() . "]/"; + } + $this->mInterwiki = $this->mFragment = ""; $this->mNamespace = 0; $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform ); - if ( "_" == $t{0} ) { $t = substr( $t, 1 ); } + if ( "_" == $t{0} ) { + $t = substr( $t, 1 ); + } $l = strlen( $t ); - if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); } + if ( $l && ( "_" == $t{$l-1} ) ) { + $t = substr( $t, 0, $l-1 ); + } if ( "" == $t ) { wfProfileOut( $fname ); return false; @@ -374,7 +393,6 @@ class Title { $this->mDbkeyform = $t; $done = false; - $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":"; if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) { $t = substr( $t, 1 ); } @@ -391,14 +409,11 @@ class Title { $t = $m[2]; $this->mInterwiki = $p; - if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", - $t, $m ) ) { - $p = strtolower( $m[1] ); - } else { + if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) { $done = true; - } - if($this->mInterwiki != $wgLocalInterwiki) + } elseif($this->mInterwiki != $wgLocalInterwiki) { $done = true; + } } } $r = $t; @@ -418,8 +433,7 @@ class Title { } # Strip illegal characters. # - $tc = Title::legalChars(); - $t = preg_replace( "/[^{$tc}]/", "", $r ); + $t = preg_replace( $rxTc, "", $r ); if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t ); $this->mDbkeyform = $t; -- 2.20.1