r111905 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111904‎ | r111905 | r111906 >
Date:01:58, 20 February 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
finishing up import script
Modified paths:
  • /trunk/extensions/EducationProgram/includes/EPRoleObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/maintenance/importWEPData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/maintenance/importWEPData.php
@@ -62,10 +62,24 @@
6363 }
6464 }
6565
66 - echo 'Found ' . count( $orgs ) . ' orgs, ' . count( $courses ) . ' courses and ' . count( $students ) . " students.\n";
 66+ echo "\nFound " . count( $orgs ) . ' orgs, ' . count( $courses ) . ' courses and ' . count( $students ) . " students.\n\n";
6767
6868 echo "Inserting orgs ...";
 69+ $this->insertOrgs( $orgs );
 70+ echo " done!\n";
6971
 72+ echo "Inserting courses ...\n";
 73+ $courseIds = $this->insertCourses( $courses, $orgs );
 74+ echo "Inserted courses\n";
 75+
 76+ echo "Inserting students ...\n";
 77+ $this->insertStudents( $students, $courseIds );
 78+ echo "Inserted students\n";
 79+
 80+ echo "Import completed!\n\n";
 81+ }
 82+
 83+ protected function insertOrgs( array $orgs ) {
7084 wfGetDB( DB_MASTER )->begin();
7185
7286 foreach ( $orgs as $org => &$id ) {
@@ -81,12 +95,12 @@
8296 $id = $org->getId();
8397 }
8498
85 - wfGetDB( DB_MASTER )->commit();
 99+ wfGetDB( DB_MASTER )->commit();
 100+ }
 101+
 102+ protected function insertCourses( array $courses, array $orgs ) {
 103+ $courseIds = array();
86104
87 - echo " done!\n";
88 -
89 - echo "Inserting courses ...\n";
90 -
91105 foreach ( $courses as $course => $org ) {
92106 $name = $course;
93107
@@ -101,17 +115,76 @@
102116
103117 try{
104118 $course->save();
 119+ $courseIds[$name] = $course->getId();
105120 }
106121 catch ( Exception $ex ) {
107122 echo "Failed to insert course '$name'.\n";
108123 }
109124 }
110 -
111 - echo "Inserted courses\n";
112 -
113 - echo "\n\n";
 125+
 126+ return $courseIds;
114127 }
115128
 129+ protected function insertStudents( array $students, array $courseIds ) {
 130+ foreach ( $students as $student => $courseNames ) {
 131+ $name = $student;
 132+ $user = User::newFromName( $student );
 133+
 134+ if ( $user === false ) {
 135+ echo "Failed to insert student '$name'. (invalid user name)\n";
 136+ }
 137+ else {
 138+ if ( $user->getId() === 0 ) {
 139+ $user->setPassword( 'ohithere' );
 140+ $user->addToDatabase();
 141+ }
 142+
 143+ if ( $user->getId() === 0 ) {
 144+ echo "Failed to insert student '$name'. (failed to create user)\n";
 145+ }
 146+ else {
 147+ $student = EPStudent::newFromUser( $user );
 148+
 149+ if ( $student === false ) {
 150+ $student = new EPStudent(
 151+ array(
 152+ 'user_id' => $user->getId(),
 153+ 'first_enroll' => wfTimestamp( TS_MW )
 154+ ),
 155+ true
 156+ );
 157+
 158+ if ( !$student->save() ) {
 159+ echo "Failed to insert student '$name'. (failed create student profile)\n";
 160+ continue;
 161+ }
 162+ }
 163+
 164+ $courses = array();
 165+
 166+ foreach ( $courseNames as $courseName ) {
 167+ if ( array_key_exists( $courseName, $courseIds ) ) {
 168+ $courses[] = EPCourses::singleton()->newFromArray( array(
 169+ 'id' => $courseIds[$courseName],
 170+ 'students' => array(),
 171+ ) );
 172+ }
 173+ else {
 174+ echo "Failed to associate student '$name' with course '$courseName'.\n";
 175+ }
 176+ }
 177+
 178+ if ( $student->associateWithCourses( $courses ) ) {
 179+ echo "Imported student $name\n";
 180+ }
 181+ else {
 182+ echo "Failed to insert student '$name'. (failed to associate courses)\n";
 183+ }
 184+ }
 185+ }
 186+ }
 187+ }
 188+
116189 }
117190
118191 $maintClass = 'ImportWEPData';
Index: trunk/extensions/EducationProgram/includes/EPRoleObject.php
@@ -137,6 +137,8 @@
138138 public function associateWithCourses( array /* of EPCourse */ $courses ) {
139139 $success = true;
140140
 141+ $courseIds = array();
 142+
141143 foreach ( $courses as /* EPCourse */ $course ) {
142144 $courseIds[] = $course->getId();
143145 $course->setUpdateSummaries( false );

Status & tagging log