Move checkRedirect() from FileRepo to LocalRepo, leaving only a stub behind. It's...
[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 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 );
18 } else {
19 throw new MWException( __METHOD__.': invalid row' );
20 }
21 }
22
23 function newFromArchiveName( $title, $archiveName ) {
24 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
25 }
26
27 /**
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
31 * remote operation.
32 * @return FileRepoStatus
33 */
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";
42 $dbw->begin();
43 $inuse = $dbw->selectField( 'filearchive', '1',
44 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
45 __METHOD__, array( 'FOR UPDATE' ) );
46 if( !$inuse ) {
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 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
54 __METHOD__, array( 'FOR UPDATE' ) );
55 }
56 if ( !$inuse ) {
57 wfDebug( __METHOD__ . ": deleting $key\n" );
58 if ( !@unlink( $path ) ) {
59 $status->error( 'undelete-cleanup-error', $path );
60 $status->failCount++;
61 }
62 } else {
63 wfDebug( __METHOD__ . ": $key still in use\n" );
64 $status->successCount++;
65 }
66 $dbw->commit();
67 }
68 return $status;
69 }
70
71 /**
72 * Checks if there is a redirect named as $title
73 *
74 * @param Title $title Title of image
75 */
76 function checkRedirect( $title ) {
77 global $wgMemc;
78
79 if( is_string( $title ) ) {
80 $title = Title::newFromTitle( $title );
81 }
82 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
83 $title = Title::makeTitle( NS_FILE, $title->getText() );
84 }
85
86 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
87 $cachedValue = $wgMemc->get( $memcKey );
88 if( $cachedValue ) {
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
91 return false;
92 }
93
94 $id = $this->getArticleID( $title );
95 if( !$id ) {
96 $wgMemc->set( $memcKey, " ", 9000 );
97 return false;
98 }
99 $dbr = $this->getSlaveDB();
100 $row = $dbr->selectRow(
101 'redirect',
102 array( 'rd_title', 'rd_namespace' ),
103 array( 'rd_from' => $id ),
104 __METHOD__
105 );
106
107 if( $row ) $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
108 $wgMemc->set( $memcKey, ($row ? $targetTitle->getPrefixedDBkey() : " "), 9000 );
109 if( !$row ) {
110 return false;
111 }
112 return $targetTitle;
113 }
114
115
116 /**
117 * Function link Title::getArticleID().
118 * We can't say Title object, what database it should use, so we duplicate that function here.
119 */
120 protected function getArticleID( $title ) {
121 if( !$title instanceof Title ) {
122 return 0;
123 }
124 $dbr = $this->getSlaveDB();
125 $id = $dbr->selectField(
126 'page', // Table
127 'page_id', //Field
128 array( //Conditions
129 'page_namespace' => $title->getNamespace(),
130 'page_title' => $title->getDBKey(),
131 ),
132 __METHOD__ //Function name
133 );
134 return $id;
135 }
136
137
138
139 function findBySha1( $hash ) {
140 $dbr = $this->getSlaveDB();
141 $res = $dbr->select(
142 'image',
143 LocalFile::selectFields(),
144 array( 'img_sha1' => $hash )
145 );
146
147 $result = array();
148 while ( $row = $res->fetchObject() )
149 $result[] = $this->newFileFromRow( $row );
150 $res->free();
151 return $result;
152 }
153
154 /*
155 * Find many files using one query
156 */
157 function findFiles( $titles ) {
158 // FIXME: Only accepts a $titles array where the keys are the sanitized
159 // file names.
160
161 if ( count( $titles ) == 0 ) return array();
162
163 $dbr = $this->getSlaveDB();
164 $res = $dbr->select(
165 'image',
166 LocalFile::selectFields(),
167 array( 'img_name' => array_keys( $titles ) )
168 );
169
170 $result = array();
171 while ( $row = $res->fetchObject() ) {
172 $result[$row->img_name] = $this->newFileFromRow( $row );
173 }
174 $res->free();
175 return $result;
176 }
177 }