X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Ffilerepo%2FICRepo.php;h=805dc02ccfd3f05cf51388e74f39190e4c0d8f32;hb=3611f529a88722cb81b819da87af4582de2cd479;hp=083876a6e6df45e147c2d64eee401b93964478b0;hpb=6fa0cb711cd86f414770bb1da081e1220fe64f79;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/filerepo/ICRepo.php b/includes/filerepo/ICRepo.php index 083876a6e6..805dc02ccf 100644 --- a/includes/filerepo/ICRepo.php +++ b/includes/filerepo/ICRepo.php @@ -1,24 +1,24 @@ directory = $info['directory']; $this->url = $info['url']; $this->hashLevels = $info['hashLevels']; if(isset($info['cache'])){ $this->cache = getcwd().'/images/'.$info['cache']; - } - } + } + } } /** @@ -26,30 +26,30 @@ class ICRepo extends LocalRepo { */ class ICFile extends LocalFile{ static function newFromTitle($title,$repo){ - return new self($title, $repo); + return new self($title, $repo); } - + /** * Returns true if the file comes from the local file repository. * * @return bool */ - function isLocal() { - return true; + function isLocal() { + return true; } - + function load(){ if (!$this->dataLoaded ) { if ( !$this->loadFromCache() ) { if(!$this->loadFromDB()){ $this->loadFromIC(); - } - $this->saveToCache(); + } + $this->saveToCache(); } $this->dataLoaded = true; - } + } } - + /** * Load file metadata from the DB */ @@ -62,15 +62,15 @@ class ICFile extends LocalFile{ $dbr = $this->repo->getSlaveDB(); $row = $dbr->selectRow( 'ic_image', $this->getCacheFields( 'img_' ), - array( 'img_name' => $this->getName() ), __METHOD__ ); + array( 'img_name' => $this->getName() ), __METHOD__ ); if ( $row ) { if (trim($row->img_media_type)==NULL) { $this->upgradeRow(); $this->upgraded = true; - } + } $this->loadFromRow( $row ); //This means that these files are local so the repository locations are local - $this->setUrlPathLocal(); + $this->setUrlPathLocal(); $this->fileExists = true; //var_dump($this); exit; } else { @@ -78,10 +78,10 @@ class ICFile extends LocalFile{ } wfProfileOut( __METHOD__ ); - + return $this->fileExists; } - + /** * Fix assorted version-related problems with the image row by reloading it from the file */ @@ -110,106 +110,102 @@ class ICFile extends LocalFile{ $this->saveToCache(); wfProfileOut( __METHOD__ ); } - + function exists(){ $this->load(); return $this->fileExists; - } - + /** * Fetch the file from the repository. Check local ic_images table first. If not available, check remote server - */ - function loadFromIC(){ - # Unconditionally set loaded=true, we don't want the accessors constantly rechecking + */ + function loadFromIC(){ + # Unconditionally set loaded=true, we don't want the accessors constantly rechecking $this->dataLoaded = true; - $icUrl = $this->repo->directory.'&media='.$this->title->mDbkeyform; - if($h = @fopen($icUrl, 'rb')){ - $contents = fread($h, 3000); - $image = $this->api_xml_to_array($contents); - if($image['fileExists']){ - foreach($image as $property=>$value){ - if($property=="url"){$value=$this->repo->url.$value; } - $this->$property = $value; - } - if($this->curl_file_get_contents($this->repo->url.$image['url'], $this->repo->cache.'/'.$image['name'])){ - //Record the image - $this->recordDownload("Downloaded with InstantCommons"); - - //Then cache it - }else{//set fileExists back to false - $this->fileExists = false; - } - } + $icUrl = $this->repo->directory.'&media='.$this->title->mDbkeyform; + if($h = @fopen($icUrl, 'rb')){ + $contents = fread($h, 3000); + $image = $this->api_xml_to_array($contents); + if($image['fileExists']){ + foreach($image as $property=>$value){ + if($property=="url"){$value=$this->repo->url.$value; } + $this->$property = $value; + } + if($this->curl_file_get_contents($this->repo->url.$image['url'], $this->repo->cache.'/'.$image['name'])){ + //Record the image + $this->recordDownload("Downloaded with InstantCommons"); + + //Then cache it + }else{//set fileExists back to false + $this->fileExists = false; + } + } } - } - - - function setUrlPathLocal(){ - global $wgScriptPath; - $path = $wgScriptPath.'/'.substr($this->repo->cache, strlen($wgScriptPath)); - $this->repo->url = $path;//.'/'.rawurlencode($this->title->mDbkeyform); + } + + function setUrlPathLocal(){ + global $wgScriptPath; + $path = $wgScriptPath.'/'.substr($this->repo->cache, strlen($wgScriptPath)); + $this->repo->url = $path;//.'/'.rawurlencode($this->title->mDbkeyform); $this->repo->directory = $this->repo->cache;//.'/'.rawurlencode($this->title->mDbkeyform); - - } - - function getThumbPath( $suffix=false ){ - $path = $this->repo->cache; - if ( $suffix !== false ) { + + } + + function getThumbPath( $suffix=false ){ + $path = $this->repo->cache; + if ( $suffix !== false ) { $path .= '/thumb/' . rawurlencode( $suffix ); } return $path; - } - function getThumbUrl( $suffix=false ){ - global $wgScriptPath; + } + function getThumbUrl( $suffix=false ){ + global $wgScriptPath; $path = $wgScriptPath.'/'.substr($this->repo->cache, strlen($wgScriptPath)); - if ( $suffix !== false ) { + if ( $suffix !== false ) { $path .= '/thumb/' . rawurlencode( $suffix ); } return $path; - } - - /** - * Convert the InstantCommons Server API XML Response to an associative array - */ - function api_xml_to_array($xml){ - preg_match("//",$xml,$match); - preg_match_all("/(.*?=\".*?\")/",$match[1], $matches); - foreach($matches[1] as $match){ - list($key,$value) = split("=",$match); - $image[trim($key,'<" ')]=trim($value,' "'); - } - return $image; - } - + } + + /** + * Convert the InstantCommons Server API XML Response to an associative array + */ + function api_xml_to_array($xml){ + preg_match("//",$xml,$match); + preg_match_all("/(.*?=\".*?\")/",$match[1], $matches); + foreach($matches[1] as $match){ + list($key,$value) = split("=",$match); + $image[trim($key,'<" ')]=trim($value,' "'); + } + return $image; + } + /** - * Use cURL to read the content of a URL into a string - * ref: http://groups-beta.google.com/group/comp.lang.php/browse_thread/thread/8efbbaced3c45e3c/d63c7891cf8e380b?lnk=raot - * @param string $url - the URL to fetch - * @param resource $fp - filename to write file contents to - * @param boolean $bg - call cURL in the background (don't hang page until complete) - * @param int $timeout - cURL connect timeout - */ - function curl_file_get_contents($url, $fp, $bg=TRUE, $timeout = 1) { - { - # Call curl in the background to download the file - $cmd = 'curl '.wfEscapeShellArg($url).' -o '.$fp.' &'; - wfDebug('Curl download initiated='.$cmd ); - $success = false; - $file_contents = array(); - $file_contents['err'] = wfShellExec($cmd, $file_contents['return']); - if($file_contents['err']==0){//Success - $success = true; - } - } - return $success; - } - + * Use cURL to read the content of a URL into a string + * ref: http://groups-beta.google.com/group/comp.lang.php/browse_thread/thread/8efbbaced3c45e3c/d63c7891cf8e380b?lnk=raot + * @param string $url - the URL to fetch + * @param resource $fp - filename to write file contents to + * @param boolean $bg - call cURL in the background (don't hang page until complete) + * @param int $timeout - cURL connect timeout + */ + function curl_file_get_contents($url, $fp, $bg=TRUE, $timeout = 1) { + # Call curl in the background to download the file + $cmd = 'curl '.wfEscapeShellArg($url).' -o '.$fp.' &'; + wfDebug('Curl download initiated='.$cmd ); + $success = false; + $file_contents = array(); + $file_contents['err'] = wfShellExec($cmd, $file_contents['return']); + if($file_contents['err']==0){//Success + $success = true; + } + return $success; + } + function getMasterDB() { if ( !isset( $this->dbConn ) ) { $class = 'Database' . ucfirst( $this->dbType ); - $this->dbConn = new $class( $this->dbServer, $this->dbUser, - $this->dbPassword, $this->dbName, false, $this->dbFlags, + $this->dbConn = new $class( $this->dbServer, $this->dbUser, + $this->dbPassword, $this->dbName, false, $this->dbFlags, $this->tablePrefix ); } return $this->dbConn; @@ -219,10 +215,10 @@ class ICFile extends LocalFile{ * Record a file upload in the upload log and the image table */ private function recordDownload($comment='', $timestamp = false ){ - global $wgUser; + global $wgUser; $dbw = $this->repo->getMasterDB(); - + if ( $timestamp === false ) { $timestamp = $dbw->timestamp(); } @@ -252,7 +248,7 @@ class ICFile extends LocalFile{ ); if( $dbw->affectedRows() == 0 ) { - # Collision, this is an update of a file + # Collision, this is an update of a file # Update the current image row $dbw->update( 'ic_image', array( /* SET */ @@ -290,13 +286,14 @@ class ICFile extends LocalFile{ # Create a null revision $nullRevision = Revision::newNullRevision( $dbw, $descTitle->getArticleId(), $log->getRcComment(), false ); $nullRevision->insertOn( $dbw ); + $article->updateRevisionOn( $dbw, $nullRevision ); # Invalidate the cache for the description page $descTitle->invalidateCache(); $descTitle->purgeSquid(); } - + # Commit the transaction now, in case something goes wrong later # The most important thing is that files don't get lost, especially archives $dbw->immediateCommit(); @@ -307,6 +304,4 @@ class ICFile extends LocalFile{ return true; } - } -