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
104 function checkRedirect( $title ) {
107 if( is_string( $title ) ) {
108 $title = Title
::newFromTitle( $title );
110 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
111 $title = Title
::makeTitle( NS_IMAGE
, $title->getText() );
114 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
115 $cachedValue = $wgMemc->get( $memcKey );
117 return Title
::newFromDbKey( $cachedValue );
118 } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
122 $id = $this->getArticleID( $title );
124 $wgMemc->set( $memcKey, " ", 9000 );
127 $dbr = $this->getSlaveDB();
128 $row = $dbr->selectRow(
130 array( 'rd_title', 'rd_namespace' ),
131 array( 'rd_from' => $id ),
135 if( $row ) $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
136 $wgMemc->set( $memcKey, ($row ?
$targetTitle->getPrefixedDBkey() : " "), 9000 );
143 function invalidateImageRedirect( $title ) {
145 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
146 $wgMemc->delete( $memcKey );
149 function findBySha1( $hash ) {
150 $dbr = $this->getSlaveDB();
153 LocalFile
::selectFields(),
154 array( 'img_sha1' => $hash )
158 while ( $row = $res->fetchObject() )
159 $result[] = $this->newFileFromRow( $row );
165 * Find many files using one query
167 function findFiles( $titles, $flags ) {
168 // FIXME: Comply with $flags
169 // FIXME: Only accepts a $titles array where the keys are the sanitized
172 if ( count( $titles ) == 0 ) return array();
174 $dbr = $this->getSlaveDB();
177 LocalFile
::selectFields(),
178 array( 'img_name' => array_keys( $titles ) )
182 while ( $row = $res->fetchObject() ) {
183 $result[$row->img_name
] = $this->newFileFromRow( $row );