Tweak timestamp handling to allow no lower limit
[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 function checkRedirect( $title ) {
105 global $wgMemc;
106
107 if( is_string( $title ) ) {
108 $title = Title::newFromTitle( $title );
109 }
110 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
111 $title = Title::makeTitle( NS_FILE, $title->getText() );
112 }
113
114 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
115 $cachedValue = $wgMemc->get( $memcKey );
116 if( $cachedValue ) {
117 return Title::newFromDbKey( $cachedValue );
118 } 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 return false;
120 }
121
122 $id = $this->getArticleID( $title );
123 if( !$id ) {
124 $wgMemc->set( $memcKey, " ", 9000 );
125 return false;
126 }
127 $dbr = $this->getSlaveDB();
128 $row = $dbr->selectRow(
129 'redirect',
130 array( 'rd_title', 'rd_namespace' ),
131 array( 'rd_from' => $id ),
132 __METHOD__
133 );
134
135 if( $row ) $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
136 $wgMemc->set( $memcKey, ($row ? $targetTitle->getPrefixedDBkey() : " "), 9000 );
137 if( !$row ) {
138 return false;
139 }
140 return $targetTitle;
141 }
142
143 function invalidateImageRedirect( $title ) {
144 global $wgMemc;
145 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
146 $wgMemc->delete( $memcKey );
147 }
148
149 function findBySha1( $hash ) {
150 $dbr = $this->getSlaveDB();
151 $res = $dbr->select(
152 'image',
153 LocalFile::selectFields(),
154 array( 'img_sha1' => $hash )
155 );
156
157 $result = array();
158 while ( $row = $res->fetchObject() )
159 $result[] = $this->newFileFromRow( $row );
160 $res->free();
161 return $result;
162 }
163
164 /*
165 * Find many files using one query
166 */
167 function findFiles( $titles ) {
168 // FIXME: Only accepts a $titles array where the keys are the sanitized
169 // file names.
170
171 if ( count( $titles ) == 0 ) return array();
172
173 $dbr = $this->getSlaveDB();
174 $res = $dbr->select(
175 'image',
176 LocalFile::selectFields(),
177 array( 'img_name' => array_keys( $titles ) )
178 );
179
180 $result = array();
181 while ( $row = $res->fetchObject() ) {
182 $result[$row->img_name] = $this->newFileFromRow( $row );
183 }
184 $res->free();
185 return $result;
186 }
187 }