MediaWiki  REL1_19
GenderCache.php
Go to the documentation of this file.
00001 <?php
00002 
00008 class GenderCache {
00009         protected $cache = array();
00010         protected $default;
00011         protected $misses = 0;
00012         protected $missLimit = 1000;
00013 
00017         public static function singleton() {
00018                 static $that = null;
00019                 if ( $that === null ) {
00020                         $that = new self();
00021                 }
00022                 return $that;
00023         }
00024 
00025         protected function __construct() {}
00026 
00031         protected function getDefault() {
00032                 if ( $this->default === null ) {
00033                         $this->default = User::getDefaultOption( 'gender' );
00034                 }
00035                 return $this->default;
00036         }
00037 
00044         public function getGenderOf( $username, $caller = '' ) {
00045                 global $wgUser;
00046 
00047                 $username = strtr( $username, '_', ' ' );
00048                 if ( !isset( $this->cache[$username] ) ) {
00049 
00050                         if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
00051                                 if( $this->misses === $this->missLimit ) {
00052                                         $this->misses++;
00053                                         wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
00054                                 }
00055                                 return $this->getDefault();
00056 
00057                         } else {
00058                                 $this->misses++;
00059                                 if ( !User::isValidUserName( $username ) ) {
00060                                         $this->cache[$username] = $this->getDefault();
00061                                 } else {
00062                                         $this->doQuery( $username, $caller );
00063                                 }
00064                         }
00065 
00066                 }
00067 
00068                 /* Undefined if there is a valid username which for some reason doesn't
00069                  * exist in the database.
00070                  */
00071                 return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault();
00072         }
00073 
00080         public function doLinkBatch( $data, $caller = '' ) {
00081                 $users = array();
00082                 foreach ( $data as $ns => $pagenames ) {
00083                         if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue;
00084                         foreach ( array_keys( $pagenames ) as $username ) {
00085                                 if ( isset( $this->cache[$username] ) ) continue;
00086                                 $users[$username] = true;
00087                         }
00088                 }
00089 
00090                 $this->doQuery( array_keys( $users ), $caller );
00091         }
00092 
00098         public function doQuery( $users, $caller = '' ) {
00099                 $default = $this->getDefault();
00100 
00101                 foreach ( (array) $users as $index => $value ) {
00102                         $name = strtr( $value, '_', ' ' );
00103                         if ( isset( $this->cache[$name] ) ) {
00104                                 // Skip users whose gender setting we already know
00105                                 unset( $users[$index] );
00106                         } else {
00107                                 $users[$index] = $name;
00108                                 // For existing users, this value will be overwritten by the correct value
00109                                 $this->cache[$name] = $default;
00110                         }
00111                 }
00112 
00113                 if ( count( $users ) === 0 ) {
00114                         return;
00115                 }
00116 
00117                 $dbr = wfGetDB( DB_SLAVE );
00118                 $table = array( 'user', 'user_properties' );
00119                 $fields = array( 'user_name', 'up_value' );
00120                 $conds = array( 'user_name' => $users );
00121                 $joins = array( 'user_properties' =>
00122                         array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
00123 
00124                 $comment = __METHOD__;
00125                 if ( strval( $caller ) !== '' ) {
00126                         $comment .= "/$caller";
00127                 }
00128                 $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins );
00129 
00130                 foreach ( $res as $row ) {
00131                         $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;
00132                 }
00133         }
00134 
00135 }