https://www.mediawiki.org/wiki/Special:Code/MediaWiki/111905

Revision: 111905
Author:   jeroendedauw
Date:     2012-02-20 01:58:23 +0000 (Mon, 20 Feb 2012)
Log Message:
-----------
finishing up import script

Modified Paths:
--------------
    trunk/extensions/EducationProgram/includes/EPRoleObject.php
    trunk/extensions/EducationProgram/maintenance/importWEPData.php

Modified: trunk/extensions/EducationProgram/includes/EPRoleObject.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPRoleObject.php 2012-02-20 
01:21:09 UTC (rev 111904)
+++ trunk/extensions/EducationProgram/includes/EPRoleObject.php 2012-02-20 
01:58:23 UTC (rev 111905)
@@ -137,6 +137,8 @@
        public function associateWithCourses( array /* of EPCourse */ $courses 
) {
                $success = true;
 
+               $courseIds = array();
+               
                foreach ( $courses as /* EPCourse */ $course ) {
                        $courseIds[] = $course->getId();
                        $course->setUpdateSummaries( false );

Modified: trunk/extensions/EducationProgram/maintenance/importWEPData.php
===================================================================
--- trunk/extensions/EducationProgram/maintenance/importWEPData.php     
2012-02-20 01:21:09 UTC (rev 111904)
+++ trunk/extensions/EducationProgram/maintenance/importWEPData.php     
2012-02-20 01:58:23 UTC (rev 111905)
@@ -62,10 +62,24 @@
                        }
                }
                
-               echo 'Found ' . count( $orgs ) . ' orgs, ' . count( $courses ) 
. ' courses and ' . count( $students ) . " students.\n";
+               echo "\nFound " . count( $orgs ) . ' orgs, ' . count( $courses 
) . ' courses and ' . count( $students ) . " students.\n\n";
                
                echo "Inserting orgs ...";
+               $this->insertOrgs( $orgs );
+               echo " done!\n";
                
+               echo "Inserting courses ...\n";
+               $courseIds = $this->insertCourses( $courses, $orgs );
+               echo "Inserted courses\n";
+               
+               echo "Inserting students ...\n";
+               $this->insertStudents( $students, $courseIds );
+               echo "Inserted students\n";
+               
+               echo "Import completed!\n\n";
+       }
+       
+       protected function insertOrgs( array $orgs ) {
                wfGetDB( DB_MASTER )->begin();
                
                foreach ( $orgs as $org => &$id ) {
@@ -81,12 +95,12 @@
                        $id = $org->getId();
                }
                
-               wfGetDB( DB_MASTER )->commit();
+               wfGetDB( DB_MASTER )->commit(); 
+       }
+       
+       protected function insertCourses( array $courses, array $orgs ) {
+               $courseIds = array();
                
-               echo " done!\n";
-               
-               echo "Inserting courses ...\n";
-               
                foreach ( $courses as $course => $org ) {
                        $name = $course;
                        
@@ -101,17 +115,76 @@
                        
                        try{
                                $course->save();
+                               $courseIds[$name] = $course->getId();
                        }
                        catch ( Exception $ex ) {
                                echo "Failed to insert course '$name'.\n";
                        }
                }
-               
-               echo "Inserted courses\n";
-               
-               echo "\n\n";
+
+               return $courseIds;
        }
        
+       protected function insertStudents( array $students, array $courseIds ) {
+               foreach ( $students as $student => $courseNames ) {
+                       $name = $student;
+                       $user = User::newFromName( $student );
+                       
+                       if ( $user === false ) {
+                               echo "Failed to insert student '$name'. 
(invalid user name)\n";
+                       }
+                       else {
+                               if ( $user->getId() === 0 ) {
+                                       $user->setPassword( 'ohithere' );
+                                       $user->addToDatabase();
+                               }
+                               
+                               if ( $user->getId() === 0 ) {
+                                       echo "Failed to insert student '$name'. 
(failed to create user)\n";
+                               }
+                               else {
+                                       $student = EPStudent::newFromUser( 
$user );
+                                       
+                                       if ( $student === false ) {
+                                               $student = new EPStudent(
+                                                       array(
+                                                               'user_id' => 
$user->getId(),
+                                                               'first_enroll' 
=> wfTimestamp( TS_MW )
+                                                       ),
+                                                       true
+                                               );
+                                               
+                                               if ( !$student->save() ) {
+                                                       echo "Failed to insert 
student '$name'. (failed create student profile)\n";
+                                                       continue;
+                                               }
+                                       }
+                                       
+                                       $courses = array();
+                                       
+                                       foreach ( $courseNames as $courseName ) 
{
+                                               if ( array_key_exists( 
$courseName, $courseIds ) ) {
+                                                       $courses[] = 
EPCourses::singleton()->newFromArray( array(
+                                                               'id' => 
$courseIds[$courseName],
+                                                               'students' => 
array(),
+                                                       ) );
+                                               }
+                                               else {
+                                                       echo "Failed to 
associate student '$name' with course '$courseName'.\n";
+                                               }
+                                       }
+                                       
+                                       if ( $student->associateWithCourses( 
$courses ) ) {
+                                               echo "Imported student $name\n";
+                                       }
+                                       else {
+                                               echo "Failed to insert student 
'$name'. (failed to associate courses)\n";
+                                       }
+                               }
+                       }
+               }       
+       }
+       
 }
 
 $maintClass = 'ImportWEPData';


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to