Legoktm has uploaded a new change for review.

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

Change subject: Subclass WikiPage and implement remote content functions
......................................................................

Subclass WikiPage and implement remote content functions

This makes tabs like those on foreign file repos show up, as well as
signalling to other code that the content is remote.

Bug: T94126
Change-Id: Ic792120a06a0bc657577c5e94f3b0c4c3e454156
Depends-On: Ib3d7dcbefe95da351872e63f306799eef83e00a7
---
M GlobalUserPage.body.php
M GlobalUserPage.hooks.php
A GlobalUserPagePage.php
M extension.json
4 files changed, 150 insertions(+), 82 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GlobalUserPage 
refs/changes/39/316039/1

diff --git a/GlobalUserPage.body.php b/GlobalUserPage.body.php
index 419a74c..1538cdd 100644
--- a/GlobalUserPage.body.php
+++ b/GlobalUserPage.body.php
@@ -30,9 +30,9 @@
 
        public function __construct( Title $title, Config $config ) {
                global $wgMemc;
-               parent::__construct( $title );
                $this->config = $config;
                $this->cache = $wgMemc;
+               parent::__construct( $title );
        }
 
        public function showMissingArticle() {
@@ -61,7 +61,7 @@
                if ( $footerKey ) {
                        $out->addHTML( '<div class="mw-globaluserpage-footer 
plainlinks">' .
                                "\n" . $out->msg( $footerKey )
-                                       ->params( $this->getUsername(), 
$this->getRemoteURL() )->parse() .
+                                       ->params( $this->getUsername(), 
$this->mPage->getSourceURL() )->parse() .
                                "\n</div>"
                        );
                }
@@ -167,7 +167,7 @@
 
                global $wgGlobalUserPageDBname;
                $lb = wfGetLB( $wgGlobalUserPageDBname );
-               $dbr = $lb->getConnection( DB_SLAVE, array(), 
$wgGlobalUserPageDBname );
+               $dbr = $lb->getConnectionRef( DB_REPLICA, array(), 
$wgGlobalUserPageDBname );
                $row = $dbr->selectRow(
                        [ 'page', 'page_props' ],
                        [ 'page_touched', 'pp_propname' ],
@@ -190,7 +190,6 @@
                } else {
                        $touched = false;
                }
-               $lb->reuseConnection( $dbr );
 
                self::$touchedCache->set( $user->getName(), $touched );
 
@@ -223,7 +222,7 @@
         * @return string
         */
        public function getUsername() {
-               return $this->getTitle()->getText();
+               return $this->mPage->getUsername();
        }
 
        /**
@@ -279,81 +278,11 @@
        }
 
        /**
-        * Makes an API request to the central wiki
-        *
-        * @param $params array
-        * @return array|bool false if the request failed
+        * @param Title $title
+        * @return GlobalUserPagePage
         */
-       protected function makeAPIRequest( $params ) {
-               $params['format'] = 'json';
-               $url = wfAppendQuery( $this->config->get( 
'GlobalUserPageAPIUrl' ), $params );
-               wfDebugLog( 'GlobalUserPage', "Making a request to $url" );
-               $req = MWHttpRequest::factory(
-                       $url,
-                       array( 'timeout' => $this->config->get( 
'GlobalUserPageTimeout' ) )
-               );
-               $status = $req->execute();
-               if ( !$status->isOK() ) {
-                       wfDebugLog( 'GlobalUserPage', __METHOD__ . " Error: 
{$status->getWikitext()}" );
-                       return false;
-               }
-               $json = $req->getContent();
-               $decoded = FormatJson::decode( $json, true );
-               return $decoded;
-       }
-
-       /**
-        * Returns a URL to the user page on the central wiki,
-        * attempts to use SiteConfiguration if possible, else
-        * falls back to using an API request
-        *
-        * @return string
-        */
-       protected function getRemoteURL() {
-               $url = WikiMap::getForeignURL(
-                       $this->config->get( 'GlobalUserPageDBname' ),
-                       'User:' . $this->getUsername()
-               );
-
-               if ( $url !== false ) {
-                       return $url;
-               } else {
-                       // Fallback to the API
-                       return $this->getRemoteURLFromAPI();
-               }
-       }
-
-       /**
-        * Returns a URL to the user page on the central wiki;
-        * if MW >= 1.24, this will be the cannonical url, otherwise
-        * it will be using whatever protocol was specified in
-        * $wgGlobalUserPageAPIUrl.
-        *
-        * @return string
-        */
-       protected function getRemoteURLFromAPI() {
-               $key = 'globaluserpage:url:' . md5( $this->getUsername() );
-               $data = $this->cache->get( $key );
-               if ( $data === false ) {
-                       $params = array(
-                               'action' => 'query',
-                               'titles' => 'User:' . $this->getUsername(),
-                               'prop' => 'info',
-                               'inprop' => 'url',
-                               'formatversion' => '2',
-                       );
-                       $resp = $this->makeAPIRequest( $params );
-                       if ( $resp === false ) {
-                               // Don't cache upon failure
-                               return '';
-                       }
-                       $data = $resp['query']['pages'][0]['canonicalurl'];
-                       // Don't set an expiry since we expect people not to 
change the
-                       // url to their wiki without clearing their caches!
-                       $this->cache->set( $key, $data );
-               }
-
-               return $data;
+       public function newPage( Title $title ) {
+               return new GlobalUserPagePage( $title, $this->config );
        }
 
        /**
@@ -361,7 +290,7 @@
         *
         * @param Title $title
         * @param string $langCode
-        * @return array
+        * @return array|bool
         */
        protected function parseWikiText( Title $title, $langCode ) {
                $unLocalizedName = MWNamespace::getCanonicalName( NS_USER ) . 
':' . $title->getText();
@@ -376,7 +305,7 @@
                        'prop' => 'text|modules|jsconfigvars',
                        'formatversion' => 2
                );
-               $data = $this->makeAPIRequest( $params );
+               $data = $this->mPage->makeAPIRequest( $params );
                return $data !== false ? $data['parse'] : false;
        }
 
diff --git a/GlobalUserPage.hooks.php b/GlobalUserPage.hooks.php
index 6883808..27d524b 100644
--- a/GlobalUserPage.hooks.php
+++ b/GlobalUserPage.hooks.php
@@ -139,4 +139,21 @@
        public static function onGetDoubleUnderscoreIDs( array &$ids ) {
                $ids[] = 'noglobal';
        }
+
+       /**
+        * @param Title $title
+        * @param $page
+        * @return bool
+        */
+       public static function onWikiPageFactory( Title $title, &$page ) {
+               if ( GlobalUserPage::shouldDisplayGlobalPage( $title ) ) {
+                       $page = new GlobalUserPagePage(
+                               $title,
+                               
ConfigFactory::getDefaultInstance()->makeConfig( 'globaluserpage' )
+                       );
+                       return false;
+               }
+
+               return true;
+       }
 }
diff --git a/GlobalUserPagePage.php b/GlobalUserPagePage.php
new file mode 100644
index 0000000..cd3c863
--- /dev/null
+++ b/GlobalUserPagePage.php
@@ -0,0 +1,120 @@
+<?php
+
+class GlobalUserPagePage extends WikiPage {
+
+       /**
+        * @var Config
+        */
+       private $config;
+
+       /**
+        * @var BagOStuff
+        */
+       private $cache;
+
+       public function __construct( Title $title, Config $config ) {
+               global $wgMemc;
+               parent::__construct( $title );
+               $this->config = $config;
+               $this->cache = $wgMemc;
+       }
+
+       public function isLocal() {
+               return $this->getTitle()->exists();
+       }
+
+       /**
+        * @return string
+        */
+       public function getWikiDisplayName() {
+               $url = $this->getSourceURL();
+               return wfParseUrl( $url )['host'];
+       }
+
+       /**
+        * Username for the given global user page
+        *
+        * @return string
+        */
+       public function getUsername() {
+               return $this->getTitle()->getText();
+       }
+
+       /**
+        * Returns a URL to the user page on the central wiki,
+        * attempts to use SiteConfiguration if possible, else
+        * falls back to using an API request
+        *
+        * @return string
+        */
+       public function getSourceURL() {
+               $url = WikiMap::getForeignURL(
+                       $this->config->get( 'GlobalUserPageDBname' ),
+                       'User:' . $this->getUsername()
+               );
+
+               if ( $url !== false ) {
+                       return $url;
+               } else {
+                       // Fallback to the API
+                       return $this->getRemoteURLFromAPI();
+               }
+       }
+
+       /**
+        * Returns a URL to the user page on the central wiki;
+        * if MW >= 1.24, this will be the cannonical url, otherwise
+        * it will be using whatever protocol was specified in
+        * $wgGlobalUserPageAPIUrl.
+        *
+        * @return string
+        */
+       protected function getRemoteURLFromAPI() {
+               $key = 'globaluserpage:url:' . md5( $this->getUsername() );
+               $data = $this->cache->get( $key );
+               if ( $data === false ) {
+                       $params = array(
+                               'action' => 'query',
+                               'titles' => 'User:' . $this->getUsername(),
+                               'prop' => 'info',
+                               'inprop' => 'url',
+                               'formatversion' => '2',
+                       );
+                       $resp = $this->makeAPIRequest( $params );
+                       if ( $resp === false ) {
+                               // Don't cache upon failure
+                               return '';
+                       }
+                       $data = $resp['query']['pages'][0]['canonicalurl'];
+                       // Don't set an expiry since we expect people not to 
change the
+                       // url to their wiki without clearing their caches!
+                       $this->cache->set( $key, $data );
+               }
+
+               return $data;
+       }
+
+       /**
+        * Makes an API request to the central wiki
+        *
+        * @param $params array
+        * @return array|bool false if the request failed
+        */
+       public function makeAPIRequest( $params ) {
+               $params['format'] = 'json';
+               $url = wfAppendQuery( $this->config->get( 
'GlobalUserPageAPIUrl' ), $params );
+               wfDebugLog( 'GlobalUserPage', "Making a request to $url" );
+               $req = MWHttpRequest::factory(
+                       $url,
+                       array( 'timeout' => $this->config->get( 
'GlobalUserPageTimeout' ) )
+               );
+               $status = $req->execute();
+               if ( !$status->isOK() ) {
+                       wfDebugLog( 'GlobalUserPage', __METHOD__ . " Error: 
{$status->getWikitext()}" );
+                       return false;
+               }
+               $json = $req->getContent();
+               $decoded = FormatJson::decode( $json, true );
+               return $decoded;
+       }
+}
diff --git a/extension.json b/extension.json
index 1fc2d0a..8e55626 100644
--- a/extension.json
+++ b/extension.json
@@ -42,7 +42,8 @@
                "TitleGetEditNotices": [
                        "GlobalUserPageHooks::onTitleGetEditNotices"
                ],
-               "GetDoubleUnderscoreIDs": 
"GlobalUserPageHooks::onGetDoubleUnderscoreIDs"
+               "GetDoubleUnderscoreIDs": 
"GlobalUserPageHooks::onGetDoubleUnderscoreIDs",
+               "WikiPageFactory": "GlobalUserPageHooks::onWikiPageFactory"
        },
        "config": {
                "GlobalUserPageCacheExpiry": 604800,
@@ -70,6 +71,7 @@
        },
        "AutoloadClasses": {
                "GlobalUserPage": "GlobalUserPage.body.php",
+               "GlobalUserPagePage": "GlobalUserPagePage.php",
                "GlobalUserPageHooks": "GlobalUserPage.hooks.php",
                "GlobalUserPageCacheInvalidator": 
"GlobalUserPageCacheInvalidator.php",
                "GlobalUserPageLocalJobSubmitJob": 
"GlobalUserPageLocalJobSubmitJob.php",

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic792120a06a0bc657577c5e94f3b0c4c3e454156
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GlobalUserPage
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

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

Reply via email to