Jack Phoenix has submitted this change and it was merged.

Change subject: Create {{#avatar:Username}} parser function
......................................................................


Create {{#avatar:Username}} parser function

Allowing size as a second parameter, giving a size code (s/m/ml/l), or a
px value, with the parser function selecting the appropriate size avatar
to load for the given px value

Change the way getAvatarURL() constructs the <img>, using HTML::element,
and allowing other parameters to be passed in.

I'll notice all these style errors on my own someday!

Bug: 34008
Change-Id: I54257a4a142bff1ae2b5026d57f8cb29fff24ad3
---
M SocialProfile.php
A UserProfile/Avatar.magic.i18n.php
M UserProfile/AvatarClass.php
A UserProfile/AvatarParserFunction.php
4 files changed, 117 insertions(+), 3 deletions(-)

Approvals:
  Jack Phoenix: Verified; Looks good to me, approved



diff --git a/SocialProfile.php b/SocialProfile.php
index 049b036..917cef1 100644
--- a/SocialProfile.php
+++ b/SocialProfile.php
@@ -33,6 +33,8 @@
 $wgExtensionMessagesFiles['SocialProfileNamespaces'] = $dir . 
'SocialProfile.namespaces.php';
 $wgExtensionMessagesFiles['SocialProfileAlias'] = $dir . 
'SocialProfile.alias.php';
 
+$wgExtensionMessagesFiles['AvatarMagic'] = $dir . 
'UserProfile/Avatar.magic.i18n.php';
+
 // Classes to be autoloaded
 $wgAutoloadClasses['GenerateTopUsersReport'] = $dir . 
'UserStats/GenerateTopUsersReport.php';
 
@@ -61,6 +63,7 @@
 $wgAutoloadClasses['TopFansRecent'] = $dir . 'UserStats/TopFansRecent.php';
 $wgAutoloadClasses['TopUsersPoints'] = $dir . 'UserStats/TopUsers.php';
 $wgAutoloadClasses['wAvatar'] = $dir . 'UserProfile/AvatarClass.php';
+$wgAutoloadClasses['AvatarParserFunction'] = $dir . 
'UserProfile/AvatarParserFunction.php';
 
 // New special pages
 $wgSpecialPages['AddRelationship'] = 'SpecialAddRelationship';
@@ -197,6 +200,13 @@
        'url' => 'https://www.mediawiki.org/wiki/Extension:SocialProfile',
        'description' => 'A special page for viewing all relationships by type',
 );
+$wgExtensionCredits['parserhook'][] = array(
+       'path' => __FILE__,
+       'name' => 'Avatar',
+       'author' => 'Adam Carter',
+       'url' => 'https://www.mediawiki.org/wiki/Extension:SocialProfile',
+       'description' => 'A parser function to get the avatar of a given user',
+);
 
 // Hooked functions
 // This has to be either here or even earlier on because the loader files mess
@@ -211,6 +221,8 @@
 
 $wgHooks['CanonicalNamespaces'][] = 
'SocialProfileHooks::onCanonicalNamespaces';
 $wgHooks['LoadExtensionSchemaUpdates'][] = 
'SocialProfileHooks::onLoadExtensionSchemaUpdates';
+$wgHooks['ParserFirstCallInit'][] = 
'AvatarParserFunction::setupAvatarParserFunction';
+
 // For the Renameuser extension
 //$wgHooks['RenameUserSQL'][] = 'SocialProfileHooks::onRenameUserSQL';
 
diff --git a/UserProfile/Avatar.magic.i18n.php 
b/UserProfile/Avatar.magic.i18n.php
new file mode 100644
index 0000000..a4279b3
--- /dev/null
+++ b/UserProfile/Avatar.magic.i18n.php
@@ -0,0 +1,13 @@
+<?php
+/**
+ * Internationalization file for the {{#avatar}} parser function.
+ *
+ * @file
+ */
+
+$magicWords = array();
+
+/** English */
+$magicWords['en'] = array(
+       'avatar' => array( 0, 'avatar' ),
+);
\ No newline at end of file
diff --git a/UserProfile/AvatarClass.php b/UserProfile/AvatarClass.php
index 900958e..08ffba5 100644
--- a/UserProfile/AvatarClass.php
+++ b/UserProfile/AvatarClass.php
@@ -54,9 +54,20 @@
                return $avatar_filename;
        }
 
-       /** @return String: <img> HTML tag with full path to the avatar image */
-       function getAvatarURL() {
+       /** 
+        * @param Array $extraParams: array of extra parameters to give to the 
image
+        * @return String: <img> HTML tag with full path to the avatar image 
+        * */
+       function getAvatarURL( $extraParams = array() ) {
                global $wgUploadPath;
-               return "<img 
src=\"{$wgUploadPath}/avatars/{$this->getAvatarImage()}\" alt=\"avatar\" 
border=\"0\" />";
+
+               $defaultParams = array(
+                       'src' => 
"{$wgUploadPath}/avatars/{$this->getAvatarImage()}",
+                       'alt' => 'avatar',
+                       'border' => '0',
+               );
+               $params = array_merge( $extraParams, $defaultParams );
+
+               return Html::element( 'img', $params, '' );
        }
 }
diff --git a/UserProfile/AvatarParserFunction.php 
b/UserProfile/AvatarParserFunction.php
new file mode 100644
index 0000000..fee0f7f
--- /dev/null
+++ b/UserProfile/AvatarParserFunction.php
@@ -0,0 +1,78 @@
+<?php
+
+class AvatarParserFunction {
+
+       /**
+        * Setup function for the {{#avatar:Username}} function
+        *
+        * @param Parser $parser: MW parser object
+        * @return boolean
+        */
+       static function setupAvatarParserFunction( &$parser ) {
+               $parser->setFunctionHook( 'avatar', 
'AvatarParserFunction::renderAvatarParserFunction' );
+
+               return true;
+       }
+
+       /**
+        * Function to render the {{#avatar:Username}} function
+        *
+        * @param Parser $parser: MW parser object
+        * @param string $username: Username of user to show avatar for
+        * @param string $size: Size of avatar to return (s/m/ml/l), or px 
value (100px, 10px, etc)
+        * @return array: output of function, and options for the parser
+        */
+       static function renderAvatarParserFunction( $parser, $username = '', 
$givenSize = 'm' ) {
+               global $wgUploadPath;
+
+               $sizes = array( 's', 'm', 'ml', 'l' );
+
+               if ( in_array( $givenSize, $sizes ) ) { // if given size is a 
code,
+                       $size = $givenSize;                                // 
use code,
+                       $px = '';                                               
   // and leave px value empty
+               } elseif ( substr( $givenSize, -2 ) == 'px' ) { //given size is 
a value in px
+                       $givenPx = intval( substr( $givenSize, 0, strlen( 
$givenSize ) - 2 ) ); //get int value of given px size
+
+                       if ( !is_int( $givenPx ) ) { // if px value is not int
+                               $size = 'm';                     // give 
default avatar
+                               $px = '';                                // 
with no px value
+                       }
+
+                       if ( $givenPx <= 16 ) { // if given px value is smaller 
than small,
+                               $size = 's';       // use the small avatar,
+                               $px = $givenSize;  // and the given px value
+                       } elseif ( $givenPx <= 30 ) { // if given px value is 
smaller than medium,
+                               $size = 'm';                     // use the 
medium avatar,
+                               $px = $givenSize;                // and the 
given px value
+                       } elseif ( $givenPx <= 50 ) { // if given px value is 
smaller than medium-large,
+                               $size = 'ml';                    // use the 
medium-large avatar,
+                               $px = $givenSize;                // and the 
given px value
+                       } else {                          // if given px value 
is bigger then medium large,
+                               $size = 'l';      // use the large avatar,
+                               $px = $givenSize; // and the given px value
+                       }
+               } else { // size value is not code or px
+                       $size = 'm'; // give default avatar
+                       $px = '';        // with no px value
+               }
+
+               $user = User::newFromName( $username );
+               if ( $user instanceof User ) {
+                       $id = $user->getId();
+                       $avatar = new wAvatar( $id, $size );
+               } else {
+                       // Fallback for the case where an invalid (nonexistent) 
user name
+                       // was supplied...
+                       $avatar = new wAvatar( -1 , 'm' ); // not very nice, 
but -1 will get the default avatar
+               }
+
+               if ( $px ) { // if px value needed, set height to it
+                       $output = $avatar->getAvatarURL( array( 'height' => $px 
) );
+               } else { // but if not needed, don't
+                       $output = $avatar->getAvatarURL();
+               }
+
+               return array( $output, 'noparse' => true, 'isHTML' => true );
+       }
+
+}
\ No newline at end of file

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I54257a4a142bff1ae2b5026d57f8cb29fff24ad3
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/SocialProfile
Gerrit-Branch: master
Gerrit-Owner: UltrasonicNXT <[email protected]>
Gerrit-Reviewer: Jack Phoenix <[email protected]>
Gerrit-Reviewer: Raimond Spekking <[email protected]>
Gerrit-Reviewer: UltrasonicNXT <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to