r34885 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34884‎ | r34885 | r34886 >
Date:19:17, 15 May 2008
Author:btongminh
Status:old
Tags:
Comment:
Do some refactoring on ImagePage. Shuffled the links section; Use Title::getRedirectsHere; Move duplicate detection to its own function.
Modified paths:
  • /trunk/phase3/includes/ImagePage.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/includes/filerepo/File.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/ImagePage.php
@@ -18,6 +18,7 @@
1919 /* private */ var $img; // Image object this page is shown for
2020 /* private */ var $repo;
2121 var $mExtraDescription = false;
 22+ var $dupes;
2223
2324 function __construct( $title, $time = false ) {
2425 parent::__construct( $title );
@@ -28,7 +29,8 @@
2930 } else {
3031 $this->current = $time ? wfLocalFile( $this->mTitle ) : $this->img;
3132 }
32 - $this->repo = $this->img->repo;
 33+ $this->repo = $this->img->getRepo();
 34+ $this->dupes = null;
3335 }
3436
3537 /**
@@ -104,11 +106,15 @@
105107 $this->closeShowImage();
106108 $this->imageHistory();
107109 // TODO: Cleanup the following
108 - $this->imageLinks();
 110+
 111+ $wgOut->addHTML( Xml::element( 'h2',
 112+ array( 'id' => 'filelinks' ),
 113+ wfMsg( 'imagelinks' ) ) . "\n" );
109114 $this->imageDupes();
110115 // TODO: We may want to find local images redirecting to a foreign
111116 // file: "The following local files redirect to this file"
112 - if ( $this->img->isLocal() ) $this->imageRedirects();
 117+ if ( $this->img->isLocal() ) $this->imageRedirects();
 118+ $this->imageLinks();
113119
114120 if ( $showmeta ) {
115121 global $wgStylePath, $wgStyleVersion;
@@ -155,6 +161,28 @@
156162 public function getFile() {
157163 return $this->img;
158164 }
 165+
 166+ public function getDuplicates() {
 167+ if ( !is_null($this->dupes) ) return $this->dupes;
 168+
 169+ if ( !( $hash = $this->img->getSha1() ) )
 170+ return $this->dupes = array();
 171+ $dupes = RepoGroup::singleton()->findBySha1( $hash );
 172+
 173+ // Remove duplicates with self and non matching file sizes
 174+ $self = $this->img->getRepoName().':'.$this->img->getName();
 175+ $size = $this->img->getSize();
 176+ foreach ( $dupes as $index => $file ) {
 177+ $key = $file->getRepoName().':'.$file->getName();
 178+ if ( $key == $self )
 179+ unset( $dupes[$index] );
 180+ if ( $file->getSize() != $size )
 181+ unset( $dupes[$index] );
 182+ }
 183+ return $this->dupes = $dupes;
 184+
 185+ }
 186+
159187
160188 /**
161189 * Create the TOC
@@ -438,6 +466,9 @@
439467 }
440468 }
441469
 470+ /*
 471+ * Check for files with the same name on the foreign repos.
 472+ */
442473 function checkSharedConflict() {
443474 global $wgOut, $wgUser;
444475 $repoGroup = RepoGroup::singleton();
@@ -561,8 +592,6 @@
562593
563594 $limit = 100;
564595
565 - $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
566 -
567596 $dbr = wfGetDB( DB_SLAVE );
568597
569598 $res = $dbr->select(
@@ -603,53 +632,26 @@
604633 {
605634 global $wgUser, $wgOut;
606635
607 - $dbr = wfGetDB( DB_SLAVE );
608 - $res = $dbr->select(
609 - array( 'redirect', 'page' ),
610 - array( 'page_title' ),
611 - array(
612 - 'rd_namespace' => NS_IMAGE,
613 - 'rd_title' => $this->mTitle->getDBkey(),
614 - 'page_namespace' => NS_IMAGE,
615 - 'rd_from = page_id'
616 - ),
617 - __METHOD__
618 - );
 636+ $redirects = $this->getTitle()->getRedirectsHere( NS_IMAGE );
 637+ if ( count( $redirects ) == 0 ) return;
619638
620639
621 - if ( 0 == $dbr->numRows( $res ) )
622 - return;
623 -
624640 $wgOut->addWikiMsg( 'redirectstofile' );
625641 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
626642
627643 $sk = $wgUser->getSkin();
628 - while ( $row = $dbr->fetchObject( $res ) ) {
629 - $name = Title::makeTitle( NS_IMAGE, $row->page_title );
630 - $link = $sk->makeKnownLinkObj( $name, "" );
631 - wfDebug("Image redirect: {$row->page_title}\n");
 644+ foreach ( $redirects as $title ) {
 645+ $link = $sk->makeKnownLinkObj( $title, "" );
632646 $wgOut->addHTML( "<li>{$link}</li>\n" );
633647 }
634648 $wgOut->addHTML( "</ul>\n" );
635 -
636 - $res->free();
 649+
637650 }
638651
639652 function imageDupes() {
640653 global $wgOut, $wgUser;
641654
642 - if ( !( $hash = $this->img->getSha1() ) ) return;
643 - // Probably deprecates checkSharedConflict?
644 - $dupes = RepoGroup::singleton()->findBySha1( $hash );
645 - //$dupes = RepoGroup::singleton()->getLocalRepo()->findBySha1( $hash );
646 -
647 - // Don't dupe with self
648 - $self = $this->img->getRepoName().':'.$this->img->getName();
649 - foreach ( $dupes as $index => $file ) {
650 - $key = $file->getRepoName().':'.$file->getName();
651 - if ( $key == $self )
652 - unset( $dupes[$index] );
653 - }
 655+ $dupes = $this->getDuplicates();
654656 if ( count( $dupes ) == 0 ) return;
655657
656658 $wgOut->addWikiMsg( 'duplicatesoffile' );
Index: trunk/phase3/includes/filerepo/File.php
@@ -904,6 +904,12 @@
905905 function getRepoName() {
906906 return $this->repo ? $this->repo->getName() : 'unknown';
907907 }
 908+ /*
 909+ * Returns the repository
 910+ */
 911+ function getRepo() {
 912+ return $this->repo;
 913+ }
908914
909915 /**
910916 * Returns true if the image is an old version
Index: trunk/phase3/includes/Title.php
@@ -3126,12 +3126,25 @@
31273127 return MWNamespace::isContent( $this->getNamespace() );
31283128 }
31293129
3130 - public function getRedirectsHere() {
 3130+ public function getRedirectsHere( $ns = null ) {
31313131 $redirs = array();
3132 - $dbr = wfGetDB( DB_SLAVE );
3133 - list($page,$redirect) = $dbr->tableNamesN( 'page', 'redirect' );
3134 - $result = $dbr->query( "SELECT page_title, page_namespace FROM $page JOIN $redirect ON page_id = rd_from WHERE rd_title = "
3135 - . $dbr->addQuotes( $this->getDBKey() ) . " AND rd_namespace = " . $this->getNamespace(), __METHOD__ );
 3132+
 3133+ $dbr = wfGetDB( DB_SLAVE );
 3134+ $where = array(
 3135+ 'rd_namespace' => $this->getNamespace(),
 3136+ 'rd_title' => $this->getDBkey(),
 3137+ 'rd_from = page_id'
 3138+ );
 3139+ if ( !is_null($ns) ) $where['page_namespace'] = $ns;
 3140+
 3141+ $result = $dbr->select(
 3142+ array( 'redirect', 'page' ),
 3143+ array( 'page_namespace', 'page_title' ),
 3144+ $where,
 3145+ __METHOD__
 3146+ );
 3147+
 3148+
31363149 while( $row = $dbr->fetchObject( $result ) ) {
31373150 $redirs[] = self::newFromRow( $row );
31383151 }

Status & tagging log