Krinkle has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/363979 )

Change subject: Use MediaWiki-like escaping for section link
......................................................................

Use MediaWiki-like escaping for section link

Previously, an automatic edit summary like "/* External link */"
resulted in a link to "/wiki/Page_name#External link" without
any escaping.

* Remove the duplicate htmlspecialchars() call.
* Add unit tests.

Bug: T165549
Change-Id: I4516d5a7ef03c4c046ac4b24a96bf0d64728251c
---
M .gitignore
M composer.json
A phpunit.xml.dist
M src/App.php
M src/Wiki.php
A tests/WikiTest.php
6 files changed, 64 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/tools/guc 
refs/changes/79/363979/1

diff --git a/.gitignore b/.gitignore
index 4210414..834faf0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
-vendor
-composer.lock
-cache/*.json
+/cache/*.json
+/composer.lock
+/coverage
+/vendor
diff --git a/composer.json b/composer.json
index 9c5eb1d..574aa02 100644
--- a/composer.json
+++ b/composer.json
@@ -15,12 +15,15 @@
        },
        "require-dev": {
                "jakub-onderka/php-parallel-lint": "^0.9.2",
-               "squizlabs/php_codesniffer": "^2.7.1"
+               "squizlabs/php_codesniffer": "^2.7.1",
+               "phpunit/phpunit": "4.8.*"
        },
        "scripts": {
                "test": [
                        "parallel-lint . --exclude vendor",
+                       "phpunit",
                        "phpcs -p -s"
-               ]
+               ],
+               "cover": "phpunit --coverage-html coverage/"
        }
 }
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
new file mode 100644
index 0000000..d3772cd
--- /dev/null
+++ b/phpunit.xml.dist
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit colors="true">
+       <testsuites>
+               <testsuite name="GUC Tests">
+                       <directory>./tests</directory>
+               </testsuite>
+       </testsuites>
+       <filter>
+               <whitelist addUncoveredFilesFromWhitelist="true">
+                       <directory suffix=".php">./src</directory>
+               </whitelist>
+       </filter>
+</phpunit>
diff --git a/src/App.php b/src/App.php
index c1ae19c..db38076 100644
--- a/src/App.php
+++ b/src/App.php
@@ -174,8 +174,11 @@
                 $section = str_replace(']]', '', $section);
                 // See MediaWiki/Sanitizer::normalizeSectionNameWhitespace() 
and Language::getArrow()
                 $section =  trim(preg_replace('/[ _]+/', ' ', $section));
+                // See MediaWiki/Sanitizer::escapeId() – called by 
Linker::makeCommentLink(),
+                // via LinkerRenderer, via Title::escapeFragmentForURL().
                 $link = '<a href="' . htmlspecialchars(
-                    "$server/w/index.php?title=" . 
htmlspecialchars(Wiki::urlencode($page)) . "#$section"
+                    "$server/w/index.php?title=" . Wiki::urlencode($page) .
+                    "#" . Wiki::escapeId($section)
                 ) . '">→</a>';
 
                 if ($isPost) {
diff --git a/src/Wiki.php b/src/Wiki.php
index 9649a47..09b847f 100644
--- a/src/Wiki.php
+++ b/src/Wiki.php
@@ -95,6 +95,24 @@
     }
 
     /**
+     * Based on MediaWiki's Sanitizer::escapeId()
+     *
+     * @param string $id
+     * @return string
+     */
+    public static function escapeId($id) {
+        // HTML4-style escaping
+        static $replace = [
+            '%3A' => ':',
+            '%' => '.',
+        ];
+
+        $id = urlencode(strtr($id, ' ', '_'));
+        $id = strtr($id, $replace);
+        return $id;
+    }
+
+    /**
      * Based on MediaWiki 1.25's Sanitizer::escapeHtmlAllowEntities
      *
      * @param string $wikitext
diff --git a/tests/WikiTest.php b/tests/WikiTest.php
new file mode 100644
index 0000000..f3096bd
--- /dev/null
+++ b/tests/WikiTest.php
@@ -0,0 +1,20 @@
+<?php
+
+use Guc\Wiki;
+
+class WikiTest extends PHPUnit_Framework_TestCase {
+
+    public function provideEscapeId() {
+        return [
+            ['Foo bar', 'Foo_bar'],
+            ['Foo/bar', 'Foo.2Fbar'],
+        ];
+    }
+
+    /**
+     * @dataProvider provideEscapeId
+     */
+    public function testEscapeId($input, $expected) {
+        $this->assertEquals($expected, Wiki::escapeId($input));
+    }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4516d5a7ef03c4c046ac4b24a96bf0d64728251c
Gerrit-PatchSet: 1
Gerrit-Project: labs/tools/guc
Gerrit-Branch: master
Gerrit-Owner: Krinkle <krinklem...@gmail.com>

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

Reply via email to