r93354 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93353‎ | r93354 | r93355 >
Date:00:57, 28 July 2011
Author:nelson
Status:deferred
Tags:
Comment:
move resolveVirtualUrl into Repo where it belongs; add support for reverting to a file already in the repo.
Modified paths:
  • /trunk/extensions/SwiftMedia/LocalSettings.php (modified) (history)
  • /trunk/extensions/SwiftMedia/SwiftMedia.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SwiftMedia/LocalSettings.php
@@ -55,7 +55,7 @@
5656 $wgDBpassword = "leakywiks";
5757
5858 # MySQL specific settings
59 -$wgDBprefix = "";
 59+$wgDBprefix = "mw2";
6060
6161 # MySQL table options to use during installation or update
6262 $wgDBTableOptions = "ENGINE=InnoDB, DEFAULT CHARSET=binary";
@@ -99,11 +99,11 @@
100100 # Site language code, should be one of ./languages/Language(.*).php
101101 $wgLanguageCode = "en";
102102
103 -$wgSecretKey = "790d7ee286c2fb4187b434ceb0fef12b45f30023a3d4613af8cddb164d7705aa";
 103+$wgSecretKey = "cea5a22adfa63e596b38ff892af5ab63fecaca26a43a4c9df1a35c0a85e85259";
104104
105105 # Site upgrade key. Must be set to a string (default provided) to turn on the
106106 # web installer while LocalSettings.php is in place
107 -$wgUpgradeKey = "ab66efc4edb006fd";
 107+$wgUpgradeKey = "5fd6cb729fc715bf";
108108
109109 ## Default skin: you can change the default skin. Use the internal symbolic
110110 ## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
@@ -122,12 +122,15 @@
123123 # Path to the GNU diff3 utility. Used for conflict resolution.
124124 $wgDiff3 = "/usr/bin/diff3";
125125
 126+# Enabled Extensions. Most extensions are enabled by including the base extension file here
 127+# but check specific extension documentation for more details
 128+# The following extensions were automatically enabled:
 129+require( "extensions/SwiftMedia/SwiftMedia.php" );
126130
 131+
127132 # End of automatically generated settings.
128133 # Add more configuration options below.
129134
130 -require_once( "$IP/extensions/SwiftMedia/SwiftMedia.php" );
131 -
132135 $wgUploadDirectory = "$IP/images/swift";
133136 // we don't need this and will ignore it. $wgDeletedDirectory = "{$wgUploadDirectory}/deleted";
134137 $wgUploadPath = "http://alsted.wikimedia.org/images/swift";
@@ -155,3 +158,4 @@
156159 $wgDebugLogFile = "/var/www/debug/abcd";
157160 $wgDebugTimestamps = true;
158161 $wgShowExceptionDetails = true;
 162+
Index: trunk/extensions/SwiftMedia/SwiftMedia.body.php
@@ -98,12 +98,14 @@
9999 /** isVisible inherited */
100100
101101 function getPath() {
102 - return $this->getLocalCopy($this->repo->container, $this->getRel());
 102+ $this->temp_path = $this->repo->getLocalCopy($this->repo->container, $this->getRel());
 103+ return $this->temp_path;
103104 }
104105
105106 /** Get the path of the archive directory, or a particular file if $suffix is specified */
106107 function getArchivePath( $suffix = false ) {
107 - return $this->getLocalCopy($this->repo->getZoneContainer('public'), $this->getArchiveRel( $suffix ));
 108+ $this->temp_path = $this->repo->getLocalCopy($this->repo->getZoneContainer('public'), $this->getArchiveRel( $suffix ));
 109+ return $this->temp_path;
108110 }
109111
110112 /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
@@ -112,76 +114,14 @@
113115 if ( $suffix !== false ) {
114116 $path .= '/' . $suffix;
115117 }
116 - return $this->getLocalCopy($this->repo->getZoneContainer('thumb'), $path);
117 - }
118 -
119 - /**
120 - * Get a local path corresponding to a virtual URL
121 - */
122 - function getContainerRel( $url ) {
123 - if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
124 - throw new MWException( __METHOD__.": unknown protocol" );
125 - }
126 -
127 - $bits = explode( '/', substr( $url, 9 ), 3 );
128 - if ( count( $bits ) != 3 ) {
129 - throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
130 - }
131 - list( $repo, $zone, $rel ) = $bits;
132 - if ( $repo !== $this->name ) {
133 - throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
134 - }
135 - $container = $this->getZoneContainer( $zone );
136 - if ( $container === false) {
137 - throw new MWException( __METHOD__.": invalid zone: $zone" );
138 - }
139 - return array($container, rawurldecode( $rel ));
140 - }
141 -
142 - /**
143 - * Called from elsewhere to turn a virtual URL into a path.
144 - */
145 - function resolveVirtualUrl( $url ) {
146 - $path = getContainerRel( $url );
147 - list($c, $r) = $path;
148 - return $this->getLocalCopy($c, $r);
149 - }
150 -
151 -
152 - /** Given a container and relative path, return an absolute path pointing at a copy of the file */
153 - function getLocalCopy($container, $rel) {
154 - // if we already have a local copy, return it.
155 - if ($this->temp_path) { return $this->temp_path; }
156 -
157 - // get a temporary place to put the original.
158 - $this->temp_path = tempnam( wfTempDir(), 'swift_in_' );
159 -
160 - /* Fetch the image out of Swift */
161 - $conn = $this->repo->connect();
162 - $cont = $this->repo->get_container($conn,$container);
163 -
164 - try {
165 - $obj = $cont->get_object($rel);
166 - } catch (NoSuchObjectException $e) {
167 - throw new MWException( "Unable to open original file at $container/$rel");
168 - }
169 -
170 - wfDebug( __METHOD__ . " writing to " . $this->temp_path . "\n");
171 - try {
172 - $obj->save_to_filename( $this->temp_path);
173 - } catch (IOException $e) {
174 - throw new MWException( __METHOD__ . ": error opening '$e'" );
175 - } catch (InvalidResponseException $e) {
176 - throw new MWException( __METHOD__ . "unexpected response '$e'" );
177 - }
178 -
 118+ $this->temp_path = $this->repo->getLocalCopy($this->repo->getZoneContainer('thumb'), $path);
179119 return $this->temp_path;
180120 }
181121
182122 function __destruct() {
183123 if ($this->temp_path) {
184124 // Clean up temporary data.
185 - unlink($this->temp_path);
 125+ unlink( $this->temp_path );
186126 $this->temp_path = null;
187127 }
188128 }
@@ -707,8 +647,6 @@
708648 * that the source files should be deleted if possible
709649 */
710650 function publishBatch( $triplets, $flags = 0 ) {
711 - $conn = $this->connect();
712 - $container = $this->get_container($conn,$this->container);
713651
714652 # paranoia
715653 $status = $this->newGood( array() );
@@ -721,16 +659,15 @@
722660 if ( !$this->validateFilename( $archiveRel ) ) {
723661 throw new MWException( "Validation error in $archiveRel" );
724662 }
725 - if ( !is_file( $srcPath ) ) {
726 - // Make a list of files that don't exist for return to the caller
727 - $status->fatal( 'filenotfound', $srcPath );
728 - }
729663 }
730664
731665 if ( !$status->ok ) {
732666 return $status;
733667 }
734668
 669+ $conn = $this->connect();
 670+ $container = $this->get_container($conn,$this->container);
 671+
735672 foreach ( $triplets as $i => $triplet ) {
736673 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
737674
@@ -748,12 +685,25 @@
749686 $status->value[$i] = 'new';
750687 }
751688
 689+ // Where are we copying this from?
 690+ if (self::isVirtualUrl( $srcPath )) {
 691+ $src = $this->getContainerRel( $srcPath );
 692+ list ($srcContainer, $srcRel) = $src;
 693+ $srcc = $this->get_container($conn, $srcContainer);
 694+
 695+ $this->swiftcopy($srcc, $srcRel, $container, $dstRel);
 696+ if ( $flags & self::DELETE_SOURCE ) {
 697+ $this->swift_delete( $srcc, $srcRel );
 698+ }
 699+ } else {
 700+ $this->write_swift_object( $srcPath, $container, $dstRel);
 701+ // php-cloudfiles throws exceptions, so failure never gets here.
 702+ if ( $flags & self::DELETE_SOURCE ) {
 703+ unlink ( $srcPath );
 704+ }
 705+ }
 706+
752707 $good = true;
753 - $this->write_swift_object( $srcPath, $container, $dstRel);
754 - // php-cloudfiles throws exceptions, so failure never gets here.
755 - if ( $flags & self::DELETE_SOURCE ) {
756 - unlink ( $srcPath );
757 - }
758708
759709 if ( $good ) {
760710 $status->successCount++;
@@ -863,7 +813,31 @@
864814 return false;
865815 }
866816 }
 817+
867818 /**
 819+ * Get a local path corresponding to a virtual URL
 820+ */
 821+ function getContainerRel( $url ) {
 822+ if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
 823+ throw new MWException( __METHOD__.": unknown protocol" );
 824+ }
 825+
 826+ $bits = explode( '/', substr( $url, 9 ), 3 );
 827+ if ( count( $bits ) != 3 ) {
 828+ throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
 829+ }
 830+ list( $repo, $zone, $rel ) = $bits;
 831+ if ( $repo !== $this->name ) {
 832+ throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
 833+ }
 834+ $container = $this->getZoneContainer( $zone );
 835+ if ( $container === false) {
 836+ throw new MWException( __METHOD__.": invalid zone: $zone" );
 837+ }
 838+ return array($container, rawurldecode( $rel ));
 839+ }
 840+
 841+ /**
868842 * Remove a temporary file or mark it for garbage collection
869843 * @param $virtualUrl String: the virtual URL returned by storeTemp
870844 * @return Boolean: true on success, false on failure
@@ -882,6 +856,62 @@
883857 }
884858
885859 /**
 860+ * Called from elsewhere to turn a virtual URL into a path.
 861+ * Make sure you delete this file after you've used it!!
 862+ */
 863+ function resolveVirtualUrl( $url ) {
 864+ $path = $this->getContainerRel( $url );
 865+ list($c, $r) = $path;
 866+ return $this->getLocalCopy($c, $r);
 867+ }
 868+
 869+
 870+ /**
 871+ * Given a container and relative path, return an absolute path pointing at a
 872+ * copy of the file MUST delete the produced file, or else store it in
 873+ * SwiftFile->temp_path so it will be deleted when the object goes out of
 874+ * scope.
 875+ */
 876+ function getLocalCopy($container, $rel) {
 877+
 878+ // get a temporary place to put the original.
 879+ $temp_path = tempnam( wfTempDir(), 'swift_in_' );
 880+
 881+ /* Fetch the image out of Swift */
 882+ $conn = $this->connect();
 883+ $cont = $this->get_container($conn,$container);
 884+
 885+ try {
 886+ $obj = $cont->get_object($rel);
 887+ } catch (NoSuchObjectException $e) {
 888+ throw new MWException( "Unable to open original file at $container/$rel");
 889+ }
 890+
 891+ wfDebug( __METHOD__ . " writing to " . $temp_path . "\n");
 892+ try {
 893+ $obj->save_to_filename( $temp_path);
 894+ } catch (IOException $e) {
 895+ throw new MWException( __METHOD__ . ": error opening '$e'" );
 896+ } catch (InvalidResponseException $e) {
 897+ throw new MWException( __METHOD__ . "unexpected response '$e'" );
 898+ }
 899+
 900+ return $temp_path;
 901+ }
 902+
 903+
 904+ /**
 905+ * Get properties of a file with a given virtual URL
 906+ * The virtual URL must refer to this repo
 907+ */
 908+ function getFileProps( $virtualUrl ) {
 909+ $path = $this->resolveVirtualUrl( $virtualUrl );
 910+ $ret = File::getPropsFromPath( $path );
 911+ unlink( $path );
 912+ }
 913+
 914+
 915+ /**
886916 * Get an UploadStash associated with this repo.
887917 *
888918 * @return UploadStash
@@ -892,9 +922,31 @@
893923 }
894924
895925 class SwiftStash extends UploadStash {
 926+ /**
 927+ * Wrapper function for subclassing.
 928+ */
 929+ protected function newFile( $path, $key, $data ) {
 930+ wfDebug( __METHOD__ . ": deleting $key\n" );
 931+ return new SwiftStashFile( $this, $this->repo, $path, $key, $data );
 932+ }
 933+
896934 }
897935
898936 class SwiftStashFile extends UploadStashFile {
 937+ //public function __construct( $stash, $repo, $path, $key, $data ) {
 938+ // // We don't call parent:: because UploadStashFile expects to be able to call $this->resolveURL() and get a pathname.
 939+ // $this->sessionStash = $stash;
 940+ // $this->sessionKey = $key;
 941+ // $this->sessionData = $data;
 942+ // wfDebug( __METHOD__ . ": ($stash, $repo, $path, $key, $data)\n" );
 943+
 944+ // UnregisteredLocalFile::__construct( false, $repo, $path, false );
 945+ // $this->name = basename( $this->path );
 946+
 947+ //}
 948+
 949+ //function getPath() {
 950+ //}
899951 }
900952
901953 /**

Status & tagging log