BryanDavis has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/266077

Change subject: Cleanup error pages
......................................................................

Cleanup error pages

Apply MediaWiki code style to error pages and consolidate common logic
into functions in an include file.

Change-Id: I9b8eac8ae8d09cc773c3f57e03b60f86d49a7f00
---
M www/content/403.php
M www/content/404.php
M www/content/500.php
M www/content/503.php
A www/content/common.inc.php
5 files changed, 122 insertions(+), 175 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/toollabs 
refs/changes/77/266077/1

diff --git a/www/content/403.php b/www/content/403.php
index 9977e82..1961072 100644
--- a/www/content/403.php
+++ b/www/content/403.php
@@ -6,47 +6,23 @@
 <meta name="viewport" content="initial-scale=1.0, user-scalable=yes, 
width=device-width">
 </head>
 <body>
-<?php $uri = $_SERVER['HTTP_X_ORIGINAL_URI']; ?>
-      <h1>Forbidden</h1>
-      <p>The URI you have requested, <a href="<?= $uri ?>"><code><?= $uri 
?></code></a>, might exist but the server has been instructed not to let you 
reach it.</p>
-      <?php $tool = '';
-         if(preg_match("@^/([^/]+)/@", $uri, $part)) {
-           $gr = posix_getgrnam("tools.".$part[1]);
-           if($gr) {
-             $tool = $part[1];
-             $maintainers = $gr['members'];
-           }
-         }
-         if($tool != ''):
-      ?>
-      <h2>If you have reached this page from somewhere else...</h2>
-      <p>This URI is managed by the <a href="/?tool=<?= $tool ?>"><code><?= 
$tool?></code></a> tool, maintained by 
-      <?php foreach($maintainers as $num => $maint):
-           $mu = posix_getpwnam($maint);
-           if($mu):
-             $wtu = $mu['gecos'];
-             ?><A HREF="https://wikitech.wikimedia.org/wiki/User:<?= $wtu 
?>"><?= ucfirst($wtu) ?></A><?php
-           else:
-             echo ucfirst($maint);
-           endif;
-           if($num < count($maintainers)-1) {
-             if($num == count($maintainers)-2) {
-               if($num == 0)
-                 print " and ";
-               else
-                 print ", and ";
-             } else
-               print ", ";
-           }
-           endforeach;
-      ?>.</p>
-      <p>Perhaps this content can only be accessed from the secret underground 
lair of the maintainers, or the link you've followed doesn't actually lead 
somewhere useful?</p>
-      <p>If you're pretty sure this shouldn't be an error, you may wish to 
notify the tool's maintainers (above) about the error and how you ended up 
here.</p>
-      <h2>If you maintain this tool</h2>
-      <p>The error might be caused by incorrect permissions, or by the absence 
of an index file (this webserver does not list directory contents by 
default).</p>
-      <?php else: ?>
-      <p>Perhaps the webserver has temporarily lost its mind, or the link 
you've followed doesn't actually lead somewhere useful?</p>
-      <p>If you're pretty sure this shouldn't be an error, you may wish to 
notify the <a href="/?tool=admin">project administrators</a> about the error 
and how you ended up here.</p>
-      <?php endif ?>
+<?php
+require_once dirname( __FILE__ ) . '/common.inc.php';
+$uri = $_SERVER['HTTP_X_ORIGINAL_URI'];
+list( $tool, $maintainers ) = getToolInfo( $uri );
+?>
+<h1>Forbidden</h1>
+<p>The URI you have requested, <a href="<?= $uri ?>"><code><?= $uri 
?></code></a>, might exist but the server has been instructed not to let you 
reach it.</p>
+<?php if ( $tool !== false ) { ?>
+<h2>If you have reached this page from somewhere else...</h2>
+<p>This URI is managed by the <a href="/?tool=<?= urlencode( $tool ) 
?>"><code><?= htmlentities( $tool )?></code></a> tool, maintained by <?php 
printMaintainers( $maintainers ) ?>.</p>
+<p>Perhaps this content can only be accessed from the secret underground lair 
of the maintainers, or the link you've followed doesn't actually lead somewhere 
useful?</p>
+<p>If you're pretty sure this shouldn't be an error, you may wish to notify 
the tool's maintainers (above) about the error and how you ended up here.</p>
+<h2>If you maintain this tool</h2>
+<p>The error might be caused by incorrect permissions, or by the absence of an 
index file (this webserver does not list directory contents by default).</p>
+<?php } else { ?>
+<p>Perhaps the webserver has temporarily lost its mind, or the link you've 
followed doesn't actually lead somewhere useful?</p>
+<p>If you're pretty sure this shouldn't be an error, you may wish to notify 
the <a href="/?tool=admin">project administrators</a> about the error and how 
you ended up here.</p>
+<?php } ?>
 </body>
 </html>
diff --git a/www/content/404.php b/www/content/404.php
index b8f2693..5f778b1 100644
--- a/www/content/404.php
+++ b/www/content/404.php
@@ -6,45 +6,21 @@
 <meta name="viewport" content="initial-scale=1.0, user-scalable=yes, 
width=device-width">
 </head>
 <body>
-<?php $uri = $_SERVER['HTTP_X_ORIGINAL_URI']; ?>
-      <h1>Four hundred and four!</h1>
-      <p>The URI you have requested, <code><?= htmlspecialchars( $uri ) 
?></code>, doesn't seem to actually exist.</p>
-      <?php $tool = '';
-         if(preg_match("@^/([^/]+)/@", $uri, $part)) {
-           $gr = posix_getgrnam("tools.".$part[1]);
-           if($gr) {
-             $tool = $part[1];
-             $maintainers = $gr['members'];
-           }
-         }
-         if($tool != ''):
-      ?>
-      <h2>If you have reached this page from somewhere else...</h2>
-      <p>This URI is managed by the <a href="/?tool=<?= $tool ?>"><code><?= 
$tool?></code></a> tool, maintained by 
-      <?php foreach($maintainers as $num => $maint):
-           $mu = posix_getpwnam($maint);
-           if($mu):
-             $wtu = $mu['gecos'];
-             ?><A HREF="https://wikitech.wikimedia.org/wiki/User:<?= $wtu 
?>"><?= ucfirst($wtu) ?></A><?php
-           else:
-             echo ucfirst($maint);
-           endif;
-           if($num < count($maintainers)-1) {
-             if($num == count($maintainers)-2) {
-               if($num == 0)
-                 print " and ";
-               else
-                 print ", and ";
-             } else
-               print ", ";
-           }
-           endforeach;
-      ?>.</p>
-      <p>Perhaps its files are on vacation, or the link you've followed 
doesn't actually lead somewhere useful?</p>
-      <p>You might want to looks at the <a href="/?list">list of tools</a> to 
find what you were looking for, or one of the links on the sidebar to the left. 
If you're pretty sure this shouldn't be an error, you may wish to notify the 
tool's maintainers (above) about the error and how you ended up here.</p>
-      <?php else: ?>
-      <p>Perhaps the webserver has temporarily lost its mind, or the link 
you've followed doesn't actually lead somewhere useful?</p>
-      <p>You might want to looks at the <a href="/?list">list of tools</a> to 
find what you were looking for, or one of the links on the sidebar to the left. 
If you're pretty sure this shouldn't be an error, you may wish to notify the <a 
href="/?tool=admin">project administrators</a> about the error and how you 
ended up here.</p>
-      <?php endif ?>
+<?php
+require_once dirname( __FILE__ ) . '/common.inc.php';
+$uri = $_SERVER['HTTP_X_ORIGINAL_URI'];
+list( $tool, $maintainers ) = getToolInfo( $uri );
+?>
+<h1>Four hundred and four!</h1>
+<p>The URI you have requested, <code><?= htmlspecialchars( $uri ) ?></code>, 
doesn't seem to actually exist.</p>
+<?php if ( $tool !== false ) { ?>
+<h2>If you have reached this page from somewhere else...</h2>
+<p>This URI is managed by the <a href="/?tool=<?= urlencode( $tool ) 
?>"><code><?= htmlspecialchars( $tool ) ?></code></a> tool, maintained by <?php 
printMaintainers( $maintainers ) ?>.</p>
+<p>Perhaps its files are on vacation, or the link you've followed doesn't 
actually lead somewhere useful?</p>
+<p>You might want to looks at the <a href="/?list">list of tools</a> to find 
what you were looking for, or one of the links on the sidebar to the left. If 
you're pretty sure this shouldn't be an error, you may wish to notify the 
tool's maintainers (above) about the error and how you ended up here.</p>
+<?php } else { ?>
+<p>Perhaps the webserver has temporarily lost its mind, or the link you've 
followed doesn't actually lead somewhere useful?</p>
+<p>You might want to looks at the <a href="/?list">list of tools</a> to find 
what you were looking for, or one of the links on the sidebar to the left. If 
you're pretty sure this shouldn't be an error, you may wish to notify the <a 
href="/?tool=admin">project administrators</a> about the error and how you 
ended up here.</p>
+<?php } ?>
 </body>
 </html>
diff --git a/www/content/500.php b/www/content/500.php
index d985199..9e5c5b5 100644
--- a/www/content/500.php
+++ b/www/content/500.php
@@ -6,52 +6,23 @@
 <meta name="viewport" content="initial-scale=1.0, user-scalable=yes, 
width=device-width">
 </head>
 <body>
-<?php $uri = $_SERVER['HTTP_X_ORIGINAL_URI']; ?>
-      <h1>Internal error</h1>
-      <p>The URI you have requested, <a href="<?= $uri ?>"><code><?= $uri 
?></code></a>,
-      appears to be non-functional at this time.</p>
-      <?php $tool = '';
-         if(preg_match("@^/([^/]+)/@", $uri, $part)) {
-           $gr = posix_getgrnam("tools.".$part[1]);
-           if($gr) {
-             $tool = $part[1];
-             $maintainers = $gr['members'];
-           }
-         }
-         if($tool != ''):
-      ?>
-        <h2>If you have reached this page from somewhere else...</h2>
-        <p>This URI is part of the <a href="/?tool=<?= $tool ?>"><code><?= 
$tool?></code></a> tool, maintained by 
-        <?php foreach($maintainers as $num => $maint):
-             $mu = posix_getpwnam($maint);
-             if($mu):
-               $wtu = $mu['gecos'];
-               ?><a href="https://wikitech.wikimedia.org/wiki/User:<?= $wtu 
?>"><?= ucfirst($wtu) ?></A><?php
-             else:
-               echo ucfirst($maint);
-             endif;
-             if($num < count($maintainers)-1) {
-               if($num == count($maintainers)-2) {
-                 if($num == 0)
-                   print " and ";
-                 else
-                   print ", and ";
-               } else
-                 print ", ";
-             }
-             endforeach;
-        ?>.</p>
-        <p>Perhaps its magical script elves are temporarily ill, or the link 
you've followed doesn't actually lead
-        somewhere useful?<p>
-        <p>If you're pretty sure this shouldn't be an error, you may wish to 
notify the tool's maintainers (above)
-        about the error and how you ended up here.</p>
-        <h2>If you maintain this tool</h2>
-        <p>The error might be caused by incorrect permission, or by an error 
in the script or CGI that was meant
-        to execute here.  You may wish to check your logs or <a 
href="https://wikitech.wikimedia.org/wiki/Nova_Resource:Tools/Help#Logs";>common 
causes for errors</a> in the help documentation.</p>
-      <?php else: ?>
-        <p>Perhaps the webserver has temporarily lost its mind, or the link 
you've followed doesn't actually lead
-        somewhere useful?</p>
-        <p>If you're pretty sure this shouldn't be an error, you may wish to 
notify the <a href="/?tool=admin">project administrators</a> about the error 
and how you ended up here.</p>
-      <?php endif ?>
+<?php
+require_once dirname( __FILE__ ) . '/common.inc.php';
+$uri = $_SERVER['HTTP_X_ORIGINAL_URI'];
+list( $tool, $maintainers ) = getToolInfo( $uri );
+?>
+<h1>Internal error</h1>
+<p>The URI you have requested, <a href="<?= htmlspecialchars( $uri ) 
?>"><code><?= htmlspecialchars( $uri ) ?></code></a>, appears to be 
non-functional at this time.</p>
+<?php if ( $tool !== false ) { ?>
+<h2>If you have reached this page from somewhere else...</h2>
+<p>This URI is part of the <a href="/?tool=<?= urlencode( $tool ) 
?>"><code><?= htmlspecialchars( $tool ) ?></code></a> tool, maintained by <?php 
printMaintainers( $maintainers ) ?>.</p>
+<p>Perhaps its magical script elves are temporarily ill, or the link you've 
followed doesn't actually lead somewhere useful?<p>
+<p>If you're pretty sure this shouldn't be an error, you may wish to notify 
the tool's maintainers (above) about the error and how you ended up here.</p>
+<h2>If you maintain this tool</h2>
+<p>The error might be caused by incorrect permission, or by an error in the 
script or CGI that was meant to execute here.  You may wish to check your logs 
or <a 
href="https://wikitech.wikimedia.org/wiki/Nova_Resource:Tools/Help#Logs";>common 
causes for errors</a> in the help documentation.</p>
+<?php } else { ?>
+<p>Perhaps the webserver has temporarily lost its mind, or the link you've 
followed doesn't actually lead somewhere useful?</p>
+<p>If you're pretty sure this shouldn't be an error, you may wish to notify 
the <a href="/?tool=admin">project administrators</a> about the error and how 
you ended up here.</p>
+<?php } ?>
 </body>
 </html>
diff --git a/www/content/503.php b/www/content/503.php
index a2ad497..86b70b6 100644
--- a/www/content/503.php
+++ b/www/content/503.php
@@ -6,51 +6,26 @@
 <meta name="viewport" content="initial-scale=1.0, user-scalable=yes, 
width=device-width">
 </head>
 <body>
-<?php $uri = $_SERVER['HTTP_X_ORIGINAL_URI']; ?>
-      <h1>No webservice</h1>
-      <p>The URI you have requested, <a href="<?= $uri ?>"><code><?= $uri 
?></code></a>,
-        is not currently serviced.</p>
-      <?php $tool = '';
-         if(preg_match("@^/([^/]+)/@", $uri, $part)) {
-           $gr = posix_getgrnam("tools.".$part[1]);
-           if($gr) {
-             $tool = $part[1];
-             $maintainers = $gr['members'];
-           }
-         }
-         if($tool != ''):
-      ?>
-        <h2>If you have reached this page from somewhere else...</h2>
-        <p>This URI is part of the <a href="/?tool=<?= $tool ?>"><code><?= 
$tool?></code></a> tool, maintained by 
-        <?php foreach($maintainers as $num => $maint):
-             $mu = posix_getpwnam($maint);
-             if($mu):
-               $wtu = $mu['gecos'];
-               ?><a href="https://wikitech.wikimedia.org/wiki/User:<?= $wtu 
?>"><?= ucfirst($wtu) ?></A><?php
-             else:
-               echo ucfirst($maint);
-             endif;
-             if($num < count($maintainers)-1) {
-               if($num == count($maintainers)-2) {
-                 if($num == 0)
-                   print " and ";
-                 else
-                   print ", and ";
-               } else
-                 print ", ";
-             }
-             endforeach;
-        ?>.</p>
-        <p>That tool might not have a web interface, or it may currently be 
disabled.</p>
-        <p>If you're pretty sure this shouldn't be an error, you may wish to 
notify the tool's maintainers (above)
-        about the error and how you ended up here.</p>
-        <h2>If you maintain this tool</h2>
-        <p>You have not enabled a web service for your tool, or it has stopped 
working because of a fatal error.
-        You may wish to check your logs or <a 
href="https://wikitech.wikimedia.org/wiki/Nova_Resource:Tools/Help#Logs";>common 
causes for errors</a> in the help documentation.</p>
-      <?php else: ?>
-        <h2>If you have reached this page from somewhere else...</h2>
-        <p>This URI is not currently part of any tool.</p>
-        <p>If you're pretty sure this shouldn't be an error, you may wish to 
notify the <a href="/?tool=admin">project administrators</a> about the error 
and how you ended up here.</p>
-      <?php endif ?>
+<?php
+require_once dirname( __FILE__ ) . '/common.inc.php';
+$uri = $_SERVER['HTTP_X_ORIGINAL_URI'];
+list( $tool, $maintainers ) = getToolInfo( $uri );
+?>
+<h1>No webservice</h1>
+<p>The URI you have requested, <a href="<?= htmlspecialchars( $uri ) 
?>"><code><?= htmlspecialchars( $uri ) ?></code></a>, is not currently 
serviced.</p>
+<?php if ( $tool !== false ) { ?>
+<h2>If you have reached this page from somewhere else...</h2>
+<p>This URI is part of the <a href="/?tool=<?= urlencode( $tool ) 
?>"><code><?= htmlspecialchars( $tool )?></code></a> tool, maintained by <?php 
printMaintainers( $maintainers ) ?>.</p>
+<p>That tool might not have a web interface, or it may currently be 
disabled.</p>
+<p>If you're pretty sure this shouldn't be an error, you may wish to notify 
the tool's maintainers (above) about the error and how you ended up here.</p>
+<h2>If you maintain this tool</h2>
+<p>You have not enabled a web service for your tool, or it has stopped working 
because of a fatal error. You may wish to check your logs or <a 
href="https://wikitech.wikimedia.org/wiki/Nova_Resource:Tools/Help#Logs";>common 
causes for errors</a> in the help documentation.</p>
+<?php
+} else {
+?>
+<h2>If you have reached this page from somewhere else...</h2>
+<p>This URI is not currently part of any tool.</p>
+<p>If you're pretty sure this shouldn't be an error, you may wish to notify 
the <a href="/?tool=admin">project administrators</a> about the error and how 
you ended up here.</p>
+<?php } ?>
 </body>
 </html>
diff --git a/www/content/common.inc.php b/www/content/common.inc.php
new file mode 100644
index 0000000..b5e3cb0
--- /dev/null
+++ b/www/content/common.inc.php
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * Get information about a tool based on the given URI.
+ *
+ * @param string $uri
+ * @return array Tool name and list of maintainers. Tool name will be false if
+ *   URI does not correspond to a known tool
+ */
+function getToolInfo( $uri ) {
+       if ( preg_match( '@^/([^/]+)/@', $uri, $part ) ) {
+               $gr = posix_getgrnam( 'tools.' . $part[1] );
+               if ( $gr ) {
+                       return array( $part[1], $gr['members'] );
+               }
+       }
+       return array( false, array() );
+}
+
+/**
+ * Print a nicely formatted list of maintainers.
+ *
+ * @param array $maintainers List of usernames
+ */
+function printMaintainers( $maintainers ) {
+       $numMaintainers = count( $maintainers );
+       foreach ( $maintainers as $num => $maint ) {
+               $mu = posix_getpwnam( $maint );
+               if ( $mu ) {
+                       $wtu = $mu['gecos'];
+                       echo '<a 
href="https://wikitech.wikimedia.org/wiki/User:';
+                       echo urlencode( $wtu ), '">', htmlspecialchars( 
ucfirst($wtu) );
+                       echo '</a>';
+               } else {
+                       echo htmlspecialchars( ucfirst( $maint ) );
+               }
+               if ( $num < $numMaintainers - 1 ) {
+                       if ( $num == $numMaintainers - 2 ) {
+                               if ( $num === 0 ) {
+                                       echo ' and ';
+                               } else {
+                                       echo ', and ';
+                               }
+                       } else {
+                               echo ', ';
+                       }
+               }
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/266077
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9b8eac8ae8d09cc773c3f57e03b60f86d49a7f00
Gerrit-PatchSet: 1
Gerrit-Project: labs/toollabs
Gerrit-Branch: master
Gerrit-Owner: BryanDavis <bda...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to