Index: trunk/extensions/EducationProgram/includes/EPStudent.php |
— | — | @@ -0,0 +1,51 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class representing a single student. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file EPStudent.php |
| 10 | + * @ingroup EducationProgram |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +class EPStudent extends EPDBObject { |
| 16 | + |
| 17 | + /** |
| 18 | + * @see parent::getFieldTypes |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * |
| 22 | + * @return string |
| 23 | + */ |
| 24 | + public static function getDBTable() { |
| 25 | + return 'ep_students'; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @see parent::getFieldTypes |
| 30 | + * |
| 31 | + * @since 0.1 |
| 32 | + * |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + protected static function getFieldPrefix() { |
| 36 | + return 'student_'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @see parent::getFieldTypes |
| 41 | + * |
| 42 | + * @since 0.1 |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + protected static function getFieldTypes() { |
| 47 | + return array( |
| 48 | + 'id' => 'id', |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | +} |
Property changes on: trunk/extensions/EducationProgram/includes/EPStudent.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 53 | + native |
Index: trunk/extensions/EducationProgram/includes/EPOrg.php |
— | — | @@ -0,0 +1,51 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class representing a single organization/institution. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file EPOrg.php |
| 10 | + * @ingroup EducationProgram |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +class EPOrg extends EPDBObject { |
| 16 | + |
| 17 | + /** |
| 18 | + * @see parent::getFieldTypes |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * |
| 22 | + * @return string |
| 23 | + */ |
| 24 | + public static function getDBTable() { |
| 25 | + return 'ep_orgs'; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @see parent::getFieldTypes |
| 30 | + * |
| 31 | + * @since 0.1 |
| 32 | + * |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + protected static function getFieldPrefix() { |
| 36 | + return 'org_'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @see parent::getFieldTypes |
| 41 | + * |
| 42 | + * @since 0.1 |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + protected static function getFieldTypes() { |
| 47 | + return array( |
| 48 | + 'id' => 'id', |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | +} |
Property changes on: trunk/extensions/EducationProgram/includes/EPOrg.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 53 | + native |
Index: trunk/extensions/EducationProgram/includes/EPDBObject.php |
— | — | @@ -0,0 +1,882 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract base class for representing objects that are stored in some DB table. |
| 6 | + * |
| 7 | + * These methods must be implemented in deriving classes: |
| 8 | + * * getDBTable |
| 9 | + * * getFieldPrefix |
| 10 | + * * getFieldTypes |
| 11 | + * |
| 12 | + * These methods are likely candidates for overriding: |
| 13 | + * * getDefaults |
| 14 | + * |
| 15 | + * @since 0.1 |
| 16 | + * |
| 17 | + * @file EPDBObject.php |
| 18 | + * @ingroup Review |
| 19 | + * |
| 20 | + * @licence GNU GPL v3 or later |
| 21 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 22 | + */ |
| 23 | +abstract class EPDBObject { |
| 24 | + |
| 25 | + /** |
| 26 | + * The fields of the object. |
| 27 | + * field name (w/o prefix) => value |
| 28 | + * |
| 29 | + * @since 0.1 |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + protected $fields = array( 'id' => null ); |
| 33 | + |
| 34 | + /** |
| 35 | + * The database connection to use for read operations. |
| 36 | + * |
| 37 | + * @since 0.2 |
| 38 | + * @var integer DB_ enum |
| 39 | + */ |
| 40 | + protected static $readDb = DB_SLAVE; |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns the name of the database table objects of this type are stored in. |
| 44 | + * |
| 45 | + * @since 0.1 |
| 46 | + * |
| 47 | + * @throws MWException |
| 48 | + * @return string |
| 49 | + */ |
| 50 | + public static function getDBTable() { |
| 51 | + throw new MWException( 'Class did not implement getDBTable' ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets the db field prefix. |
| 56 | + * |
| 57 | + * @since 0.1 |
| 58 | + * |
| 59 | + * @throws MWException |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + protected static function getFieldPrefix() { |
| 63 | + throw new MWException( 'Class did not implement getFieldPrefix' ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns an array with the fields and their types this object contains. |
| 68 | + * This corresponds directly to the fields in the database, without prefix. |
| 69 | + * |
| 70 | + * field name => type |
| 71 | + * |
| 72 | + * Allowed types: |
| 73 | + * * id |
| 74 | + * * str |
| 75 | + * * int |
| 76 | + * * float |
| 77 | + * * bool |
| 78 | + * * array |
| 79 | + * |
| 80 | + * @since 0.1 |
| 81 | + * |
| 82 | + * @throws MWException |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + protected static function getFieldTypes() { |
| 86 | + throw new MWException( 'Class did not implement getFieldTypes' ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns a list of default field values. |
| 91 | + * field name => field value |
| 92 | + * |
| 93 | + * @since 0.1 |
| 94 | + * |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public static function getDefaults() { |
| 98 | + return array(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Constructor. |
| 103 | + * |
| 104 | + * @since 0.1 |
| 105 | + * |
| 106 | + * @param array|null $fields |
| 107 | + * @param boolean $loadDefaults |
| 108 | + */ |
| 109 | + public function __construct( $fields = null, $loadDefaults = false ) { |
| 110 | + if ( !is_array( $fields ) ) { |
| 111 | + $fields = array(); |
| 112 | + } |
| 113 | + |
| 114 | + if ( $loadDefaults ) { |
| 115 | + $fields = array_merge( $this->getDefaults(), $fields ); |
| 116 | + } |
| 117 | + |
| 118 | + $this->setFields( $fields ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Load the specified fields from the database. |
| 123 | + * |
| 124 | + * @since 0.1 |
| 125 | + * |
| 126 | + * @param array|null $fields |
| 127 | + * @param boolean $override |
| 128 | + * |
| 129 | + * @return Success indicator |
| 130 | + */ |
| 131 | + public function loadFields( $fields = null, $override = true ) { |
| 132 | + if ( is_null( $this->getId() ) ) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + if ( is_null( $fields ) ) { |
| 137 | + $fields = array_keys( $this->getFieldTypes() ); |
| 138 | + } |
| 139 | + |
| 140 | + $results = $this->rawSelect( |
| 141 | + $this->getPrefixedFields( $fields ), |
| 142 | + array( $this->getPrefixedField( 'id' ) => $this->getId() ), |
| 143 | + array( 'LIMIT' => 1 ) |
| 144 | + ); |
| 145 | + |
| 146 | + foreach ( $results as $result ) { |
| 147 | + $this->setFields( $this->getFieldsFromDBResult( $result ), $override ); |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Gets the value of a field. |
| 156 | + * |
| 157 | + * @since 0.1 |
| 158 | + * |
| 159 | + * @param string $name |
| 160 | + * |
| 161 | + * @throws MWException |
| 162 | + * @return mixed |
| 163 | + */ |
| 164 | + public function getField( $name ) { |
| 165 | + if ( $this->hasField( $name ) ) { |
| 166 | + return $this->fields[$name]; |
| 167 | + } else { |
| 168 | + throw new MWException( 'Attempted to get not-set field ' . $name ); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Remove a field. |
| 174 | + * |
| 175 | + * @since 0.1 |
| 176 | + * |
| 177 | + * @param string $name |
| 178 | + */ |
| 179 | + public function removeField( $name ) { |
| 180 | + unset( $this->fields[$name] ); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Returns the objects database id. |
| 185 | + * |
| 186 | + * @since 0.1 |
| 187 | + * |
| 188 | + * @return integer|null |
| 189 | + */ |
| 190 | + public function getId() { |
| 191 | + return $this->getField( 'id' ); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Sets the objects database id. |
| 196 | + * |
| 197 | + * @since 0.1 |
| 198 | + * |
| 199 | + * @param integere|null $id |
| 200 | + */ |
| 201 | + public function setId( $id ) { |
| 202 | + return $this->setField( 'id', $id ); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Gets if a certain field is set. |
| 207 | + * |
| 208 | + * @since 0.1 |
| 209 | + * |
| 210 | + * @param string $name |
| 211 | + * |
| 212 | + * @return boolean |
| 213 | + */ |
| 214 | + public function hasField( $name ) { |
| 215 | + return array_key_exists( $name, $this->fields ); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Gets if the id field is set. |
| 220 | + * |
| 221 | + * @since 0.1 |
| 222 | + * |
| 223 | + * @return boolean |
| 224 | + */ |
| 225 | + public function hasIdField() { |
| 226 | + return $this->hasField( 'id' ) |
| 227 | + && !is_null( $this->getField( 'id' ) ); |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Sets multiple fields. |
| 232 | + * |
| 233 | + * @since 0.1 |
| 234 | + * |
| 235 | + * @param array $fields The fields to set |
| 236 | + * @param boolean $override Override already set fields with the provided values? |
| 237 | + */ |
| 238 | + public function setFields( array $fields, $override = true ) { |
| 239 | + foreach ( $fields as $name => $value ) { |
| 240 | + if ( $override || !$this->hasField( $name ) ) { |
| 241 | + $this->setField( $name, $value ); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Gets the fields => values to write to the table. |
| 248 | + * |
| 249 | + * @since 0.1 |
| 250 | + * |
| 251 | + * @return array |
| 252 | + */ |
| 253 | + protected function getWriteValues() { |
| 254 | + $values = array(); |
| 255 | + |
| 256 | + foreach ( $this->getFieldTypes() as $name => $type ) { |
| 257 | + if ( array_key_exists( $name, $this->fields ) ) { |
| 258 | + $value = $this->fields[$name]; |
| 259 | + |
| 260 | + switch ( $type ) { |
| 261 | + case 'array': |
| 262 | + $value = serialize( (array)$value ); |
| 263 | + } |
| 264 | + |
| 265 | + $values[$this->getFieldPrefix() . $name] = $value; |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + return $values; |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Serializes the object to an associative array which |
| 274 | + * can then easily be converted into JSON or similar. |
| 275 | + * |
| 276 | + * @since 0.1 |
| 277 | + * |
| 278 | + * @param null|array $fields |
| 279 | + * @param boolean $incNullId |
| 280 | + * |
| 281 | + * @return array |
| 282 | + */ |
| 283 | + public function toArray( $fields = null, $incNullId = false ) { |
| 284 | + $data = array(); |
| 285 | + $setFields = array(); |
| 286 | + |
| 287 | + if ( !is_array( $fields ) ) { |
| 288 | + $setFields = $this->getSetFieldNames(); |
| 289 | + } else { |
| 290 | + foreach ( $fields as $field ) { |
| 291 | + if ( $this->hasField( $field ) ) { |
| 292 | + $setFields[] = $field; |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + foreach ( $setFields as $field ) { |
| 298 | + if ( $incNullId || $field != 'id' || $this->hasIdField() ) { |
| 299 | + $data[$field] = $this->getField( $field ); |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + return $data; |
| 304 | + } |
| 305 | + |
| 306 | + /** |
| 307 | + * Load the default values, via getDefaults. |
| 308 | + * |
| 309 | + * @since 0.1 |
| 310 | + * |
| 311 | + * @param boolean $override |
| 312 | + */ |
| 313 | + public function loadDefaults( $override = true ) { |
| 314 | + $this->setFields( $this->getDefaults(), $override ); |
| 315 | + } |
| 316 | + |
| 317 | + /** |
| 318 | + * Writes the answer to the database, either updating it |
| 319 | + * when it already exists, or inserting it when it doesn't. |
| 320 | + * |
| 321 | + * @since 0.1 |
| 322 | + * |
| 323 | + * @return boolean Success indicator |
| 324 | + */ |
| 325 | + public function writeToDB() { |
| 326 | + if ( $this->hasIdField() ) { |
| 327 | + return $this->updateInDB(); |
| 328 | + } else { |
| 329 | + return $this->insertIntoDB(); |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * Updates the object in the database. |
| 335 | + * |
| 336 | + * @since 0.1 |
| 337 | + * |
| 338 | + * @return boolean Success indicator |
| 339 | + */ |
| 340 | + protected function updateInDB() { |
| 341 | + $dbw = wfGetDB( DB_MASTER ); |
| 342 | + |
| 343 | + return $dbw->update( |
| 344 | + $this->getDBTable(), |
| 345 | + $this->getWriteValues(), |
| 346 | + array( $this->getFieldPrefix() . 'id' => $this->getId() ), |
| 347 | + __METHOD__ |
| 348 | + ); |
| 349 | + } |
| 350 | + |
| 351 | + /** |
| 352 | + * Inserts the object into the database. |
| 353 | + * |
| 354 | + * @since 0.1 |
| 355 | + * |
| 356 | + * @return boolean Success indicator |
| 357 | + */ |
| 358 | + protected function insertIntoDB() { |
| 359 | + $dbw = wfGetDB( DB_MASTER ); |
| 360 | + |
| 361 | + $result = $dbw->insert( |
| 362 | + $this->getDBTable(), |
| 363 | + $this->getWriteValues(), |
| 364 | + __METHOD__, |
| 365 | + array( 'IGNORE' ) |
| 366 | + ); |
| 367 | + |
| 368 | + $this->setField( 'id', $dbw->insertId() ); |
| 369 | + |
| 370 | + return $result; |
| 371 | + } |
| 372 | + |
| 373 | + /** |
| 374 | + * Removes the object from the database. |
| 375 | + * |
| 376 | + * @since 0.1 |
| 377 | + * |
| 378 | + * @return boolean Success indicator |
| 379 | + */ |
| 380 | + public function removeFromDB() { |
| 381 | + $success = $this->delete( array( 'id' => $this->getId() ) ); |
| 382 | + |
| 383 | + if ( $success ) { |
| 384 | + $this->setField( 'id', null ); |
| 385 | + } |
| 386 | + |
| 387 | + return $success; |
| 388 | + } |
| 389 | + |
| 390 | + /** |
| 391 | + * Return the names and values of the fields. |
| 392 | + * |
| 393 | + * @since 0.1 |
| 394 | + * |
| 395 | + * @return array |
| 396 | + */ |
| 397 | + public function getFields() { |
| 398 | + return $this->fields; |
| 399 | + } |
| 400 | + |
| 401 | + /** |
| 402 | + * Return the names of the fields. |
| 403 | + * |
| 404 | + * @since 0.1 |
| 405 | + * |
| 406 | + * @return array |
| 407 | + */ |
| 408 | + public function getSetFieldNames() { |
| 409 | + return array_keys( $this->fields ); |
| 410 | + } |
| 411 | + |
| 412 | + /** |
| 413 | + * Sets the value of a field. |
| 414 | + * Strings can be provided for other types, |
| 415 | + * so this method can be called from unserialization handlers. |
| 416 | + * |
| 417 | + * @since 0.1 |
| 418 | + * |
| 419 | + * @param string $name |
| 420 | + * @param mixed $value |
| 421 | + * @param boolean $haxPrefix |
| 422 | + * |
| 423 | + * @throws MWException |
| 424 | + */ |
| 425 | + public function setField( $name, $value ) { |
| 426 | + $fields = $this->getFieldTypes(); |
| 427 | + |
| 428 | + if ( array_key_exists( $name, $fields ) ) { |
| 429 | + switch ( $fields[$name] ) { |
| 430 | + case 'int': |
| 431 | + $value = (int)$value; |
| 432 | + break; |
| 433 | + case 'float': |
| 434 | + $value = (float)$value; |
| 435 | + break; |
| 436 | + case 'bool': |
| 437 | + if ( is_string( $value ) ) { |
| 438 | + $value = $value !== '0'; |
| 439 | + } elseif ( is_int( $value ) ) { |
| 440 | + $value = $value !== 0; |
| 441 | + } |
| 442 | + break; |
| 443 | + case 'array': |
| 444 | + if ( is_string( $value ) ) { |
| 445 | + $value = unserialize( $value ); |
| 446 | + } |
| 447 | + break; |
| 448 | + case 'id': |
| 449 | + if ( is_string( $value ) ) { |
| 450 | + $value = (int)$value; |
| 451 | + } |
| 452 | + break; |
| 453 | + } |
| 454 | + |
| 455 | + $this->fields[$name] = $value; |
| 456 | + } else { |
| 457 | + throw new MWException( 'Attempted to set unknown field ' . $name ); |
| 458 | + } |
| 459 | + } |
| 460 | + |
| 461 | + /** |
| 462 | + * Get a new instance of the class from an array. |
| 463 | + * |
| 464 | + * @since 0.1 |
| 465 | + * |
| 466 | + * @param array $data |
| 467 | + * @param boolean $loadDefaults |
| 468 | + * |
| 469 | + * @return EPDBObject |
| 470 | + */ |
| 471 | + public static function newFromArray( array $data, $loadDefaults = false ) { |
| 472 | + return new static( $data, $loadDefaults ); |
| 473 | + } |
| 474 | + |
| 475 | + /** |
| 476 | + * Get the database type used for read operations. |
| 477 | + * |
| 478 | + * @since 0.2 |
| 479 | + * @return integer DB_ enum |
| 480 | + */ |
| 481 | + public static function getReadDb() { |
| 482 | + return self::$readDb; |
| 483 | + } |
| 484 | + |
| 485 | + /** |
| 486 | + * Set the database type to use for read operations. |
| 487 | + * |
| 488 | + * @param integer $db |
| 489 | + * |
| 490 | + * @since 0.2 |
| 491 | + */ |
| 492 | + public static function setReadDb( $db ) { |
| 493 | + self::$readDb = $db; |
| 494 | + } |
| 495 | + |
| 496 | + /** |
| 497 | + * Gets if the object can take a certain field. |
| 498 | + * |
| 499 | + * @since 0.1 |
| 500 | + * |
| 501 | + * @param string $name |
| 502 | + * |
| 503 | + * @return boolean |
| 504 | + */ |
| 505 | + public static function canHasField( $name ) { |
| 506 | + return array_key_exists( $name, static::getFieldTypes() ); |
| 507 | + } |
| 508 | + |
| 509 | + /** |
| 510 | + * Takes in a field or array of fields and returns an |
| 511 | + * array with their prefixed versions, ready for db usage. |
| 512 | + * |
| 513 | + * @since 0.1 |
| 514 | + * |
| 515 | + * @param array|string $fields |
| 516 | + * |
| 517 | + * @return array |
| 518 | + */ |
| 519 | + public static function getPrefixedFields( $fields ) { |
| 520 | + $fields = (array)$fields; |
| 521 | + |
| 522 | + foreach ( $fields as &$field ) { |
| 523 | + $field = static::getFieldPrefix() . $field; |
| 524 | + } |
| 525 | + |
| 526 | + return $fields; |
| 527 | + } |
| 528 | + |
| 529 | + /** |
| 530 | + * Takes in a field and returns an it's prefixed version, ready for db usage. |
| 531 | + * |
| 532 | + * @since 0.1 |
| 533 | + * |
| 534 | + * @param string $field |
| 535 | + * |
| 536 | + * @return string |
| 537 | + */ |
| 538 | + public static function getPrefixedField( $field ) { |
| 539 | + return static::getFieldPrefix() . $field; |
| 540 | + } |
| 541 | + |
| 542 | + /** |
| 543 | + * Takes in an associative array with field names as keys and |
| 544 | + * their values as value. The field names are prefixed with the |
| 545 | + * db field prefix. |
| 546 | + * |
| 547 | + * @since 0.1 |
| 548 | + * |
| 549 | + * @param array $values |
| 550 | + * |
| 551 | + * @return array |
| 552 | + */ |
| 553 | + public static function getPrefixedValues( array $values ) { |
| 554 | + $prefixedValues = array(); |
| 555 | + |
| 556 | + foreach ( $values as $field => $value ) { |
| 557 | + $prefixedValues[static::getFieldPrefix() . $field] = $value; |
| 558 | + } |
| 559 | + |
| 560 | + return $prefixedValues; |
| 561 | + } |
| 562 | + |
| 563 | + /** |
| 564 | + * Get an array with fields from a database result, |
| 565 | + * that can be fed directly to the constructor or |
| 566 | + * to setFields. |
| 567 | + * |
| 568 | + * @since 0.1 |
| 569 | + * |
| 570 | + * @param object $result |
| 571 | + * |
| 572 | + * @return array |
| 573 | + */ |
| 574 | + public static function getFieldsFromDBResult( $result ) { |
| 575 | + $result = (array)$result; |
| 576 | + return array_combine( |
| 577 | + static::unprefixFieldNames( array_keys( $result ) ), |
| 578 | + array_values( $result ) |
| 579 | + ); |
| 580 | + } |
| 581 | + |
| 582 | + /** |
| 583 | + * Takes a field name with prefix and returns the unprefixed equivalent. |
| 584 | + * |
| 585 | + * @since 0.1 |
| 586 | + * |
| 587 | + * @param string $fieldName |
| 588 | + * |
| 589 | + * @return string |
| 590 | + */ |
| 591 | + public static function unprefixFieldName( $fieldName ) { |
| 592 | + return substr( $fieldName, strlen( static::getFieldPrefix() ) ); |
| 593 | + } |
| 594 | + |
| 595 | + /** |
| 596 | + * Takes an array of field names with prefix and returns the unprefixed equivalent. |
| 597 | + * |
| 598 | + * @since 0.1 |
| 599 | + * |
| 600 | + * @param array $fieldNames |
| 601 | + * |
| 602 | + * @return array |
| 603 | + */ |
| 604 | + public static function unprefixFieldNames( array $fieldNames ) { |
| 605 | + return array_map( 'static::unprefixFieldName', $fieldNames ); |
| 606 | + } |
| 607 | + |
| 608 | + /** |
| 609 | + * Get a new instance of the class from a database result. |
| 610 | + * |
| 611 | + * @since 0.1 |
| 612 | + * |
| 613 | + * @param stdClass $result |
| 614 | + * |
| 615 | + * @return EPDBObject |
| 616 | + */ |
| 617 | + public static function newFromDBResult( stdClass $result ) { |
| 618 | + return static::newFromArray( static::getFieldsFromDBResult( $result ) ); |
| 619 | + } |
| 620 | + |
| 621 | + /** |
| 622 | + * Removes the object from the database. |
| 623 | + * |
| 624 | + * @since 0.1 |
| 625 | + * |
| 626 | + * @param array $conditions |
| 627 | + * |
| 628 | + * @return boolean Success indicator |
| 629 | + */ |
| 630 | + public static function delete( array $conditions ) { |
| 631 | + return wfGetDB( DB_MASTER )->delete( |
| 632 | + static::getDBTable(), |
| 633 | + static::getPrefixedValues( $conditions ) |
| 634 | + ); |
| 635 | + } |
| 636 | + |
| 637 | + /** |
| 638 | + * Add an amount (can be negative) to the specified field (needs to be numeric). |
| 639 | + * |
| 640 | + * @since 0.1 |
| 641 | + * |
| 642 | + * @param string $field |
| 643 | + * @param integer $amount |
| 644 | + * |
| 645 | + * @return boolean Success indicator |
| 646 | + */ |
| 647 | + public static function addToField( $field, $amount ) { |
| 648 | + if ( $amount == 0 ) { |
| 649 | + return true; |
| 650 | + } |
| 651 | + |
| 652 | + if ( !static::hasIdField() ) { |
| 653 | + return false; |
| 654 | + } |
| 655 | + |
| 656 | + $absoluteAmount = abs( $amount ); |
| 657 | + $isNegative = $amount < 0; |
| 658 | + |
| 659 | + $dbw = wfGetDB( DB_MASTER ); |
| 660 | + |
| 661 | + $fullField = static::getPrefixedField( $field ); |
| 662 | + |
| 663 | + $success = $dbw->update( |
| 664 | + static::getDBTable(), |
| 665 | + array( "$fullField=$fullField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ), |
| 666 | + array( static::getPrefixedField( 'id' ) => static::getId() ), |
| 667 | + __METHOD__ |
| 668 | + ); |
| 669 | + |
| 670 | + if ( $success && static::hasField( $field ) ) { |
| 671 | + static::setField( $field, static::getField( $field ) + $amount ); |
| 672 | + } |
| 673 | + |
| 674 | + return $success; |
| 675 | + } |
| 676 | + |
| 677 | + /** |
| 678 | + * Selects the the specified fields of the records matching the provided |
| 679 | + * conditions. Field names get prefixed. |
| 680 | + * |
| 681 | + * @since 0.1 |
| 682 | + * |
| 683 | + * @param array|string|null $fields |
| 684 | + * @param array $conditions |
| 685 | + * @param array $options |
| 686 | + * |
| 687 | + * @return array of self |
| 688 | + */ |
| 689 | + public static function select( $fields = null, array $conditions = array(), array $options = array() ) { |
| 690 | + if ( is_null( $fields ) ) { |
| 691 | + $fields = array_keys( static::getFieldTypes() ); |
| 692 | + } |
| 693 | + |
| 694 | + $result = static::rawSelect( |
| 695 | + static::getPrefixedFields( $fields ), |
| 696 | + static::getPrefixedValues( $conditions ), |
| 697 | + $options |
| 698 | + ); |
| 699 | + |
| 700 | + $objects = array(); |
| 701 | + |
| 702 | + foreach ( $result as $record ) { |
| 703 | + $objects[] = static::newFromDBResult( $record ); |
| 704 | + } |
| 705 | + |
| 706 | + return $objects; |
| 707 | + } |
| 708 | + |
| 709 | + /** |
| 710 | + * Selects the the specified fields of the first matching record. |
| 711 | + * Field names get prefixed. |
| 712 | + * |
| 713 | + * @since 0.1 |
| 714 | + * |
| 715 | + * @param array|string|null $fields |
| 716 | + * @param array $conditions |
| 717 | + * @param array $options |
| 718 | + * |
| 719 | + * @return self|false |
| 720 | + */ |
| 721 | + public static function selectRow( $fields = null, array $conditions = array(), array $options = array() ) { |
| 722 | + $options['LIMIT'] = 1; |
| 723 | + |
| 724 | + $objects = static::select( $fields, $conditions, $options ); |
| 725 | + |
| 726 | + return count( $objects ) > 0 ? $objects[0] : false; |
| 727 | + } |
| 728 | + |
| 729 | + /** |
| 730 | + * Returns if there is at least one record matching the provided conditions. |
| 731 | + * Condition field names get prefixed. |
| 732 | + * |
| 733 | + * @since 0.1 |
| 734 | + * |
| 735 | + * @param array $conditions |
| 736 | + * |
| 737 | + * @return boolean |
| 738 | + */ |
| 739 | + public static function has( array $conditions = array() ) { |
| 740 | + return static::selectRow( array( 'id' ), $conditions ) !== false; |
| 741 | + } |
| 742 | + |
| 743 | + /** |
| 744 | + * Returns the amount of matching records. |
| 745 | + * Condition field names get prefixed. |
| 746 | + * |
| 747 | + * @since 0.1 |
| 748 | + * |
| 749 | + * @param array $conditions |
| 750 | + * @param array $options |
| 751 | + * |
| 752 | + * @return integer |
| 753 | + */ |
| 754 | + public static function count( array $conditions = array(), array $options = array() ) { |
| 755 | + $res = static::rawSelect( |
| 756 | + array( 'COUNT(*) AS rowcount' ), |
| 757 | + static::getPrefixedValues( $conditions ), |
| 758 | + $options |
| 759 | + )->fetchObject(); |
| 760 | + |
| 761 | + return $res->rowcount; |
| 762 | + } |
| 763 | + |
| 764 | + /** |
| 765 | + * Selects the the specified fields of the records matching the provided |
| 766 | + * conditions. Field names do NOT get prefixed. |
| 767 | + * |
| 768 | + * @since 0.1 |
| 769 | + * |
| 770 | + * @param array|null $fields |
| 771 | + * @param array $conditions |
| 772 | + * @param array $options |
| 773 | + * |
| 774 | + * @return ResultWrapper |
| 775 | + */ |
| 776 | + public static function rawSelect( $fields = null, array $conditions = array(), array $options = array() ) { |
| 777 | + $dbr = wfGetDB( static::getReadDb() ); |
| 778 | + |
| 779 | + return $dbr->select( |
| 780 | + static::getDBTable(), |
| 781 | + $fields, |
| 782 | + count( $conditions ) == 0 ? '' : $conditions, |
| 783 | + __METHOD__, |
| 784 | + $options |
| 785 | + ); |
| 786 | + } |
| 787 | + |
| 788 | + /** |
| 789 | + * Update the records matching the provided conditions by |
| 790 | + * setting the fields that are keys in the $values patam to |
| 791 | + * their corresponding values. |
| 792 | + * |
| 793 | + * @since 0.1 |
| 794 | + * |
| 795 | + * @param array $values |
| 796 | + * @param array $conditions |
| 797 | + * |
| 798 | + * @return boolean Success indicator |
| 799 | + */ |
| 800 | + public static function update( array $values, array $conditions = array() ) { |
| 801 | + $dbw = wfGetDB( DB_MASTER ); |
| 802 | + |
| 803 | + return $dbw->update( |
| 804 | + static::getDBTable(), |
| 805 | + static::getPrefixedValues( $values ), |
| 806 | + static::getPrefixedValues( $conditions ), |
| 807 | + __METHOD__ |
| 808 | + ); |
| 809 | + } |
| 810 | + |
| 811 | + /** |
| 812 | + * Return the names of the fields. |
| 813 | + * |
| 814 | + * @since 0.1 |
| 815 | + * |
| 816 | + * @return array |
| 817 | + */ |
| 818 | + public static function getFieldNames() { |
| 819 | + return array_keys( static::getFieldTypes() ); |
| 820 | + } |
| 821 | + |
| 822 | + /** |
| 823 | + * Returns an array with the fields and their descriptions. |
| 824 | + * |
| 825 | + * field name => field description |
| 826 | + * |
| 827 | + * @since 0.1 |
| 828 | + * |
| 829 | + * @return array |
| 830 | + */ |
| 831 | + public static function getFieldDescriptions() { |
| 832 | + return array(); |
| 833 | + } |
| 834 | + |
| 835 | + /** |
| 836 | + * Get API parameters for the fields supported by this object. |
| 837 | + * |
| 838 | + * @since 0.1 |
| 839 | + * |
| 840 | + * @param boolean $requireParams |
| 841 | + * @param boolean $setDefaults |
| 842 | + * |
| 843 | + * @return array |
| 844 | + */ |
| 845 | + public static function getAPIParams( $requireParams = false, $setDefaults = false ) { |
| 846 | + $typeMap = array( |
| 847 | + 'id' => 'integer', |
| 848 | + 'int' => 'integer', |
| 849 | + 'float' => 'NULL', |
| 850 | + 'str' => 'string', |
| 851 | + 'bool' => 'integer', |
| 852 | + 'array' => 'string' |
| 853 | + ); |
| 854 | + |
| 855 | + $params = array(); |
| 856 | + $defaults = static::getDefaults(); |
| 857 | + |
| 858 | + foreach ( static::getFieldTypes() as $field => $type ) { |
| 859 | + if ( $field == 'id' ) { |
| 860 | + continue; |
| 861 | + } |
| 862 | + |
| 863 | + $hasDefault = array_key_exists( $field, $defaults ); |
| 864 | + |
| 865 | + $params[$field] = array( |
| 866 | + ApiBase::PARAM_TYPE => $typeMap[$type], |
| 867 | + ApiBase::PARAM_REQUIRED => $requireParams && !$hasDefault |
| 868 | + ); |
| 869 | + |
| 870 | + if ( $type == 'array' ) { |
| 871 | + $params[$field][ApiBase::PARAM_ISMULTI] = true; |
| 872 | + } |
| 873 | + |
| 874 | + if ( $setDefaults && $hasDefault ) { |
| 875 | + $default = is_array( $defaults[$field] ) ? implode( '|', $defaults[$field] ) : $defaults[$field]; |
| 876 | + $params[$field][ApiBase::PARAM_DFLT] = $default; |
| 877 | + } |
| 878 | + } |
| 879 | + |
| 880 | + return $params; |
| 881 | + } |
| 882 | + |
| 883 | +} |
Property changes on: trunk/extensions/EducationProgram/includes/EPDBObject.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 884 | + native |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -0,0 +1,51 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class representing a single course. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file EPCourse.php |
| 10 | + * @ingroup EducationProgram |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +class EPCourse extends EPDBObject { |
| 16 | + |
| 17 | + /** |
| 18 | + * @see parent::getFieldTypes |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * |
| 22 | + * @return string |
| 23 | + */ |
| 24 | + public static function getDBTable() { |
| 25 | + return 'ep_courses'; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @see parent::getFieldTypes |
| 30 | + * |
| 31 | + * @since 0.1 |
| 32 | + * |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + protected static function getFieldPrefix() { |
| 36 | + return 'course_'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @see parent::getFieldTypes |
| 41 | + * |
| 42 | + * @since 0.1 |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + protected static function getFieldTypes() { |
| 47 | + return array( |
| 48 | + 'id' => 'id', |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | +} |
Property changes on: trunk/extensions/EducationProgram/includes/EPCourse.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 53 | + native |
Index: trunk/extensions/EducationProgram/includes/EPMentor.php |
— | — | @@ -0,0 +1,51 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class representing a single mentor. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file EPMentor.php |
| 10 | + * @ingroup EducationProgram |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +class EPMentor extends EPDBObject { |
| 16 | + |
| 17 | + /** |
| 18 | + * @see parent::getFieldTypes |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * |
| 22 | + * @return string |
| 23 | + */ |
| 24 | + public static function getDBTable() { |
| 25 | + return 'ep_mentors'; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @see parent::getFieldTypes |
| 30 | + * |
| 31 | + * @since 0.1 |
| 32 | + * |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + protected static function getFieldPrefix() { |
| 36 | + return 'mentor_'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @see parent::getFieldTypes |
| 41 | + * |
| 42 | + * @since 0.1 |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + protected static function getFieldTypes() { |
| 47 | + return array( |
| 48 | + 'id' => 'id', |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | +} |
Property changes on: trunk/extensions/EducationProgram/includes/EPMentor.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 53 | + native |
Index: trunk/extensions/EducationProgram/EducationProgram.php |
— | — | @@ -49,10 +49,11 @@ |
50 | 50 | $wgAutoloadClasses['EPHooks'] = dirname( __FILE__ ) . '/EducationProgram.hooks.php'; |
51 | 51 | $wgAutoloadClasses['EPSettings'] = dirname( __FILE__ ) . '/EducationProgram.settings.php'; |
52 | 52 | |
| 53 | +$wgAutoloadClasses['EPCourse'] = dirname( __FILE__ ) . '/includes/EPCourse.php'; |
| 54 | +$wgAutoloadClasses['EPDBObject'] = dirname( __FILE__ ) . '/includes/EPDBObject.php'; |
| 55 | +$wgAutoloadClasses['EPMentor'] = dirname( __FILE__ ) . '/includes/EPMentor.php'; |
53 | 56 | $wgAutoloadClasses['EPOrg'] = dirname( __FILE__ ) . '/includes/EPOrg.php'; |
54 | 57 | $wgAutoloadClasses['EPStudent'] = dirname( __FILE__ ) . '/includes/EPStudent.php'; |
55 | | -$wgAutoloadClasses['EPMentor'] = dirname( __FILE__ ) . '/includes/EPMentor.php'; |
56 | | -$wgAutoloadClasses['EPCourse'] = dirname( __FILE__ ) . '/includes/EPCourse.php'; |
57 | 58 | |
58 | 59 | $wgAutoloadClasses['SpecialMyCourses'] = dirname( __FILE__ ) . '/specials/SpecialMyCourses.php'; |
59 | 60 | $wgAutoloadClasses['SpecialInstitution'] = dirname( __FILE__ ) . '/specials/SpecialInstitution.php'; |