Kipcool has submitted this change and it was merged.
Change subject: DM redlinks solutions (page rendering only)
......................................................................
DM redlinks solutions (page rendering only)
When wkitext pages are rendered, in wikilinks to DefinedMeaning pages,
replace link targets having non-canoncal page names with their canonical
ones, and link targets having incorrect DefinedMeaning IDs with ones
going to the (invalid) DefinedMeaning ID 0 (as contrary to normal wiki
pages, one cannot know or predict the canonical names of DefinedMeaning
pages before they have actually been created)
Thus avoid bogus red links and several other issues.
P2: removed spaces.
P3: removed unnecessary echo
P4: forgot to amend P3
P5: not all cases were covered.
P6: Do not treat OW namespaces. Replace link titles-tags by canonical
page titles.
P7: minor corrections
P8: SQLite compatibility
P9: Make regexp accept more of the good cases.
P10: Accept any number of closing parentheses after DM id.
P11: corrected errors Kip pointed out. Used verifyDefinedMeaningId,
which was refactored to be accessed from the OwDatabaseAPI.
P12: Safety againt Omegawiki namespaces being added added or deleted.
P13: corrects the bug of P12, used API function definingExpression,
deleted the redundant function, and created the parseText function
to simplify checkDMRedLinks code.
P14: Basically redone: Want to store links to canonical titles on page
save. That does not work. Pages are rendered as wanted but are stored
without the changes.
P16: Postpone the auto-correction during page save.
Fixing this during renderig is already a valid step forward.
Bug:T118066
Bug:T118149
Change-Id: I493cd004a242713c78c660aa13f7335f08888473
---
M App.php
A OmegaWiki.hooks.php
M OmegaWiki/OmegaWikiDatabaseAPI.php
M OmegaWiki/WikiDataAPI.php
4 files changed, 152 insertions(+), 30 deletions(-)
Approvals:
Kipcool: Verified; Looks good to me, approved
diff --git a/App.php b/App.php
index 03294fb..05b9a4e 100644
--- a/App.php
+++ b/App.php
@@ -33,7 +33,9 @@
'Kim Bruning',
'Maarten van Hoof',
'André Malafaya Baptista',
- 'Kipcool'
+ 'Kipcool',
+ '庄向荣',
+ 'Purodha Blissenbach'
),
'url' =>
'http://www.mediawiki.org/wiki/Extension:WikiLexicalData',
'descriptionmsg' => 'wikidata-desc',
@@ -70,6 +72,8 @@
);
$wgAutoloadClasses['WikiLexicalDataHooks'] = $dir . 'Wikidata.hooks.php';
+$wgAutoloadClasses['OwDatabaseAPI'] = $dir .
'OmegaWiki/OmegaWikiDatabaseAPI.php';
+$wgAutoloadClasses['OmegaWikiHooks'] = $dir . 'OmegaWiki.hooks.php';
$wgAutoloadClasses['WikidataArticle' ] = $dir .
'includes/WikidataArticle.php';
$wgAutoloadClasses['WikidataEditPage' ] = $dir .
'includes/WikidataEditPage.php';
@@ -169,6 +173,12 @@
$wgHooks['LoadExtensionSchemaUpdates'][] = 'WikiLexicalDataHooks::loadSchema';
$wgHooks['SpecialStatsAddExtra'][] =
'WikiLexicalDataHooks::onSpecialStatsAddExtra';
+# OmegaWikiHooks
+# $wgHooks['InternalParseBeforeLinks'][] =
'OmegaWikiHooks::onInternalParseBeforeLinks';
+# $wgHooks['ParserBeforeStrip'][] =
'OmegaWikiHooks::onInternalParseBeforeLinks';
+# $wgHooks['ParserBeforeInternalParse'][] =
'OmegaWikiHooks::onInternalParseBeforeLinks';
+$wgHooks['InternalParseBeforeSanitize'][] =
'OmegaWikiHooks::onInternalParseBeforeLinks';
+
// Jobs
require_once( $wgWldSetupScriptPath . "OWJobs.php" );
diff --git a/OmegaWiki.hooks.php b/OmegaWiki.hooks.php
new file mode 100644
index 0000000..daa466c
--- /dev/null
+++ b/OmegaWiki.hooks.php
@@ -0,0 +1,64 @@
+<?php
+
+// currently shares globals with @ "OmegaWiki/WikiDataGlobals.php".
+// @note if we need to separate Omegawiki from WikiLexicalData, we need
+// to transfer all OmegaWiki hooks here.
+
+class OmegaWikiHooks extends WikiLexicalDataHooks {
+
+ /** @brief links to DefinedMeaning pages re modified to use cononical
page titles
+ *
+ * Link having target pages having canonical titles are left untouched.
+ * Target pages with valid DefinedMeaning IDs in links are replaced by
their canonical titles.
+ * Target pages having invalid DefinedMeaning IDs are replaced by a
link to (invalid) DefinedMeaning ID 0.
+ */
+ public static function onInternalParseBeforeLinks( Parser &$parser,
&$text ) {
+ global $wgExtraNamespaces, $wgWldOwScriptPath;
+ // FIXME: skip if not action=submit
+ // FIXME: skip if not page text
+ if ( True ) {
+ $nspace = 'DefinedMeaning' ; // FIXME: compute the
standard (english) name, do not use a constant.
+ $namspce = $wgExtraNamespaces[NS_DEFINEDMEANING];
+ if ( $nspace !== $namspce ) {
+ $nspace .= '|';
+ $nspace .= $namspce;
+ }
+ // case insensitivly find all internal links going to
DefinedMeaning pages
+ $pattern = '/\\[\\[(\\s*(' . $nspace . ')\\s*' .
+
':(([^]\\|]*)\((\\d+)\\)[^]\\|]*))(\\|[^]]*)?\\]\\]/i';
+ preg_match_all( $pattern, $text, $match );
+ if ( $match[0] ) {
+ // collect all DefinedMeaning IDs, all links to
any of them, point to their array position
+ foreach( $match[5] as $index => $dmNumber ) {
+ $dmIds[0+$dmNumber][$match[0][$index]]
= $index;
+ }
+ foreach( $dmIds as $dmId => $links ) {
+ if (
OwDatabaseAPI::verifyDefinedMeaningId( $dmId ) ) {
+ $title = definingExpression(
$dmId ) . '_(' . $dmId . ')' ;
+ } else {
+ $title = '_(0)' ;
+ }
+ foreach( $links as $link => $index ) {
+ if ( trim( $match[3][$index] )
!= $title ) {
+ // alter only if it
would change
+ switch ( strlen( trim(
$match[6][$index] ) ) ) {
+ case 0: //
there was no "|" in the link
+ $replace = '|'
. $match[1][$index] ;
+ break;
+ case 1: //there
was an "|" not followed by text
+ $replace = '|'
. $match[3][$index] ;
+ break;
+ default: //there
was an "|" followed by text
+ $replace =
$match[6][$index] ;
+ }
+ $replace = '[[' .
$namspce . ':' . $title . $replace . ']]' ;
+ $text = str_replace(
$link , $replace , $text );
+ }
+ }
+ }
+ }
+ }
+ return True;
+ }
+
+}
diff --git a/OmegaWiki/OmegaWikiDatabaseAPI.php
b/OmegaWiki/OmegaWikiDatabaseAPI.php
index 88a620d..970fb30 100644
--- a/OmegaWiki/OmegaWikiDatabaseAPI.php
+++ b/OmegaWiki/OmegaWikiDatabaseAPI.php
@@ -21,6 +21,30 @@
public function __construct() {
}
+ /** @addtogroup OwDbAPIcomFn OwDatabaseAPI's Common database functions
+ * @{
+ */
+
+/**
+ * returns the value of column if exist
+ * null if not found
+ * @param table table name
+ * @param column column nane
+ * @param value column value
+ * @param isDc if has DataSet Context(boolean)
+ */
+ public static function verifyColumn( $table, $column, $value, $isDc ) {
+ $api = new OwDatabaseAPI;
+ $dc = null;
+ if ( isset( $options['dc'] ) ) {
+ $dc = $options['dc'];
+ }
+ $api->settings( 'omegawiki', $dc );
+ return $api->OmegaWiki->verifyColumn( $table, $column, $value,
$isDc );
+ }
+
+ /*! @} group OwDbAPIcomFn ends here.*/
+
/** @addtogroup OwDbAPIeFn OwDatabaseAPI's Expression functions
* @{
*/
@@ -236,6 +260,10 @@
$api = new OwDatabaseAPI;
$api->settings( 'definedMeaning', $dc );
return
$api->DefinedMeaning->getTranslatedContentIdDefinedMeaningId(
$translatedContentId, $options, $api->dc );
+ }
+
+ public static function verifyDefinedMeaningId( $definedMeaningId ) {
+ return self::verifyColumn( 'defined_meaning',
'defined_meaning_id', $definedMeaningId, 1 );
}
/*! @} group OwDbAPIdmFn ends here.*/
@@ -546,6 +574,8 @@
$this->Syntrans = new Syntrans; break;
case 'transaction': require_once( 'Transaction.php' );
$this->Transaction = new Transactions; break;
+ case 'omegawiki':
+ $this->OmegaWiki = new OmegaWikiDataBase; break;
}
}
diff --git a/OmegaWiki/WikiDataAPI.php b/OmegaWiki/WikiDataAPI.php
index 659a982..d220155 100644
--- a/OmegaWiki/WikiDataAPI.php
+++ b/OmegaWiki/WikiDataAPI.php
@@ -6,6 +6,43 @@
require_once( 'Transaction.php' );
require_once( 'WikiDataGlobals.php' );
+/** @brief non specific entity class that access the database directly
+ *
+ */
+class OmegaWikiDataBase {
+
+/**
+ * returns the value of column if exist
+ * null if not found
+ * @param table table name
+ * @param column column nane
+ * @param value column value
+ * @param isDc if has DataSet Context(boolean)
+ */
+ public static function verifyColumn( $table, $column, $value, $isDc ) {
+ if ( $isDc == 1 ) {
+ $dc = wdGetDataSetContext() . '_';
+ } else {
+ $dc = '';
+ }
+ $dbr = wfGetDB( DB_SLAVE );
+
+ $existId = $dbr->selectField(
+ "{$dc}{$table}",
+ $column,
+ array(
+ $column => $value,
+ "remove_transaction_id" => null
+ ), __METHOD__
+ );
+
+ if ( $existId ) {
+ return $existId;
+ }
+ return null;
+ }
+}
+
/** @brief returns an expression ( spelling/word )
*
* @param expressionId req'd int The expression id.
@@ -2551,34 +2588,13 @@
}
/**
- * returns the value of column if exist
- * null if not found
- * @param table table name
- * @param column column nane
- * @param value column value
- * @param isDc if has DataSet Context(boolean)
+ * @deprecated use OwDatabaseAPI::verifyColumn instead
+ *
+ * @todo refactor all functions that uses this function to the latest method.
~he
*/
function verifyColumn( $table, $column, $value, $isDc ) {
- if ( $isDc == 1 ) {
- $dc = wdGetDataSetContext() . '_';
- } else {
- $dc = '';
- }
- $dbr = wfGetDB( DB_SLAVE );
-
- $existId = $dbr->selectField(
- "{$dc}{$table}",
- $column,
- array(
- $column => $value,
- "remove_transaction_id" => null
- ), __METHOD__
- );
-
- if ( $existId ) {
- return $existId;
- }
- return null;
+ require_once( 'OmegaWikiDatabaseAPI.php' );
+ return OwDatabaseAPI::verifyColumn( $table, $column, $value, $isDc );
}
/**
@@ -2603,11 +2619,13 @@
}
/**
- * returns back the definedMeaningId if it exist
- * null if not found
+ * @deprecated use OwDatabaseAPI::verifyDefinedMeaningId instead
+ *
+ * @todo refactor all functions that uses this function to the latest method.
~he
*/
function verifyDefinedMeaningId( $definedMeaningId ) {
- return verifyColumn('defined_meaning', 'defined_meaning_id',
$definedMeaningId, 1 );
+ require_once( 'OmegaWikiDatabaseAPI.php' );
+ return OwDatabaseAPI::verifyDefinedMeaningId( $definedMeaningId );
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/251735
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I493cd004a242713c78c660aa13f7335f08888473
Gerrit-PatchSet: 18
Gerrit-Project: mediawiki/extensions/WikiLexicalData
Gerrit-Branch: master
Gerrit-Owner: Hiong3-eng5 <[email protected]>
Gerrit-Reviewer: Hiong3-eng5 <[email protected]>
Gerrit-Reviewer: Kipcool <[email protected]>
Gerrit-Reviewer: Purodha <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits