r27759 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r27758‎ | r27759 | r27760 >
Date:15:54, 22 November 2007
Author:thomasv
Status:old
Tags:
Comment:
* got rid of magic codes for colours.* added hook for link recolouring by extensions* merged redundant stub tests in one single function, getLinkColour (Linker.php)
* deprecated makeStubLink and makeStubLinkObj
Modified paths:
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/Linker.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -504,6 +504,10 @@
505505 $url: string value as output (out parameter, can modify)
506506 $query: query options passed to Title::getInternalURL()
507507
 508+'GetLinkColours': modify the CSS class of an array of page links
 509+$linkcolour_ids: array of prefixed DB keys of the pages linked to, indexed by page_id.
 510+&$colours: (output) array of CSS classes, indexed by prefixed DB keys
 511+
508512 'GetLocalURL': modify local URLs as output into page links
509513 $title: Title object of page
510514 $url: string value as output (out parameter, can modify)
Index: trunk/phase3/includes/Linker.php
@@ -52,19 +52,11 @@
5353 }
5454
5555 /** @todo document */
56 - function getInternalLinkAttributes( $link, $text, $broken = false ) {
 56+ function getInternalLinkAttributes( $link, $text, $class='' ) {
5757 $link = urldecode( $link );
5858 $link = str_replace( '_', ' ', $link );
5959 $link = htmlspecialchars( $link );
60 -
61 - if( $broken == 'stub' ) {
62 - $r = ' class="stub"';
63 - } else if ( $broken == 'yes' ) {
64 - $r = ' class="new"';
65 - } else {
66 - $r = '';
67 - }
68 -
 60+ $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : '';
6961 $r .= " title=\"{$link}\"";
7062 return $r;
7163 }
@@ -72,22 +64,35 @@
7365 /**
7466 * @param $nt Title object.
7567 * @param $text String: FIXME
76 - * @param $broken Boolean: FIXME, default 'false'.
 68+ * @param $class String: CSS class of the link, default ''.
7769 */
78 - function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
79 - if( $broken == 'stub' ) {
80 - $r = ' class="stub"';
81 - } else if ( $broken == 'yes' ) {
82 - $r = ' class="new"';
83 - } else {
84 - $r = '';
85 - }
86 -
 70+ function getInternalLinkAttributesObj( &$nt, $text, $class='' ) {
 71+ $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : '';
8772 $r .= ' title="' . $nt->getEscapedText() . '"';
8873 return $r;
8974 }
9075
9176 /**
 77+ * Return the CSS colour of a known link
 78+ *
 79+ * @param mixed $s
 80+ * @param integer $id
 81+ * @param integer $threshold
 82+ */
 83+ function getLinkColour( $s, $threshold ) {
 84+ if( $threshold > 0 && $s!=false ) {
 85+ $colour = ( $s->page_len >= $threshold ||
 86+ $s->page_is_redirect ||
 87+ !Namespace::isContent( $s->page_namespace )
 88+ ? '' : 'stub' );
 89+ }
 90+ else {
 91+ $colour = '';
 92+ }
 93+ return $colour;
 94+ }
 95+
 96+ /**
9297 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
9398 * it if you already have a title object handy. See makeLinkObj for further documentation.
9499 *
@@ -155,6 +160,8 @@
156161 }
157162
158163 /**
 164+ * @deprecated use makeColouredLinkObj
 165+ *
159166 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
160167 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
161168 *
@@ -240,7 +247,7 @@
241248 if ( 0 == $aid ) {
242249 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
243250 } else {
244 - $stub = false;
 251+ $colour = '';
245252 if ( $nt->isContentPage() ) {
246253 $threshold = $wgUser->getOption('stubthreshold');
247254 if ( $threshold > 0 ) {
@@ -250,15 +257,10 @@
251258 array( 'page_len',
252259 'page_is_redirect' ),
253260 array( 'page_id' => $aid ), __METHOD__ ) ;
254 - $stub = ( $s !== false && !$s->page_is_redirect &&
255 - $s->page_len < $threshold );
 261+ $colour=$this->getLinkColour( $s, $threshold );
256262 }
257263 }
258 - if ( $stub ) {
259 - $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
260 - } else {
261 - $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
262 - }
 264+ $retVal = $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
263265 }
264266 wfProfileOut( __METHOD__.'-immediate' );
265267 }
@@ -345,7 +347,7 @@
346348 if ( '' == $text ) {
347349 $text = htmlspecialchars( $nt->getPrefixedText() );
348350 }
349 - $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
 351+ $style = $this->getInternalLinkAttributesObj( $nt, $text, 'new' );
350352
351353 list( $inside, $trail ) = Linker::splitTrail( $trail );
352354 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
@@ -355,6 +357,8 @@
356358 }
357359
358360 /**
 361+ * @deprecated use makeColouredLinkObj
 362+ *
359363 * Make a brown link to a short article.
360364 *
361365 * @param $title String: the text of the title
@@ -365,7 +369,25 @@
366370 * the end of the link.
367371 */
368372 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
369 - $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
 373+ makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
 374+ }
 375+
 376+ /**
 377+ * Make a coloured link.
 378+ *
 379+ * @param $title String: the text of the title
 380+ * @param $colour Integer: colour of the link
 381+ * @param $text String: link text
 382+ * @param $query String: optional query part
 383+ * @param $trail String: optional trail. Alphabetic characters at the start of this string will
 384+ * be included in the link text. Other characters will be appended after
 385+ * the end of the link.
 386+ */
 387+ function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
 388+
 389+ if($colour != ''){
 390+ $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
 391+ } else $style = '';
370392 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
371393 }
372394
@@ -384,11 +406,8 @@
385407 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
386408 global $wgUser;
387409 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
388 - if( $size < $threshold ) {
389 - return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
390 - } else {
391 - return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
392 - }
 410+ $colour = ( $size < $threshold ) ? 'stub' : '';
 411+ return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
393412 }
394413
395414 /**
@@ -709,7 +728,7 @@
710729 if( $query != '' )
711730 $q .= '&' . $query;
712731 list( $inside, $trail ) = self::splitTrail( $trail );
713 - $style = $this->getInternalLinkAttributesObj( $title, $text, 'yes' );
 732+ $style = $this->getInternalLinkAttributesObj( $title, $text, 'new' );
714733 wfProfileOut( __METHOD__ );
715734 return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
716735 . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
Index: trunk/phase3/includes/Parser.php
@@ -4315,10 +4315,7 @@
43164316 /**
43174317 * Replace <!--LINK--> link placeholders with actual links, in the buffer
43184318 * Placeholders created in Skin::makeLinkObj()
4319 - * Returns an array of links found, indexed by PDBK:
4320 - * 0 - broken
4321 - * 1 - normal link
4322 - * 2 - stub
 4319+ * Returns an array of link CSS classes, indexed by PDBK.
43234320 * $options is a bit field, RLH_FOR_UPDATE to select for update
43244321 */
43254322 function replaceLinkHolders( &$text, $options = 0 ) {
@@ -4330,6 +4327,7 @@
43314328
43324329 $pdbks = array();
43334330 $colours = array();
 4331+ $linkcolour_ids = array();
43344332 $sk = $this->mOptions->getSkin();
43354333 $linkCache =& LinkCache::singleton();
43364334
@@ -4358,14 +4356,14 @@
43594357
43604358 # Check if it's a static known link, e.g. interwiki
43614359 if ( $title->isAlwaysKnown() ) {
4362 - $colours[$pdbk] = 1;
 4360+ $colours[$pdbk] = '';
43634361 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
4364 - $colours[$pdbk] = 1;
 4362+ $colours[$pdbk] = '';
43654363 $this->mOutput->addLink( $title, $id );
43664364 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
4367 - $colours[$pdbk] = 0;
 4365+ $colours[$pdbk] = 'new';
43684366 } elseif ( $title->getNamespace() == NS_SPECIAL && !SpecialPage::exists( $pdbk ) ) {
4369 - $colours[$pdbk] = 0;
 4367+ $colours[$pdbk] = 'new';
43704368 } else {
43714369 # Not in the link cache, add it to the query
43724370 if ( !isset( $current ) ) {
@@ -4395,20 +4393,17 @@
43964394
43974395 # Fetch data and form into an associative array
43984396 # non-existent = broken
4399 - # 1 = known
4400 - # 2 = stub
44014397 while ( $s = $dbr->fetchObject($res) ) {
44024398 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
44034399 $pdbk = $title->getPrefixedDBkey();
44044400 $linkCache->addGoodLinkObj( $s->page_id, $title );
44054401 $this->mOutput->addLink( $title, $s->page_id );
4406 -
4407 - $colours[$pdbk] = ( $threshold == 0 || (
4408 - $s->page_len >= $threshold || # always true if $threshold <= 0
4409 - $s->page_is_redirect ||
4410 - !Namespace::isContent( $s->page_namespace ) )
4411 - ? 1 : 2 );
 4402+ $colours[$pdbk] = $sk->getLinkColour( $s, $threshold );
 4403+ //add id to the extension todolist
 4404+ $linkcolour_ids[$s->page_id] = $pdbk;
44124405 }
 4406+ //pass an array of page_ids to an extension
 4407+ wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
44134408 }
44144409 wfProfileOut( $fname.'-check' );
44154410
@@ -4504,18 +4499,10 @@
45054500
45064501 // set pdbk and colour
45074502 $pdbks[$key] = $varPdbk;
4508 - if ( $threshold > 0 ) {
4509 - $size = $s->page_len;
4510 - if ( $s->page_is_redirect || $s->page_namespace != 0 || $size >= $threshold ) {
4511 - $colours[$varPdbk] = 1;
4512 - } else {
4513 - $colours[$varPdbk] = 2;
4514 - }
4515 - }
4516 - else {
4517 - $colours[$varPdbk] = 1;
4518 - }
 4503+ $colours[$varPdbk] = $sk->getLinkColour( $s, $threshold );
 4504+ $linkcolour_ids[$s->page_id] = $pdbk;
45194505 }
 4506+ wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
45204507 }
45214508
45224509 // check if the object is a variant of a category
@@ -4548,21 +4535,17 @@
45494536 $pdbk = $pdbks[$key];
45504537 $searchkey = "<!--LINK $key-->";
45514538 $title = $this->mLinkHolders['titles'][$key];
4552 - if ( empty( $colours[$pdbk] ) ) {
 4539+ if ( !isset( $colours[$pdbk] ) ) {
45534540 $linkCache->addBadLinkObj( $title );
4554 - $colours[$pdbk] = 0;
 4541+ $colours[$pdbk] = 'new';
45554542 $this->mOutput->addLink( $title, 0 );
45564543 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
45574544 $this->mLinkHolders['texts'][$key],
45584545 $this->mLinkHolders['queries'][$key] );
4559 - } elseif ( $colours[$pdbk] == 1 ) {
4560 - $replacePairs[$searchkey] = $sk->makeKnownLinkObj( $title,
 4546+ } else {
 4547+ $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
45614548 $this->mLinkHolders['texts'][$key],
45624549 $this->mLinkHolders['queries'][$key] );
4563 - } elseif ( $colours[$pdbk] == 2 ) {
4564 - $replacePairs[$searchkey] = $sk->makeStubLinkObj( $title,
4565 - $this->mLinkHolders['texts'][$key],
4566 - $this->mLinkHolders['queries'][$key] );
45674550 }
45684551 }
45694552 $replacer = new HashtableReplacer( $replacePairs, 1 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r27906Fix regressions in r27759 -- pages already marked in link cache at link parsi...brion20:45, 27 November 2007

Status & tagging log