400 likes | 411 Views
Files and Filesystem. Martin Kruli š. Working with Files. Low Level C-like API fopen () , fread () , fwrite () , fclose () , … Data are treated as strings Note that per-byte processing is extremely slow File-wide Operations
E N D
Files and Filesystem Martin Kruliš by Martin Kruliš (v1.1)
Working with Files • Low Level C-like API • fopen(), fread(), fwrite(), fclose(),… • Data are treated as strings • Note that per-byte processing is extremely slow • File-wide Operations • fpassthru(), readfile() – whole file is spilled to the output • file_get_contents(), file_put_contents() • Read/write the entire file to/from a string • file() – reads entire file as an array of lines by Martin Kruliš (v1.1)
File System • POSIX Based API • Only partial functionality on non-POSIX OSes • touch() – sets current time to a file • tmpfile() – creates temporary file • rename(), unlink() – unlink ~ delete • mkdir(), rmdir() – make/remove dir • Synchronization Issues • POSIX atomic operations (append, mkdir, …) • Advisory file locking mechanism – flock() • Locks are bounded to processes, multi-threaded web server need not guarantee sync. between PHP scripts by Martin Kruliš (v1.1)
File Properties and Rights • File Properties (Size, Type, Creation Time, …) • Dedicated functions for some properties • filesize(), filetype(), filectime() • Reading all properties at once • stat(), lstat(), fstat() • POSIX Access Rights (rwx) • Testing functions • is_file(), is_readable(), is_writeable(), … • Modification of rights and owner • chmod(), chown(), chgrp() by Martin Kruliš (v1.1)
Directories and Paths • Traversing Directories • Iterative – opendir(), readdir(), closedir() • scandir() – returns array with directory contents • dir() – returns object of Directory class • Directory Paths • glob() – expanding wildcards • dirname(), basename() – get part of path • pathinfo() – parses path and return components • realpath() – converts symbolic to real path • Absolute path without links, ./, and ../ by Martin Kruliš (v1.1)
Formatted Files • CSV Files • Comma (semicolon) separated values • API uses file handlers from fopen() • fgetcsv() – reads one CSV line • fputcsv() – writes one CSV line • INI Files • Configuration files with “name = value” items • parse_ini_file() – reads the entire file and yields an associative array • Optionally divides the data to sections by Martin Kruliš (v1.1)
Compression • ZIP Files (*.zip) • Rather complicated (ZIP is multi-file container) • Represented by ZipArchive class • Zlib – gzip library (*.gz) • Similar functions to standard C-like API • gzopen(), gzread(), gzwrite(), gzclose(), … • BZip Library (*.bz2) • Similar to Zlib(bzopen(), …) • Other libraries are available (RAR, LZF, …) • TAR only as PECL extension by Martin Kruliš (v1.1)
XML and JSON Martin Kruliš by Martin Kruliš (v1.1)
Parsing XML • Parsing Process • Loading XML from text file into memory structures • Simple API for XML (SAX) • The document is processed sequentially • Important “events” are reported to the user • Opening/closing an element, attribute found, … • Document Object Model (DOM) • Tree-based structure with object oriented API • Nodes of the tree represent elements, attributes, text content, …. • More suitable for mutable documents by Martin Kruliš (v1.1)
XPath and XSLT • XPath Query Language • Inspired by filesys. paths (elements ~ directories) • Various types of queries that could yield • Boolean, number, string, or set of DOM nodes • Quite expressive (num. operations, conditions, …) • XSLT Transformations • XSLT describes transformation of one XML document to another (or to HTML, or to plain-text) • XSLT rules are stored in XML document • Uses XPath for search queries • Turing complete language by Martin Kruliš (v1.1)
XML and PHP • XML Utilities in PHP • SAX parser • Procedural interface • DOM API and parser • Object-oriented interface • XPath query engine (over a DOM API) • XSLT processor • PHP-specific Simple XML API (simplified DOM) • Simple XML is based on specific properties of PHP classes (e.g., iterators or magic methods) by Martin Kruliš (v1.1)
PHP SAX Parser • Event Driven Parsing • The document is processed as a stream • In a single call of xml_parse() • Important encounters are reportedvia callbacks • xml_set_element_handler() • xml_set_character_handler() • … Example 1 by Martin Kruliš (v1.1)
PHP DOM Implementation • Document Object Model in PHP • Set of PHP-native classes • Implementing DOM 3 as defined by W3C • DOMDocument – entire XML document • DOMElement – tree node of one XML element • DOMAttr – attribute of an element • DOMNode – base class for all tree classes • DOMCharacterData – plain text contents • … by Martin Kruliš (v1.1)
PHP XML Validation and XPath • Document Validation • DOMDocument methods for validation • validate(), schemaValidate() • Automatic validation when the document is parsed • If validateOnParse == true • DOMXPath Class for XPath Search • Operates on DOMDocument • Search is performed by query() or evaluate() • The result is either basic PHP type (boolean, float, or string) or DOMNodeList object Example 2 by Martin Kruliš (v1.1)
PHP and XSLT • XSLTProcessor Class • Uses libxslt external library • Transformation rules are loaded from a DOM document or from a Simple XML Element • Transformation input is a DOMDocument object • The result is yielded • As a new DOMDocument object • String with serialized XML/HTML/text document • Directly to a file by Martin Kruliš (v1.1)
Simple XML in PHP • Simplified DOM API • Also loads the document into tree structure • All nodes are SimpleXMLElement objects • Tree traversal is implemented by overloading class iterators and magic methods __get(), __set(), … • The structure can be modified and saved • Some advanced functions are also available • Integrated XPath query processor • Conversions to/from DOM structure Example 3 by Martin Kruliš (v1.1)
JSON - Revision • JavaScript Object Notation (JSON) • Lightweight interchange format for structured data • Based on subset of JavaScript language • Otherwise language independent • Many parsers exist with frontends for many languages • Intended for replacing XML in simple scenarios • Syntax • Two basic structures: collections and lists • Supports strings, numbers, bools, and null type • Unicode safe by Martin Kruliš (v1.1)
JSON – Revision • JSON Example [ { "StudentId": 42, "Name": "John Smith" }, { "StudentId": 54, "Name": "Jane Johnson", "Graduated": true } ] Ordered list Number (int) Unicode string Named collection Boolean literal by Martin Kruliš (v1.1)
JSON in PHP • JSON Functions • json_decode() – JSON string -> object/array struct. • json_encode() – serialize any PHP type (except resource) into JSON string • json_last_error(), json_last_error_msg() • Customizing Serialization Process • JsonSerializable interface • Abstract method jsonSerialize() that converts object into anything that can be processed by json_encode() by Martin Kruliš (v1.1)
Image Processing in PHP Martin Kruliš by Martin Kruliš (v1.1)
Non-textual Data • PHP: Hypertext Preprocessor • Not restricted only to (hyper)text, despite the name • HTTP with MIME can accommodate any content • Compressed packages (zip, tar.gz, …) • Application documents (PDF, …) • Images • Caveats • PHP is text oriented • Binary data are represented as strings • PHP is interpreted • Processing binary data might be (very) slow by Martin Kruliš (v1.1)
Images in PHP • GD Library • The php_gd2 module • Supports various formats (JPEG, GIF, PNG, …) • Working with Images • Image is a resource $image = imagecreate(400, 300); • Large variety of functions for manipulation • Image is not compressed in internal representation • Can be saved or written to output • imagepng(), imagejpg(), imagegif() • Corresponding MIME type needs to be set by Martin Kruliš (v1.1)
GD Library • Functions • Loading/saving supported formats • Basic pixel operations • Drawing simple geometric shapes • Lines, circles, polygons, … • Text rendering • Built-in fonts, PostScript fonts, TrueType fonts • Transformation and resampling • Copying, alpha blending • Functions are version specific • And time/memory demanding Example 4 by Martin Kruliš (v1.1)
EXIF • Exchangeable Image FileFormat • Metadata for photographs • Extension of JPEG and TIFF containers • PHP API • Read only functions • exif_imagetype() – returns image type • exif_read_data() – reads meta-data from image • exif_thumbnail() – returns the image thumbnail (if present) as binary string Example 5 by Martin Kruliš (v1.1)
Other Libraries • Cairo • Fast 2D drawing library written in C • Supports transformations and Bezier curves • Gmagic • A Swiss army knife for image manipulation • Large variety of supported formats • ImageMagic • Well established library for image manipulation • Designed even for more complex operations by Martin Kruliš (v1.1)
PHP and Database Management Systems Martin Kruliš by Martin Kruliš (v1.1)
MySQL • MySQLRevision • Original mysql API is deprecated (as of PHP 5.5) • MySQL Improved (mysqli) API • Dual object/procedural interface • Procedural interface is similar to original (deprecated) API • Advanced connectivity features • Persistent connections, compression, encryption • Directly supports transactions • MySQL Native Driver (mysqlnd) extension • More direct access to MySQL server • Additional features (e.g., asynchronous queries) by Martin Kruliš (v1.1)
MySQL • MySQLi API • mysqliclass – the connection to the DBMS • Manage controls, settings, info, stats, … • Issue queries and multi-queries • Performs transaction control • mysqli_stmt class – SQL statement representation • SQL statement preparation and configuration • Argument bindings • mysqli_result class – SELECT result wrapper • Various methods for accessing data in the result by Martin Kruliš (v1.1)
MySQL • MySQLi Query Example $mysqli = newmysqli('server', 'login', ...); $stmt = $mysqli->prepare('SELECT * FROM subjects WHERE students = ? AND credits >= ?'); $stmt->bind_param('si', 'I2', 3); $stmt->execute(); $res = $stmt->get_result(); while ($subj = $res->fetch_object()) { echo"$subj->fullname\n"; } $res->close(); by Martin Kruliš (v1.1)
MySQL • Extensions • mysqlnd_qc • Transparent cache for MySQL queries • mysqlnd_memcache • Special extension that utilizes InnoDBMemcache • Translates SQL statements to Memcache protocol • mysqlnd_uh • Allows user to insert hooks for low-level calls • Can be used for monitoring or auditing • mysqlnd_mux • Transparent client-side connection multiplexing by Martin Kruliš (v1.1)
Other Database Systems • PostrgreSQL • Quite powerful alternative for MySQL • Similar API to MySQL • pg_connect(), pg_query(), … • SQLite • SQL engine running directly on filesystem • Small projects,where regular DBMS is not available • Commercial Giants • MSSQL, Oracle, dBase, IBM DB2, … by Martin Kruliš (v1.1)
PDO Interface • Abstract Database Layers • Solving the problem of portability • If the application refrains from using specific SQL statements • PHP Data Objects (PDO) • Generic interface for various RDBMS • Extension php_pdo, individual DB systems are in separate extensions (php_pdo_mysql, …) • Simple object oriented API • PDO and PDOStatement classes • Minimalistic, no special functions by Martin Kruliš (v1.1)
PHP Abstraction • Database Abstraction Library (DiBi) • Simpler work with statements, result extraction, … dibi::connect([ 'driver' => 'mysqli', … ]); $r = dibi::query('SELECT name FROM [users] WHERE [login] = %s', $login); $username = $r->fetchSingle(); // $r->fetchAll(), $r->fetchPairs(), foreach($r ...) dibi::query('INSERT INTO [table]', [ 'number' => 1, 'str' => "What’s up?" ]); Automated sanitization Data fetching INSERT INTO `table` (`number`, `str`) VALUES (1, 'What\'s up?') by Martin Kruliš (v1.1)
ORM • Object-relational Mapping (ORM) • Technique which creates object-oriented API over (relational) database • Benefits • Much simpler for the programmer (no need for SQL) • Much less error prone • Object-relational impedance mismatch • Set of difficulties which are encountered when matching relational DB and OOP API • E.g., how to save a list of objects of a class User or derived classes (Admin, Editor, …) into relational database? • Mapping IS-A hierarchy, private members, references, … by Martin Kruliš (v1.1)
ORM • Doctrine • Very popular database framework for PHP • Contains both DB abstraction layer and O/R mapping • Provides transparent persistence of PHP objects • The mapping must be provided by the means of XML, YAML, or PHP annotations • A DB schema can be generated from the PHP classes • The interface is provided by EntityManager frontend • Provides methods to load/store objects of specific type • Allows utilizing SQL for more complex cases and converts results into mapped objects by Martin Kruliš (v1.1)
ORM • Doctrine Example /** @Entity @Table(name="subjects") **/ classSubject { /** @Id @Column(type="integer") @GeneratedValue**/ protected $id; /** @Column(type="string") **/ protected$fullname; /** @ManyToOne(targetEntity="User", inversedBy="teach_subjects") **/ protected$teacher; ... public function getDescriptionString() { ... } public function getStudents() { ... } } by Martin Kruliš (v1.1)
ORM • Doctrine Example $entityManager= EntityManager::create($conn, $config); $subj = $entityManager->find('Subject', (int)$id); $subj->setName($newName); $entityManager->flush(); $subjs = $entityManager->getRepository('Subject') ->findBy([ 'students' => 'I2' ]); foreach($subjs as $subj) { echo $subj->getDescriptionString(); foreach($subj->getStudents() as $student) { ... } } by Martin Kruliš (v1.1)
NotORM • Not-ORM Abstraction • Stands somewhere between simple abstraction like DiBi and full ORM system • SQL queries are not written in strings, but semi-automatically procedurally composed • Programmer has to be familiar with SQL • Unlike ORM, the API does not have to know complete DB schema • But knowing/inferring FK relations is very usefulin order to generate table joins properly • Original API was proposed and implemented by Jakub Vrána by Martin Kruliš (v1.1)
NotORM • NotORMExample $db= newNotORM(...); $subj = $db->subjects[42]; $subj->fullname = 'Web Applications'; $subj->update(); foreach($db->subjects() ->where('students', 'I2') ->order('teacher.name') as $subj) { echo $subj->teacher['name'], ' ', $subj['fullname']; } by Martin Kruliš (v1.1)
Discussion by Martin Kruliš (v1.1)