b096ce0cf0573279849c6f4ebe668133c86e3c59
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 __construct( $info ) {
14 parent
::__construct( $info );
16 # Initialize simpleCleanPairs, to make errors less misleading
18 $this->simpleCleanPairs
= array(
19 $this->directory
=> '$wgUploadDirectory',
20 wfTempDir() => '{wfTempDir()}',
23 if ( $this->deletedDir
) {
24 $this->simpleCleanPairs
[$this->deletedDir
] = '{$wgFileStore[\'deleted\'][\'directory\']}';
28 function getSlaveDB() {
29 return wfGetDB( DB_SLAVE
);
32 function getMasterDB() {
33 return wfGetDB( DB_MASTER
);
36 function getMemcKey( $key ) {
37 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
40 function newFileFromRow( $row ) {
41 if ( isset( $row->img_name
) ) {
42 return call_user_func( $this->fileFromRowFactory
, $row, $this );
43 } elseif ( isset( $row->oi_name
) ) {
44 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
46 throw new MWException( __METHOD__
.': invalid row' );
50 function newFromArchiveName( $title, $archiveName ) {
51 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
55 * Delete files in the deleted directory if they are not referenced in the
56 * filearchive table. This needs to be done in the repo because it needs to
57 * interleave database locks with file operations, which is potentially a
59 * @return FileRepoStatus
61 function cleanupDeletedBatch( $storageKeys ) {
62 $root = $this->getZonePath( 'deleted' );
63 $dbw = $this->getMasterDB();
64 $status = $this->newGood();
65 $storageKeys = array_unique($storageKeys);
66 foreach ( $storageKeys as $key ) {
67 $hashPath = $this->getDeletedHashPath( $key );
68 $path = "$root/$hashPath$key";
70 $inuse = $dbw->selectField( 'filearchive', '1',
71 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
72 __METHOD__
, array( 'FOR UPDATE' ) );
74 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
75 $ext = substr( $key, strcspn($key,'.') +
1 );
76 $ext = File
::normalizeExtension($ext);
77 $inuse = $dbw->selectField( 'oldimage', '1',
78 array( 'oi_sha1' => $sha1,
79 "oi_archive_name LIKE '%.{$ext}'",
80 'oi_deleted & '.File
::DELETED_FILE
=> File
::DELETED_FILE
),
81 __METHOD__
, array( 'FOR UPDATE' ) );
84 wfDebug( __METHOD__
. ": deleting $key\n" );
85 if ( !@unlink
( $path ) ) {
86 $status->error( 'undelete-cleanup-error', $path );
90 wfDebug( __METHOD__
. ": $key still in use\n" );
91 $status->successCount++
;
99 * Function link Title::getArticleID().
100 * We can't say Title object, what database it should use, so we duplicate that function here.
102 protected function getArticleID( $title ) {
103 if( !$title instanceof Title
) {
106 $dbr = $this->getSlaveDB();
107 $id = $dbr->selectField(
111 'page_namespace' => $title->getNamespace(),
112 'page_title' => $title->getDbKey(),
114 __METHOD__
//Function name
119 function checkRedirect( $title ) {
122 if( is_string( $title ) ) {
123 $title = Title
::newFromTitle( $title );
125 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
126 $title = Title
::makeTitle( NS_IMAGE
, $title->getText() );
129 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
130 $cachedValue = $wgMemc->get( $memcKey );
132 return Title
::newFromDbKey( $cachedValue );
133 } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
137 $id = $this->getArticleID( $title );
139 $wgMemc->set( $memcKey, " ", 9000 );
142 $dbr = $this->getSlaveDB();
143 $row = $dbr->selectRow(
145 array( 'rd_title', 'rd_namespace' ),
146 array( 'rd_from' => $id ),
150 if( $row ) $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
151 $wgMemc->set( $memcKey, ($row ?
$targetTitle->getPrefixedDBkey() : " "), 9000 );
158 function invalidateImageRedirect( $title ) {
160 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
161 $wgMemc->delete( $memcKey );
164 function findBySha1( $hash ) {
165 $dbr = $this->getSlaveDB();
168 LocalFile
::selectFields(),
169 array( 'img_sha1' => $hash )
173 while ( $row = $res->fetchObject() )
174 $result[] = $this->newFileFromRow( $row );
180 * Find many files using one query
182 function findFiles( $titles, $flags ) {
183 // FIXME: Comply with $flags
184 // FIXME: Only accepts a $titles array where the keys are the sanitized
187 if ( count( $titles ) == 0 ) return array();
189 $dbr = $this->getSlaveDB();
192 LocalFile
::selectFields(),
193 array( 'img_name' => array_keys( $titles ) )
197 while ( $row = $res->fetchObject() ) {
198 $result[$row->img_name
] = $this->newFileFromRow( $row );