PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/filerepo/LocalRepo.php

https://github.com/daevid/MWFork
PHP | 241 lines | 152 code | 20 blank | 69 comment | 21 complexity | 1435aa450874d7be27895407e0024685 MD5 | raw file
  1. <?php
  2. /**
  3. * Local repository that stores files in the local filesystem and registers them
  4. * in the wiki's own database.
  5. *
  6. * @file
  7. * @ingroup FileRepo
  8. */
  9. /**
  10. * A repository that stores files in the local filesystem and registers them
  11. * in the wiki's own database. This is the most commonly used repository class.
  12. * @ingroup FileRepo
  13. */
  14. class LocalRepo extends FSRepo {
  15. var $fileFactory = array( 'LocalFile', 'newFromTitle' );
  16. var $fileFactoryKey = array( 'LocalFile', 'newFromKey' );
  17. var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
  18. var $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
  19. var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
  20. var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
  21. /**
  22. * @throws MWException
  23. * @param $row
  24. * @return File
  25. */
  26. function newFileFromRow( $row ) {
  27. if ( isset( $row->img_name ) ) {
  28. return call_user_func( $this->fileFromRowFactory, $row, $this );
  29. } elseif ( isset( $row->oi_name ) ) {
  30. return call_user_func( $this->oldFileFromRowFactory, $row, $this );
  31. } else {
  32. throw new MWException( __METHOD__.': invalid row' );
  33. }
  34. }
  35. /**
  36. * @param $title
  37. * @param $archiveName
  38. * @return OldLocalFile
  39. */
  40. function newFromArchiveName( $title, $archiveName ) {
  41. return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
  42. }
  43. /**
  44. * Delete files in the deleted directory if they are not referenced in the
  45. * filearchive table. This needs to be done in the repo because it needs to
  46. * interleave database locks with file operations, which is potentially a
  47. * remote operation.
  48. *
  49. * @param $storageKeys array
  50. *
  51. * @return FileRepoStatus
  52. */
  53. function cleanupDeletedBatch( $storageKeys ) {
  54. $root = $this->getZonePath( 'deleted' );
  55. $dbw = $this->getMasterDB();
  56. $status = $this->newGood();
  57. $storageKeys = array_unique( $storageKeys );
  58. foreach ( $storageKeys as $key ) {
  59. $hashPath = $this->getDeletedHashPath( $key );
  60. $path = "$root/$hashPath$key";
  61. $dbw->begin();
  62. $inuse = $dbw->selectField( 'filearchive', '1',
  63. array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
  64. __METHOD__, array( 'FOR UPDATE' ) );
  65. if( !$inuse ) {
  66. $sha1 = self::getHashFromKey( $key );
  67. $ext = substr( $key, strcspn( $key, '.' ) + 1 );
  68. $ext = File::normalizeExtension($ext);
  69. $inuse = $dbw->selectField( 'oldimage', '1',
  70. array( 'oi_sha1' => $sha1,
  71. 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
  72. $dbw->bitAnd('oi_deleted', File::DELETED_FILE) => File::DELETED_FILE ),
  73. __METHOD__, array( 'FOR UPDATE' ) );
  74. }
  75. if ( !$inuse ) {
  76. wfDebug( __METHOD__ . ": deleting $key\n" );
  77. wfSuppressWarnings();
  78. $unlink = unlink( $path );
  79. wfRestoreWarnings();
  80. if ( !$unlink ) {
  81. $status->error( 'undelete-cleanup-error', $path );
  82. $status->failCount++;
  83. }
  84. } else {
  85. wfDebug( __METHOD__ . ": $key still in use\n" );
  86. $status->successCount++;
  87. }
  88. $dbw->commit();
  89. }
  90. return $status;
  91. }
  92. /**
  93. * Gets the SHA1 hash from a storage key
  94. *
  95. * @param string $key
  96. * @return string
  97. */
  98. public static function getHashFromKey( $key ) {
  99. return strtok( $key, '.' );
  100. }
  101. /**
  102. * Checks if there is a redirect named as $title
  103. *
  104. * @param $title Title of file
  105. */
  106. function checkRedirect( $title ) {
  107. global $wgMemc;
  108. if( is_string( $title ) ) {
  109. $title = Title::newFromText( $title );
  110. }
  111. if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
  112. $title = Title::makeTitle( NS_FILE, $title->getText() );
  113. }
  114. $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
  115. if ( $memcKey === false ) {
  116. $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
  117. $expiry = 300; // no invalidation, 5 minutes
  118. } else {
  119. $expiry = 86400; // has invalidation, 1 day
  120. }
  121. $cachedValue = $wgMemc->get( $memcKey );
  122. if ( $cachedValue === ' ' || $cachedValue === '' ) {
  123. // Does not exist
  124. return false;
  125. } elseif ( strval( $cachedValue ) !== '' ) {
  126. return Title::newFromText( $cachedValue, NS_FILE );
  127. } // else $cachedValue is false or null: cache miss
  128. $id = $this->getArticleID( $title );
  129. if( !$id ) {
  130. $wgMemc->set( $memcKey, " ", $expiry );
  131. return false;
  132. }
  133. $dbr = $this->getSlaveDB();
  134. $row = $dbr->selectRow(
  135. 'redirect',
  136. array( 'rd_title', 'rd_namespace' ),
  137. array( 'rd_from' => $id ),
  138. __METHOD__
  139. );
  140. if( $row && $row->rd_namespace == NS_FILE ) {
  141. $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
  142. $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
  143. return $targetTitle;
  144. } else {
  145. $wgMemc->set( $memcKey, '', $expiry );
  146. return false;
  147. }
  148. }
  149. /**
  150. * Function link Title::getArticleID().
  151. * We can't say Title object, what database it should use, so we duplicate that function here.
  152. * @param $title Title
  153. */
  154. protected function getArticleID( $title ) {
  155. if( !$title instanceof Title ) {
  156. return 0;
  157. }
  158. $dbr = $this->getSlaveDB();
  159. $id = $dbr->selectField(
  160. 'page', // Table
  161. 'page_id', //Field
  162. array( //Conditions
  163. 'page_namespace' => $title->getNamespace(),
  164. 'page_title' => $title->getDBkey(),
  165. ),
  166. __METHOD__ //Function name
  167. );
  168. return $id;
  169. }
  170. /**
  171. * Get an array or iterator of file objects for files that have a given
  172. * SHA-1 content hash.
  173. */
  174. function findBySha1( $hash ) {
  175. $dbr = $this->getSlaveDB();
  176. $res = $dbr->select(
  177. 'image',
  178. LocalFile::selectFields(),
  179. array( 'img_sha1' => $hash )
  180. );
  181. $result = array();
  182. foreach ( $res as $row ) {
  183. $result[] = $this->newFileFromRow( $row );
  184. }
  185. $res->free();
  186. return $result;
  187. }
  188. /**
  189. * Get a connection to the slave DB
  190. */
  191. function getSlaveDB() {
  192. return wfGetDB( DB_SLAVE );
  193. }
  194. /**
  195. * Get a connection to the master DB
  196. */
  197. function getMasterDB() {
  198. return wfGetDB( DB_MASTER );
  199. }
  200. /**
  201. * Get a key on the primary cache for this repository.
  202. * Returns false if the repository's cache is not accessible at this site.
  203. * The parameters are the parts of the key, as for wfMemcKey().
  204. */
  205. function getSharedCacheKey( /*...*/ ) {
  206. $args = func_get_args();
  207. return call_user_func_array( 'wfMemcKey', $args );
  208. }
  209. /**
  210. * Invalidates image redirect cache related to that image
  211. *
  212. * @param $title Title of page
  213. */
  214. function invalidateImageRedirect( $title ) {
  215. global $wgMemc;
  216. $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
  217. if ( $memcKey ) {
  218. $wgMemc->delete( $memcKey );
  219. }
  220. }
  221. }