Skip to content

Commit

Permalink
Merge pull request #39 from wikimedia/url-normalization
Browse files Browse the repository at this point in the history
Normalize URLs of the translate page
  • Loading branch information
samwilson committed Jan 3, 2019
2 parents e266472 + 785f8bf commit 6c0954a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
9 changes: 2 additions & 7 deletions src/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace App\Controller;

use App\Model\Title;
use App\OOUI\SearchWidget;
use Krinkle\Intuition\Intuition;
use OOUI\ActionFieldLayout;
Expand Down Expand Up @@ -59,12 +60,6 @@ public function search(Request $request):Response
return $this->redirectToRoute('home');
}

$filePrefixPos = stripos($filename, 'File:');
if (false !== $filePrefixPos) {
// Strip any 'File:' prefix, including if a URL has been supplied.
$filename = substr($filename, $filePrefixPos + strlen('File:'));
}

return $this->redirectToRoute('translate', ['filename' => $filename]);
return $this->redirectToRoute('translate', ['filename' => Title::normalize($filename)]);
}
}
54 changes: 27 additions & 27 deletions src/Controller/TranslateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,34 @@ class TranslateController extends AbstractController
{

/**
* Get the filename parameter from the given Request.
* @param Request $request Normalized filename.
* @return string
* The translate page.
*
* The 'prefix' part of this route is actually a fixed string 'File', but in order to make it
* case-insensitive we have to make it a route variable with a regex requirement. Then we can
* also check it and redirect to the canonical form when required.
* @Route( "/{prefix}:{filename}",
* name="translate",
* methods={"GET"},
* requirements={"prefix"="(?i:File)", "filename"="(.+)"}
* )
*/
protected function getFilename(Request $request):string
{
return str_replace('_', ' ', $request->get('filename'));
}
public function translate(
Request $request,
Intuition $intuition,
Session $session,
FileCache $cache,
string $filename,
string $prefix = 'File'
): Response {
$normalizedFilename = Title::normalize($filename);

// Redirect to normalized URL if required.
if ('File' !== $prefix || $filename !== $normalizedFilename) {
return $this->redirectToRoute('translate', ['filename' => $normalizedFilename]);
}

/**
* @Route("/File:{filename<.+>}", name="translate", methods={"GET"})
*/
public function translate(Request $request, Intuition $intuition, Session $session, FileCache $cache):Response
{
// Fetch the SVG from Commons.
$filename = $this->getFilename($request);
$fileName = Title::normalize($filename);
$path = $cache->getPath($fileName);
$path = $cache->getPath($filename);
$svgFile = new SvgFile($path);

// Upload and download buttons.
Expand Down Expand Up @@ -130,8 +140,8 @@ public function translate(Request $request, Intuition $intuition, Session $sessi

return $this->render('translate.html.twig', [
'page_class' => 'translate',
'title' => $filename,
'filename' => $filename,
'title' => Title::text($filename),
'filename' => $normalizedFilename,
'fields' => $formFields,
'download_button' => $downloadButton,
'upload_button' => $uploadButton,
Expand All @@ -140,14 +150,4 @@ public function translate(Request $request, Intuition $intuition, Session $sessi
'target_lang' => $targetLangDefault,
]);
}

/**
* @Route("/File:{filename<.+>}", name="svg", methods={"POST"}))
*/
public function svg(Request $request, Intuition $intuition, Session $session):Response
{
$filename = $this->getFilename($request);
return $this->redirectToRoute('translate', ['filename' => $filename]);
// @TODO Modify and return SVG.
}
}
13 changes: 12 additions & 1 deletion src/Model/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ final class Title
*/
public static function removeNamespace(string $title): string
{
return preg_replace('/^file\s*:\s*/i', '', $title);
return preg_replace('/.*file\s*:\s*/i', '', $title);
}

/**
* Get the 'DB key' form of a title (underscores not spaces, and uppercase first character).
* @param string $title
* @return string
*/
Expand All @@ -30,4 +31,14 @@ public static function normalize(string $title): string

return $title;
}

/**
* Get the 'text' form of a title (spaces not underscores).
* @param string $title
* @return string
*/
public static function text(string $title): string
{
return str_replace('_', ' ', static::normalize($title));
}
}
2 changes: 1 addition & 1 deletion templates/translate.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% endblock %}

{% block description %}
<h1>{{ filename }}</h1>
<h1>{{ title }}</h1>
<p>
<a href="https://commons.wikimedia.org/wiki/File:{{ filename }}" target="_blank"
title="{{ msg('opens-in-new-tab') }}">{{ msg('view-on-commons') }}</a>
Expand Down
4 changes: 4 additions & 0 deletions tests/Model/TitleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function provideNormalize(): array
['file:тест_123.svg', 'Тест_123.svg'],
['Тест.svg', 'Тест.svg'],
['тест_123.svg', 'Тест_123.svg'],
[
'https://commons.wikimedia.org/wiki/File:Flag_of_Pakistan_Construction.svg',
'Flag_of_Pakistan_Construction.svg',
],
];
}
}

0 comments on commit 6c0954a

Please sign in to comment.