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 newFileFromRow( $row ) {
14 if ( isset( $row->img_name
) ) {
15 return call_user_func( $this->fileFromRowFactory
, $row, $this );
16 } elseif ( isset( $row->oi_name
) ) {
17 return call_user_func( $this->oldFileFromRowFactory
, $row, $this );
19 throw new MWException( __METHOD__
.': invalid row' );
23 function newFromArchiveName( $title, $archiveName ) {
24 return OldLocalFile
::newFromArchiveName( $title, $this, $archiveName );
28 * Delete files in the deleted directory if they are not referenced in the
29 * filearchive table. This needs to be done in the repo because it needs to
30 * interleave database locks with file operations, which is potentially a
32 * @return FileRepoStatus
34 function cleanupDeletedBatch( $storageKeys ) {
35 $root = $this->getZonePath( 'deleted' );
36 $dbw = $this->getMasterDB();
37 $status = $this->newGood();
38 $storageKeys = array_unique($storageKeys);
39 foreach ( $storageKeys as $key ) {
40 $hashPath = $this->getDeletedHashPath( $key );
41 $path = "$root/$hashPath$key";
43 $inuse = $dbw->selectField( 'filearchive', '1',
44 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
45 __METHOD__
, array( 'FOR UPDATE' ) );
47 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
48 $ext = substr( $key, strcspn($key,'.') +
1 );
49 $ext = File
::normalizeExtension($ext);
50 $inuse = $dbw->selectField( 'oldimage', '1',
51 array( 'oi_sha1' => $sha1,
52 "oi_archive_name LIKE '%.{$ext}'",
53 $dbw->bitAnd('oi_deleted', File
::DELETED_FILE
) => File
::DELETED_FILE
),
54 __METHOD__
, array( 'FOR UPDATE' ) );
57 wfDebug( __METHOD__
. ": deleting $key\n" );
58 if ( !@unlink
( $path ) ) {
59 $status->error( 'undelete-cleanup-error', $path );
63 wfDebug( __METHOD__
. ": $key still in use\n" );
64 $status->successCount++
;
72 * Checks if there is a redirect named as $title
74 * @param Title $title Title of image
76 function checkRedirect( $title ) {
79 if( is_string( $title ) ) {
80 $title = Title
::newFromTitle( $title );
82 if( $title instanceof Title
&& $title->getNamespace() == NS_MEDIA
) {
83 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
86 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
87 $cachedValue = $wgMemc->get( $memcKey );
89 return Title
::newFromDbKey( $cachedValue );
90 } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
94 $id = $this->getArticleID( $title );
96 $wgMemc->set( $memcKey, " ", 9000 );
99 $dbr = $this->getSlaveDB();
100 $row = $dbr->selectRow(
102 array( 'rd_title', 'rd_namespace' ),
103 array( 'rd_from' => $id ),
107 if( $row ) $targetTitle = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
108 $wgMemc->set( $memcKey, ($row ?
$targetTitle->getPrefixedDBkey() : " "), 9000 );
117 * Function link Title::getArticleID().
118 * We can't say Title object, what database it should use, so we duplicate that function here.
120 protected function getArticleID( $title ) {
121 if( !$title instanceof Title
) {
124 $dbr = $this->getSlaveDB();
125 $id = $dbr->selectField(
129 'page_namespace' => $title->getNamespace(),
130 'page_title' => $title->getDBkey(),
132 __METHOD__
//Function name
139 function findBySha1( $hash ) {
140 $dbr = $this->getSlaveDB();
143 LocalFile
::selectFields(),
144 array( 'img_sha1' => $hash )
148 while ( $row = $res->fetchObject() )
149 $result[] = $this->newFileFromRow( $row );
155 * Find many files using one query
157 function findFiles( $titles ) {
158 // FIXME: Only accepts a $titles array where the keys are the sanitized
161 if ( count( $titles ) == 0 ) return array();
163 $dbr = $this->getSlaveDB();
166 LocalFile
::selectFields(),
167 array( 'img_name' => array_keys( $titles ) )
171 while ( $row = $res->fetchObject() ) {
172 $result[$row->img_name
] = $this->newFileFromRow( $row );