Source for file users.php

Documentation is available at users.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  MediaWiki
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * MediaWiki API Users class for the Joomla Platform.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  MediaWiki
  17.  * @since       12.3
  18.  */
  19. {
  20.     /**
  21.      * Method to login and get authentication tokens.
  22.      *
  23.      * @param   string  $lgname      User Name.
  24.      * @param   string  $lgpassword  Password.
  25.      * @param   string  $lgdomain    Domain (optional).
  26.      *
  27.      * @return  object 
  28.      *
  29.      * @since   12.3
  30.      */
  31.     public function login($lgname$lgpassword$lgdomain null)
  32.     {
  33.         // Build the request path.
  34.         $path '?action=login&lgname=' $lgname '&lgpassword=' $lgpassword;
  35.  
  36.         if (isset($lgdomain))
  37.         {
  38.             $path .= '&lgdomain=' $lgdomain;
  39.         }
  40.  
  41.         // Send the request.
  42.         $response $this->client->post($this->fetchUrl($path)null);
  43.  
  44.         // Request path with login token.
  45.         $path '?action=login&lgname=' $lgname '&lgpassword=' $lgpassword '&lgtoken=' $this->validateResponse($response)->login['token'];
  46.  
  47.         if (isset($lgdomain))
  48.         {
  49.             $path .= '&lgdomain=' $lgdomain;
  50.         }
  51.  
  52.         // Set the session cookies returned.
  53.         $headers = (array) $this->options->get('headers');
  54.         $headers['Cookie'!empty($headers['Cookie']empty($headers['Cookie']'';
  55.         $headers['Cookie'$headers['Cookie'$response->headers['Set-Cookie'];
  56.         $this->options->set('headers'$headers);
  57.  
  58.         // Send the request again with the token.
  59.         $response $this->client->post($this->fetchUrl($path)null);
  60.         $response_body $this->validateResponse($response);
  61.  
  62.         $headers = (array) $this->options->get('headers');
  63.         $cookie_prefix $response_body->login['cookieprefix'];
  64.         $cookie $cookie_prefix 'UserID=' $response_body->login['lguserid''; ' $cookie_prefix
  65.             . 'UserName=' $response_body->login['lgusername'];
  66.         $headers['Cookie'$headers['Cookie''; ' $response->headers['Set-Cookie''; ' $cookie;
  67.         $this->options->set('headers'$headers);
  68.  
  69.         return $this->validateResponse($response);
  70.     }
  71.  
  72.     /**
  73.      * Method to logout and clear session data.
  74.      *
  75.      * @return  object 
  76.      *
  77.      * @since   12.3
  78.      */
  79.     public function logout()
  80.     {
  81.         // Build the request path.
  82.         $path '?action=login';
  83.  
  84.         // @TODO clear internal data as well
  85.  
  86.         // Send the request.
  87.         $response $this->client->get($this->fetchUrl($path));
  88.  
  89.         return $this->validateResponse($response);
  90.     }
  91.  
  92.     /**
  93.      * Method to get user information.
  94.      *
  95.      * @param   array  $ususers  A list of users to obtain the same information for.
  96.      * @param   array  $usprop   What pieces of information to include.
  97.      *
  98.      * @return  object 
  99.      *
  100.      * @since   12.3
  101.      */
  102.     public function getUserInfo(array $ususersarray $usprop null)
  103.     {
  104.         // Build the request path.
  105.         $path '?action=query&list=users';
  106.  
  107.         // Append users to the request.
  108.         $path .= '&ususers=' $this->buildParameter($ususers);
  109.  
  110.         if (isset($usprop))
  111.         {
  112.             $path .= '&usprop' $this->buildParameter($usprop);
  113.         }
  114.  
  115.         // Send the request.
  116.         $response $this->client->get($this->fetchUrl($path));
  117.  
  118.         return $this->validateResponse($response);
  119.     }
  120.  
  121.     /**
  122.      * Method to get current user information.
  123.      *
  124.      * @param   array  $uiprop  What pieces of information to include.
  125.      *
  126.      * @return  object 
  127.      *
  128.      * @since   12.3
  129.      */
  130.     public function getCurrentUserInfo(array $uiprop null)
  131.     {
  132.         // Build the request path.
  133.         $path '?action=query&meta=userinfo';
  134.  
  135.         if (isset($uiprop))
  136.         {
  137.             $path .= '&uiprop' $this->buildParameter($uiprop);
  138.         }
  139.  
  140.         // Send the request.
  141.         $response $this->client->get($this->fetchUrl($path));
  142.  
  143.         return $this->validateResponse($response);
  144.     }
  145.  
  146.     /**
  147.      * Method to get user contributions.
  148.      *
  149.      * @param   string   $ucuser        The users to retrieve contributions for.
  150.      * @param   string   $ucuserprefix  Retrieve contibutions for all users whose names begin with this value.
  151.      * @param   integer  $uclimit       The users to retrieve contributions for.
  152.      * @param   string   $ucstart       The start timestamp to return from.
  153.      * @param   string   $ucend         The end timestamp to return to.
  154.      * @param   boolean  $uccontinue    When more results are available, use this to continue.
  155.      * @param   string   $ucdir         In which direction to enumerate.
  156.      * @param   array    $ucnamespace   Only list contributions in these namespaces.
  157.      * @param   array    $ucprop        Include additional pieces of information.
  158.      * @param   array    $ucshow        Show only items that meet this criteria.
  159.      * @param   string   $uctag         Only list revisions tagged with this tag.
  160.      * @param   string   $uctoponly     Only list changes which are the latest revision
  161.      *
  162.      * @return  object 
  163.      *
  164.      * @since   12.3
  165.      */
  166.     public function getUserContribs($ucuser null$ucuserprefix null$uclimit null$ucstart null$ucend null$uccontinue null,
  167.         $ucdir nullarray $ucnamespace nullarray $ucprop nullarray $ucshow null$uctag null$uctoponly null)
  168.     {
  169.         // Build the request path.
  170.         $path '?action=query&list=usercontribs';
  171.  
  172.         if (isset($ucuser))
  173.         {
  174.             $path .= '&ucuser=' $ucuser;
  175.         }
  176.  
  177.         if (isset($ucuserprefix))
  178.         {
  179.             $path .= '&ucuserprefix=' $ucuserprefix;
  180.         }
  181.  
  182.         if (isset($uclimit))
  183.         {
  184.             $path .= '&uclimit=' $uclimit;
  185.         }
  186.  
  187.         if (isset($ucstart))
  188.         {
  189.             $path .= '&ucstart=' $ucstart;
  190.         }
  191.  
  192.         if (isset($ucend))
  193.         {
  194.             $path .= '&ucend=' $ucend;
  195.         }
  196.  
  197.         if ($uccontinue)
  198.         {
  199.             $path .= '&uccontinue=';
  200.         }
  201.  
  202.         if (isset($ucdir))
  203.         {
  204.             $path .= '&ucdir=' $ucdir;
  205.         }
  206.  
  207.         if (isset($ucnamespace))
  208.         {
  209.             $path .= '&ucnamespace=' $this->buildParameter($ucnamespace);
  210.         }
  211.  
  212.         if (isset($ucprop))
  213.         {
  214.             $path .= '&ucprop=' $this->buildParameter($ucprop);
  215.         }
  216.  
  217.         if (isset($ucshow))
  218.         {
  219.             $path .= '&ucshow=' $this->buildParameter($ucshow);
  220.         }
  221.  
  222.         if (isset($uctag))
  223.         {
  224.             $path .= '&uctag=' $uctag;
  225.         }
  226.  
  227.         if (isset($uctoponly))
  228.         {
  229.             $path .= '&uctoponly=' $uctoponly;
  230.         }
  231.  
  232.         // Send the request.
  233.         $response $this->client->get($this->fetchUrl($path));
  234.  
  235.         return $this->validateResponse($response);
  236.     }
  237.  
  238.     /**
  239.      * Method to block a user.
  240.      *
  241.      * @param   string   $user           Username, IP address or IP range you want to block.
  242.      * @param   string   $expiry         Relative expiry time, Default: never.
  243.      * @param   string   $reason         Reason for block (optional).
  244.      * @param   boolean  $anononly       Block anonymous users only.
  245.      * @param   boolean  $nocreate       Prevent account creation.
  246.      * @param   boolean  $autoblock      Automatically block the last used IP address, and any subsequent IP addresses they try to login from.
  247.      * @param   boolean  $noemail        Prevent user from sending e-mail through the wiki.
  248.      * @param   boolean  $hidename       Hide the username from the block log.
  249.      * @param   boolean  $allowusertalk  Allow the user to edit their own talk page.
  250.      * @param   boolean  $reblock        If the user is already blocked, overwrite the existing block.
  251.      * @param   boolean  $watchuser      Watch the user/IP's user and talk pages.
  252.      *
  253.      * @return  object 
  254.      *
  255.      * @since   12.3
  256.      */
  257.     public function blockUser($user$expiry null$reason null$anononly null$nocreate null$autoblock null$noemail null,
  258.         $hidename null$allowusertalk null$reblock null$watchuser null)
  259.     {
  260.         // Get the token.
  261.         $token $this->getToken($user'block');
  262.  
  263.         // Build the request path.
  264.         $path '?action=unblock';
  265.  
  266.         // Build the request data.
  267.         $data array(
  268.             'user' => $user,
  269.             'token' => $token,
  270.             'expiry' => $expiry,
  271.             'reason' => $reason,
  272.             'anononly' => $anononly,
  273.             'nocreate' => $nocreate,
  274.             'autoblock' => $autoblock,
  275.             'noemail' => $noemail,
  276.             'hidename' => $hidename,
  277.             'allowusetalk' => $allowusertalk,
  278.             'reblock' => $reblock,
  279.             'watchuser' => $watchuser
  280.         );
  281.  
  282.         // Send the request.
  283.         $response $this->client->post($this->fetchUrl($path)$data);
  284.  
  285.         return $this->validateResponse($response);
  286.     }
  287.  
  288.     /**
  289.      * Method to unblock a user.
  290.      *
  291.      * @param   string  $user    Username, IP address or IP range you want to unblock.
  292.      * @param   string  $reason  Reason for unblock (optional).
  293.      *
  294.      * @return  object 
  295.      *
  296.      * @since   12.3
  297.      */
  298.     public function unBlockUserByName($user$reason null)
  299.     {
  300.         // Get the token.
  301.         $token $this->getToken($user'unblock');
  302.  
  303.         // Build the request path.
  304.         $path '?action=unblock';
  305.  
  306.         // Build the request data.
  307.         $data array(
  308.                 'user' => $user,
  309.                 'token' => $token,
  310.                 'reason' => $reason,
  311.         );
  312.  
  313.         // Send the request.
  314.         $response $this->client->post($this->fetchUrl($path)$data);
  315.  
  316.         return $this->validateResponse($response);
  317.     }
  318.  
  319.     /**
  320.      * Method to unblock a user.
  321.      *
  322.      * @param   int     $id      Username, IP address or IP range you want to unblock.
  323.      * @param   string  $reason  Reason for unblock (optional).
  324.      *
  325.      * @return  object 
  326.      *
  327.      * @since   12.3
  328.      */
  329.     public function unBlockUserByID($id$reason null)
  330.     {
  331.         // Get the token.
  332.         $token $this->getToken($id'unblock');
  333.  
  334.         // Build the request path.
  335.         $path '?action=unblock';
  336.  
  337.         // Build the request data.
  338.         // TODO: $data doesn't seem to be used!
  339.         $data array(
  340.             'id' => $id,
  341.             'token' => $token,
  342.             'reason' => $reason,
  343.         );
  344.  
  345.         // Send the request.
  346.         $response $this->client->get($this->fetchUrl($path));
  347.  
  348.         return $this->validateResponse($response);
  349.     }
  350.  
  351.     /**
  352.      * Method to assign a user to a group.
  353.      *
  354.      * @param   string  $username  User name.
  355.      * @param   array   $add       Add the user to these groups.
  356.      * @param   array   $remove    Remove the user from these groups.
  357.      * @param   string  $reason    Reason for the change.
  358.      *
  359.      * @return  object 
  360.      *
  361.      * @since   12.3
  362.      */
  363.     public function assignGroup($username$add null$remove null$reason null)
  364.     {
  365.         // Get the token.
  366.         $token $this->getToken($username'unblock');
  367.  
  368.         // Build the request path.
  369.         $path '?action=userrights';
  370.  
  371.         // Build the request data.
  372.         $data array(
  373.             'username' => $username,
  374.             'token' => $token,
  375.             'add' => $add,
  376.             'remove' => $remove,
  377.             'reason' => $reason
  378.         );
  379.  
  380.         // Send the request.
  381.         $response $this->client->post($this->fetchUrl($path)$data);
  382.  
  383.         return $this->validateResponse($response);
  384.     }
  385.  
  386.     /**
  387.      * Method to email a user.
  388.      *
  389.      * @param   string   $target   User to send email to.
  390.      * @param   string   $subject  Subject header.
  391.      * @param   string   $text     Mail body.
  392.      * @param   boolean  $ccme     Send a copy of this mail to me.
  393.      *
  394.      * @return  object 
  395.      *
  396.      * @since   12.3
  397.      */
  398.     public function emailUser($target$subject null$text null$ccme null)
  399.     {
  400.         // Get the token.
  401.         $token $this->getToken($target'emailuser');
  402.  
  403.         // Build the request path.
  404.         $path '?action=emailuser';
  405.  
  406.         // Build the request data.
  407.         $data array(
  408.             'target' => $target,
  409.             'token' => $token,
  410.             'subject' => $subject,
  411.             'text' => $text,
  412.             'ccme' => $ccme
  413.         );
  414.  
  415.         // Send the request.
  416.         $response $this->client->post($this->fetchUrl($path)$data);
  417.  
  418.         return $this->validateResponse($response);
  419.     }
  420.  
  421.     /**
  422.      * Method to get access token.
  423.      *
  424.      * @param   string  $user     The User to get token.
  425.      * @param   string  $intoken  The type of token.
  426.      *
  427.      * @return  object 
  428.      *
  429.      * @since   12.3
  430.      */
  431.     public function getToken($user$intoken)
  432.     {
  433.         // Build the request path.
  434.         $path '?action=query&prop=info&intoken=' $intoken '&titles=User:' $user;
  435.  
  436.         // Send the request.
  437.         $response $this->client->post($this->fetchUrl($path)null);
  438.  
  439.         return (string) $this->validateResponse($response)->query->pages->page[$intoken 'token'];
  440.     }
  441. }

Documentation generated on Tue, 19 Nov 2013 15:16:40 +0100 by phpDocumentor 1.4.3