r38166 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38165‎ | r38166 | r38167 >
Date:01:01, 29 July 2008
Author:simetrical
Status:old
Tags:
Comment:
Merge r38165 and part of r38107 from trunk: I changed the interface of a hook added in 1.13, so the change should be in the 1.13 release.
Modified paths:
  • /branches/REL1_13/phase3/docs/hooks.txt (modified) (history)
  • /branches/REL1_13/phase3/includes/AutoLoader.php (modified) (history)
  • /branches/REL1_13/phase3/includes/ObjectArray.php (added) (history)
  • /branches/REL1_13/phase3/includes/ObjectArray.php (added) (history)
  • /branches/REL1_13/phase3/includes/UserArray.php (deleted) (history)

Diff [purge]

Index: branches/REL1_13/phase3/docs/hooks.txt
@@ -879,6 +879,12 @@
880880 whether to use the content language (true) or site language (false) (bool)
881881 &$transform: whether or not to expand variables and templates in the message (bool)
882882
 883+'ObjectArrayFromResult': called when creating an ObjectArray object from a
 884+ database result.
 885+$class: The class of object that this array represents (e.g., 'User', 'Title')
 886+&$array: set this to an object to override the default object returned
 887+$res: database result used to create the object
 888+
883889 'OpenSearchUrls': Called when constructing the OpenSearch description XML.
884890 Hooks can alter or append to the array of URLs for search & suggestion formats.
885891 &$urls: array of associative arrays with Url element attributes
@@ -1234,10 +1240,6 @@
12351241 'UploadComplete': Upon completion of a file upload
12361242 $uploadForm: Upload form object. File can be accessed by $uploadForm->mLocalFile.
12371243
1238 -'UserArrayFromResult': called when creating an UserArray object from a database result
1239 -&$userArray: set this to an object to override the default object returned
1240 -$res: database result used to create the object
1241 -
12421244 'userCan': To interrupt/advise the "user can do X to Y article" check.
12431245 If you want to display an error message, try getUserPermissionsErrors.
12441246 $title: Title object being checked against
Index: branches/REL1_13/phase3/includes/UserArray.php
@@ -1,62 +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 - function current() {
41 - return $this->current;
42 - }
43 -
44 - function key() {
45 - return $this->key;
46 - }
47 -
48 - function next() {
49 - $row = $this->res->next();
50 - $this->setCurrent( $row );
51 - $this->key++;
52 - }
53 -
54 - function rewind() {
55 - $this->res->rewind();
56 - $this->key = 0;
57 - $this->setCurrent( $this->res->current() );
58 - }
59 -
60 - function valid() {
61 - return $this->current !== false;
62 - }
63 -}
Index: branches/REL1_13/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: branches/REL1_13/phase3/includes/ObjectArray.php
___________________________________________________________________
Name: svn:eol-style
1109 + native
Index: branches/REL1_13/phase3/includes/AutoLoader.php
@@ -129,6 +129,7 @@
130130 'MWNamespace' => 'includes/Namespace.php',
131131 'MySQLSearchResultSet' => 'includes/SearchMySQL.php',
132132 'Namespace' => 'includes/NamespaceCompat.php', // Compat
 133+ 'ObjectArray' => 'includes/ObjectArray.php',
133134 'OldChangesList' => 'includes/ChangesList.php',
134135 'OracleSearchResultSet' => 'includes/SearchOracle.php',
135136 'OutputPage' => 'includes/OutputPage.php',
@@ -188,6 +189,7 @@
189190 'TableDiffFormatter' => 'includes/DifferenceEngine.php',
190191 'TablePager' => 'includes/Pager.php',
191192 'ThumbnailImage' => 'includes/MediaTransformOutput.php',
 193+ 'TitleArray' => 'includes/ObjectArray.php',
192194 'TitleDependency' => 'includes/CacheDependency.php',
193195 'Title' => 'includes/Title.php',
194196 'TitleListDependency' => 'includes/CacheDependency.php',
@@ -196,7 +198,7 @@
197199 'UnifiedDiffFormatter' => 'includes/DifferenceEngine.php',
198200 'UnlistedSpecialPage' => 'includes/SpecialPage.php',
199201 'User' => 'includes/User.php',
200 - 'UserArray' => 'includes/UserArray.php',
 202+ 'UserArray' => 'includes/ObjectArray.php',
201203 'UserArrayFromResult' => 'includes/UserArray.php',
202204 'UserMailer' => 'includes/UserMailer.php',
203205 'UserRightsProxy' => 'includes/UserRightsProxy.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r38186Revert r38166, per r38185 (breaks CentralAuth).simetrical14:56, 29 July 2008

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r38107* Add count() method to TitleArray and UserArray....simetrical18:59, 27 July 2008
r38165Merge TitleArray and UserArray into one unified class, ObjectArray. Adding s...simetrical00:51, 29 July 2008

Status & tagging log