From 5ad18a96737814c04d3f7e03005ed727e41f9862 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 27 Apr 2009 18:31:28 -0400 Subject: [PATCH] Various improvements. * Remove legacy SVN gunk * Add consistency to paths * Make rss-from-git, rss-from-svn and rss-from-vc consistent: last is the preferred method * Create XHTMLCOMPILER constant in view of future "put your xhtml-compiler" directory anywhere functionality * Delay error reporting so parse errors get spit out * Improve htaccess.php code; shorten $new_contents and error msg. Signed-off-by: Edward Z. Yang --- .gitignore | 1 - XHTMLCompiler.php | 4 +- XHTMLCompiler/DOMFilter/RSSFromGit.php | 12 +++--- XHTMLCompiler/FilterManager.php | 3 +- common.php | 23 ++++++---- config.default.php | 12 +----- error.xsl | 3 +- functions.php | 37 ---------------- generate.php | 2 +- htaccess.php | 78 +++++++++++++++++----------------- smoketests/entities.xhtml | 2 +- tests/XHTMLCompiler/FunctionsTest.php | 30 ++++++------- tests/index.php | 3 +- 13 files changed, 83 insertions(+), 127 deletions(-) diff --git a/.gitignore b/.gitignore index 63e91df..376f01d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,6 @@ /mute.txt # Cache/Testing -/cache/svn/log/*.ser /tests/tmp/* # Doctrine diff --git a/XHTMLCompiler.php b/XHTMLCompiler.php index ec13215..7f8c5bb 100644 --- a/XHTMLCompiler.php +++ b/XHTMLCompiler.php @@ -57,14 +57,14 @@ class XHTMLCompiler protected $configKeys = array('allowed_dirs', 'directory_index', 'indexed_dirs', 'web_path', 'web_domain', 'viewvc_url', - 'svn_headurl_munge', 'debug', 'error_xsl', 'admin_email', 'from_email', + 'debug', 'error_xsl', 'admin_email', 'from_email', 'error_log', 'error_mute', 'configdoc', 'smtp_transport', 'dsn'); protected $config = array(); protected $filterManager; public function loadConf() { $filters = new XHTMLCompiler_FilterManager(); - require 'config.default.php'; // defaults + require dirname(__FILE__) . '/config.default.php'; // defaults // user configuration if (file_exists($f = dirname(__FILE__) . '/conf/config.php')) require $f; diff --git a/XHTMLCompiler/DOMFilter/RSSFromGit.php b/XHTMLCompiler/DOMFilter/RSSFromGit.php index 2640e98..74014fd 100644 --- a/XHTMLCompiler/DOMFilter/RSSFromGit.php +++ b/XHTMLCompiler/DOMFilter/RSSFromGit.php @@ -12,7 +12,7 @@ class XHTMLCompiler_DOMFilter_RSSFromGit extends XHTMLCompiler_DOMFilter protected $description = 'Git changelog for %s'; /** Compact title to place in and other places */ protected $title = 'Log for %s'; - protected $xcAttr = array('rss-from-git'); + protected $xcAttr = array('rss-from-vc', 'rss-from-git', 'rss-from-svn'); public function process(DOMDocument $dom, $page, $manager) { $xc = XHTMLCompiler::getInstance(); @@ -21,7 +21,8 @@ class XHTMLCompiler_DOMFilter_RSSFromGit extends XHTMLCompiler_DOMFilter $logs = $page->getLog(); // see if we need to make the link ourself - $trigger = $this->confiscateAttr($dom->documentElement, $this->ns, 'rss-from-git'); + $trigger = $this->confiscateAttr($dom->documentElement, $this->ns, 'rss-from-vc'); + if (!$trigger) $trigger = $this->confiscateAttr($dom->documentElement, $this->ns, 'rss-from-git'); if ($trigger == 'yes') { // this shows up twice because we have to perform all // confiscations before aborting @@ -35,12 +36,13 @@ class XHTMLCompiler_DOMFilter_RSSFromGit extends XHTMLCompiler_DOMFilter $head->appendChild($link); } else { // grab the document's links to RSS feeds - // link must be marked with xc:rss-from-svn + // link must be marked with xc:rss-from-git (or + // xc:rss-from-svn, for BC reasons) // only one allowed - $link = $this->query('//html:link[@xc:rss-from-svn]')->item(0); + $link = $this->query('//html:link[@xc:rss-from-vc]')->item(0); if (!$link) return; // nothing to do - $trigger = $this->confiscateAttr($link, $this->ns, 'rss-from-svn'); + $trigger = $this->confiscateAttr($link, $this->ns, 'rss-from-vc'); if ($trigger != 'yes') return; if (!$logs) return; } diff --git a/XHTMLCompiler/FilterManager.php b/XHTMLCompiler/FilterManager.php index 4aad0e2..51b116d 100644 --- a/XHTMLCompiler/FilterManager.php +++ b/XHTMLCompiler/FilterManager.php @@ -227,8 +227,7 @@ class XHTMLCompiler_FilterManager * Sets up an XML catalog to speed up entity resolution */ public function setupXMLCatalog() { - $catalog = str_replace(array(' ', '\\'), array('%20', '/'), - dirname(__FILE__)) . '/../catalog/catalog.xml'; + $catalog = XHTMLCOMPILER . '/catalog/catalog.xml'; if ($catalog[1] == ':') $catalog = substr($catalog, 2); // remove drive putenv('XML_CATALOG_FILES=' . $catalog); } diff --git a/common.php b/common.php index c361231..069ab3b 100644 --- a/common.php +++ b/common.php @@ -5,14 +5,8 @@ if (!version_compare(PHP_VERSION, "5", ">=")) exit('Requires PHP 5.'); // setup our error reporting, ideally this would be in php.ini but shared // hosting environment requires a little more flexibility. error_reporting(E_STRICT | E_ALL); -ini_set('display_errors', false); -ini_set('log_errors', true); -ini_set('error_log', dirname(__FILE__) . '/errors.log'); -// set working directory to parent directory (slightly hacky) -// if we want this to be able to be included in other apps, -// this must be removed -chdir( dirname(__FILE__) . '/../'); +define('XHTMLCOMPILER', dirname(__FILE__)); require_once 'functions.php'; @@ -38,13 +32,24 @@ require_once 'external/swiftmailer/lib/swift_required.php'; set_exception_handler('xhtmlcompiler_exception_handler'); $xc = XHTMLCompiler::getInstance(); // invoke the super-object + check_errors(); register_shutdown_function('check_errors'); + +ini_set('display_errors', false); +ini_set('log_errors', true); +ini_set('error_log', dirname(__FILE__) . '/errors.log'); + if($xc->getConf('debug')) { restore_error_handler(); ini_set('display_errors', true); } +// set working directory to parent directory (slightly hacky) +// if we want this to be able to be included in other apps, +// this must be removed +chdir( dirname(__FILE__) . '/..' ); + require_once 'external/phpgit/library/Git.php'; require_once 'external/htmlpurifier/library/HTMLPurifier.auto.php'; require_once 'external/htmlpurifier/extras/HTMLPurifierExtras.auto.php'; @@ -53,8 +58,8 @@ require_once 'external/htmlpurifier/extras/HTMLPurifierExtras.auto.php'; require_once 'external/doctrine/lib/Doctrine.php'; spl_autoload_register(array('Doctrine', 'autoload')); -Doctrine::loadModels('xhtml-compiler/models/generated'); -Doctrine::loadModels('xhtml-compiler/models'); +Doctrine::loadModels(dirname(__FILE__) . '/models/generated'); +Doctrine::loadModels(dirname(__FILE__) . '/models'); Doctrine_Manager::connection($xc->getConf('dsn')); diff --git a/config.default.php b/config.default.php index 4752674..9b0ce10 100644 --- a/config.default.php +++ b/config.default.php @@ -49,16 +49,6 @@ else $web_domain = null; // No trailing slash $viewvc_url = false; -// ** Replacement pairs to munge SVN head URLs. -// Useful if your working copy is svn+ssh:// but your key is not available -// to the script. Format is a numerically indexed array of arrays in the -// format of array(match, replace), i.e. -// array( -// 0 => array('svn+ssh://user@example.com/', 'http://example.com/') -// ) -// Will only replace if match is at the beginning of the string -$svn_headurl_munge = array(); - // ** Whether or not to print debug information when Exceptions are thrown. // Debug information can disclose full-paths, so enabling them is not // recommended on production environments. @@ -67,7 +57,7 @@ $debug = false; // ** Error XML transformation stylesheet // This is the file linked to by XML error messages generated by our // default exception handler -$error_xsl = 'xhtml-compiler/error.xsl'; +$error_xsl = dirname(__FILE__) . '/error.xsl'; // note about error handling: if a default error page is defined that's // within XHTML Compiler's jurisdiction, that error page will be managed diff --git a/error.xsl b/error.xsl index 40ddc0a..9913a8f 100644 --- a/error.xsl +++ b/error.xsl @@ -7,6 +7,7 @@ xml:lang="en"> Error: <xsl:value-of select="error/title"/> + @@ -31,4 +32,4 @@ - \ No newline at end of file + diff --git a/functions.php b/functions.php index 1b23dd6..11f1b98 100644 --- a/functions.php +++ b/functions.php @@ -162,43 +162,6 @@ function shortenFile($file, $base) { } /** -* Retrieves the last $limit log entries. -* @param $repos_url Repository URL of item to get logs for -* @param $limit Integer limit of items -*/ -function svn_log_limit($repos_url, $limit, $page = null) { - $limit = (int) $limit; - if ($limit <= 0) return array(); - // attempt the cache - $cache_filename = 'xhtml-compiler/cache/svn/log/' . md5($repos_url) . '.ser'; - $cached_rev = false; - if ($page && file_exists($cache_filename)) { - $logs = unserialize(file_get_contents($cache_filename)); - $cached_rev = !empty($logs[0]) ? $logs[0]['rev'] : false; - // determine current revision number - $rev = $page->getSVNRevision(); - if ($cached_rev == $rev && count($logs) == $limit) return $logs; - } - // -q flag used to prevent server from sending log messages - // most recent is retrieved first - $output = shell_exec("svn log -q --limit $limit $repos_url"); - preg_match_all('/^r(\d+) /m', $output, $matches); - $ret = array(); - foreach ($matches[1] as $rev) { - // reuse previously cached entries - if ($rev <= $cached_rev) { - $ret[] = array_shift($logs); - continue; - } - $log = svn_log($repos_url, (int) $rev); - $ret[] = $log[0]; // log is only one item long - } - // save to cache - file_put_contents($cache_filename, serialize($ret)); - return $ret; -} - -/** * Checks for errors, and sends a notification email if necessary */ function check_errors() { diff --git a/generate.php b/generate.php index d64bd76..add869c 100644 --- a/generate.php +++ b/generate.php @@ -4,7 +4,7 @@ require_once 'common.php'; if (!is_cli()) throw new XHTMLCompiler_Exception(403, "Cannot perform database generation from web"); -chdir('xhtml-compiler'); +chdir(XHTMLCOMPILER); Doctrine::dropDatabases(); Doctrine::createDatabases(); diff --git a/htaccess.php b/htaccess.php index 45fab73..199a8d7 100644 --- a/htaccess.php +++ b/htaccess.php @@ -21,11 +21,11 @@ if (file_exists('.htaccess')) { // do time check $files_to_check = array( '.htaccess.in', - 'xhtml-compiler/config.php', - 'xhtml-compiler/config.default.php', - 'xhtml-compiler/config.smoketest.php', - 'xhtml-compiler/conf/config.php', - 'xhtml-compiler/htaccess.php', + XHTMLCOMPILER . '/config.php', + XHTMLCOMPILER . '/config.default.php', + XHTMLCOMPILER . '/config.smoketest.php', + XHTMLCOMPILER . '/conf/config.php', + XHTMLCOMPILER . '/htaccess.php', 'redirects.txt', ); @@ -41,9 +41,7 @@ if (file_exists('.htaccess')) { if ($no_changes_needed) { throw new XHTMLCompiler_Exception(503, false, - 'No changes detected in xhtml-compiler/config.php, - xhtml-compiler/config.default.php or - xhtml-compiler/htaccess.php.'); + 'No changes detected in relevant files'); } if (!file_exists('.htaccess.in')) { @@ -79,10 +77,10 @@ if (file_exists('.htaccess')) { } // build the new htaccess -$new_contents = array(); -$new_contents[] = $identifier_begin; -$new_contents[] = 'RewriteEngine on'; -$new_contents[] = 'RewriteBase ' . $xc->getConf('web_path') . '/'; +$n = array(); +$n[] = $identifier_begin; +$n[] = 'RewriteEngine on'; +$n[] = 'RewriteBase ' . $xc->getConf('web_path') . '/'; // create permanent redirects if (file_exists('redirects.txt')) { @@ -94,7 +92,7 @@ if (file_exists('redirects.txt')) { list($src, $dest, $p) = explode(' ', $redirect); if ($p !== '[P]') $src = '^' . preg_quote($src) . '$'; // We use rewrite to prevent the appending of ?f= querystring - $new_contents[] = 'RewriteRule ' . $src . ' ' . $dest . ' [R=permanent,L]'; + $n[] = 'RewriteRule ' . $src . ' ' . $dest . ' [R=permanent,L]'; } } @@ -118,53 +116,53 @@ foreach ($allowed_dirs as $dir => $recursive) { } if (is_string($directory_index) && $intercept) { // setup index rewrite - $new_contents[] = "RewriteRule ^($dir_exp)$ \$1$directory_index"; + $n[] = "RewriteRule ^($dir_exp)$ \$1$directory_index"; } $big_exp[] = $dir_exp; } $full_dir_exp = implode('|', $big_exp); -$new_contents[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]'; -$new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]'; -$new_contents[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1'; -$new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]"; +$n[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]'; +$n[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]'; +$n[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1'; +$n[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]"; // if purge is set, also handle directories -$new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1'; -$new_contents[] = "RewriteRule ^($full_dir_exp)$ xhtml-compiler/main.php?f=\$1 [L,QSA]"; +$n[] = 'RewriteCond %{QUERY_STRING} purge=1'; +$n[] = "RewriteRule ^($full_dir_exp)$ xhtml-compiler/main.php?f=\$1 [L,QSA]"; // add application/xhtml+xml if the browser supports it -$new_contents[] = 'RewriteCond %{HTTP_ACCEPT} application/xhtml\\+xml'; -$new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ - \"[T=application/xhtml+xml,L]\""; +$n[] = 'RewriteCond %{HTTP_ACCEPT} application/xhtml\\+xml'; +$n[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ - \"[T=application/xhtml+xml,L]\""; // xc-deps are forbidden to outside world -$new_contents[] = ''; -$new_contents[] = ' Order allow,deny'; -$new_contents[] = ' Deny from all'; -$new_contents[] = ''; +$n[] = ''; +$n[] = ' Order allow,deny'; +$n[] = ' Deny from all'; +$n[] = ''; // errors.log is forbidden to outside world. In theory, this will occur only // in xhtml-compiler/, but it won't hurt to deny it everywhere. -$new_contents[] = ''; -$new_contents[] = ' Order allow,deny'; -$new_contents[] = ' Deny from all'; -$new_contents[] = ''; +$n[] = ''; +$n[] = ' Order allow,deny'; +$n[] = ' Deny from all'; +$n[] = ''; // setup RSS -$new_contents[] = 'AddType application/rss+xml rss'; -$new_contents[] = 'AddCharset UTF-8 .rss'; -$new_contents[] = ''; -$new_contents[] = ' '; -$new_contents[] = ' Header append Cache-Control "no-cache, must-revalidate"'; -$new_contents[] = ' '; -$new_contents[] = ''; +$n[] = 'AddType application/rss+xml rss'; +$n[] = 'AddCharset UTF-8 .rss'; +$n[] = ''; +$n[] = ' '; +$n[] = ' Header append Cache-Control "no-cache, must-revalidate"'; +$n[] = ' '; +$n[] = ''; // set UTF-8 for HTML pages -$new_contents[] = 'AddCharset UTF-8 .html'; +$n[] = 'AddCharset UTF-8 .html'; -$new_contents[] = $identifier_end; +$n[] = $identifier_end; -$contents = str_replace($identifier_here, implode($new_contents, PHP_EOL), $contents); +$contents = str_replace($identifier_here, implode($n, PHP_EOL), $contents); file_put_contents('.htaccess', $contents); chmod('.htaccess', 0644); diff --git a/smoketests/entities.xhtml b/smoketests/entities.xhtml index a742f62..f08de74 100644 --- a/smoketests/entities.xhtml +++ b/smoketests/entities.xhtml @@ -20,4 +20,4 @@
  • &smoketest.test2;
  • - \ No newline at end of file + diff --git a/tests/XHTMLCompiler/FunctionsTest.php b/tests/XHTMLCompiler/FunctionsTest.php index 99ce31f..d3516d2 100644 --- a/tests/XHTMLCompiler/FunctionsTest.php +++ b/tests/XHTMLCompiler/FunctionsTest.php @@ -2,15 +2,15 @@ class XHTMLCompiler_FunctionsTest extends XHTMLCompilerHarness { - + function test_set_response_code() { $this->php->expectAt(0, 'header', array($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404)); $this->php->expectAt(1, 'header', array('Status: 404 Not Found')); set_response_code(404); } - + function test_exception_handler() { - $this->xc->setReturnValue('getConf', 'xhtml-compiler/error.xsl', array('error_xsl')); + $this->xc->setReturnValue('getConf', XHTMLCOMPILER . '/error.xsl', array('error_xsl')); $this->php->expect('paint', array(new PatternExpectation('/404 Not Found/'))); xhtmlcompiler_exception_handler(new XHTMLCompiler_Exception(404)); $this->php->expect('paint', array(new PatternExpectation('/WTF/'))); @@ -18,65 +18,65 @@ class XHTMLCompiler_FunctionsTest extends XHTMLCompilerHarness $this->php->expect('paint', array(new PatternExpectation('/Could not find the file/'))); xhtmlcompiler_exception_handler(new XHTMLCompiler_Exception(404, false, 'Could not find the file')); } - + function test_get_page_from_server() { $this->php->setReturnValue('getRequestURI', '/index.html'); $this->php->setReturnValue('getPHPSelf', '/xhtml-compiler/main.php'); $this->assertEqual(get_page_from_server(), 'index.html'); } - + function test_get_page_from_server_InSubdir() { $this->php->setReturnValue('getRequestURI', '/subdir/foobar.html'); $this->php->setReturnValue('getPHPSelf', '/subdir/xhtml-compiler/main.php'); $this->assertEqual(get_page_from_server(), 'foobar.html'); } - + function test_get_page_from_server_InSubdirDifferentAppDir() { $this->php->setReturnValue('getRequestURI', '/subdir/foobar.html'); $this->php->setReturnValue('getPHPSelf', '/subdir/xc/main.php'); $this->assertEqual(get_page_from_server(), 'foobar.html'); } - + function test_get_page_from_server_InSubdirPageInDirectory() { $this->php->setReturnValue('getRequestURI', '/subdir/foo/foobar.html'); $this->php->setReturnValue('getPHPSelf', '/subdir/xhtml-compiler/main.php'); $this->assertEqual(get_page_from_server(), 'foo/foobar.html'); } - + function test_normalize_index_Blank() { $this->assertEqual(normalize_index('', 'index.html'), 'index.html'); } - + function test_normalize_index_File() { $this->php->expectOnce('isDir', array('main.html')); $this->php->setReturnValue('isDir', false); $this->assertEqual(normalize_index('main.html', 'index.html'), 'main.html'); } - + function test_normalize_index_LooksLikeFileButIsDir() { $this->php->expectOnce('isDir', array('main.html')); $this->php->setReturnValue('isDir', true); $this->assertEqual(normalize_index('main.html', 'index.html'), 'main.html/index.html'); } - + function test_normalize_index_SubDir() { $this->php->expectOnce('isDir', array('foo/')); $this->php->setReturnValue('isDir', true); $this->assertEqual(normalize_index('foo/', 'index.html'), 'foo/index.html'); } - + function test_normalize_index_SubDirMissingSlash() { $this->php->expectOnce('isDir', array('foo')); $this->php->setReturnValue('isDir', true); $this->assertEqual(normalize_index('foo', 'index.html'), 'foo/index.html'); } - + function test_normalize_index_FileWithoutExtension() { $this->php->expectOnce('isDir', array('foo')); $this->php->setReturnValue('isDir', false); $this->assertEqual(normalize_index('foo', 'index.html'), 'foo'); } - + } -?> \ No newline at end of file +?> diff --git a/tests/index.php b/tests/index.php index 6fdb388..199c75e 100644 --- a/tests/index.php +++ b/tests/index.php @@ -21,7 +21,7 @@ require_once $simpletest_location . 'mock_objects.php'; // load our libraries require_once '../common.php'; -chdir('xhtml-compiler/tests'); +chdir(dirname(__FILE__)); Mock::generate('XHTMLCompiler', 'XHTMLCompilerMock'); Mock::generate('XHTMLCompiler_PHP', 'XHTMLCompiler_PHPMock'); Mock::generate('XHTMLCompiler_DOMFilter', 'XHTMLCompiler_DOMFilterMock'); @@ -49,4 +49,3 @@ else $reporter = new HTMLReporter('UTF-8'); $test->run($reporter); -?> \ No newline at end of file -- 2.11.4.GIT