00001 <?php
00002
00003 class LinkHolderArray {
00004 var $internals = array(), $interwikis = array();
00005 var $size = 0;
00006 var $parent;
00007
00008 function __construct( $parent ) {
00009 $this->parent = $parent;
00010 }
00011
00015 function __destruct() {
00016 foreach ( $this as $name => $value ) {
00017 unset( $this->$name );
00018 }
00019 }
00020
00024 function merge( $other ) {
00025 foreach ( $other->internals as $ns => $entries ) {
00026 $this->size += count( $entries );
00027 if ( !isset( $this->internals[$ns] ) ) {
00028 $this->internals[$ns] = $entries;
00029 } else {
00030 $this->internals[$ns] += $entries;
00031 }
00032 }
00033 $this->interwikis += $other->interwikis;
00034 }
00035
00039 function isBig() {
00040 global $wgLinkHolderBatchSize;
00041 return $this->size > $wgLinkHolderBatchSize;
00042 }
00043
00048 function clear() {
00049 $this->internals = array();
00050 $this->interwikis = array();
00051 $this->size = 0;
00052 }
00053
00061 function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
00062 wfProfileIn( __METHOD__ );
00063 if ( ! is_object($nt) ) {
00064 # Fail gracefully
00065 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
00066 } else {
00067 # Separate the link trail from the rest of the link
00068 list( $inside, $trail ) = Linker::splitTrail( $trail );
00069
00070 $entry = array(
00071 'title' => $nt,
00072 'text' => $prefix.$text.$inside,
00073 'pdbk' => $nt->getPrefixedDBkey(),
00074 );
00075 if ( $query !== '' ) {
00076 $entry['query'] = $query;
00077 }
00078
00079 if ( $nt->isExternal() ) {
00080
00081 $key = $this->parent->nextLinkID();
00082 $this->interwikis[$key] = $entry;
00083 $retVal = "<!--IWLINK $key-->{$trail}";
00084 } else {
00085 $key = $this->parent->nextLinkID();
00086 $ns = $nt->getNamespace();
00087 $this->internals[$ns][$key] = $entry;
00088 $retVal = "<!--LINK $ns:$key-->{$trail}";
00089 }
00090 $this->size++;
00091 }
00092 wfProfileOut( __METHOD__ );
00093 return $retVal;
00094 }
00095
00099 function getStubThreshold() {
00100 global $wgUser;
00101 if ( !isset( $this->stubThreshold ) ) {
00102 $this->stubThreshold = $wgUser->getOption('stubthreshold');
00103 }
00104 return $this->stubThreshold;
00105 }
00106
00112 function replace( &$text ) {
00113 wfProfileIn( __METHOD__ );
00114
00115 $colours = $this->replaceInternal( $text );
00116 $this->replaceInterwiki( $text );
00117
00118 wfProfileOut( __METHOD__ );
00119 return $colours;
00120 }
00121
00125 protected function replaceInternal( &$text ) {
00126 if ( !$this->internals ) {
00127 return;
00128 }
00129
00130 wfProfileIn( __METHOD__ );
00131 global $wgContLang;
00132
00133 $colours = array();
00134 $sk = $this->parent->getOptions()->getSkin();
00135 $linkCache = LinkCache::singleton();
00136 $output = $this->parent->getOutput();
00137
00138 wfProfileIn( __METHOD__.'-check' );
00139 $dbr = wfGetDB( DB_SLAVE );
00140 $page = $dbr->tableName( 'page' );
00141 $threshold = $this->getStubThreshold();
00142
00143 # Sort by namespace
00144 ksort( $this->internals );
00145
00146 # Generate query
00147 $query = false;
00148 $current = null;
00149 foreach ( $this->internals as $ns => $entries ) {
00150 foreach ( $entries as $index => $entry ) {
00151 $key = "$ns:$index";
00152 $title = $entry['title'];
00153 $pdbk = $entry['pdbk'];
00154
00155 # Skip invalid entries.
00156 # Result will be ugly, but prevents crash.
00157 if ( is_null( $title ) ) {
00158 continue;
00159 }
00160
00161 # Check if it's a static known link, e.g. interwiki
00162 if ( $title->isAlwaysKnown() ) {
00163 $colours[$pdbk] = '';
00164 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
00165 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
00166 $output->addLink( $title, $id );
00167 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
00168 $colours[$pdbk] = 'new';
00169 } else {
00170 # Not in the link cache, add it to the query
00171 if ( !isset( $current ) ) {
00172 $current = $ns;
00173 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
00174 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
00175 } elseif ( $current != $ns ) {
00176 $current = $ns;
00177 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
00178 } else {
00179 $query .= ', ';
00180 }
00181
00182 $query .= $dbr->addQuotes( $title->getDBkey() );
00183 }
00184 }
00185 }
00186 if ( $query ) {
00187 $query .= '))';
00188
00189 $res = $dbr->query( $query, __METHOD__ );
00190
00191 # Fetch data and form into an associative array
00192 # non-existent = broken
00193 $linkcolour_ids = array();
00194 while ( $s = $dbr->fetchObject($res) ) {
00195 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
00196 $pdbk = $title->getPrefixedDBkey();
00197 $linkCache->addGoodLinkObj( $s->page_id, $title, $s->page_len, $s->page_is_redirect );
00198 $output->addLink( $title, $s->page_id );
00199 # FIXME: convoluted data flow
00200 # The redirect status and length is passed to getLinkColour via the LinkCache
00201 # Use formal parameters instead
00202 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
00203
00204 $linkcolour_ids[$s->page_id] = $pdbk;
00205 }
00206 unset( $res );
00207
00208 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
00209 }
00210 wfProfileOut( __METHOD__.'-check' );
00211
00212 # Do a second query for different language variants of links and categories
00213 if($wgContLang->hasVariants()) {
00214 $this->doVariants( $colours );
00215 }
00216
00217 # Construct search and replace arrays
00218 wfProfileIn( __METHOD__.'-construct' );
00219 $replacePairs = array();
00220 foreach ( $this->internals as $ns => $entries ) {
00221 foreach ( $entries as $index => $entry ) {
00222 $pdbk = $entry['pdbk'];
00223 $title = $entry['title'];
00224 $query = isset( $entry['query'] ) ? $entry['query'] : '';
00225 $key = "$ns:$index";
00226 $searchkey = "<!--LINK $key-->";
00227 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] == 'new' ) {
00228 $linkCache->addBadLinkObj( $title );
00229 $colours[$pdbk] = 'new';
00230 $output->addLink( $title, 0 );
00231 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
00232 $entry['text'],
00233 $query );
00234 } else {
00235 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
00236 $entry['text'],
00237 $query );
00238 }
00239 }
00240 }
00241 $replacer = new HashtableReplacer( $replacePairs, 1 );
00242 wfProfileOut( __METHOD__.'-construct' );
00243
00244 # Do the thing
00245 wfProfileIn( __METHOD__.'-replace' );
00246 $text = preg_replace_callback(
00247 '/(<!--LINK .*?-->)/',
00248 $replacer->cb(),
00249 $text);
00250
00251 wfProfileOut( __METHOD__.'-replace' );
00252 wfProfileOut( __METHOD__ );
00253 }
00254
00258 protected function replaceInterwiki( &$text ) {
00259 if ( empty( $this->interwikis ) ) {
00260 return;
00261 }
00262
00263 wfProfileIn( __METHOD__ );
00264 # Make interwiki link HTML
00265 $sk = $this->parent->getOptions()->getSkin();
00266 $replacePairs = array();
00267 foreach( $this->interwikis as $key => $link ) {
00268 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
00269 }
00270 $replacer = new HashtableReplacer( $replacePairs, 1 );
00271
00272 $text = preg_replace_callback(
00273 '/<!--IWLINK (.*?)-->/',
00274 $replacer->cb(),
00275 $text );
00276 wfProfileOut( __METHOD__ );
00277 }
00278
00282 protected function doVariants( &$colours ) {
00283 global $wgContLang;
00284 $linkBatch = new LinkBatch();
00285 $variantMap = array();
00286 $output = $this->parent->getOutput();
00287 $linkCache = LinkCache::singleton();
00288 $sk = $this->parent->getOptions()->getSkin();
00289 $threshold = $this->getStubThreshold();
00290
00291
00292 foreach ( $this->internals as $ns => $entries ) {
00293 foreach ( $entries as $index => $entry ) {
00294 $key = "$ns:$index";
00295 $pdbk = $entry['pdbk'];
00296 $title = $entry['title'];
00297 $titleText = $title->getText();
00298
00299
00300 $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
00301
00302
00303 if ( !isset($colours[$pdbk]) ){
00304 foreach($allTextVariants as $textVariant){
00305 if($textVariant != $titleText){
00306 $variantTitle = Title::makeTitle( $ns, $textVariant );
00307 if(is_null($variantTitle)) continue;
00308 $linkBatch->addObj( $variantTitle );
00309 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
00310 }
00311 }
00312 }
00313 }
00314 }
00315
00316
00317 $categoryMap = array();
00318 $varCategories = array();
00319 foreach( $output->getCategoryLinks() as $category ){
00320 $variants = $wgContLang->convertLinkToAllVariants($category);
00321 foreach($variants as $variant){
00322 if($variant != $category){
00323 $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
00324 if(is_null($variantTitle)) continue;
00325 $linkBatch->addObj( $variantTitle );
00326 $categoryMap[$variant] = $category;
00327 }
00328 }
00329 }
00330
00331
00332 if(!$linkBatch->isEmpty()){
00333
00334 $dbr = wfGetDB( DB_SLAVE );
00335 $page = $dbr->tableName( 'page' );
00336 $titleClause = $linkBatch->constructSet('page', $dbr);
00337 $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
00338 $variantQuery .= " FROM $page WHERE $titleClause";
00339 $varRes = $dbr->query( $variantQuery, __METHOD__ );
00340 $linkcolour_ids = array();
00341
00342
00343 while ( $s = $dbr->fetchObject($varRes) ) {
00344
00345 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
00346 $varPdbk = $variantTitle->getPrefixedDBkey();
00347 $vardbk = $variantTitle->getDBkey();
00348
00349 $holderKeys = array();
00350 if(isset($variantMap[$varPdbk])){
00351 $holderKeys = $variantMap[$varPdbk];
00352 $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
00353 $output->addLink( $variantTitle, $s->page_id );
00354 }
00355
00356
00357 foreach($holderKeys as $key){
00358 list( $ns, $index ) = explode( ':', $key, 2 );
00359 $entry =& $this->internals[$ns][$index];
00360 $pdbk = $entry['pdbk'];
00361
00362 if(!isset($colours[$pdbk])){
00363
00364 $entry['title'] = $variantTitle;
00365 $entry['pdbk'] = $varPdbk;
00366
00367
00368 # FIXME: convoluted data flow
00369 # The redirect status and length is passed to getLinkColour via the LinkCache
00370 # Use formal parameters instead
00371 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
00372 $linkcolour_ids[$s->page_id] = $pdbk;
00373 }
00374 }
00375
00376
00377 if(isset($categoryMap[$vardbk])){
00378 $oldkey = $categoryMap[$vardbk];
00379 if($oldkey != $vardbk)
00380 $varCategories[$oldkey]=$vardbk;
00381 }
00382 }
00383 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
00384
00385
00386 if(count($varCategories)>0){
00387 $newCats = array();
00388 $originalCats = $output->getCategories();
00389 foreach($originalCats as $cat => $sortkey){
00390
00391 if( array_key_exists($cat,$varCategories) )
00392 $newCats[$varCategories[$cat]] = $sortkey;
00393 else $newCats[$cat] = $sortkey;
00394 }
00395 $output->setCategoryLinks($newCats);
00396 }
00397 }
00398 }
00399
00406 function replaceText( $text ) {
00407 wfProfileIn( __METHOD__ );
00408
00409 $text = preg_replace_callback(
00410 '/<!--(LINK|IWLINK) (.*?)-->/',
00411 array( &$this, 'replaceTextCallback' ),
00412 $text );
00413
00414 wfProfileOut( __METHOD__ );
00415 return $text;
00416 }
00417
00423 function replaceTextCallback( $matches ) {
00424 $type = $matches[1];
00425 $key = $matches[2];
00426 if( $type == 'LINK' ) {
00427 list( $ns, $index ) = explode( ':', $key, 2 );
00428 if( isset( $this->internals[$ns][$index]['text'] ) ) {
00429 return $this->internals[$ns][$index]['text'];
00430 }
00431 } elseif( $type == 'IWLINK' ) {
00432 if( isset( $this->interwikis[$key]['text'] ) ) {
00433 return $this->interwikis[$key]['text'];
00434 }
00435 }
00436 return $matches[0];
00437 }
00438 }