Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / cache / dependency / FileDependency.php
1 <?php
2 /**
3 * Data caching with dependencies.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * @ingroup Cache
26 */
27 class FileDependency extends CacheDependency {
28 private $filename;
29 private $timestamp;
30
31 /**
32 * Create a file dependency
33 *
34 * @param string $filename The name of the file, preferably fully qualified
35 * @param null|bool|int $timestamp The unix last modified timestamp, or false if the
36 * file does not exist. If omitted, the timestamp will be loaded from
37 * the file.
38 *
39 * A dependency on a nonexistent file will be triggered when the file is
40 * created. A dependency on an existing file will be triggered when the
41 * file is changed.
42 */
43 function __construct( $filename, $timestamp = null ) {
44 $this->filename = $filename;
45 $this->timestamp = $timestamp;
46 }
47
48 /**
49 * @return array
50 */
51 function __sleep() {
52 $this->loadDependencyValues();
53
54 return [ 'filename', 'timestamp' ];
55 }
56
57 function loadDependencyValues() {
58 if ( is_null( $this->timestamp ) ) {
59 Wikimedia\suppressWarnings();
60 # Dependency on a non-existent file stores "false"
61 # This is a valid concept!
62 $this->timestamp = filemtime( $this->filename );
63 Wikimedia\restoreWarnings();
64 }
65 }
66
67 /**
68 * @return bool
69 */
70 function isExpired() {
71 Wikimedia\suppressWarnings();
72 $lastmod = filemtime( $this->filename );
73 Wikimedia\restoreWarnings();
74 if ( $lastmod === false ) {
75 if ( $this->timestamp === false ) {
76 # Still nonexistent
77 return false;
78 }
79
80 # Deleted
81 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
82
83 return true;
84 }
85
86 if ( $lastmod > $this->timestamp ) {
87 # Modified or created
88 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
89
90 return true;
91 }
92
93 # Not modified
94 return false;
95 }
96 }