diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/Utils/File.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/src/Utils/File.php | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/Utils/File.php b/www/wiki/extensions/SemanticMediaWiki/src/Utils/File.php new file mode 100644 index 00000000..f17154dd --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/src/Utils/File.php @@ -0,0 +1,98 @@ +<?php + +namespace SMW\Utils; + +use RuntimeException; +use SMW\Exception\FileNotWritableException; + +/** + * @license GNU GPL v2+ + * @since 3.0 + * + * @author mwjames + */ +class File { + + /** + * @since 3.1 + * + * @param string $file + * + * @return string + */ + public static function dir( $file ) { + return str_replace( [ '\\', '//', '/' ], DIRECTORY_SEPARATOR, $file ); + } + + /** + * @since 3.0 + * + * @param string $file + * @param string $content + * @param integer $flags + */ + public function write( $file, $contents, $flags = 0 ) { + + $file = self::dir( $file ); + + if ( !is_writable( dirname( $file ) ) ) { + throw new FileNotWritableException( "$file" ); + } + + file_put_contents( $file, $contents, $flags ); + } + + /** + * @since 3.0 + * + * @param string $file + * + * @return boolean + */ + public function exists( $file ) { + return file_exists( $file ); + } + + /** + * @since 3.0 + * + * @param string $file + * @param integer|null $checkSum + * + * @return string + * @throws RuntimeException + */ + public function read( $file, $checkSum = null ) { + + if ( !is_readable( $file ) ) { + throw new RuntimeException( "$file is not readable." ); + } + + if ( $checkSum !== null && $this->getCheckSum( $file ) !== $checkSum ) { + throw new RuntimeException( "Processing of $file failed with a checkSum error." ); + } + + return file_get_contents( $file ); + } + + /** + * @since 3.0 + * + * @param string $file + */ + public function delete( $file ) { + @unlink( $file ); + } + + /** + * @since 3.0 + * + * @param string $file + * + * @return integer + */ + public function getCheckSum( $file ) { + return md5_file( $file ); + } + +} |