From: Aryeh Gregor Date: Tue, 29 Jul 2008 00:51:08 +0000 (+0000) Subject: Merge TitleArray and UserArray into one unified class, ObjectArray. Adding support... X-Git-Tag: 1.31.0-rc.0~46310 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/?a=commitdiff_plain;h=c51f8ce69d4b2bc1544982c18f4b61efa79fbe53;p=lhc%2Fweb%2Fwiklou.git Merge TitleArray and UserArray into one unified class, ObjectArray. Adding support for a new type of object will now just take a few lines. --- diff --git a/docs/hooks.txt b/docs/hooks.txt index 98ceb42681..e09528ed2f 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -871,6 +871,12 @@ $baseID: the revision ID this was based off, if any whether to use the content language (true) or site language (false) (bool) &$transform: whether or not to expand variables and templates in the message (bool) +'ObjectArrayFromResult': called when creating an ObjectArray object from a + database result. +$class: The class of object that this array represents (e.g., 'User', 'Title') +&$array: set this to an object to override the default object returned +$res: database result used to create the object + 'OpenSearchUrls': Called when constructing the OpenSearch description XML. Hooks can alter or append to the array of URLs for search & suggestion formats. &$urls: array of associative arrays with Url element attributes @@ -1179,10 +1185,6 @@ $term: string of search term 'SpecialVersionExtensionTypes': called when generating the extensions credits, use this to change the tables headers $extTypes: associative array of extensions types -'TitleArrayFromResult': called when creating an TitleArray object from a database result -&$titleArray: set this to an object to override the default object returned -$res: database result used to create the object - 'TitleMoveComplete': after moving an article (title) $old: old title $nt: new title @@ -1230,10 +1232,6 @@ string &$error: output: HTML error to show if upload canceled by returning false 'UploadComplete': Upon completion of a file upload $uploadForm: Upload form object. File can be accessed by $uploadForm->mLocalFile. -'UserArrayFromResult': called when creating an UserArray object from a database result -&$userArray: set this to an object to override the default object returned -$res: database result used to create the object - 'userCan': To interrupt/advise the "user can do X to Y article" check. If you want to display an error message, try getUserPermissionsErrors. $title: Title object being checked against diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 0351017145..0af6bfd909 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -130,6 +130,7 @@ $wgAutoloadLocalClasses = array( 'MWNamespace' => 'includes/Namespace.php', 'MySQLSearchResultSet' => 'includes/SearchMySQL.php', 'Namespace' => 'includes/NamespaceCompat.php', // Compat + 'ObjectArray' => 'includes/ObjectArray.php', 'OldChangesList' => 'includes/ChangesList.php', 'OracleSearchResultSet' => 'includes/SearchOracle.php', 'OutputPage' => 'includes/OutputPage.php', @@ -191,14 +192,14 @@ $wgAutoloadLocalClasses = array( 'ThumbnailImage' => 'includes/MediaTransformOutput.php', 'TitleDependency' => 'includes/CacheDependency.php', 'Title' => 'includes/Title.php', - 'TitleArray' => 'includes/TitleArray.php', + 'TitleArray' => 'includes/ObjectArray.php', 'TitleListDependency' => 'includes/CacheDependency.php', 'TransformParameterError' => 'includes/MediaTransformOutput.php', 'TurckBagOStuff' => 'includes/BagOStuff.php', 'UnifiedDiffFormatter' => 'includes/DifferenceEngine.php', 'UnlistedSpecialPage' => 'includes/SpecialPage.php', 'User' => 'includes/User.php', - 'UserArray' => 'includes/UserArray.php', + 'UserArray' => 'includes/ObjectArray.php', 'UserArrayFromResult' => 'includes/UserArray.php', 'UserMailer' => 'includes/UserMailer.php', 'UserRightsProxy' => 'includes/UserRightsProxy.php', diff --git a/includes/ObjectArray.php b/includes/ObjectArray.php new file mode 100644 index 0000000000..429d369134 --- /dev/null +++ b/includes/ObjectArray.php @@ -0,0 +1,107 @@ +select( + * 'user', '*', $conds, $opts, __METHOD__ + * ) ); + * foreach( $users as $user ) { + * ...use $user's methods here, it's a User object!... + * } + */ +abstract class ObjectArray implements Iterator { + static function newFromClassAndResult( $class, $res ) { + $array = null; + if ( !wfRunHooks( 'ObjectArrayFromResult', array( $class, &$array, $res ) ) ) { + return null; + } + if ( $array === null ) { + $array = self::newFromResult_internal( $class, $res ); + } + return $array; + } + + protected static function newFromResult_internal( $class, $res ) { + return new ObjectArrayFromResult( $class, $res ); + } +} + +class ObjectArrayFromResult extends ObjectArray { + var $res, $class; + var $key = 0, $current = false; + + function __construct( $class, $res ) { + $this->class = $class; + $this->res = $res; + $this->key = 0; + $this->setCurrent( $this->res->current() ); + } + + protected function setCurrent( $row ) { + if ( $row === false ) { + $this->current = false; + } else { + $this->current = call_user_func( + array( $this->class, 'newFromRow' ), $row + ); + } + } + + public function count() { + return $this->res->numRows(); + } + + function current() { + return $this->current; + } + + function key() { + return $this->key; + } + + function next() { + $row = $this->res->next(); + $this->setCurrent( $row ); + $this->key++; + } + + function rewind() { + $this->res->rewind(); + $this->key = 0; + $this->setCurrent( $this->res->current() ); + } + + function valid() { + return $this->current !== false; + } +} + +abstract class UserArray extends ObjectArray { + static function newFromResult( $res ) { + return parent::newFromClassAndResult( 'User', $res ); + } +} + +abstract class TitleArray extends ObjectArray { + static function newFromResult( $res ) { + return parent::newFromClassAndResult( 'Title', $res ); + } +} diff --git a/includes/TitleArray.php b/includes/TitleArray.php deleted file mode 100644 index f7a9e1dc64..0000000000 --- a/includes/TitleArray.php +++ /dev/null @@ -1,81 +0,0 @@ -res = $res; - $this->key = 0; - $this->setCurrent( $this->res->current() ); - } - - protected function setCurrent( $row ) { - if ( $row === false ) { - $this->current = false; - } else { - $this->current = Title::newFromRow( $row ); - } - } - - public function count() { - return $this->res->numRows(); - } - - function current() { - return $this->current; - } - - function key() { - return $this->key; - } - - function next() { - $row = $this->res->next(); - $this->setCurrent( $row ); - $this->key++; - } - - function rewind() { - $this->res->rewind(); - $this->key = 0; - $this->setCurrent( $this->res->current() ); - } - - function valid() { - return $this->current !== false; - } -} diff --git a/includes/UserArray.php b/includes/UserArray.php deleted file mode 100644 index a2f54b7f27..0000000000 --- a/includes/UserArray.php +++ /dev/null @@ -1,66 +0,0 @@ -res = $res; - $this->key = 0; - $this->setCurrent( $this->res->current() ); - } - - protected function setCurrent( $row ) { - if ( $row === false ) { - $this->current = false; - } else { - $this->current = User::newFromRow( $row ); - } - } - - public function count() { - return $this->res->numRows(); - } - - function current() { - return $this->current; - } - - function key() { - return $this->key; - } - - function next() { - $row = $this->res->next(); - $this->setCurrent( $row ); - $this->key++; - } - - function rewind() { - $this->res->rewind(); - $this->key = 0; - $this->setCurrent( $this->res->current() ); - } - - function valid() { - return $this->current !== false; - } -}