From 41231616b63c41fcacdc1631330b22c593f27425 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Sat, 14 May 2011 12:20:45 +0000 Subject: [PATCH] (bug 23002) Imagelinks table not updated after imagemove. The actual bug was inconsistent behaviour between imagelinks and pagelinks for redirects. * Parser now only adds the redirect source to imagelinks, like it does in pagelinks * ImagePage now shows the file redirects in-line with the normal "The following pages link to this file:" ** Added message linkstoimage-redirect ** Removed the separate file redirects section and removed associated message redirectstofile ** ImagePage::imageLinks will first fetch image links to the file, determine which are redirects, and if there are fewer links than the limit, fetch the redirect target links. --- includes/ImagePage.php | 116 ++++++++++++++++------------- includes/parser/Parser.php | 2 - languages/messages/MessagesEn.php | 2 +- languages/messages/MessagesQqq.php | 4 +- 4 files changed, 69 insertions(+), 55 deletions(-) diff --git a/includes/ImagePage.php b/includes/ImagePage.php index 6d4aedf114..54c676c4fc 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -140,7 +140,6 @@ class ImagePage extends Article { $this->imageDupes(); # TODO! FIXME! For some freaky reason, we can't redirect to foreign images. # Yet we return metadata about the target. Definitely an issue in the FileRepo - $this->imageRedirects(); $this->imageLinks(); # Allow extensions to add something after the image links @@ -660,21 +659,45 @@ EOT } } - protected function imageLinks() { - global $wgUser, $wgOut, $wgLang; - - $limit = 100; - + protected function queryImageLinks( $target, $limit ) { $dbr = wfGetDB( DB_SLAVE ); - $res = $dbr->select( + return $dbr->select( array( 'imagelinks', 'page' ), - array( 'page_namespace', 'page_title' ), - array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ), + array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ), + array( 'il_to' => $target, 'il_from = page_id' ), __METHOD__, array( 'LIMIT' => $limit + 1 ) - ); - $count = $dbr->numRows( $res ); + ); + } + + protected function imageLinks() { + global $wgUser, $wgOut, $wgLang; + + $limit = 100; + + $res = $this->queryImageLinks( $this->mTitle->getDbKey(), $limit + 1); + $rows = array(); + $redirects = array(); + foreach ( $res as $row ) { + if ( $row->page_is_redirect ) { + $redirects[$row->page_title] = array(); + } + $rows[] = $row; + } + $count = count( $rows ); + + $hasMore = $count > $limit; + if ( !$hasMore && count( $redirects ) ) { + $res = $this->queryImageLinks( array_keys( $redirects ), + $limit - count( $rows ) + 1 ); + foreach ( $res as $row ) { + $redirects[$row->il_to][] = $row; + $count++; + } + $hasMore = ( $res->numRows() + count( $rows ) ) > $limit; + } + if ( $count == 0 ) { $wgOut->wrapWikiMsg( Html::rawElement( 'div', @@ -685,7 +708,7 @@ EOT } $wgOut->addHTML( "
\n" ); - if ( $count <= $limit - 1 ) { + if ( !$hasMore ) { $wgOut->addWikiMsg( 'linkstoimage', $count ); } else { // More links than the limit. Add a link to [[Special:Whatlinkshere]] @@ -701,25 +724,44 @@ EOT ); $sk = $wgUser->getSkin(); $count = 0; - $elements = array(); - foreach ( $res as $s ) { - $count++; - if ( $count <= $limit ) { - // We have not yet reached the extra one that tells us there is more to fetch - $elements[] = $s; - } - } // Sort the list by namespace:title - usort( $elements, array( $this, 'compare' ) ); + usort( $rows, array( $this, 'compare' ) ); // Create links for every element - foreach( $elements as $element ) { + $currentCount = 0; + foreach( $rows as $element ) { + $currentCount++; + if ( $currentCount > $limit ) { + break; + } + $link = $sk->linkKnown( Title::makeTitle( $element->page_namespace, $element->page_title ) ); + if ( !isset( $redirects[$element->page_title] ) ) { + $liContents = $link; + } else { + $ul = "'; + $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams( + $link, $ul )->parse(); + } $wgOut->addHTML( Html::rawElement( 'li', array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace ), - $link + $liContents ) . "\n" ); @@ -733,34 +775,6 @@ EOT } $wgOut->addHTML( Html::closeElement( 'div' ) . "\n" ); } - - protected function imageRedirects() { - global $wgUser, $wgOut, $wgLang; - - $redirects = $this->getTitle()->getRedirectsHere( NS_FILE ); - if ( count( $redirects ) == 0 ) { - return; - } - - $wgOut->addHTML( "
\n" ); - $wgOut->addWikiMsg( 'redirectstofile', - $wgLang->formatNum( count( $redirects ) ) - ); - $wgOut->addHTML( "
    \n" ); - - $sk = $wgUser->getSkin(); - foreach ( $redirects as $title ) { - $link = $sk->link( - $title, - null, - array(), - array( 'redirect' => 'no' ), - array( 'known', 'noclasses' ) - ); - $wgOut->addHTML( "
  • {$link}
  • \n" ); - } - $wgOut->addHTML( "
\n" ); - } protected function imageDupes() { global $wgOut, $wgUser, $wgLang; diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index e1cafa79df..1f780fb2da 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -3508,8 +3508,6 @@ class Parser { # Register the file as a dependency... $this->mOutput->addImage( $title->getDBkey(), $time, $sha1 ); if ( $file && !$title->equals( $file->getTitle() ) ) { - # We fetched a rev from a different title; register it too... - $this->mOutput->addImage( $file->getTitle()->getDBkey(), $time, $sha1 ); # Update fetched file title $title = $file->getTitle(); } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 098211e56f..751a0abc01 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -2333,7 +2333,7 @@ The following list shows the {{PLURAL:$1|first page link|first $1 page links}} t A [[Special:WhatLinksHere/$2|full list]] is available.', 'nolinkstoimage' => 'There are no pages that link to this file.', 'morelinkstoimage' => 'View [[Special:WhatLinksHere/$1|more links]] to this file.', -'redirectstofile' => 'The following {{PLURAL:$1|file redirects|$1 files redirect}} to this file:', +'linkstoimage-redirect' => '$1 (file redirect) $2', 'duplicatesoffile' => 'The following {{PLURAL:$1|file is a duplicate|$1 files are duplicates}} of this file ([[Special:FileDuplicateSearch/$2|more details]]):', 'sharedupload' => 'This file is from $1 and may be used by other projects.', 'sharedupload-desc-there' => 'This file is from $1 and may be used by other projects. diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 5b43b065f8..4e20402eb0 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -1901,8 +1901,10 @@ Example: [[:Image:Addon-icn.png]]', * $1: limit. At the moment hardcoded at 100 * $2: filename', +'linkstoimage-redirect' => 'Item in the "the following pages link to this file" section on a file page if the item is a redirect. +* $1: an HTML link to the file +* $2: the list of files that link to the redirect (may be empty)', 'nolinkstoimage' => 'Displayed on image description pages, see for exampe [[:Image:Tournesol.png#filelinks]].', -'redirectstofile' => 'Used on file description pages after the list of pages which used this file', 'duplicatesoffile' => 'Shown on file description pages when a file is duplicated * $1: Number of identical files -- 2.20.1