]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/filerepo/LocalRepo.php
MediaWiki 1.15.0
[autoinstallsdev/mediawiki.git] / includes / filerepo / LocalRepo.php
1 <?php
2 /**
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.
5  * @ingroup FileRepo
6  */
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' );
12
13         function getMemcKey( $key ) {
14                 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
15         }
16
17         function newFileFromRow( $row ) {
18                 if ( isset( $row->img_name ) ) {
19                         return call_user_func( $this->fileFromRowFactory, $row, $this );
20                 } elseif ( isset( $row->oi_name ) ) {
21                         return call_user_func( $this->oldFileFromRowFactory, $row, $this );
22                 } else {
23                         throw new MWException( __METHOD__.': invalid row' );
24                 }
25         }
26
27         function newFromArchiveName( $title, $archiveName ) {
28                 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
29         }
30
31         /**
32          * Delete files in the deleted directory if they are not referenced in the
33          * filearchive table. This needs to be done in the repo because it needs to
34          * interleave database locks with file operations, which is potentially a
35          * remote operation.
36          * @return FileRepoStatus
37          */
38         function cleanupDeletedBatch( $storageKeys ) {
39                 $root = $this->getZonePath( 'deleted' );
40                 $dbw = $this->getMasterDB();
41                 $status = $this->newGood();
42                 $storageKeys = array_unique($storageKeys);
43                 foreach ( $storageKeys as $key ) {
44                         $hashPath = $this->getDeletedHashPath( $key );
45                         $path = "$root/$hashPath$key";
46                         $dbw->begin();
47                         $inuse = $dbw->selectField( 'filearchive', '1',
48                                 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
49                                 __METHOD__, array( 'FOR UPDATE' ) );
50                         if( !$inuse ) {
51                                 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
52                                 $ext = substr( $key, strcspn($key,'.') + 1 );
53                                 $ext = File::normalizeExtension($ext);
54                                 $inuse = $dbw->selectField( 'oldimage', '1',
55                                         array( 'oi_sha1' => $sha1,
56                                                 "oi_archive_name LIKE '%.{$ext}'",
57                                                 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
58                                         __METHOD__, array( 'FOR UPDATE' ) );
59                         }
60                         if ( !$inuse ) {
61                                 wfDebug( __METHOD__ . ": deleting $key\n" );
62                                 if ( !@unlink( $path ) ) {
63                                         $status->error( 'undelete-cleanup-error', $path );
64                                         $status->failCount++;
65                                 }
66                         } else {
67                                 wfDebug( __METHOD__ . ": $key still in use\n" );
68                                 $status->successCount++;
69                         }
70                         $dbw->commit();
71                 }
72                 return $status;
73         }
74
75         /**
76          * Function link Title::getArticleID().
77          * We can't say Title object, what database it should use, so we duplicate that function here.
78          */
79         protected function getArticleID( $title ) {
80                 if( !$title instanceof Title ) {
81                         return 0;
82                 }
83                 $dbr = $this->getSlaveDB();
84                 $id = $dbr->selectField(
85                         'page', // Table
86                         'page_id',      //Field
87                         array(  //Conditions
88                                 'page_namespace' => $title->getNamespace(),
89                                 'page_title' => $title->getDBKey(),
90                         ),
91                         __METHOD__      //Function name
92                 );
93                 return $id;
94         }
95
96
97         
98         function findBySha1( $hash ) {
99                 $dbr = $this->getSlaveDB();
100                 $res = $dbr->select(
101                         'image',
102                         LocalFile::selectFields(),
103                         array( 'img_sha1' => $hash )
104                 );
105                 
106                 $result = array();
107                 while ( $row = $res->fetchObject() )
108                         $result[] = $this->newFileFromRow( $row );
109                 $res->free();
110                 return $result;
111         }
112         
113         /*
114          * Find many files using one query
115          */
116         function findFiles( $titles ) {
117                 // FIXME: Only accepts a $titles array where the keys are the sanitized
118                 // file names.
119                  
120                 if ( count( $titles ) == 0 ) return array();            
121         
122                 $dbr = $this->getSlaveDB();
123                 $res = $dbr->select(
124                         'image',
125                         LocalFile::selectFields(),
126                         array( 'img_name' => array_keys( $titles ) )            
127                 );
128                 
129                 $result = array();
130                 while ( $row = $res->fetchObject() ) {
131                         $result[$row->img_name] = $this->newFileFromRow( $row );
132                 }
133                 $res->free();
134                 return $result;
135         }
136 }