r38165 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38164‎ | r38165 | r38166 >
Date:00:51, 29 July 2008
Author:simetrical
Status:old
Tags:
Comment:
Merge TitleArray and UserArray into one unified class, ObjectArray. Adding support for a new type of object will now just take a few lines.
Modified paths:
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/ObjectArray.php (added) (history)
  • /trunk/phase3/includes/ObjectArray.php (added) (history)
  • /trunk/phase3/includes/TitleArray.php (deleted) (history)
  • /trunk/phase3/includes/UserArray.php (deleted) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -871,6 +871,12 @@
872872 whether to use the content language (true) or site language (false) (bool)
873873 &$transform: whether or not to expand variables and templates in the message (bool)
874874
 875+'ObjectArrayFromResult': called when creating an ObjectArray object from a
 876+ database result.
 877+$class: The class of object that this array represents (e.g., 'User', 'Title')
 878+&$array: set this to an object to override the default object returned
 879+$res: database result used to create the object
 880+
875881 'OpenSearchUrls': Called when constructing the OpenSearch description XML.
876882 Hooks can alter or append to the array of URLs for search & suggestion formats.
877883 &$urls: array of associative arrays with Url element attributes
@@ -1179,10 +1185,6 @@
11801186 'SpecialVersionExtensionTypes': called when generating the extensions credits, use this to change the tables headers
11811187 $extTypes: associative array of extensions types
11821188
1183 -'TitleArrayFromResult': called when creating an TitleArray object from a database result
1184 -&$titleArray: set this to an object to override the default object returned
1185 -$res: database result used to create the object
1186 -
11871189 'TitleMoveComplete': after moving an article (title)
11881190 $old: old title
11891191 $nt: new title
@@ -1230,10 +1232,6 @@
12311233 'UploadComplete': Upon completion of a file upload
12321234 $uploadForm: Upload form object. File can be accessed by $uploadForm->mLocalFile.
12331235
1234 -'UserArrayFromResult': called when creating an UserArray object from a database result
1235 -&$userArray: set this to an object to override the default object returned
1236 -$res: database result used to create the object
1237 -
12381236 'userCan': To interrupt/advise the "user can do X to Y article" check.
12391237 If you want to display an error message, try getUserPermissionsErrors.
12401238 $title: Title object being checked against
Index: trunk/phase3/includes/TitleArray.php
@@ -1,81 +0,0 @@
2 -<?php
3 -/**
4 - * Note: this entire file is a byte-for-byte copy of UserArray.php with
5 - * s/User/Title/. If anyone can figure out how to do this nicely with inheri-
6 - * tance or something, please do so.
7 - */
8 -
9 -/**
10 - * The TitleArray class only exists to provide the newFromResult method at pre-
11 - * sent.
12 - */
13 -abstract class TitleArray implements Iterator {
14 - /**
15 - * @param $res result A MySQL result including at least page_namespace and
16 - * page_title -- also can have page_id, page_len, page_is_redirect,
17 - * page_latest (if those will be used). See Title::newFromRow.
18 - * @return TitleArray
19 - */
20 - static function newFromResult( $res ) {
21 - $array = null;
22 - if ( !wfRunHooks( 'TitleArrayFromResult', array( &$array, $res ) ) ) {
23 - return null;
24 - }
25 - if ( $array === null ) {
26 - $array = self::newFromResult_internal( $res );
27 - }
28 - return $array;
29 - }
30 -
31 - protected static function newFromResult_internal( $res ) {
32 - $array = new TitleArrayFromResult( $res );
33 - return $array;
34 - }
35 -}
36 -
37 -class TitleArrayFromResult extends TitleArray {
38 - var $res;
39 - var $key, $current;
40 -
41 - function __construct( $res ) {
42 - $this->res = $res;
43 - $this->key = 0;
44 - $this->setCurrent( $this->res->current() );
45 - }
46 -
47 - protected function setCurrent( $row ) {
48 - if ( $row === false ) {
49 - $this->current = false;
50 - } else {
51 - $this->current = Title::newFromRow( $row );
52 - }
53 - }
54 -
55 - public function count() {
56 - return $this->res->numRows();
57 - }
58 -
59 - function current() {
60 - return $this->current;
61 - }
62 -
63 - function key() {
64 - return $this->key;
65 - }
66 -
67 - function next() {
68 - $row = $this->res->next();
69 - $this->setCurrent( $row );
70 - $this->key++;
71 - }
72 -
73 - function rewind() {
74 - $this->res->rewind();
75 - $this->key = 0;
76 - $this->setCurrent( $this->res->current() );
77 - }
78 -
79 - function valid() {
80 - return $this->current !== false;
81 - }
82 -}
Index: trunk/phase3/includes/UserArray.php
@@ -1,66 +0,0 @@
2 -<?php
3 -
4 -abstract class UserArray implements Iterator {
5 - static function newFromResult( $res ) {
6 - $userArray = null;
7 - if ( !wfRunHooks( 'UserArrayFromResult', array( &$userArray, $res ) ) ) {
8 - return null;
9 - }
10 - if ( $userArray === null ) {
11 - $userArray = self::newFromResult_internal( $res );
12 - }
13 - return $userArray;
14 - }
15 -
16 - protected static function newFromResult_internal( $res ) {
17 - $userArray = new UserArrayFromResult( $res );
18 - return $userArray;
19 - }
20 -}
21 -
22 -class UserArrayFromResult extends UserArray {
23 - var $res;
24 - var $key, $current;
25 -
26 - function __construct( $res ) {
27 - $this->res = $res;
28 - $this->key = 0;
29 - $this->setCurrent( $this->res->current() );
30 - }
31 -
32 - protected function setCurrent( $row ) {
33 - if ( $row === false ) {
34 - $this->current = false;
35 - } else {
36 - $this->current = User::newFromRow( $row );
37 - }
38 - }
39 -
40 - public function count() {
41 - return $this->res->numRows();
42 - }
43 -
44 - function current() {
45 - return $this->current;
46 - }
47 -
48 - function key() {
49 - return $this->key;
50 - }
51 -
52 - function next() {
53 - $row = $this->res->next();
54 - $this->setCurrent( $row );
55 - $this->key++;
56 - }
57 -
58 - function rewind() {
59 - $this->res->rewind();
60 - $this->key = 0;
61 - $this->setCurrent( $this->res->current() );
62 - }
63 -
64 - function valid() {
65 - return $this->current !== false;
66 - }
67 -}
Index: trunk/phase3/includes/ObjectArray.php
@@ -0,0 +1,107 @@
 2+<?php
 3+/**
 4+ * ObjectArray is a class that stores arrays of objects in an efficient manner.
 5+ * It stores a minimum of information about each object, and then constructs
 6+ * them on the fly for iteration in foreach() loops. Currently this can only
 7+ * be done by storing the info in the form of a database result, which is a
 8+ * good choice because 1) it tends to be readily available and 2) it uses less
 9+ * memory than a PHP array. Other storage methods may be developed in the fu-
 10+ * ture.
 11+ *
 12+ * Currently you can get TitleArrays and UserArrays. Any other class could be
 13+ * easily added: it just needs a newFromRow() method that will accept a data-
 14+ * base row as its sole argument, and return an object. For the fields that
 15+ * need to be provided in the result you pass to the newFromResult() construc-
 16+ * tor, consult the appropriate object class' newFromRow() documentation.
 17+ *
 18+ * In addition to the usual Iterator methods, there's a count() method that
 19+ * will return the number of objects in the array. When later versions of PHP
 20+ * are supported, we may be able to avoid this by implementing the Countable
 21+ * interface, making this act more like a real array.
 22+ *
 23+ * Sample usage:
 24+ * $users = UserArray::newFromResult( $dbr->select(
 25+ * 'user', '*', $conds, $opts, __METHOD__
 26+ * ) );
 27+ * foreach( $users as $user ) {
 28+ * ...use $user's methods here, it's a User object!...
 29+ * }
 30+ */
 31+abstract class ObjectArray implements Iterator {
 32+ static function newFromClassAndResult( $class, $res ) {
 33+ $array = null;
 34+ if ( !wfRunHooks( 'ObjectArrayFromResult', array( $class, &$array, $res ) ) ) {
 35+ return null;
 36+ }
 37+ if ( $array === null ) {
 38+ $array = self::newFromResult_internal( $class, $res );
 39+ }
 40+ return $array;
 41+ }
 42+
 43+ protected static function newFromResult_internal( $class, $res ) {
 44+ return new ObjectArrayFromResult( $class, $res );
 45+ }
 46+}
 47+
 48+class ObjectArrayFromResult extends ObjectArray {
 49+ var $res, $class;
 50+ var $key = 0, $current = false;
 51+
 52+ function __construct( $class, $res ) {
 53+ $this->class = $class;
 54+ $this->res = $res;
 55+ $this->key = 0;
 56+ $this->setCurrent( $this->res->current() );
 57+ }
 58+
 59+ protected function setCurrent( $row ) {
 60+ if ( $row === false ) {
 61+ $this->current = false;
 62+ } else {
 63+ $this->current = call_user_func(
 64+ array( $this->class, 'newFromRow' ), $row
 65+ );
 66+ }
 67+ }
 68+
 69+ public function count() {
 70+ return $this->res->numRows();
 71+ }
 72+
 73+ function current() {
 74+ return $this->current;
 75+ }
 76+
 77+ function key() {
 78+ return $this->key;
 79+ }
 80+
 81+ function next() {
 82+ $row = $this->res->next();
 83+ $this->setCurrent( $row );
 84+ $this->key++;
 85+ }
 86+
 87+ function rewind() {
 88+ $this->res->rewind();
 89+ $this->key = 0;
 90+ $this->setCurrent( $this->res->current() );
 91+ }
 92+
 93+ function valid() {
 94+ return $this->current !== false;
 95+ }
 96+}
 97+
 98+abstract class UserArray extends ObjectArray {
 99+ static function newFromResult( $res ) {
 100+ return parent::newFromClassAndResult( 'User', $res );
 101+ }
 102+}
 103+
 104+abstract class TitleArray extends ObjectArray {
 105+ static function newFromResult( $res ) {
 106+ return parent::newFromClassAndResult( 'Title', $res );
 107+ }
 108+}
Property changes on: trunk/phase3/includes/ObjectArray.php
___________________________________________________________________
Name: svn:eol-style
1109 + native
Index: trunk/phase3/includes/AutoLoader.php
@@ -130,6 +130,7 @@
131131 'MWNamespace' => 'includes/Namespace.php',
132132 'MySQLSearchResultSet' => 'includes/SearchMySQL.php',
133133 'Namespace' => 'includes/NamespaceCompat.php', // Compat
 134+ 'ObjectArray' => 'includes/ObjectArray.php',
134135 'OldChangesList' => 'includes/ChangesList.php',
135136 'OracleSearchResultSet' => 'includes/SearchOracle.php',
136137 'OutputPage' => 'includes/OutputPage.php',
@@ -191,14 +192,14 @@
192193 'ThumbnailImage' => 'includes/MediaTransformOutput.php',
193194 'TitleDependency' => 'includes/CacheDependency.php',
194195 'Title' => 'includes/Title.php',
195 - 'TitleArray' => 'includes/TitleArray.php',
 196+ 'TitleArray' => 'includes/ObjectArray.php',
196197 'TitleListDependency' => 'includes/CacheDependency.php',
197198 'TransformParameterError' => 'includes/MediaTransformOutput.php',
198199 'TurckBagOStuff' => 'includes/BagOStuff.php',
199200 'UnifiedDiffFormatter' => 'includes/DifferenceEngine.php',
200201 'UnlistedSpecialPage' => 'includes/SpecialPage.php',
201202 'User' => 'includes/User.php',
202 - 'UserArray' => 'includes/UserArray.php',
 203+ 'UserArray' => 'includes/ObjectArray.php',
203204 'UserArrayFromResult' => 'includes/UserArray.php',
204205 'UserMailer' => 'includes/UserMailer.php',
205206 'UserRightsProxy' => 'includes/UserRightsProxy.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r38166Merge r38165 and part of r38107 from trunk: I changed the interface of a hook...simetrical01:01, 29 July 2008
r38185Revert r38165 for now, breaks CentralAuth and I don't have that installed any...simetrical14:53, 29 July 2008

Status & tagging log