r64770 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64769‎ | r64770 | r64771 >
Date:01:54, 9 April 2010
Author:jdpond
Status:deferred
Tags:
Comment:
Alpha release version:
1. Fixes several thumbnail bugs
2. Allows all namespaces > 1000 (used to be only 100-999)
3. Does not fix thumbnails on archived files (Still working on this)
Modified paths:
  • /trunk/extensions/NSFileRepo/NSFileRepo.php (modified) (history)

Diff [purge]

Index: trunk/extensions/NSFileRepo/NSFileRepo.php
@@ -35,7 +35,7 @@
3636 'path' => __FILE__,
3737 'name' => 'NSFileRepo',
3838 'author' => 'Jack D. Pond',
39 - 'version' => '1.3',
 39+ 'version' => '1.4alpha',
4040 'url' => 'http://www.mediawiki.org/wiki/Extension:NSFileRepo',
4141 'descriptionmsg' => 'nsfilerepo-desc'
4242 );
@@ -113,6 +113,86 @@
114114 $filename = $bits[count($bits)-1];
115115 return $this->getHashPath() . rawurlencode( $filename );
116116 }
 117+
 118+ /** Get the URL of the thumbnail directory, or a particular file if $suffix is specified */
 119+ function getThumbUrl( $suffix = false ) {
 120+ $path = $this->repo->getZoneUrl('thumb') . '/' . $this->getUrlRel();
 121+ if ( $suffix !== false ) {
 122+ $path .= '/' . rawurlencode( $this->getFileNameStripped($suffix) );
 123+ }
 124+ return $path;
 125+ }
 126+
 127+ /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
 128+ function getThumbPath( $suffix = false ) {
 129+ $path = $this->repo->getZonePath('thumb') . '/' . $this->getRel();
 130+ if ( $suffix !== false ) {
 131+ $path .= '/' . $this->getFileNameStripped($suffix);
 132+ }
 133+ return $path;
 134+ }
 135+
 136+ /** Get the relative path for an archive file */
 137+ function getArchiveRel( $suffix = false ) {
 138+ $path = 'archive/' . $this->getHashPath();
 139+ if ( $suffix === false ) {
 140+ $path = substr( $path, 0, -1 );
 141+ } else {
 142+ $path .= $this->getFileNameStripped($suffix);
 143+ }
 144+ return $path;
 145+ }
 146+
 147+ /** Get the URL of the archive directory, or a particular file if $suffix is specified */
 148+ function getArchiveUrl( $suffix = false ) {
 149+ $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
 150+ if ( $suffix === false ) {
 151+ $path = substr( $path, 0, -1 );
 152+ } else {
 153+ $path .= rawurlencode( $this->getFileNameStripped($suffix) );
 154+ }
 155+ return $path;
 156+ }
 157+
 158+ /** Get the virtual URL for an archive file or directory */
 159+ function getArchiveVirtualUrl( $suffix = false ) {
 160+ $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
 161+ if ( $suffix === false ) {
 162+ $path = substr( $path, 0, -1 );
 163+ } else {
 164+ $path .= rawurlencode( $this->getFileNameStripped($suffix) );
 165+ }
 166+ return $path;
 167+ }
 168+
 169+ /** Get the virtual URL for a thumbnail file or directory */
 170+ function getThumbVirtualUrl( $suffix = false ) {
 171+ $path = $this->repo->getVirtualUrl() . '/thumb/' . $this->getUrlRel();
 172+ if ( $suffix !== false ) {
 173+ $path .= '/' . rawurlencode( $this->getFileNameStripped($suffix) );
 174+ }
 175+ return $path;
 176+ }
 177+
 178+ /** Get the virtual URL for the file itself */
 179+ function getVirtualUrl( $suffix = false ) {
 180+ $path = $this->repo->getVirtualUrl() . '/public/' . $this->getUrlRel();
 181+ if ( $suffix !== false ) {
 182+ $path .= '/' . rawurlencode( $this->getFileNameStripped($suffix) );
 183+ }
 184+ return $path;
 185+ }
 186+
 187+ /** Strip namespace (if any) from file name */
 188+ function getFileNameStripped($suffix) {
 189+ $bits=explode(':',$suffix);
 190+ $filename = $bits[count($bits)-1];
 191+ $pxpos = strpos($suffix,"px-");
 192+ if (count($bits) > 1 && $pxpos) $filename= substr($suffix,0,$pxpos+3).$filename;
 193+ return $filename;
 194+ }
 195+
 196+
117197 /**
118198 * This function overrides the LocalFile because the archive name should not contain the namespace in the
119199 * filename. Otherwise the function would have worked. This only affects reuploads
@@ -136,8 +216,7 @@
137217 $this->lock();
138218 $dstRel = $this->getRel();
139219 /* This is the part that changed from LocalFile */
140 - $bits=explode(':',$this->getName());
141 - $archiveName = gmdate( 'YmdHis' ) . '!'.$bits[count($bits)-1];
 220+ $archiveName = gmdate( 'YmdHis' ) . '!'.$this->getFileNameStripped($this->getName());
142221 /* End of changes */
143222 $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
144223 $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0;
@@ -151,8 +230,46 @@
152231 return $status;
153232 }
154233
 234+ /**
 235+ * The only thing changed here is that the array needs to strip the NS from the file name for the has (oldname is already fixed)
 236+ * Add the old versions of the image to the batch
 237+ */
 238+ function addOlds() {
 239+ $archiveBase = 'archive';
 240+ $this->olds = array();
 241+ $this->oldCount = 0;
155242
 243+ $result = $this->db->select( 'oldimage',
 244+ array( 'oi_archive_name', 'oi_deleted' ),
 245+ array( 'oi_name' => $this->oldName ),
 246+ __METHOD__
 247+ );
 248+ while( $row = $this->db->fetchObject( $result ) ) {
 249+ $oldName = $row->oi_archive_name;
 250+ $bits = explode( '!', $oldName, 2 );
 251+ if( count( $bits ) != 2 ) {
 252+ wfDebug( "Invalid old file name: $oldName \n" );
 253+ continue;
 254+ }
 255+ list( $timestamp, $filename ) = $bits;
 256+ if( $this->oldName != $filename ) {
 257+ wfDebug( "Invalid old file name: $oldName \n" );
 258+ continue;
 259+ }
 260+ $this->oldCount++;
 261+ // Do we want to add those to oldCount?
 262+ if( $row->oi_deleted & File::DELETED_FILE ) {
 263+ continue;
 264+ }
 265+ $this->olds[] = array(
 266+ "{$archiveBase}/{$this->oldHash}{$oldName}",
 267+ "{$archiveBase}/{$this->newHash}{$timestamp}!".$this->getFileNameStripped($this->newName)
 268+ );
 269+ }
 270+ $this->db->freeResult( $result );
 271+ }
156272
 273+
157274 /** Instantiating this class using "self"
158275 * If you're reading this, you're problably wondering why on earth are the following static functions, which are copied
159276 * verbatim from the original extended class "LocalFIle" included here?
@@ -178,7 +295,7 @@
179296 */
180297
181298 static function newFromRow( $row, $repo ) {
182 - $title = Title::makeTitle( NS_FILE, $row->img_name );
 299+ $title = Title::makeTitle( NS_IMAGE, $row->img_name );
183300 $file = new self( $title, $repo );
184301 $file->loadFromRow( $row );
185302 return $file;
@@ -195,6 +312,33 @@
196313 function publish( $srcPath, $flags = 0 ) {
197314 return NSLocalFile::publish( $srcPath, $flags );
198315 }
 316+ function getThumbUrl( $suffix = false ) {
 317+ return(NSLocalFile::getThumbUrl( $suffix ) );
 318+ }
 319+ function getThumbPath( $suffix = false ) {
 320+ return(NSLocalFile::getThumbPath( $suffix ));
 321+ }
 322+ function getArchiveRel( $suffix = false ) {
 323+ return(NSLocalFile::getArchiveRel( $suffix ));
 324+ }
 325+ function getArchiveUrl( $suffix = false ) {
 326+ return(NSLocalFile::getArchiveUrl( $suffix ));
 327+ }
 328+ function getArchiveVirtualUrl( $suffix = false ) {
 329+ return(NSLocalFile::getArchiveVirtualUrl( $suffix ));
 330+ }
 331+ function getThumbVirtualUrl( $suffix = false ) {
 332+ return(NSLocalFile::getArchiveVirtualUrl( $suffix ));
 333+ }
 334+ function getVirtualUrl( $suffix = false ) {
 335+ return(NSLocalFile::getVirtualUrl( $suffix ));
 336+ }
 337+ function getThumbStripped($suffix) {
 338+ return(NSLocalFile::getThumbStripped($suffix));
 339+ }
 340+ function addOlds() {
 341+ return(NSLocalFile::addOlds());
 342+ }
199343
200344 /** See comment about Instantiating this class using "self", above */
201345
@@ -210,7 +354,7 @@
211355 }
212356
213357 static function newFromRow( $row, $repo ) {
214 - $title = Title::makeTitle( NS_FILE, $row->oi_name );
 358+ $title = Title::makeTitle( NS_IMAGE, $row->oi_name );
215359 $file = new self( $title, $repo, null, $row->oi_archive_name );
216360 $file->loadFromRow( $row, 'oi_' );
217361 return $file;
@@ -251,7 +395,7 @@
252396 global $wgWhitelistRead;
253397 if (in_array($title->getPrefixedText(), $wgWhitelistRead)) return true;
254398 if (function_exists('lockdownUserCan')){
255 - if($title->getNamespace() == NS_FILE) {
 399+ if($title->getNamespace() == NS_IMAGE) {
256400 $ntitle = Title::newFromText($title->mDbkeyform);
257401 return ($ntitle->mNamespace < 100) ? true : lockdownUserCan($ntitle, $user, $action, $result);
258402 }
@@ -265,9 +409,10 @@
266410 # See if stored in a NS path
267411
268412 $subdirs = explode('/',$path);
269 - $x = (strlen($subdirs[1]) <> 3 && ($subdirs[1] == "archive" || $subdirs[1] == "deleted" || $subdirs[1] == "thumb")) ? 2 : 1;
270 - if (strlen($subdirs[$x]) == 3 && is_numeric($subdirs[$x]) && $subdirs[$x] >= 100) {
271 - $title = Title::makeTitleSafe( NS_FILE, $wgContLang->getNsText($subdirs[$x]).":".$name );
 413+ $x = (!is_numeric($subdirs[1]) && ($subdirs[1] == "archive" || $subdirs[1] == "deleted" || $subdirs[1] == "thumb")) ? 2 : 1;
 414+ $x = ($x == 2 && $subdirs[1] == "thumb" && $subdirs[2] == "archive") ? 3 : $x;
 415+ if (strlen($subdirs[$x]) >= 3 && is_numeric($subdirs[$x]) && $subdirs[$x] >= 100) {
 416+ $title = Title::makeTitleSafe( NS_IMAGE, $wgContLang->getNsText($subdirs[$x]).":".$name );
272417 if( !$title instanceof Title ) {
273418 $result = array('img-auth-accessdenied','img-auth-badtitle',$name);
274419 return false;

Status & tagging log