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 newFileFromRow( $row ) {
19 if ( isset( $row->img_name
) ) {
20 return LocalFile
::newFromRow( $row, $this );
21 } elseif ( isset( $row->oi_name
) ) {
22 return OldLocalFile
::newFromRow( $row, $this );
24 throw new MWException( __METHOD__
.': invalid row' );
28 function newFromArchiveName( $title, $archiveName ) {
29 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
33 * Delete files in the deleted directory if they are not referenced in the
34 * filearchive table. This needs to be done in the repo because it needs to
35 * interleave database locks with file operations, which is potentially a
37 * @return FileRepoStatus
39 function cleanupDeletedBatch( $storageKeys ) {
40 $root = $this->getZonePath( 'deleted' );
41 $dbw = $this->getMasterDB();
42 $status = $this->newGood();
43 $storageKeys = array_unique($storageKeys);
44 foreach ( $storageKeys as $key ) {
45 $hashPath = $this->getDeletedHashPath( $key );
46 $path = "$root/$hashPath$key";
48 $inuse = $dbw->selectField( 'filearchive', '1',
49 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
50 __METHOD__
, array( 'FOR UPDATE' ) );
52 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
53 $ext = substr( $key, strcspn($key,'.') +
1 );
54 $ext = File
::normalizeExtension($ext);
55 $inuse = $dbw->selectField( 'oldimage', '1',
56 array( 'oi_sha1' => $sha1,
57 "oi_archive_name LIKE '%.{$ext}'",
58 'oi_deleted & '.File
::DELETED_FILE
=> File
::DELETED_FILE
),
59 __METHOD__
, array( 'FOR UPDATE' ) );
62 wfDebug( __METHOD__
. ": deleting $key\n" );
63 if ( !@unlink
( $path ) ) {
64 $status->error( 'undelete-cleanup-error', $path );
68 wfDebug( __METHOD__
. ": $key still in use\n" );
69 $status->successCount++
;
77 * Function link Title::getArticleID().
78 * We can't say Title object, what database it should use, so we duplicate that function here.
80 protected function getArticleID( $title ) {
81 if( !$title instanceof Title
) {
84 $dbr = $this->getSlaveDB();
85 $id = $dbr->selectField(
89 'page_namespace' => $title->getNamespace(),
90 'page_title' => $title->getDbKey(),
92 __METHOD__
//Function name
97 function checkRedirect( $title ) {
100 if( is_string( $title ) ) {
101 $title = Title
::newFromTitle( $title );
103 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
104 $title = Title
::makeTitle( NS_IMAGE
, $title->getText() );
107 $memcKey = wfMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
108 $cachedValue = $wgMemc->get( $memcKey );
110 return Title
::newFromDbKey( $cachedValue );
111 } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
115 $id = $this->getArticleID( $title );
117 $wgMemc->set( $memcKey, " ", 9000 );
120 $dbr = $this->getSlaveDB();
121 $row = $dbr->selectRow(
123 array( 'rd_title', 'rd_namespace' ),
124 array( 'rd_from' => $id ),
128 if( $row ) $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
129 $wgMemc->set( $memcKey, ($row ?
$targetTitle->getPrefixedDBkey() : " "), 9000 );
136 function invalidateImageRedirect( $title ) {
138 $memcKey = wfMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
139 $wgMemc->delete( $memcKey );