3 * A repository that stores files in the local filesystem and registers them
4 * in the wiki's own database. This is the most commonly used repository class.
7 class LocalRepo
extends FSRepo
{
8 var $fileFactory = array( 'LocalFile', 'newFromTitle' );
9 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
10 var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
11 var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
13 function getMemcKey( $key ) {
14 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
17 function newFileFromRow( $row ) {
18 if ( isset( $row->img_name
) ) {
19 return call_user_func( $this->fileFromRowFactory
, $row, $this );
20 } elseif ( isset( $row->oi_name
) ) {
21 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
23 throw new MWException( __METHOD__
.': invalid row' );
27 function newFromArchiveName( $title, $archiveName ) {
28 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
32 * Delete files in the deleted directory if they are not referenced in the
33 * filearchive table. This needs to be done in the repo because it needs to
34 * interleave database locks with file operations, which is potentially a
36 * @return FileRepoStatus
38 function cleanupDeletedBatch( $storageKeys ) {
39 $root = $this->getZonePath( 'deleted' );
40 $dbw = $this->getMasterDB();
41 $status = $this->newGood();
42 $storageKeys = array_unique($storageKeys);
43 foreach ( $storageKeys as $key ) {
44 $hashPath = $this->getDeletedHashPath( $key );
45 $path = "$root/$hashPath$key";
47 $inuse = $dbw->selectField( 'filearchive', '1',
48 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
49 __METHOD__
, array( 'FOR UPDATE' ) );
51 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
52 $ext = substr( $key, strcspn($key,'.') +
1 );
53 $ext = File
::normalizeExtension($ext);
54 $inuse = $dbw->selectField( 'oldimage', '1',
55 array( 'oi_sha1' => $sha1,
56 "oi_archive_name LIKE '%.{$ext}'",
57 'oi_deleted & '.File
::DELETED_FILE
=> File
::DELETED_FILE
),
58 __METHOD__
, array( 'FOR UPDATE' ) );
61 wfDebug( __METHOD__
. ": deleting $key\n" );
62 if ( !@unlink
( $path ) ) {
63 $status->error( 'undelete-cleanup-error', $path );
67 wfDebug( __METHOD__
. ": $key still in use\n" );
68 $status->successCount++
;
76 * Function link Title::getArticleID().
77 * We can't say Title object, what database it should use, so we duplicate that function here.
79 protected function getArticleID( $title ) {
80 if( !$title instanceof Title
) {
83 $dbr = $this->getSlaveDB();
84 $id = $dbr->selectField(
88 'page_namespace' => $title->getNamespace(),
89 'page_title' => $title->getDBKey(),
91 __METHOD__
//Function name
98 function findBySha1( $hash ) {
99 $dbr = $this->getSlaveDB();
102 LocalFile
::selectFields(),
103 array( 'img_sha1' => $hash )
107 while ( $row = $res->fetchObject() )
108 $result[] = $this->newFileFromRow( $row );
114 * Find many files using one query
116 function findFiles( $titles ) {
117 // FIXME: Only accepts a $titles array where the keys are the sanitized
120 if ( count( $titles ) == 0 ) return array();
122 $dbr = $this->getSlaveDB();
125 LocalFile
::selectFields(),
126 array( 'img_name' => array_keys( $titles ) )
130 while ( $row = $res->fetchObject() ) {
131 $result[$row->img_name
] = $this->newFileFromRow( $row );