Florianschmidtwelzow has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/226105

Change subject: WIP: Populate langlinks in langlinks table
......................................................................

WIP: Populate langlinks in langlinks table

MobileFrontend uses the langlinks prop of the query api to get a machine
readable list of all language codes + links in which a page is already
translated. Unfortunately this doesn't work with the Translate extension,
so far. Therefor, on mobile in the worst case you see on small screens
a huge list of language links before you see anything of the really
interesting content.

This change aims to populate the language subpage links of the Translate
extension into the langlinks table for each page (source language page and
all translation subpages).

This is done with the LinksUpdate hook, which is called everytime the page
links get's updated (mostly when you save a page or use purge or something that
triggers a reparsing of the page). The hook handler basically does the same
as the languages parser function, but without any styling. The resulting array
will be used from LinksUpdate to get a list of languages to save into
the langlinks database table for that page.

If a translation page is deleted, the entry in all other pages is removed after
the next reparsing of each individual page (basically that's the same as needed
for the lang block generated by the languages parser hook).

ToDo:
 * Update langlinks of all other pages of the translated page (that's already 
done for
   the langblock of the languages hook)

Bug: T106361
Change-Id: I0ff0737ecb6e7c03e870fda095a75aec8e5d8c6d
---
M TranslateHooks.php
M tag/PageTranslationHooks.php
2 files changed, 69 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate 
refs/changes/05/226105/1

diff --git a/TranslateHooks.php b/TranslateHooks.php
index 8356af5..b83110c 100644
--- a/TranslateHooks.php
+++ b/TranslateHooks.php
@@ -117,7 +117,10 @@
                                
'PageTranslationHooks::updateTranstagOnNullRevisions';
 
                        // Register \<languages/>
-                       $wgHooks['ParserFirstCallInit'][] = 
'TranslateHooks::setupParserHooks';
+                       $wgHooks['ParserFirstCallInit'][] = 
'TranslateHooks::setupParserHooks'; 
+
+                       // Populate to langlinks table
+                       $wgHooks['LinksUpdate'][] = 
'PageTranslationHooks::onLinksUpdate';
 
                        // Strip \<translate> tags etc. from source pages when 
rendering
                        $wgHooks['ParserBeforeStrip'][] = 
'PageTranslationHooks::renderTagPage';
diff --git a/tag/PageTranslationHooks.php b/tag/PageTranslationHooks.php
index 44350d0..ccb7e07 100644
--- a/tag/PageTranslationHooks.php
+++ b/tag/PageTranslationHooks.php
@@ -174,6 +174,44 @@
        }
 
        /**
+        * Creates a new TranslatablePage object and checks, if it is the 
source language
+        * or not.
+        * @return TranslatablePage|bool Returns the object, if this isn't the 
language source page,
+        *  false otherwise.
+        */
+       private static function getTranslateablePage( Title $title ) {
+               // Check if this is a source page or a translation page
+               $page = TranslatablePage::newFromTitle( $title );
+               if ( $page->getMarkedTag() === false ) {
+                       $page = TranslatablePage::isTranslationPage( $title );
+               }
+
+               if ( $page === false || $page->getMarkedTag() === false ) {
+                       return false;
+               }
+               return $page;
+       }
+
+       /**
+        * Returns a new created Title object of the subpage, which contains 
the translated content
+        * for the given language code.
+        * @param Title $title Title object of the base (source language) page
+        * @param String $code The language code
+        * @return Title The Title object of the subpage
+        */
+       private static function getSubPage( Title $title, $code ) {
+               // Should call $page->getMessageGroup()->getSourceLanguage(), 
but
+               // group is sometimes null on WMF during page moves, reason 
unknown.
+               // This should do the same thing for now.
+               $sourceLanguage = $title->getPageLanguage()->getCode();
+               $suffix = ( $code === $sourceLanguage ) ? '' : "/$code";
+               $targetTitleString = $title->getDBkey() . $suffix;
+               $subpage = Title::makeTitle( $title->getNamespace(), 
$targetTitleString );
+
+               return $subpage;
+       }
+
+       /**
         * @param $data
         * @param $params
         * @param $parser Parser
@@ -182,13 +220,8 @@
        public static function languages( $data, $params, $parser ) {
                $currentTitle = $parser->getTitle();
 
-               // Check if this is a source page or a translation page
-               $page = TranslatablePage::newFromTitle( $currentTitle );
-               if ( $page->getMarkedTag() === false ) {
-                       $page = TranslatablePage::isTranslationPage( 
$currentTitle );
-               }
-
-               if ( $page === false || $page->getMarkedTag() === false ) {
+               $page = self::getTranslateablePage( $currentTitle );
+               if ( !$page ) {
                        return '';
                }
 
@@ -228,10 +261,6 @@
                // This way the parser knows to fragment the parser cache by 
language code
                $userLangCode = $parser->getOptions()->getUserLang();
                $userLangDir = 
$parser->getOptions()->getUserLangObj()->getDir();
-               // Should call $page->getMessageGroup()->getSourceLanguage(), 
but
-               // group is sometimes null on WMF during page moves, reason 
unknown.
-               // This should do the same thing for now.
-               $sourceLanguage = $pageTitle->getPageLanguage()->getCode();
 
                $languages = array();
                foreach ( $status as $code => $percent ) {
@@ -240,9 +269,7 @@
                        $name = htmlspecialchars( $name ); // Unlikely, but 
better safe
 
                        // Add links to other languages
-                       $suffix = ( $code === $sourceLanguage ) ? '' : "/$code";
-                       $targetTitleString = $pageTitle->getDBkey() . $suffix;
-                       $subpage = Title::makeTitle( 
$pageTitle->getNamespace(), $targetTitleString );
+                       $subpage = self::getSubPage( $pageTitle, $code );
 
                        $classes = array();
                        if ( $code === $userLangCode ) {
@@ -964,4 +991,28 @@
                        }
                }
        }
+
+       /**
+        * Populate page translations to langlinks table
+        * @param LinksUpdate $lu The LinksUpdate object for this page
+        * @return bool
+        */
+       public static function onLinksUpdate( LinksUpdate $lu ) {
+               // check, if the langlinks should be updated or not
+               $page = $page = self::getTranslateablePage( $lu->getTitle() );
+               if ( !$page ||
+                       !$status = $page->getTranslationPercentages() ) {
+                       return true;
+               }
+
+               // Fix title
+               $pageTitle = $page->getTitle();
+
+               // update the language links in LinksUpdate
+               foreach ( $status as $code => $per ) {
+                       $subPage = self::getSubPage( $pageTitle, $code );
+                       $lu->mInterlangs[$code] = $subPage->getPrefixedText();
+               }
+               return true;
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0ff0737ecb6e7c03e870fda095a75aec8e5d8c6d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com>

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

Reply via email to