25f9092e104d07fbd94aacb8658f4623ead96171
[lhc/web/wiklou.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 getSlaveDB() {
14 return wfGetDB( DB_SLAVE );
15 }
16
17 function getMasterDB() {
18 return wfGetDB( DB_MASTER );
19 }
20
21 function getMemcKey( $key ) {
22 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
23 }
24
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 );
30 } else {
31 throw new MWException( __METHOD__.': invalid row' );
32 }
33 }
34
35 function newFromArchiveName( $title, $archiveName ) {
36 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
37 }
38
39 /**
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
43 * remote operation.
44 * @return FileRepoStatus
45 */
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";
54 $dbw->begin();
55 $inuse = $dbw->selectField( 'filearchive', '1',
56 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
57 __METHOD__, array( 'FOR UPDATE' ) );
58 if( !$inuse ) {
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' ) );
67 }
68 if ( !$inuse ) {
69 wfDebug( __METHOD__ . ": deleting $key\n" );
70 if ( !@unlink( $path ) ) {
71 $status->error( 'undelete-cleanup-error', $path );
72 $status->failCount++;
73 }
74 } else {
75 wfDebug( __METHOD__ . ": $key still in use\n" );
76 $status->successCount++;
77 }
78 $dbw->commit();
79 }
80 return $status;
81 }
82
83 /**
84 * Function link Title::getArticleID().
85 * We can't say Title object, what database it should use, so we duplicate that function here.
86 */
87 protected function getArticleID( $title ) {
88 if( !$title instanceof Title ) {
89 return 0;
90 }
91 $dbr = $this->getSlaveDB();
92 $id = $dbr->selectField(
93 'page', // Table
94 'page_id', //Field
95 array( //Conditions
96 'page_namespace' => $title->getNamespace(),
97 'page_title' => $title->getDBKey(),
98 ),
99 __METHOD__ //Function name
100 );
101 return $id;
102 }
103
104
105
106 function findBySha1( $hash ) {
107 $dbr = $this->getSlaveDB();
108 $res = $dbr->select(
109 'image',
110 LocalFile::selectFields(),
111 array( 'img_sha1' => $hash )
112 );
113
114 $result = array();
115 while ( $row = $res->fetchObject() )
116 $result[] = $this->newFileFromRow( $row );
117 $res->free();
118 return $result;
119 }
120
121 /*
122 * Find many files using one query
123 */
124 function findFiles( $titles ) {
125 // FIXME: Only accepts a $titles array where the keys are the sanitized
126 // file names.
127
128 if ( count( $titles ) == 0 ) return array();
129
130 $dbr = $this->getSlaveDB();
131 $res = $dbr->select(
132 'image',
133 LocalFile::selectFields(),
134 array( 'img_name' => array_keys( $titles ) )
135 );
136
137 $result = array();
138 while ( $row = $res->fetchObject() ) {
139 $result[$row->img_name] = $this->newFileFromRow( $row );
140 }
141 $res->free();
142 return $result;
143 }
144 }