svn: /phd/trunk/phpdotnet/phd/Package/PHP/ EnhancedCHM.php

From: Date: Wed, 23 Feb 2011 13:47:46 +0000
Subject: svn: /phd/trunk/phpdotnet/phd/Package/PHP/ EnhancedCHM.php
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-7810@lists.php.net to get a copy of this message
rquadling Wed, 23 Feb 2011 13:47:46 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=308601 Log: Updated enhanced format to use TEMP and to cache the usernotes. Uses bz2 extension rather than external executable. Decompresses and converts all.bz2 on the fly - no all.bz2 or all files saved. Changed paths: U phd/trunk/phpdotnet/phd/Package/PHP/EnhancedCHM.php Modified: phd/trunk/phpdotnet/phd/Package/PHP/EnhancedCHM.php =================================================================== --- phd/trunk/phpdotnet/phd/Package/PHP/EnhancedCHM.php 2011-02-23 13:16:28 UTC (rev 308600) +++ phd/trunk/phpdotnet/phd/Package/PHP/EnhancedCHM.php 2011-02-23 13:47:46 UTC (rev 308601) @@ -7,6 +7,9 @@ // Do not bother processing notes if we haven't got them this time. protected $haveNotes = false; + // Where are the usernotes? + protected $userNotesBaseDir = null; + public function __construct() { parent::__construct(); $this->registerFormatName("PHP-EnhancedCHM"); @@ -17,66 +20,82 @@ case Render::INIT: parent::update($event, $val); - // Get http://www.php.net/backend/notes/all.bz2 and save it. - v('Downloading usernotes.', VERBOSE_MESSAGES); - if (false === ($userNotesArchive = file_get_contents('http://www.php.net/backend/notes/all.bz2'))) { - v('Failed to download usernotes.', E_USER_ERROR); - break; - } + // Use %TEMP%/usernotes as base directory for Usernotes. + $this->userNotesBaseDir = getenv('TEMP') . DIRECTORY_SEPARATOR . 'usernotes' . DIRECTORY_SEPARATOR; - // Save the usernotes. - v('Saving usernotes.', VERBOSE_MESSAGES); - if (false === file_put_contents(Config::output_dir() . 'all.bz2', $userNotesArchive)) { - v('Failed to save usernotes.', E_USER_ERROR); - break; + // Make the usernotes directory. + if(!file_exists($this->userNotesBaseDir) || is_file($this->userNotesBaseDir)) { + mkdir($this->userNotesBaseDir) or v("Can't create the usernotes directory : %s", $this->userNotesBaseDir, E_USER_ERROR); } - // Decompress the usernotes. - v('Decompressing usernotes.', VERBOSE_MESSAGES); - if (false === strpos(exec('bzip2.exe -dfkv ' . Config::output_dir() . 'all.bz2 2>&1'), 'all.bz2: done')) { - v('Failed to decompress usernotes.' . PHP_EOL . implode(PHP_EOL, $output), E_USER_ERROR); - break; - } + // Get the local last-updated value. + $userNotesLastUpdatedLocal = file_exists($this->userNotesBaseDir . 'last-updated') ? intval(file_get_contents($this->userNotesBaseDir . 'last-updated')) : 0; - // Extract the usernotes and store them by page and date. - v('Preparing usernotes.', VERBOSE_MESSAGES); + // Get the remote last-updated value to see if we need to do anything with the usernotes we already have. + v('Checking usernotes.', VERBOSE_MESSAGES); + $userNotesLastUpdatedRemote = intval(file_get_contents('http://www.php.net/backend/notes/last-updated')); - // Create usernotes directory. - $this->userNotesDir = Config::output_dir() . 'usernotes' . DIRECTORY_SEPARATOR; - if(!file_exists($this->userNotesDir) || is_file($this->userNotesDir)) { - mkdir($this->userNotesDir) or v("Can't create the usernotes directory", E_USER_ERROR); - } + // Compare the remote and local last-updated values. + if ($userNotesLastUpdatedLocal < $userNotesLastUpdatedRemote) { - // Remove any existing files. - foreach(glob($this->userNotesDir . '*' . DIRECTORY_SEPARATOR . '*') as $sectionFile) { - unlink($sectionFile); - } + // Make sure the bz2 extension is loaded. + // Whilst this PhD format is going to be used on Windows, make it non windows compliant by using the appropriate filenaming conventions. + if (!extension_loaded('bz2')) { + dl((PHP_SHLIB_SUFFIX === 'dll' ? 'php_' : '') . 'bz2.' . PHP_SHLIB_SUFFIX); + } + + if (!extension_loaded('bz2')) { + v('The BZip2 extension is not available.', E_USER_ERROR); + break; + } + + // Remove any existing files. + foreach(glob($this->userNotesBaseDir . '*' . DIRECTORY_SEPARATOR . '*') as $sectionFile) { + unlink($sectionFile); + } - // Decompress the 'all' file into single files - one file per sectionid. - $userNotesFile = fopen(Config::output_dir() . 'all', 'rt'); - while($userNotesFile && !feof($userNotesFile) && false !== ($userNote = fgetcsv($userNotesFile, 0, '|'))) { - // Usernote index - // 0 = Note ID - // 1 = Section ID - // 2 = Rate - // 3 = Timestamp - // 4 = User - // 5 = Note + // Use a decompression stream filter to save having to store anything locally other than the expanded user notes. + if (false === ($fpNotes = fopen('http://www.php.net/backend/notes/all.bz2', 'rb'))) { + v('Failed to access the usernotes archive.', E_USER_ERROR); + break; + } + + $fpsfNotes = stream_filter_append($fpNotes, 'bzip2.decompress', STREAM_FILTER_READ, array('small' => true)); - $sectionHash = md5($userNote[1]); - $sectionDir = $this->userNotesDir . $sectionHash[0]; + // Extract the usernotes and store them by page and date. + v('Preparing usernotes.', VERBOSE_MESSAGES); - if (!file_exists($sectionDir)) { - mkdir($sectionDir); + // Decompress the 'all' file into single files - one file per sectionid. + while($fpNotes && !feof($fpNotes) && false !== ($userNote = fgetcsv($fpNotes, 0, '|'))) { + // Usernote index + // 0 = Note ID + // 1 = Section ID + // 2 = Rate + // 3 = Timestamp + // 4 = User + // 5 = Note + + $sectionHash = md5($userNote[1]); + $sectionDir = $this->userNotesBaseDir . $sectionHash[0]; + + if (!file_exists($sectionDir)) { + mkdir($sectionDir); + } + + file_put_contents($sectionDir . DIRECTORY_SEPARATOR . $sectionHash, implode('|', $userNote) . PHP_EOL, FILE_APPEND); } - file_put_contents($sectionDir . DIRECTORY_SEPARATOR . $sectionHash, implode('|', $userNote) . PHP_EOL, FILE_APPEND); - } + stream_filter_remove($fpsfNotes); + fclose($fpNotes); - fclose($userNotesFile); + // Save the last-updated data. + file_put_contents($this->userNotesBaseDir . 'last-updated', $userNotesLastUpdatedRemote); - $this->haveNotes = true; - v('Usernotes prepared.', VERBOSE_MESSAGES); + $this->haveNotes = true; + v('Usernotes prepared.', VERBOSE_MESSAGES); + } else { + v('Usernotes not updated.', VERBOSE_MESSAGES); + } break; @@ -90,7 +109,7 @@ // Find usernotes file. $idHash = md5($id); - $userNotesFile = $this->userNotesDir . $idHash[0] . DIRECTORY_SEPARATOR . $idHash; + $userNotesFile = $this->userNotesBaseDir . $idHash[0] . DIRECTORY_SEPARATOR . $idHash; if (!file_exists($userNotesFile)) { $notes = ' <div class="note">There are no user contributed notes for this page.</div>'; @@ -98,6 +117,13 @@ $notes = ''; foreach(file($userNotesFile) as $userNote) { + // Usernote index + // 0 = Note ID + // 1 = Section ID + // 2 = Rate + // 3 = Timestamp + // 4 = User + // 5 = Note list($noteId, $noteSection, $noteRate, $noteTimestamp, $noteUser, $noteText) = explode('|', $userNote); if ($noteUser) {

« previous php.doc.cvs (#7810) next »