Lokal Profil has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/374901 )
Change subject: Make handling of wikidata urls more compatible with other
handlers
......................................................................
Make handling of wikidata urls more compatible with other handlers
Changes the parser for wikidata urls so as to output a result
compatible with that of the wikiproject url parser.
Change-Id: I966ad0e37173264efd175bc4fa5dfcf343486eb9
---
M api/includes/CommonFunctions.php
M api/includes/FormatHtml.php
M api/includes/FormatHtmllist.php
M api/includes/FormatWikitable.php
M api/tests/CommonFunctionsTest.php
5 files changed, 63 insertions(+), 57 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/labs/tools/heritage
refs/changes/01/374901/1
diff --git a/api/includes/CommonFunctions.php b/api/includes/CommonFunctions.php
index 164f665..37bae77 100644
--- a/api/includes/CommonFunctions.php
+++ b/api/includes/CommonFunctions.php
@@ -45,8 +45,28 @@
}
//Separates the parsing from the prettifying
+function matchWikidataLink($url) {
+ /* Determine if a string is a Wikidata page or entity link
+ * If the pattern matches, returns an array with the matching parts
+ * If the pattern doesn't match, returns NULL
+ *
+ * The format of the array is designed to be compatible with that for
+ * matchWikiprojectLink().
+ */
+ $var = NULL;
+ $pattern =
'/(https?:)?\/\/((www)\.(wikidata)\.org\/(wiki|entity)\/(.*))/';
+ if ( !preg_match( $pattern, $url, $var )) {
+ throw new Exception('The provided url was not a wikidata
link.');
+ }
+ else {
+ unset( $var[5] );
+ $var = array_values( $var ); // rebase array
+ }
+ return $var;
+}
+
function matchWikiprojectLink($text) {
- /* Determine if a string is a wikipedia link
+ /* Determine if a string is a Wikipedia link
* If the pattern matches, returns an array with the matching parts
* If the pattern doesn't match, returns NULL
*/
@@ -60,13 +80,31 @@
return $var;
}
+/**
+ * Return a mathing wikiproject or wikidata url
+ */
+function matchUrl( $url ) {
+ try {
+ $m = matchWikiprojectLink( $url );
+ } catch ( Exception $e ) {
+ // Possibly a wikidata entity/wiki link
+ try {
+ $m = matchWikidataLink( $url );
+ } catch ( Exception $e ) {
+ // Normal text
+ $m = null;
+ }
+ }
+ return $m;
+}
+
function urlencodeWikiprojectLink($var, $drop_oldid = false) {
/* Takes a matching group from matchWikiprojectLink
* and returns an url with the pagename urlencoded.
*/
$site = $var[3] . '.' . $var[4] . '.org';
$title = urlencode( $var[5] );
- if ( $drop_oldid ){
+ if ( $drop_oldid or !isset( $var[6] ) ){
return $site . '/w/index.php?title=' . $title;
}
return $site . '/w/index.php?title=' . $title . '&oldid=' . $var[6];
@@ -74,16 +112,6 @@
function replaceSpaces( $in_string ) {
return str_replace(' ', '_', $in_string);
-}
-
-function matchWikidataQid($url) {
- /* Extract the Qid from a Wikidata page or entity link */
- $var = NULL;
- $pattern = '/(https?:)?\/\/(www\.wikidata\.org\/(wiki|entity)\/(.*))/';
- if ( !preg_match( $pattern, $url, $var )) {
- throw new Exception('The provided url was not a wikidata
link.');
- }
- return $var;
}
function makeWikidataUrl($qid) {
diff --git a/api/includes/FormatHtml.php b/api/includes/FormatHtml.php
index c0a9403..7401c9a 100644
--- a/api/includes/FormatHtml.php
+++ b/api/includes/FormatHtml.php
@@ -128,20 +128,13 @@
* Make this a nice link if it is a url (source column)
*/
static function prettifyUrls( $text ) {
- try {
- $m = matchWikiprojectLink( $text );
+ $m = matchUrl( $text );
+ if ( $m ) {
$linkText = str_replace( '_', ' ', $m[5] );
$encodedLink = urlencodeWikiprojectLink( $m );
return makeHTMLlink( 'https://' . $encodedLink,
$linkText );
- } catch ( Exception $e ) {
- // Possibly a wikidata entity/wiki link
- try {
- $m = matchWikidataQid( $text );
- return self::makeHTMLlink( 'https://' . $m[2],
$m[4] );
- } catch ( Exception $e ) {
- // Normal text
- return htmlspecialchars( $text );
- }
+ } else {
+ return htmlspecialchars( $text );
}
}
diff --git a/api/includes/FormatHtmllist.php b/api/includes/FormatHtmllist.php
index ed63140..3dac82c 100644
--- a/api/includes/FormatHtmllist.php
+++ b/api/includes/FormatHtmllist.php
@@ -124,7 +124,7 @@
}
if ( isset( $row->source ) and $row->source ) {
- $m = self::matchUrl( $row->source );
+ $m = matchUrl( $row->source );
if ( $m ) {
$encodedLink = '//' . urlencodeWikiprojectLink(
$m, true );
$desc .= '<li>';
@@ -159,25 +159,6 @@
}
}
$this->outputEnd();
- }
-
- /**
- * Return a mathing wikiproject or wikidata url
- */
- static function matchUrl( $url ) {
- try {
- $m = matchWikiprojectLink( $url );
- return $m[2];
- } catch ( Exception $e ) {
- // Possibly a wikidata entity/wiki link
- try {
- $m = matchWikidataQid( $url );
- return $m[2];
- } catch ( Exception $e ) {
- // Normal text
- return null;
- }
- }
}
}
diff --git a/api/includes/FormatWikitable.php b/api/includes/FormatWikitable.php
index 6cf513d..c72a76b 100644
--- a/api/includes/FormatWikitable.php
+++ b/api/includes/FormatWikitable.php
@@ -94,8 +94,8 @@
} catch ( Exception $e ) {
// Possibly a wikidata entity/wiki link
try {
- $m = matchWikidataQid( $text );
- return '[[:d:' . $m[4] . '|' . $m[4] . ']]';
+ $m = matchWikidataLink( $text );
+ return '[[:d:' . $m[5] . '|' . $m[5] . ']]';
} catch ( Exception $e ) {
// Normal text
return htmlspecialchars( $text );
diff --git a/api/tests/CommonFunctionsTest.php
b/api/tests/CommonFunctionsTest.php
index a23c9fa..2ef635f 100644
--- a/api/tests/CommonFunctionsTest.php
+++ b/api/tests/CommonFunctionsTest.php
@@ -200,56 +200,60 @@
matchWikidataQid( $input );
}
- public function test_matchWikidataQid_entity()
+ public function test_matchWikidataLink_entity()
{
$input = "https://www.wikidata.org/entity/Q5943";
$expected = Array(
"https://www.wikidata.org/entity/Q5943",
"https:",
"www.wikidata.org/entity/Q5943",
- "entity",
+ "www",
+ "wikidata",
"Q5943"
);
- $this->assertEquals($expected, matchWikidataQid($input));
+ $this->assertEquals($expected, matchWikidataLink($input));
}
- public function test_matchWikidataQid_wikipage()
+ public function test_matchWikidataLink_wikipage()
{
$input = "https://www.wikidata.org/wiki/Q5943";
$expected = Array(
"https://www.wikidata.org/wiki/Q5943",
"https:",
"www.wikidata.org/wiki/Q5943",
- "wiki",
+ "www",
+ "wikidata",
"Q5943"
);
- $this->assertEquals($expected, matchWikidataQid($input));
+ $this->assertEquals($expected, matchWikidataLink($input));
}
- public function test_matchWikidataQid_http()
+ public function test_matchWikidataLink_http()
{
$input = "http://www.wikidata.org/entity/Q5943";
$expected = Array(
"http://www.wikidata.org/entity/Q5943",
"http:",
"www.wikidata.org/entity/Q5943",
- "entity",
+ "www",
+ "wikidata",
"Q5943"
);
- $this->assertEquals($expected, matchWikidataQid($input));
+ $this->assertEquals($expected, matchWikidataLink($input));
}
- public function test_matchWikidataQid_no_protocol()
+ public function test_matchWikidataLink_no_protocol()
{
$input = "//www.wikidata.org/entity/Q5943";
$expected = Array(
"//www.wikidata.org/entity/Q5943",
"",
"www.wikidata.org/entity/Q5943",
- "entity",
+ "www",
+ "wikidata",
"Q5943"
);
- $this->assertEquals($expected, matchWikidataQid($input));
+ $this->assertEquals($expected, matchWikidataLink($input));
}
public function test_makeWikidataUrl()
--
To view, visit https://gerrit.wikimedia.org/r/374901
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I966ad0e37173264efd175bc4fa5dfcf343486eb9
Gerrit-PatchSet: 1
Gerrit-Project: labs/tools/heritage
Gerrit-Branch: master
Gerrit-Owner: Lokal Profil <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits