Merge TitleArray and UserArray into one unified class, ObjectArray. Adding support...
authorAryeh Gregor <simetrical@users.mediawiki.org>
Tue, 29 Jul 2008 00:51:08 +0000 (00:51 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Tue, 29 Jul 2008 00:51:08 +0000 (00:51 +0000)
docs/hooks.txt
includes/AutoLoader.php
includes/ObjectArray.php [new file with mode: 0644]
includes/TitleArray.php [deleted file]
includes/UserArray.php [deleted file]

index 98ceb42..e09528e 100644 (file)
@@ -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
index 0351017..0af6bfd 100644 (file)
@@ -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 (file)
index 0000000..429d369
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+/**
+ * ObjectArray is a class that stores arrays of objects in an efficient manner.
+ * It stores a minimum of information about each object, and then constructs
+ * them on the fly for iteration in foreach() loops.  Currently this can only
+ * be done by storing the info in the form of a database result, which is a
+ * good choice because 1) it tends to be readily available and 2) it uses less
+ * memory than a PHP array.  Other storage methods may be developed in the fu-
+ * ture.
+ *
+ * Currently you can get TitleArrays and UserArrays.  Any other class could be
+ * easily added: it just needs a newFromRow() method that will accept a data-
+ * base row as its sole argument, and return an object.  For the fields that
+ * need to be provided in the result you pass to the newFromResult() construc-
+ * tor, consult the appropriate object class' newFromRow() documentation.
+ *
+ * In addition to the usual Iterator methods, there's a count() method that
+ * will return the number of objects in the array.  When later versions of PHP
+ * are supported, we may be able to avoid this by implementing the Countable
+ * interface, making this act more like a real array.
+ *
+ * Sample usage:
+ *   $users = UserArray::newFromResult( $dbr->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 (file)
index f7a9e1d..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-<?php
-/**
- * Note: this entire file is a byte-for-byte copy of UserArray.php with
- * s/User/Title/.  If anyone can figure out how to do this nicely with inheri-
- * tance or something, please do so.
- */
-
-/**
- * The TitleArray class only exists to provide the newFromResult method at pre-
- * sent.
- */
-abstract class TitleArray implements Iterator {
-       /**
-        * @param $res result A MySQL result including at least page_namespace and
-        *   page_title -- also can have page_id, page_len, page_is_redirect,
-        *   page_latest (if those will be used).  See Title::newFromRow.
-        * @return TitleArray
-        */
-       static function newFromResult( $res ) {
-               $array = null;
-               if ( !wfRunHooks( 'TitleArrayFromResult', array( &$array, $res ) ) ) {
-                       return null;
-               }
-               if ( $array === null ) {
-                       $array = self::newFromResult_internal( $res );
-               }
-               return $array;
-       }
-
-       protected static function newFromResult_internal( $res ) {
-               $array = new TitleArrayFromResult( $res );
-               return $array;
-       }
-}
-
-class TitleArrayFromResult extends TitleArray {
-       var $res;
-       var $key, $current;
-
-       function __construct( $res ) {
-               $this->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 (file)
index a2f54b7..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-<?php
-
-abstract class UserArray implements Iterator {
-       static function newFromResult( $res ) {
-               $userArray = null;
-               if ( !wfRunHooks( 'UserArrayFromResult', array( &$userArray, $res ) ) ) {
-                       return null;
-               }
-               if ( $userArray === null ) {
-                       $userArray = self::newFromResult_internal( $res );
-               }
-               return $userArray;
-       }
-
-       protected static function newFromResult_internal( $res ) {
-               $userArray = new UserArrayFromResult( $res );
-               return $userArray;
-       }
-}
-
-class UserArrayFromResult extends UserArray {
-       var $res;
-       var $key, $current;
-
-       function __construct( $res ) {
-               $this->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;
-       }
-}