25f9092e104d07fbd94aacb8658f4623ead96171
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 getSlaveDB() {
14 return wfGetDB( DB_SLAVE
);
17 function getMasterDB() {
18 return wfGetDB( DB_MASTER
);
21 function getMemcKey( $key ) {
22 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
25 function newFileFromRow( $row ) {
26 if ( isset( $row->img_name
) ) {
27 return call_user_func( $this->fileFromRowFactory
, $row, $this );
28 } elseif ( isset( $row->oi_name
) ) {
29 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
31 throw new MWException( __METHOD__
.': invalid row' );
35 function newFromArchiveName( $title, $archiveName ) {
36 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
40 * Delete files in the deleted directory if they are not referenced in the
41 * filearchive table. This needs to be done in the repo because it needs to
42 * interleave database locks with file operations, which is potentially a
44 * @return FileRepoStatus
46 function cleanupDeletedBatch( $storageKeys ) {
47 $root = $this->getZonePath( 'deleted' );
48 $dbw = $this->getMasterDB();
49 $status = $this->newGood();
50 $storageKeys = array_unique($storageKeys);
51 foreach ( $storageKeys as $key ) {
52 $hashPath = $this->getDeletedHashPath( $key );
53 $path = "$root/$hashPath$key";
55 $inuse = $dbw->selectField( 'filearchive', '1',
56 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
57 __METHOD__
, array( 'FOR UPDATE' ) );
59 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
60 $ext = substr( $key, strcspn($key,'.') +
1 );
61 $ext = File
::normalizeExtension($ext);
62 $inuse = $dbw->selectField( 'oldimage', '1',
63 array( 'oi_sha1' => $sha1,
64 "oi_archive_name LIKE '%.{$ext}'",
65 'oi_deleted & '.File
::DELETED_FILE
=> File
::DELETED_FILE
),
66 __METHOD__
, array( 'FOR UPDATE' ) );
69 wfDebug( __METHOD__
. ": deleting $key\n" );
70 if ( !@unlink
( $path ) ) {
71 $status->error( 'undelete-cleanup-error', $path );
75 wfDebug( __METHOD__
. ": $key still in use\n" );
76 $status->successCount++
;
84 * Function link Title::getArticleID().
85 * We can't say Title object, what database it should use, so we duplicate that function here.
87 protected function getArticleID( $title ) {
88 if( !$title instanceof Title
) {
91 $dbr = $this->getSlaveDB();
92 $id = $dbr->selectField(
96 'page_namespace' => $title->getNamespace(),
97 'page_title' => $title->getDBKey(),
99 __METHOD__
//Function name
106 function findBySha1( $hash ) {
107 $dbr = $this->getSlaveDB();
110 LocalFile
::selectFields(),
111 array( 'img_sha1' => $hash )
115 while ( $row = $res->fetchObject() )
116 $result[] = $this->newFileFromRow( $row );
122 * Find many files using one query
124 function findFiles( $titles ) {
125 // FIXME: Only accepts a $titles array where the keys are the sanitized
128 if ( count( $titles ) == 0 ) return array();
130 $dbr = $this->getSlaveDB();
133 LocalFile
::selectFields(),
134 array( 'img_name' => array_keys( $titles ) )
138 while ( $row = $res->fetchObject() ) {
139 $result[$row->img_name
] = $this->newFileFromRow( $row );