Legoktm has uploaded a new change for review.

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

Change subject: Extend Article instead of using a hook
......................................................................

Extend Article instead of using a hook

GlobalUserpage now extends Article, and consolidates all of the logic
that checks whether a page is enabled is in GlobalUserpage::isGlobal().

Misc. fixes:
* Created $wgGlobalUserpageDBname instead of using $wgSharedDB
* Set $wgDefaultUserOptions['globaluserpage'] = true;

Change-Id: I73ac36e6f9d3e4dfc21f518645bf8691bb6d9274
---
M GlobalUserpage.body.php
M GlobalUserpage.hooks.php
M GlobalUserpage.php
3 files changed, 126 insertions(+), 132 deletions(-)


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

diff --git a/GlobalUserpage.body.php b/GlobalUserpage.body.php
index 8c43632..fc9e84b 100644
--- a/GlobalUserpage.body.php
+++ b/GlobalUserpage.body.php
@@ -1,6 +1,98 @@
 <?php
 
-class GlobalUserpage {
+class GlobalUserpage extends Article {
+
+       public function showMissingArticle() {
+               $title = $this->getTitle();
+
+               if ( !self::isGlobal( $title ) ) {
+                       parent::showMissingArticle();
+                       return;
+               }
+
+               list( $html, ) = GlobalUserpage::getPagePlusFallbacks( 
$title->getPrefixedText() );
+               if ( $html === false ) {
+                       parent::showMissingArticle();
+                       return;
+               }
+               $out = $this->getContext()->getOutput();
+               $out->addHTML( $html );
+               $out->addModuleStyles( 'ext.GlobalUserpage' );
+       }
+
+       /**
+        * Checks whether the given page can be global
+        * doesn't check the actual database
+        * @param Title $title
+        * @return bool
+        */
+       protected static function canBeGlobal( Title $title ) {
+               global $wgGlobalUserpageDBname;
+               // Don't run this code for Hub.
+               if ( wfWikiID() === $wgGlobalUserpageDBname ) {
+                       return false;
+               }
+
+               // Must be a user page
+               if ( !$title->inNamespace( NS_USER ) ) {
+                       return false;
+               }
+
+               // Check it's a root user page
+               if ( $title->getRootText() !== $title->getText() ) {
+                       return false;
+               }
+
+               // Check valid username
+               if ( !User::newFromName( $title->getText() ) ) {
+                       return false;
+               }
+
+               return true;
+       }
+
+       /**
+        * Actually checks the database and user options
+        * @param Title $title
+        * @return bool
+        */
+       public static function isGlobal( Title $title ) {
+               global $wgGlobalUserpageDBname;
+
+               if ( !self::canBeGlobal( $title ) ) {
+                       return false;
+               }
+
+               $user = User::newFromName( $title->getText() );
+
+               if ( $user->getId() === 0 ) {
+                       return false;
+               }
+
+               if ( !$user->getOption( 'globaluserpage' ) ) {
+                       return false;
+               }
+
+               // Ewww, CentralAuth is terrible.
+               /*
+               if ( class_exists( 'CentralAuthUser' ) ) {
+                       $caUser = CentralAuthUser::getInstance( $user );
+                       if ( !$caUser->isAttached() || !$caUser->attachedOn( 
$wgGlobalUserpageDBname ) ) {
+                               return false;
+                       }
+               }*/
+
+               return self::exists( $user );
+       }
+
+       /**
+        * @param User $user
+        * @return bool
+        */
+       protected static function exists( User $user ) {
+               list( $text, ) = self::getPagePlusFallbacks( 
$user->getUserPage()->getPrefixedText() );
+               return $text !== false;
+       }
 
        /**
         * Makes an API request to the central wiki
diff --git a/GlobalUserpage.hooks.php b/GlobalUserpage.hooks.php
index 31c4f4f..063ec55 100644
--- a/GlobalUserpage.hooks.php
+++ b/GlobalUserpage.hooks.php
@@ -18,51 +18,14 @@
        }
 
        /**
-        * If the requested page doesn't exist, try to see if we can render a
-        * global user page instead.
-        *
-        * @param $article Article
+        * @param Title $title
+        * @param Article|null $page
+        * @param IContextSource $context
         * @return bool
         */
-       public static function onShowMissingArticle( $article ) {
-               global $wgDBname, $wgSharedDB;
-
-               // Don't run this code for Hub.
-               if ( $wgDBname == $wgSharedDB ) {
-                       return true;
-               }
-
-               $context = $article->getContext();
-               $output = $context->getOutput();
-               $title = $article->getTitle();
-
-               if ( $title->getNamespace() == NS_USER ) {
-                       // Try to construct a valid User object...
-                       $user = User::newFromName( $title->getText() );
-
-                       if ( !$user instanceof User ) {
-                               // Anon/nonexistent user/something else, so get 
out of here.
-                               return true;
-                       }
-
-                       $wantsGlobalUserpage = $user->getOption( 
'globaluserpage' );
-
-                       // If the user does *not* want a global userpage...get 
out of here.
-                       if ( !$wantsGlobalUserpage ) {
-                               return true;
-                       }
-
-                       // Alright, everything should be good now...
-                       list( $text, $oldid ) = 
GlobalUserpage::getPagePlusFallbacks( 'User:' . $title->getText() );
-                       if ( $text ) {
-                               // Add a notice indicating that it was taken 
from ShoutWiki Hub
-                               // ashley 23 December 2013: on a second though, 
don't.
-                               // Right now it just looks outright bad.
-                               //$output->addHTML( $context->msg( 
'globaluserpage-notice', $oldid )->parse() );
-                               $output->addHTML( $text );
-                               // Hide the "this page does not exist" notice 
and edit section links
-                               $output->addModuleStyles( 'ext.GlobalUserpage' 
);
-                       }
+       public static function onArticleFromTitle( Title &$title, &$page, 
$context ) {
+               if ( $title->inNamespace( NS_USER ) ) {
+                       $page = new GlobalUserpage( $title );
                }
 
                return true;
@@ -76,72 +39,28 @@
         * @return bool
         */
        public static function onSkinTemplateNavigationUniversal( &$sktemplate, 
&$links ) {
-               global $wgDBname, $wgSharedDB;
-
-               // Don't run this code for Hub.
-               if ( $wgDBname == $wgSharedDB ) {
-                       return true;
-               }
-
                $context = $sktemplate->getContext();
                $title = $sktemplate->getTitle();
 
-               if ( $title->getNamespace() == NS_USER ) {
-                       $user = User::newFromName( $title->getText() );
-
-                       if ( !$user instanceof User ) {
-                               // Anon/nonexistent user/something else, so get 
out of here.
-                               return true;
-                       }
-
-                       $wantsGlobalUserpage = $user->getOption( 
'globaluserpage' );
-
-                       // If the user does *not* want a global userpage...get 
out of here.
-                       if ( !$wantsGlobalUserpage ) {
-                               return true;
-                       }
-
-                       list( $text, $oldid ) = 
GlobalUserpage::getPagePlusFallbacks( 'User:' . $title->getText() );
-                       if ( $text ) {
-                               $links['namespaces']['user']['href'] = 
$title->getFullURL();
-                               $links['namespaces']['user']['class'] = 
'selected';
-                               //$links['namespaces']['user_talk']['class'] = 
'';
-                               //$links['namespaces']['user_talk']['href'] = 
'http://www.shoutwiki.com/wiki/User_talk:' . $title->getText();
-                               /*
-                               $links['views'] = array(); // Kill the 'Create' 
button @todo make this suck less
-                               $links['views'][] = array(
-                                       'class' => false,
-                                       'text' => $context->msg( 
'globaluserpage-edit-tab' ),
-                                       'href' => wfAppendQuery(
-                                               
'http://www.shoutwiki.com/w/index.php',
-                                               array(
-                                                       'action' => 'edit',
-                                                       'title' => 
$title->getPrefixedText()
-                                               )
+               if ( GlobalUserpage::isGlobal( $title ) ) {
+                       $links['namespaces']['user']['href'] = 
$title->getFullURL();
+                       $links['namespaces']['user']['class'] = 'selected';
+                       //$links['namespaces']['user_talk']['class'] = '';
+                       //$links['namespaces']['user_talk']['href'] = 
'http://www.shoutwiki.com/wiki/User_talk:' . $title->getText();
+                       /*
+                       $links['views'] = array(); // Kill the 'Create' button 
@todo make this suck less
+                       $links['views'][] = array(
+                               'class' => false,
+                               'text' => $context->msg( 
'globaluserpage-edit-tab' ),
+                               'href' => wfAppendQuery(
+                                       'http://www.shoutwiki.com/w/index.php',
+                                       array(
+                                               'action' => 'edit',
+                                               'title' => 
$title->getPrefixedText()
                                        )
-                               );
-                               */
-                       }
-               }
-
-               // When we are on the person's user talk page, we still need to 
pretend
-               // that their user page exists.
-               if ( $title->getNamespace() == NS_USER_TALK ) {
-                       $user = User::newFromName( 
$title->getSubjectPage()->getText() );
-
-                       if ( !$user instanceof User ) {
-                               // Anon/nonexistent user/something else, so get 
out of here.
-                               return true;
-                       }
-
-                       $wantsGlobalUserpage = $user->getOption( 
'globaluserpage' );
-
-                       // Perform special processing for the user tab (namely 
turn it into
-                       // blue if it were normally red), but only if we have 
to do that.
-                       if ( $wantsGlobalUserpage ) {
-                               $links['namespaces']['user']['href'] = 
$title->getSubjectPage()->getFullURL();
-                               $links['namespaces']['user']['class'] = ''; // 
remove redness
-                       }
+                               )
+                       );
+                       */
                }
 
                return true;
@@ -176,31 +95,7 @@
         * @return Boolean
         */
        public static function brokenLink( $linker, $target, &$text, 
&$customAttribs, &$query, &$options, &$ret ) {
-               global $wgDBname, $wgSharedDB;
-
-               // Don't run this code for Hub.
-               if ( $wgDBname == $wgSharedDB ) {
-                       return true;
-               }
-
-               if ( $target->getNamespace() == NS_USER ) {
-                       $user = User::newFromName( $target->getText() );
-
-                       if ( !$user instanceof User ) {
-                               // Anon/nonexistent user/something else, so get 
out of here.
-                               return true;
-                       }
-
-                       $wantsGlobalUserpage = $user->getOption( 
'globaluserpage' );
-
-                       // If the user does *not* want a global userpage...get 
out of here.
-                       if ( !$wantsGlobalUserpage ) {
-                               return true;
-                       }
-
-                       // return immediately if we know it's real
-                       // this part "borrowed" from ^demon's RemoveRedlinks, 
dunno if
-                       // we really need it anymore, but idk
+               if ( GlobalUserpage::isGlobal( $target ) ) {
                        if ( in_array( 'known', $options ) || 
$target->isKnown() ) {
                                return true;
                        } else {
diff --git a/GlobalUserpage.php b/GlobalUserpage.php
index f11b173..8f30f90 100644
--- a/GlobalUserpage.php
+++ b/GlobalUserpage.php
@@ -34,6 +34,11 @@
  */
 $wgGlobalUserpageAPIUrl = 'http://www.shoutwiki.com/w/api.php';
 
+/**
+ * Database name of the central wiki
+ */
+$wgGlobalUserpageDBname = 'shoutwiki';
+
 // Extension credits that will show up on Special:Version
 $wgExtensionCredits['other'][] = array(
        'path' => __FILE__,
@@ -49,14 +54,16 @@
 $wgAutoloadClasses['GlobalUserpage'] = $dir . '/GlobalUserpage.body.php';
 $wgAutoloadClasses['GlobalUserpageHooks'] = $dir . '/GlobalUserpage.hooks.php';
 
+$wgDefaultUserOptions['globaluserpage'] = true;
+
 // i18n
 $wgMessagesDirs['GlobalUserpage'] = __DIR__ . '/i18n';
 
 // Hooks, a.k.a the beef of this extension
 $wgHooks['GetPreferences'][] = 'GlobalUserpageHooks::onGetPreferences';
-$wgHooks['ShowMissingArticle'][] = 'GlobalUserpageHooks::onShowMissingArticle';
 $wgHooks['SkinTemplateNavigation::Universal'][] = 
'GlobalUserpageHooks::onSkinTemplateNavigationUniversal';
 $wgHooks['LinkBegin'][] = 'GlobalUserpageHooks::brokenLink';
+$wgHooks['ArticleFromTitle'][] = 'GlobalUserpageHooks::onArticleFromTitle';
 
 // Register the CSS as a module with ResourceLoader
 $wgResourceModules['ext.GlobalUserpage'] = array(

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I73ac36e6f9d3e4dfc21f518645bf8691bb6d9274
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GlobalUserPage
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to