* Fix image redirect caching so it doesn't break image redirects on shared repositories
[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 */
6 class LocalRepo extends FSRepo {
7 var $fileFactory = array( 'LocalFile', 'newFromTitle' );
8 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
9
10 function getSlaveDB() {
11 return wfGetDB( DB_SLAVE );
12 }
13
14 function getMasterDB() {
15 return wfGetDB( DB_MASTER );
16 }
17
18 function getMemcKey( $key ) {
19 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
20 }
21
22 function newFileFromRow( $row ) {
23 if ( isset( $row->img_name ) ) {
24 return LocalFile::newFromRow( $row, $this );
25 } elseif ( isset( $row->oi_name ) ) {
26 return OldLocalFile::newFromRow( $row, $this );
27 } else {
28 throw new MWException( __METHOD__.': invalid row' );
29 }
30 }
31
32 function newFromArchiveName( $title, $archiveName ) {
33 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
34 }
35
36 /**
37 * Delete files in the deleted directory if they are not referenced in the
38 * filearchive table. This needs to be done in the repo because it needs to
39 * interleave database locks with file operations, which is potentially a
40 * remote operation.
41 * @return FileRepoStatus
42 */
43 function cleanupDeletedBatch( $storageKeys ) {
44 $root = $this->getZonePath( 'deleted' );
45 $dbw = $this->getMasterDB();
46 $status = $this->newGood();
47 $storageKeys = array_unique($storageKeys);
48 foreach ( $storageKeys as $key ) {
49 $hashPath = $this->getDeletedHashPath( $key );
50 $path = "$root/$hashPath$key";
51 $dbw->begin();
52 $inuse = $dbw->selectField( 'filearchive', '1',
53 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
54 __METHOD__, array( 'FOR UPDATE' ) );
55 if( !$inuse ) {
56 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
57 $ext = substr( $key, strcspn($key,'.') + 1 );
58 $ext = File::normalizeExtension($ext);
59 $inuse = $dbw->selectField( 'oldimage', '1',
60 array( 'oi_sha1' => $sha1,
61 "oi_archive_name LIKE '%.{$ext}'",
62 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
63 __METHOD__, array( 'FOR UPDATE' ) );
64 }
65 if ( !$inuse ) {
66 wfDebug( __METHOD__ . ": deleting $key\n" );
67 if ( !@unlink( $path ) ) {
68 $status->error( 'undelete-cleanup-error', $path );
69 $status->failCount++;
70 }
71 } else {
72 wfDebug( __METHOD__ . ": $key still in use\n" );
73 $status->successCount++;
74 }
75 $dbw->commit();
76 }
77 return $status;
78 }
79
80 /**
81 * Function link Title::getArticleID().
82 * We can't say Title object, what database it should use, so we duplicate that function here.
83 */
84 protected function getArticleID( $title ) {
85 if( !$title instanceof Title ) {
86 return 0;
87 }
88 $dbr = $this->getSlaveDB();
89 $id = $dbr->selectField(
90 'page', // Table
91 'page_id', //Field
92 array( //Conditions
93 'page_namespace' => $title->getNamespace(),
94 'page_title' => $title->getDbKey(),
95 ),
96 __METHOD__ //Function name
97 );
98 return $id;
99 }
100
101 function checkRedirect( $title ) {
102 global $wgMemc;
103
104 if( is_string( $title ) ) {
105 $title = Title::newFromTitle( $title );
106 }
107 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
108 $title = Title::makeTitle( NS_IMAGE, $title->getText() );
109 }
110
111 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
112 $cachedValue = $wgMemc->get( $memcKey );
113 if( $cachedValue ) {
114 return Title::newFromDbKey( $cachedValue );
115 } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
116 return false;
117 }
118
119 $id = $this->getArticleID( $title );
120 if( !$id ) {
121 $wgMemc->set( $memcKey, " ", 9000 );
122 return false;
123 }
124 $dbr = $this->getSlaveDB();
125 $row = $dbr->selectRow(
126 'redirect',
127 array( 'rd_title', 'rd_namespace' ),
128 array( 'rd_from' => $id ),
129 __METHOD__
130 );
131
132 if( $row ) $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
133 $wgMemc->set( $memcKey, ($row ? $targetTitle->getPrefixedDBkey() : " "), 9000 );
134 if( !$row ) {
135 return false;
136 }
137 return $targetTitle;
138 }
139
140 function invalidateImageRedirect( $title ) {
141 global $wgMemc;
142 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
143 $wgMemc->delete( $memcKey );
144 }
145 }