From: Niklas Laxström Date: Tue, 10 Jan 2012 07:39:51 +0000 (+0000) Subject: Moved GenderCache to cache/ X-Git-Tag: 1.31.0-rc.0~25373 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22articles%22%2C%22id_article=%24id_article%22%29%20.%20%22?a=commitdiff_plain;h=055a1c485c31a8cf0ed929f0a36a99241e2a8091;p=lhc%2Fweb%2Fwiklou.git Moved GenderCache to cache/ --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 72e9090a40..61ae868501 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -89,7 +89,6 @@ $wgAutoloadLocalClasses = array( 'FormAction' => 'includes/Action.php', 'FormOptions' => 'includes/FormOptions.php', 'FormSpecialPage' => 'includes/SpecialPage.php', - 'GenderCache' => 'includes/GenderCache.php', 'HashtableReplacer' => 'includes/StringUtils.php', 'HistoryBlob' => 'includes/HistoryBlob.php', 'HistoryBlobCurStub' => 'includes/HistoryBlob.php', @@ -366,6 +365,7 @@ $wgAutoloadLocalClasses = array( 'DependencyWrapper' => 'includes/cache/CacheDependency.php', 'FileCacheBase' => 'includes/cache/FileCacheBase.php', 'FileDependency' => 'includes/cache/CacheDependency.php', + 'GenderCache' => 'includes/cache/GenderCache.php', 'GlobalDependency' => 'includes/cache/CacheDependency.php', 'HTMLCacheUpdate' => 'includes/cache/HTMLCacheUpdate.php', 'HTMLCacheUpdateJob' => 'includes/cache/HTMLCacheUpdate.php', diff --git a/includes/GenderCache.php b/includes/GenderCache.php deleted file mode 100644 index 342f8dba83..0000000000 --- a/includes/GenderCache.php +++ /dev/null @@ -1,135 +0,0 @@ -default === null ) { - $this->default = User::getDefaultOption( 'gender' ); - } - return $this->default; - } - - /** - * Returns the gender for given username. - * @param $username String: username - * @param $caller String: the calling method - * @return String - */ - public function getGenderOf( $username, $caller = '' ) { - global $wgUser; - - $username = strtr( $username, '_', ' ' ); - if ( !isset( $this->cache[$username] ) ) { - - if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) { - if( $this->misses === $this->missLimit ) { - $this->misses++; - wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" ); - } - return $this->getDefault(); - - } else { - $this->misses++; - if ( !User::isValidUserName( $username ) ) { - $this->cache[$username] = $this->getDefault(); - } else { - $this->doQuery( $username, $caller ); - } - } - - } - - /* Undefined if there is a valid username which for some reason doesn't - * exist in the database. - */ - return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault(); - } - - /** - * Wrapper for doQuery that processes raw LinkBatch data. - * - * @param $data - * @param $caller - */ - public function doLinkBatch( $data, $caller = '' ) { - $users = array(); - foreach ( $data as $ns => $pagenames ) { - if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue; - foreach ( array_keys( $pagenames ) as $username ) { - if ( isset( $this->cache[$username] ) ) continue; - $users[$username] = true; - } - } - - $this->doQuery( array_keys( $users ), $caller ); - } - - /** - * Preloads genders for given list of users. - * @param $users List|String: usernames - * @param $caller String: the calling method - */ - public function doQuery( $users, $caller = '' ) { - $default = $this->getDefault(); - - foreach ( (array) $users as $index => $value ) { - $name = strtr( $value, '_', ' ' ); - if ( isset( $this->cache[$name] ) ) { - // Skip users whose gender setting we already know - unset( $users[$index] ); - } else { - $users[$index] = $name; - // For existing users, this value will be overwritten by the correct value - $this->cache[$name] = $default; - } - } - - if ( count( $users ) === 0 ) { - return; - } - - $dbr = wfGetDB( DB_SLAVE ); - $table = array( 'user', 'user_properties' ); - $fields = array( 'user_name', 'up_value' ); - $conds = array( 'user_name' => $users ); - $joins = array( 'user_properties' => - array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) ); - - $comment = __METHOD__; - if ( strval( $caller ) !== '' ) { - $comment .= "/$caller"; - } - $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins ); - - foreach ( $res as $row ) { - $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default; - } - } - -} diff --git a/includes/cache/GenderCache.php b/includes/cache/GenderCache.php new file mode 100644 index 0000000000..342f8dba83 --- /dev/null +++ b/includes/cache/GenderCache.php @@ -0,0 +1,135 @@ +default === null ) { + $this->default = User::getDefaultOption( 'gender' ); + } + return $this->default; + } + + /** + * Returns the gender for given username. + * @param $username String: username + * @param $caller String: the calling method + * @return String + */ + public function getGenderOf( $username, $caller = '' ) { + global $wgUser; + + $username = strtr( $username, '_', ' ' ); + if ( !isset( $this->cache[$username] ) ) { + + if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) { + if( $this->misses === $this->missLimit ) { + $this->misses++; + wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" ); + } + return $this->getDefault(); + + } else { + $this->misses++; + if ( !User::isValidUserName( $username ) ) { + $this->cache[$username] = $this->getDefault(); + } else { + $this->doQuery( $username, $caller ); + } + } + + } + + /* Undefined if there is a valid username which for some reason doesn't + * exist in the database. + */ + return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault(); + } + + /** + * Wrapper for doQuery that processes raw LinkBatch data. + * + * @param $data + * @param $caller + */ + public function doLinkBatch( $data, $caller = '' ) { + $users = array(); + foreach ( $data as $ns => $pagenames ) { + if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue; + foreach ( array_keys( $pagenames ) as $username ) { + if ( isset( $this->cache[$username] ) ) continue; + $users[$username] = true; + } + } + + $this->doQuery( array_keys( $users ), $caller ); + } + + /** + * Preloads genders for given list of users. + * @param $users List|String: usernames + * @param $caller String: the calling method + */ + public function doQuery( $users, $caller = '' ) { + $default = $this->getDefault(); + + foreach ( (array) $users as $index => $value ) { + $name = strtr( $value, '_', ' ' ); + if ( isset( $this->cache[$name] ) ) { + // Skip users whose gender setting we already know + unset( $users[$index] ); + } else { + $users[$index] = $name; + // For existing users, this value will be overwritten by the correct value + $this->cache[$name] = $default; + } + } + + if ( count( $users ) === 0 ) { + return; + } + + $dbr = wfGetDB( DB_SLAVE ); + $table = array( 'user', 'user_properties' ); + $fields = array( 'user_name', 'up_value' ); + $conds = array( 'user_name' => $users ); + $joins = array( 'user_properties' => + array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) ); + + $comment = __METHOD__; + if ( strval( $caller ) !== '' ) { + $comment .= "/$caller"; + } + $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins ); + + foreach ( $res as $row ) { + $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default; + } + } + +}