+/**
+ * Extract the required params for thumb.php from the thumbnail request URI.
+ * At least 'width' and 'f' should be set if the result is an array.
+ *
+ * @param $uri String Thumbnail request URI path
+ * @return Array|null associative params array or null
+ */
+function wfExtractThumbParams( $uri ) {
+ $repo = RepoGroup::singleton()->getLocalRepo();
+
+ $bits = wfParseUrl( $repo->getZoneUrl( 'thumb' ) );
+ if ( !$bits ) {
+ return null;
+ }
+ $zoneUrlRegex = preg_quote( $bits['path'] );
+
+ $hashDirRegex = $subdirRegex = '';
+ for ( $i = 0; $i < $repo->getHashLevels(); $i++ ) {
+ $subdirRegex .= '[0-9a-f]';
+ $hashDirRegex .= "$subdirRegex/";
+ }
+
+ $thumbUrlRegex = "!^$zoneUrlRegex(/archive|/temp|)/$hashDirRegex([^/]*)/([^/]*)$!";
+
+ // Check if this is a valid looking thumbnail request...
+ if ( preg_match( $thumbUrlRegex, $uri, $matches ) ) {
+ list( /* all */, $archOrTemp, $filename, $thumbname ) = $matches;
+ $filename = urldecode( $filename );
+ $thumbname = urldecode( $thumbname );
+
+ $params = array( 'f' => $filename );
+ if ( $archOrTemp == '/archive' ) {
+ $params['archived'] = 1;
+ } elseif ( $archOrTemp == '/temp' ) {
+ $params['temp'] = 1;
+ }
+
+ // Check if the parameters can be extracted from the thumbnail name...
+ // @TODO: remove 'page' stuff and make ProofreadPage handle it via hook.
+ if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
+ list( /* all */, $pagefull, $pagenum, $size ) = $matches;
+ $params['width'] = $size;
+ if ( $pagenum ) {
+ $params['page'] = $pagenum;
+ }
+ return $params; // valid thumbnail URL
+ // Hooks return false if they manage to *resolve* the parameters
+ } elseif ( !wfRunHooks( 'ExtractThumbParameters', array( $thumbname, &$params ) ) ) {
+ return $params; // valid thumbnail URL (via extension or config)
+ }
+ }
+
+ return null; // not a valid thumbnail URL
+}
+
+/**
+ * Output a thumbnail generation error message
+ *
+ * @param $status integer
+ * @param $msg string
+ * @return void
+ */