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.
6 class LocalRepo
extends FSRepo
{
7 var $fileFactory = array( 'LocalFile', 'newFromTitle' );
8 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
10 function getSlaveDB() {
11 return wfGetDB( DB_SLAVE
);
14 function getMasterDB() {
15 return wfGetDB( DB_MASTER
);
18 function getMemcKey( $key ) {
19 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
22 function newFileFromRow( $row ) {
23 if ( isset( $row->img_name
) ) {
24 return LocalFile
::newFromRow( $row, $this );
25 } elseif ( isset( $row->oi_name
) ) {
26 return OldLocalFile
::newFromRow( $row, $this );
28 throw new MWException( __METHOD__
.': invalid row' );
32 function newFromArchiveName( $title, $archiveName ) {
33 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
37 * Delete files in the deleted directory if they are not referenced in the
38 * filearchive table. This needs to be done in the repo because it needs to
39 * interleave database locks with file operations, which is potentially a
41 * @return FileRepoStatus
43 function cleanupDeletedBatch( $storageKeys ) {
44 $root = $this->getZonePath( 'deleted' );
45 $dbw = $this->getMasterDB();
46 $status = $this->newGood();
47 $storageKeys = array_unique($storageKeys);
48 foreach ( $storageKeys as $key ) {
49 $hashPath = $this->getDeletedHashPath( $key );
50 $path = "$root/$hashPath$key";
52 $inuse = $dbw->selectField( 'filearchive', '1',
53 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
54 __METHOD__
, array( 'FOR UPDATE' ) );
56 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
57 $ext = substr( $key, strcspn($key,'.') +
1 );
58 $ext = File
::normalizeExtension($ext);
59 $inuse = $dbw->selectField( 'oldimage', '1',
60 array( 'oi_sha1' => $sha1,
61 "oi_archive_name LIKE '%.{$ext}'",
62 'oi_deleted & '.File
::DELETED_FILE
=> File
::DELETED_FILE
),
63 __METHOD__
, array( 'FOR UPDATE' ) );
66 wfDebug( __METHOD__
. ": deleting $key\n" );
67 if ( !@unlink
( $path ) ) {
68 $status->error( 'undelete-cleanup-error', $path );
72 wfDebug( __METHOD__
. ": $key still in use\n" );
73 $status->successCount++
;
81 * Function link Title::getArticleID().
82 * We can't say Title object, what database it should use, so we duplicate that function here.
84 protected function getArticleID( $title ) {
85 if( !$title instanceof Title
) {
88 $dbr = $this->getSlaveDB();
89 $id = $dbr->selectField(
93 'page_namespace' => $title->getNamespace(),
94 'page_title' => $title->getDbKey(),
96 __METHOD__
//Function name
101 function checkRedirect( $title ) {
104 if( is_string( $title ) ) {
105 $title = Title
::newFromTitle( $title );
107 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
108 $title = Title
::makeTitle( NS_IMAGE
, $title->getText() );
111 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
112 $cachedValue = $wgMemc->get( $memcKey );
114 return Title
::newFromDbKey( $cachedValue );
115 } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
119 $id = $this->getArticleID( $title );
121 $wgMemc->set( $memcKey, " ", 9000 );
124 $dbr = $this->getSlaveDB();
125 $row = $dbr->selectRow(
127 array( 'rd_title', 'rd_namespace' ),
128 array( 'rd_from' => $id ),
132 if( $row ) $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
133 $wgMemc->set( $memcKey, ($row ?
$targetTitle->getPrefixedDBkey() : " "), 9000 );
140 function invalidateImageRedirect( $title ) {
142 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
143 $wgMemc->delete( $memcKey );