UltrasonicNXT has uploaded a new change for review.

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


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

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


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SocialProfile 
refs/changes/85/95785/1

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..d965004
--- /dev/null
+++ b/UserProfile/Avatar.magic.i18n.php
@@ -0,0 +1,7 @@
+<?php
+
+$magicWords = array();
+
+$magicWords['en'] = array(
+               'avatar' => array( 0, 'avatar' ),
+);
\ No newline at end of file
diff --git a/UserProfile/AvatarParserFunction.php 
b/UserProfile/AvatarParserFunction.php
new file mode 100644
index 0000000..cf7cb62
--- /dev/null
+++ b/UserProfile/AvatarParserFunction.php
@@ -0,0 +1,74 @@
+<?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;
+               
+               $user = User::newFromName( $username );
+               $id = $user->getId();
+               
+               $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
+               }
+               //get avatar file
+               $avatar = new wAvatar( $id, $size );
+               $filename = 
"{$wgUploadPath}/avatars/{$avatar->getAvatarImage()}";
+               
+               if ( $px ){ //if px value needed, set height to it
+                       $output = "<img src=\"{$filename}\" height=\"{$px}\" 
alt=\"avatar\" border=\"0\" />";
+               } else {    //but if not needed, don't
+                       $output = "<img src=\"{$filename}\" alt=\"avatar\" 
border=\"0\" />";
+               }
+       
+               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: newchange
Gerrit-Change-Id: I54257a4a142bff1ae2b5026d57f8cb29fff24ad3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SocialProfile
Gerrit-Branch: master
Gerrit-Owner: UltrasonicNXT <[email protected]>

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

Reply via email to