r113670 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113669‎ | r113670 | r113671 >
Date:21:35, 12 March 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
get rid of per-row queries in student pager
Modified paths:
  • /trunk/extensions/EducationProgram/includes/EPCoursePager.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourses.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPStudentPager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/includes/EPStudentPager.php
@@ -14,6 +14,25 @@
1515 class EPStudentPager extends EPPager {
1616
1717 /**
 18+ * List of user ids mapped to user names and real names, set in doBatchLookups.
 19+ * The real names will just hold the user name when no real name is set.
 20+ * user id => array( user name, real name )
 21+ *
 22+ * @since 0.1
 23+ * @var array
 24+ */
 25+ protected $userNames = array();
 26+
 27+ /**
 28+ * List of user ids with the names of their associated courses.
 29+ * user id => array( course name 0, ... )
 30+ *
 31+ * @since 0.1
 32+ * @var array
 33+ */
 34+ protected $courseNames = array();
 35+
 36+ /**
1837 * Constructor.
1938 *
2039 * @param IContextSource $context
@@ -69,9 +88,13 @@
7089 );
7190 break;
7291 case 'user_id':
73 - $user = User::newFromId( $value );
74 - $realName = $user->getRealName() === '' ? false : $user->getRealName();
75 - $value = Linker::userLink( $value, $user->getName(), $realName ) . Linker::userToolLinks( $value, $user->getName() );
 92+ if ( array_key_exists( $value, $this->userNames ) ) {
 93+ list( $userName, $realName ) = $this->userNames[$value];
 94+ $value = Linker::userLink( $value, $userName, $realName ) . Linker::userToolLinks( $value, $userName );
 95+ }
 96+ else {
 97+ wfWarn( 'User id not in $this->userNames in ' . __METHOD__ );
 98+ }
7699 break;
77100 case 'first_enroll': case 'last_active':
78101 $value = htmlspecialchars( $this->getLanguage()->date( $value ) );
@@ -80,12 +103,16 @@
81104 $value = wfMsgHtml( $value === '1' ? 'epstudentpager-yes' : 'epstudentpager-no' );
82105 break;
83106 case '_courses_current':
84 - $value = $this->getLanguage()->pipeList( array_map(
85 - function( EPCourse $course ) {
86 - return $course->getLink();
87 - },
88 - $this->currentObject->getCourses( 'name', EPCourses::getStatusConds( 'current' ) )
89 - ) );
 107+ $userId = $this->currentObject->getField( 'user_id' );
 108+
 109+ if ( array_key_exists( $userId, $this->courseNames ) ) {
 110+ $value = $this->getLanguage()->pipeList( array_map(
 111+ function( $courseName ) {
 112+ return EPCourses::singleton()->getLinkFor( $courseName );
 113+ },
 114+ $this->courseNames[$userId]
 115+ ) );
 116+ }
90117 break;
91118 }
92119
@@ -125,4 +152,57 @@
126153 return $fields;
127154 }
128155
 156+ /**
 157+ * (non-PHPdoc)
 158+ * @see IndexPager::doBatchLookups()
 159+ */
 160+ protected function doBatchLookups() {
 161+ $userIds = array();
 162+ $field = $this->table->getPrefixedField( 'user_id' );
 163+
 164+ while( $student = $this->mResult->fetchObject() ) {
 165+ $userIds[] = (int)$student->$field;
 166+ }
 167+
 168+ if ( !empty( $userIds ) ) {
 169+ $result = wfGetDB( DB_SLAVE )->select(
 170+ 'user',
 171+ array( 'user_id', 'user_name', 'user_real_name' ),
 172+ array( 'user_id' => $userIds ),
 173+ __METHOD__
 174+ );
 175+
 176+ while( $user = $result->fetchObject() ) {
 177+ $real = $user->user_real_name === '' ? $user->user_name : $user->user_real_name;
 178+ $this->userNames[$user->user_id] = array( $user->user_name, $real );
 179+ }
 180+
 181+ $courseNameField = EPCourses::singleton()->getPrefixedField( 'name' );
 182+
 183+ $result = wfGetDB( DB_SLAVE )->select(
 184+ array( 'ep_courses', 'ep_users_per_course' ),
 185+ array( $courseNameField, 'upc_user_id' ),
 186+ array_merge( array(
 187+ 'upc_role' => EP_STUDENT,
 188+ 'upc_user_id' => $userIds,
 189+ ), EPCourses::getStatusConds( 'current', true ) ) ,
 190+ __METHOD__,
 191+ array(),
 192+ array(
 193+ 'ep_users_per_course' => array( 'INNER JOIN', array( 'upc_course_id=course_id' ) ),
 194+ )
 195+ );
 196+
 197+ while( $courseForUser = $result->fetchObject() ) {
 198+ if ( !array_key_exists( $courseForUser->upc_user_id, $this->courseNames ) ) {
 199+ $this->courseNames[$courseForUser->upc_user_id] = array();
 200+ }
 201+
 202+ $this->courseNames[$courseForUser->upc_user_id][] = $courseForUser->$courseNameField;
 203+ }
 204+ }
 205+
 206+
 207+ }
 208+
129209 }
Index: trunk/extensions/EducationProgram/includes/EPCoursePager.php
@@ -87,6 +87,9 @@
8888 if ( array_key_exists( $value, $this->orgNames ) ) {
8989 $value = EPOrgs::singleton()->getLinkFor( $this->orgNames[$value] );
9090 }
 91+ else {
 92+ wfWarn( 'Org id not in $this->orgNames in ' . __METHOD__ );
 93+ }
9194 break;
9295 case 'term':
9396 $value = htmlspecialchars( $value );
Index: trunk/extensions/EducationProgram/includes/EPCourses.php
@@ -172,8 +172,11 @@
173173 * @since 0.1
174174 *
175175 * @param string $state
 176+ * @param boolean $prefix
 177+ *
 178+ * @return array
176179 */
177 - public static function getStatusConds( $state ) {
 180+ public static function getStatusConds( $state, $prefix = false ) {
178181 $now = wfGetDB( DB_SLAVE )->addQuotes( wfTimestampNow() );
179182
180183 $conditions = array();
@@ -190,6 +193,10 @@
191194 $conditions[] = 'start > ' . $now;
192195 break;
193196 }
 197+
 198+ if ( $prefix ) {
 199+ $conditions = self::singleton()->getPrefixedValues( $conditions );
 200+ }
194201
195202 return $conditions;
196203 }

Status & tagging log