Index: trunk/extensions/EducationProgram/compat/DBTable.php |
— | — | @@ -1,658 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Abstract base class for representing a single database table. |
6 | | - * |
7 | | - * @since 1.20 |
8 | | - * |
9 | | - * @file DBTable.php |
10 | | - * |
11 | | - * @licence GNU GPL v2 or later |
12 | | - * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
13 | | - */ |
14 | | -abstract class DBTable { |
15 | | - |
16 | | - /** |
17 | | - * Returns the name of the database table objects of this type are stored in. |
18 | | - * |
19 | | - * @since 1.20 |
20 | | - * |
21 | | - * @return string |
22 | | - */ |
23 | | - public abstract function getDBTable(); |
24 | | - |
25 | | - /** |
26 | | - * Returns the name of a DBDataObject deriving class that |
27 | | - * represents single rows in this table. |
28 | | - * |
29 | | - * @since 1.20 |
30 | | - * |
31 | | - * @return string |
32 | | - */ |
33 | | - public abstract function getDataObjectClass(); |
34 | | - |
35 | | - /** |
36 | | - * Gets the db field prefix. |
37 | | - * |
38 | | - * @since 1.20 |
39 | | - * |
40 | | - * @return string |
41 | | - */ |
42 | | - protected abstract function getFieldPrefix(); |
43 | | - |
44 | | - /** |
45 | | - * Returns an array with the fields and their types this object contains. |
46 | | - * This corresponds directly to the fields in the database, without prefix. |
47 | | - * |
48 | | - * field name => type |
49 | | - * |
50 | | - * Allowed types: |
51 | | - * * id |
52 | | - * * str |
53 | | - * * int |
54 | | - * * float |
55 | | - * * bool |
56 | | - * * array |
57 | | - * |
58 | | - * @since 1.20 |
59 | | - * |
60 | | - * @return array |
61 | | - */ |
62 | | - public abstract function getFieldTypes(); |
63 | | - |
64 | | - /** |
65 | | - * The database connection to use for read operations. |
66 | | - * Can be changed via @see setReadDb. |
67 | | - * |
68 | | - * @since 1.20 |
69 | | - * @var integer DB_ enum |
70 | | - */ |
71 | | - protected $readDb = DB_SLAVE; |
72 | | - |
73 | | - /** |
74 | | - * Returns a list of default field values. |
75 | | - * field name => field value |
76 | | - * |
77 | | - * @since 1.20 |
78 | | - * |
79 | | - * @return array |
80 | | - */ |
81 | | - public function getDefaults() { |
82 | | - return array(); |
83 | | - } |
84 | | - |
85 | | - /** |
86 | | - * Returns a list of the summary fields. |
87 | | - * These are fields that cache computed values, such as the amount of linked objects of $type. |
88 | | - * This is relevant as one might not want to do actions such as log changes when these get updated. |
89 | | - * |
90 | | - * @since 1.20 |
91 | | - * |
92 | | - * @return array |
93 | | - */ |
94 | | - public function getSummaryFields() { |
95 | | - return array(); |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Selects the the specified fields of the records matching the provided |
100 | | - * conditions and returns them as DBDataObject. Field names get prefixed. |
101 | | - * |
102 | | - * @since 1.20 |
103 | | - * |
104 | | - * @param array|string|null $fields |
105 | | - * @param array $conditions |
106 | | - * @param array $options |
107 | | - * @param string|null $functionName |
108 | | - * |
109 | | - * @return array of self |
110 | | - */ |
111 | | - public function select( $fields = null, array $conditions = array(), array $options = array(), $functionName = null ) { |
112 | | - $result = $this->selectFields( $fields, $conditions, $options, false, $functionName ); |
113 | | - |
114 | | - $objects = array(); |
115 | | - |
116 | | - foreach ( $result as $record ) { |
117 | | - $objects[] = $this->newFromArray( $record ); |
118 | | - } |
119 | | - |
120 | | - return $objects; |
121 | | - } |
122 | | - |
123 | | - /** |
124 | | - * Selects the the specified fields of the records matching the provided |
125 | | - * conditions and returns them as associative arrays. |
126 | | - * Provided field names get prefixed. |
127 | | - * Returned field names will not have a prefix. |
128 | | - * |
129 | | - * When $collapse is true: |
130 | | - * If one field is selected, each item in the result array will be this field. |
131 | | - * If two fields are selected, each item in the result array will have as key |
132 | | - * the first field and as value the second field. |
133 | | - * If more then two fields are selected, each item will be an associative array. |
134 | | - * |
135 | | - * @since 1.20 |
136 | | - * |
137 | | - * @param array|string|null $fields |
138 | | - * @param array $conditions |
139 | | - * @param array $options |
140 | | - * @param boolean $collapse Set to false to always return each result row as associative array. |
141 | | - * @param string|null $functionName |
142 | | - * |
143 | | - * @return array of array |
144 | | - */ |
145 | | - public function selectFields( $fields = null, array $conditions = array(), array $options = array(), $collapse = true, $functionName = null ) { |
146 | | - if ( is_null( $fields ) ) { |
147 | | - $fields = array_keys( $this->getFieldTypes() ); |
148 | | - } |
149 | | - else { |
150 | | - $fields = (array)$fields; |
151 | | - } |
152 | | - |
153 | | - $dbr = wfGetDB( $this->getReadDb() ); |
154 | | - $result = $dbr->select( |
155 | | - $this->getDBTable(), |
156 | | - $this->getPrefixedFields( $fields ), |
157 | | - $this->getPrefixedValues( $conditions ), |
158 | | - is_null( $functionName ) ? __METHOD__ : $functionName, |
159 | | - $options |
160 | | - ); |
161 | | - |
162 | | - $objects = array(); |
163 | | - |
164 | | - foreach ( $result as $record ) { |
165 | | - $objects[] = $this->getFieldsFromDBResult( $record ); |
166 | | - } |
167 | | - |
168 | | - if ( $collapse ) { |
169 | | - if ( count( $fields ) === 1 ) { |
170 | | - $objects = array_map( 'array_shift', $objects ); |
171 | | - } |
172 | | - elseif ( count( $fields ) === 2 ) { |
173 | | - $o = array(); |
174 | | - |
175 | | - foreach ( $objects as $object ) { |
176 | | - $o[array_shift( $object )] = array_shift( $object ); |
177 | | - } |
178 | | - |
179 | | - $objects = $o; |
180 | | - } |
181 | | - } |
182 | | - |
183 | | - return $objects; |
184 | | - } |
185 | | - |
186 | | - /** |
187 | | - * Selects the the specified fields of the first matching record. |
188 | | - * Field names get prefixed. |
189 | | - * |
190 | | - * @since 1.20 |
191 | | - * |
192 | | - * @param array|string|null $fields |
193 | | - * @param array $conditions |
194 | | - * @param array $options |
195 | | - * @param string|null $functionName |
196 | | - * |
197 | | - * @return DBObject|bool False on failure |
198 | | - */ |
199 | | - public function selectRow( $fields = null, array $conditions = array(), array $options = array(), $functionName = null ) { |
200 | | - $options['LIMIT'] = 1; |
201 | | - |
202 | | - $objects = $this->select( $fields, $conditions, $options, $functionName ); |
203 | | - |
204 | | - return empty( $objects ) ? false : $objects[0]; |
205 | | - } |
206 | | - |
207 | | - /** |
208 | | - * Selects the the specified fields of the records matching the provided |
209 | | - * conditions. Field names do NOT get prefixed. |
210 | | - * |
211 | | - * @since 1.20 |
212 | | - * |
213 | | - * @param array $fields |
214 | | - * @param array $conditions |
215 | | - * @param array $options |
216 | | - * @param string|null $functionName |
217 | | - * |
218 | | - * @return ResultWrapper |
219 | | - */ |
220 | | - public function rawSelectRow( array $fields, array $conditions = array(), array $options = array(), $functionName = null ) { |
221 | | - $dbr = wfGetDB( $this->getReadDb() ); |
222 | | - |
223 | | - return $dbr->selectRow( |
224 | | - $this->getDBTable(), |
225 | | - $fields, |
226 | | - $conditions, |
227 | | - is_null( $functionName ) ? __METHOD__ : $functionName, |
228 | | - $options |
229 | | - ); |
230 | | - } |
231 | | - |
232 | | - /** |
233 | | - * Selects the the specified fields of the first record matching the provided |
234 | | - * conditions and returns it as an associative array, or false when nothing matches. |
235 | | - * This method makes use of selectFields and expects the same parameters and |
236 | | - * returns the same results (if there are any, if there are none, this method returns false). |
237 | | - * @see DBDataObject::selectFields |
238 | | - * |
239 | | - * @since 1.20 |
240 | | - * |
241 | | - * @param array|string|null $fields |
242 | | - * @param array $conditions |
243 | | - * @param array $options |
244 | | - * @param boolean $collapse Set to false to always return each result row as associative array. |
245 | | - * @param string|null $functionName |
246 | | - * |
247 | | - * @return mixed|array|bool False on failure |
248 | | - */ |
249 | | - public function selectFieldsRow( $fields = null, array $conditions = array(), array $options = array(), $collapse = true, $functionName = null ) { |
250 | | - $options['LIMIT'] = 1; |
251 | | - |
252 | | - $objects = $this->selectFields( $fields, $conditions, $options, $collapse, $functionName ); |
253 | | - |
254 | | - return empty( $objects ) ? false : $objects[0]; |
255 | | - } |
256 | | - |
257 | | - /** |
258 | | - * Returns if there is at least one record matching the provided conditions. |
259 | | - * Condition field names get prefixed. |
260 | | - * |
261 | | - * @since 1.20 |
262 | | - * |
263 | | - * @param array $conditions |
264 | | - * |
265 | | - * @return boolean |
266 | | - */ |
267 | | - public function has( array $conditions = array() ) { |
268 | | - return $this->selectRow( array( 'id' ), $conditions ) !== false; |
269 | | - } |
270 | | - |
271 | | - /** |
272 | | - * Returns the amount of matching records. |
273 | | - * Condition field names get prefixed. |
274 | | - * |
275 | | - * Note that this can be expensive on large tables. |
276 | | - * In such cases you might want to use DatabaseBase::estimateRowCount instead. |
277 | | - * |
278 | | - * @since 1.20 |
279 | | - * |
280 | | - * @param array $conditions |
281 | | - * @param array $options |
282 | | - * |
283 | | - * @return integer |
284 | | - */ |
285 | | - public function count( array $conditions = array(), array $options = array() ) { |
286 | | - $res = $this->rawSelectRow( |
287 | | - array( 'COUNT(*) AS rowcount' ), |
288 | | - $this->getPrefixedValues( $conditions ), |
289 | | - $options |
290 | | - ); |
291 | | - |
292 | | - return $res->rowcount; |
293 | | - } |
294 | | - |
295 | | - /** |
296 | | - * Removes the object from the database. |
297 | | - * |
298 | | - * @since 1.20 |
299 | | - * |
300 | | - * @param array $conditions |
301 | | - * @param string|null $functionName |
302 | | - * |
303 | | - * @return boolean Success indicator |
304 | | - */ |
305 | | - public function delete( array $conditions, $functionName = null ) { |
306 | | - return wfGetDB( DB_MASTER )->delete( |
307 | | - $this->getDBTable(), |
308 | | - $this->getPrefixedValues( $conditions ), |
309 | | - $functionName |
310 | | - ); |
311 | | - } |
312 | | - |
313 | | - /** |
314 | | - * Get API parameters for the fields supported by this object. |
315 | | - * |
316 | | - * @since 1.20 |
317 | | - * |
318 | | - * @param boolean $requireParams |
319 | | - * @param boolean $setDefaults |
320 | | - * |
321 | | - * @return array |
322 | | - */ |
323 | | - public function getAPIParams( $requireParams = false, $setDefaults = false ) { |
324 | | - $typeMap = array( |
325 | | - 'id' => 'integer', |
326 | | - 'int' => 'integer', |
327 | | - 'float' => 'NULL', |
328 | | - 'str' => 'string', |
329 | | - 'bool' => 'integer', |
330 | | - 'array' => 'string', |
331 | | - 'blob' => 'string', |
332 | | - ); |
333 | | - |
334 | | - $params = array(); |
335 | | - $defaults = $this->getDefaults(); |
336 | | - |
337 | | - foreach ( $this->getFieldTypes() as $field => $type ) { |
338 | | - if ( $field == 'id' ) { |
339 | | - continue; |
340 | | - } |
341 | | - |
342 | | - $hasDefault = array_key_exists( $field, $defaults ); |
343 | | - |
344 | | - $params[$field] = array( |
345 | | - ApiBase::PARAM_TYPE => $typeMap[$type], |
346 | | - ApiBase::PARAM_REQUIRED => $requireParams && !$hasDefault |
347 | | - ); |
348 | | - |
349 | | - if ( $type == 'array' ) { |
350 | | - $params[$field][ApiBase::PARAM_ISMULTI] = true; |
351 | | - } |
352 | | - |
353 | | - if ( $setDefaults && $hasDefault ) { |
354 | | - $default = is_array( $defaults[$field] ) ? implode( '|', $defaults[$field] ) : $defaults[$field]; |
355 | | - $params[$field][ApiBase::PARAM_DFLT] = $default; |
356 | | - } |
357 | | - } |
358 | | - |
359 | | - return $params; |
360 | | - } |
361 | | - |
362 | | - /** |
363 | | - * Returns an array with the fields and their descriptions. |
364 | | - * |
365 | | - * field name => field description |
366 | | - * |
367 | | - * @since 1.20 |
368 | | - * |
369 | | - * @return array |
370 | | - */ |
371 | | - public function getFieldDescriptions() { |
372 | | - return array(); |
373 | | - } |
374 | | - |
375 | | - /** |
376 | | - * Get the database type used for read operations. |
377 | | - * |
378 | | - * @since 1.20 |
379 | | - * |
380 | | - * @return integer DB_ enum |
381 | | - */ |
382 | | - public function getReadDb() { |
383 | | - return $this->readDb; |
384 | | - } |
385 | | - |
386 | | - /** |
387 | | - * Set the database type to use for read operations. |
388 | | - * |
389 | | - * @param integer $db |
390 | | - * |
391 | | - * @since 1.20 |
392 | | - */ |
393 | | - public function setReadDb( $db ) { |
394 | | - $this->readDb = $db; |
395 | | - } |
396 | | - |
397 | | - /** |
398 | | - * Update the records matching the provided conditions by |
399 | | - * setting the fields that are keys in the $values param to |
400 | | - * their corresponding values. |
401 | | - * |
402 | | - * @since 1.20 |
403 | | - * |
404 | | - * @param array $values |
405 | | - * @param array $conditions |
406 | | - * |
407 | | - * @return boolean Success indicator |
408 | | - */ |
409 | | - public function update( array $values, array $conditions = array() ) { |
410 | | - $dbw = wfGetDB( DB_MASTER ); |
411 | | - |
412 | | - return $dbw->update( |
413 | | - $this->getDBTable(), |
414 | | - $this->getPrefixedValues( $values ), |
415 | | - $this->getPrefixedValues( $conditions ), |
416 | | - __METHOD__ |
417 | | - ); |
418 | | - } |
419 | | - |
420 | | - /** |
421 | | - * Computes the values of the summary fields of the objects matching the provided conditions. |
422 | | - * |
423 | | - * @since 1.20 |
424 | | - * |
425 | | - * @param array|string|null $summaryFields |
426 | | - * @param array $conditions |
427 | | - */ |
428 | | - public function updateSummaryFields( $summaryFields = null, array $conditions = array() ) { |
429 | | - $this->setReadDb( DB_MASTER ); |
430 | | - |
431 | | - foreach ( $this->select( null, $conditions ) as /* DBDataObject */ $item ) { |
432 | | - $item->loadSummaryFields( $summaryFields ); |
433 | | - $item->setSummaryMode( true ); |
434 | | - $item->save(); |
435 | | - } |
436 | | - |
437 | | - $this->setReadDb( DB_SLAVE ); |
438 | | - } |
439 | | - |
440 | | - /** |
441 | | - * Takes in an associative array with field names as keys and |
442 | | - * their values as value. The field names are prefixed with the |
443 | | - * db field prefix. |
444 | | - * |
445 | | - * Field names can also be provided as an array with as first element a table name, such as |
446 | | - * $conditions = array( |
447 | | - * array( array( 'tablename', 'fieldname' ), $value ), |
448 | | - * ); |
449 | | - * |
450 | | - * @since 1.20 |
451 | | - * |
452 | | - * @param array $values |
453 | | - * |
454 | | - * @return array |
455 | | - */ |
456 | | - public function getPrefixedValues( array $values ) { |
457 | | - $prefixedValues = array(); |
458 | | - |
459 | | - foreach ( $values as $field => $value ) { |
460 | | - if ( is_integer( $field ) ) { |
461 | | - if ( is_array( $value ) ) { |
462 | | - $field = $value[0]; |
463 | | - $value = $value[1]; |
464 | | - } |
465 | | - else { |
466 | | - $value = explode( ' ', $value, 2 ); |
467 | | - $value[0] = $this->getPrefixedField( $value[0] ); |
468 | | - $prefixedValues[] = implode( ' ', $value ); |
469 | | - continue; |
470 | | - } |
471 | | - } |
472 | | - |
473 | | - $prefixedValues[$this->getPrefixedField( $field )] = $value; |
474 | | - } |
475 | | - |
476 | | - return $prefixedValues; |
477 | | - } |
478 | | - |
479 | | - /** |
480 | | - * Takes in a field or array of fields and returns an |
481 | | - * array with their prefixed versions, ready for db usage. |
482 | | - * |
483 | | - * @since 1.20 |
484 | | - * |
485 | | - * @param array|string $fields |
486 | | - * |
487 | | - * @return array |
488 | | - */ |
489 | | - public function getPrefixedFields( array $fields ) { |
490 | | - foreach ( $fields as &$field ) { |
491 | | - $field = $this->getPrefixedField( $field ); |
492 | | - } |
493 | | - |
494 | | - return $fields; |
495 | | - } |
496 | | - |
497 | | - /** |
498 | | - * Takes in a field and returns an it's prefixed version, ready for db usage. |
499 | | - * |
500 | | - * @since 1.20 |
501 | | - * |
502 | | - * @param string|array $field |
503 | | - * |
504 | | - * @return string |
505 | | - */ |
506 | | - public function getPrefixedField( $field ) { |
507 | | - return $this->getFieldPrefix() . $field; |
508 | | - } |
509 | | - |
510 | | - /** |
511 | | - * Takes an array of field names with prefix and returns the unprefixed equivalent. |
512 | | - * |
513 | | - * @since 1.20 |
514 | | - * |
515 | | - * @param array $fieldNames |
516 | | - * |
517 | | - * @return array |
518 | | - */ |
519 | | - public function unprefixFieldNames( array $fieldNames ) { |
520 | | - return array_map( array( $this, 'unprefixFieldName' ), $fieldNames ); |
521 | | - } |
522 | | - |
523 | | - /** |
524 | | - * Takes a field name with prefix and returns the unprefixed equivalent. |
525 | | - * |
526 | | - * @since 1.20 |
527 | | - * |
528 | | - * @param string $fieldName |
529 | | - * |
530 | | - * @return string |
531 | | - */ |
532 | | - public function unprefixFieldName( $fieldName ) { |
533 | | - return substr( $fieldName, strlen( $this->getFieldPrefix() ) ); |
534 | | - } |
535 | | - |
536 | | - public function __construct() { |
537 | | - |
538 | | - } |
539 | | - |
540 | | - /** |
541 | | - * Get an instance of this class. |
542 | | - * |
543 | | - * @since 1.20 |
544 | | - * |
545 | | - * @return DBtable |
546 | | - */ |
547 | | - public static function singleton() { |
548 | | - static $instance; |
549 | | - |
550 | | - if ( !isset( $instance ) ) { |
551 | | - $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); |
552 | | - $instance = new $class; |
553 | | - } |
554 | | - |
555 | | - return $instance; |
556 | | - } |
557 | | - |
558 | | - /** |
559 | | - * Compatibility fallback function so the singleton method works on PHP < 5.3. |
560 | | - * Code borrowed from http://www.php.net/manual/en/function.get-called-class.php#107445 |
561 | | - * |
562 | | - * @since 1.20 |
563 | | - * |
564 | | - * @return string |
565 | | - */ |
566 | | - protected static function get_called_class() { |
567 | | - $bt = debug_backtrace(); |
568 | | - $l = count($bt) - 1; |
569 | | - $matches = array(); |
570 | | - while(empty($matches) && $l > -1){ |
571 | | - $lines = file($bt[$l]['file']); |
572 | | - $callerLine = $lines[$bt[$l]['line']-1]; |
573 | | - preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l--]['function'].'/', |
574 | | - $callerLine, |
575 | | - $matches); |
576 | | - } |
577 | | - if (!isset($matches[1])) $matches[1]=NULL; //for notices |
578 | | - if ($matches[1] == 'self') { |
579 | | - $line = $bt[$l]['line']-1; |
580 | | - while ($line > 0 && strpos($lines[$line], 'class') === false) { |
581 | | - $line--; |
582 | | - } |
583 | | - preg_match('/class[\s]+(.+?)[\s]+/si', $lines[$line], $matches); |
584 | | - } |
585 | | - return $matches[1]; |
586 | | - } |
587 | | - |
588 | | - /** |
589 | | - * Get an array with fields from a database result, |
590 | | - * that can be fed directly to the constructor or |
591 | | - * to setFields. |
592 | | - * |
593 | | - * @since 1.20 |
594 | | - * |
595 | | - * @param stdClass $result |
596 | | - * |
597 | | - * @return array |
598 | | - */ |
599 | | - public function getFieldsFromDBResult( stdClass $result ) { |
600 | | - $result = (array)$result; |
601 | | - return array_combine( |
602 | | - $this->unprefixFieldNames( array_keys( $result ) ), |
603 | | - array_values( $result ) |
604 | | - ); |
605 | | - } |
606 | | - |
607 | | - /** |
608 | | - * Get a new instance of the class from a database result. |
609 | | - * |
610 | | - * @since 1.20 |
611 | | - * |
612 | | - * @param stdClass $result |
613 | | - * |
614 | | - * @return DBDataObject |
615 | | - */ |
616 | | - public function newFromDBResult( stdClass $result ) { |
617 | | - return $this->newFromArray( $this->getFieldsFromDBResult( $result ) ); |
618 | | - } |
619 | | - |
620 | | - /** |
621 | | - * Get a new instance of the class from an array. |
622 | | - * |
623 | | - * @since 1.20 |
624 | | - * |
625 | | - * @param array $data |
626 | | - * @param boolean $loadDefaults |
627 | | - * |
628 | | - * @return DBDataObject |
629 | | - */ |
630 | | - public function newFromArray( array $data, $loadDefaults = false ) { |
631 | | - $class = $this->getDataObjectClass(); |
632 | | - return new $class( $this, $data, $loadDefaults ); |
633 | | - } |
634 | | - |
635 | | - /** |
636 | | - * Return the names of the fields. |
637 | | - * |
638 | | - * @since 1.20 |
639 | | - * |
640 | | - * @return array |
641 | | - */ |
642 | | - public function getFieldNames() { |
643 | | - return array_keys( $this->getFieldTypes() ); |
644 | | - } |
645 | | - |
646 | | - /** |
647 | | - * Gets if the object can take a certain field. |
648 | | - * |
649 | | - * @since 1.20 |
650 | | - * |
651 | | - * @param string $name |
652 | | - * |
653 | | - * @return boolean |
654 | | - */ |
655 | | - public function canHaveField( $name ) { |
656 | | - return array_key_exists( $name, $this->getFieldTypes() ); |
657 | | - } |
658 | | - |
659 | | -} |
Index: trunk/extensions/EducationProgram/compat/DBDataObject.php |
— | — | @@ -1,667 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Abstract base class for representing objects that are stored in some DB table. |
6 | | - * This is basically an ORM-like wrapper around rows in database tables that |
7 | | - * aims to be both simple and very flexible. It is centered around an associative |
8 | | - * array of fields and various methods to do common interaction with the database. |
9 | | - * |
10 | | - * These methods must be implemented in deriving classes: |
11 | | - * * getDBTable |
12 | | - * * getFieldPrefix |
13 | | - * * getFieldTypes |
14 | | - * |
15 | | - * These methods are likely candidates for overriding: |
16 | | - * * getDefaults |
17 | | - * * remove |
18 | | - * * insert |
19 | | - * * saveExisting |
20 | | - * * loadSummaryFields |
21 | | - * * getSummaryFields |
22 | | - * |
23 | | - * Main instance methods: |
24 | | - * * getField(s) |
25 | | - * * setField(s) |
26 | | - * * save |
27 | | - * * remove |
28 | | - * |
29 | | - * Main static methods: |
30 | | - * * select |
31 | | - * * update |
32 | | - * * delete |
33 | | - * * count |
34 | | - * * has |
35 | | - * * selectRow |
36 | | - * * selectFields |
37 | | - * * selectFieldsRow |
38 | | - * |
39 | | - * @since 1.20 |
40 | | - * |
41 | | - * @file DBDataObject.php |
42 | | - * |
43 | | - * @licence GNU GPL v2 or later |
44 | | - * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
45 | | - */ |
46 | | -abstract class DBDataObject { |
47 | | - |
48 | | - /** |
49 | | - * The fields of the object. |
50 | | - * field name (w/o prefix) => value |
51 | | - * |
52 | | - * @since 1.20 |
53 | | - * @var array |
54 | | - */ |
55 | | - protected $fields = array( 'id' => null ); |
56 | | - |
57 | | - /** |
58 | | - * @since 1.20 |
59 | | - * @var DBTable |
60 | | - */ |
61 | | - protected $table; |
62 | | - |
63 | | - /** |
64 | | - * If the object should update summaries of linked items when changed. |
65 | | - * For example, update the course_count field in universities when a course in courses is deleted. |
66 | | - * Settings this to false can prevent needless updating work in situations |
67 | | - * such as deleting a university, which will then delete all it's courses. |
68 | | - * |
69 | | - * @since 1.20 |
70 | | - * @var bool |
71 | | - */ |
72 | | - protected $updateSummaries = true; |
73 | | - |
74 | | - /** |
75 | | - * Indicates if the object is in summary mode. |
76 | | - * This mode indicates that only summary fields got updated, |
77 | | - * which allows for optimizations. |
78 | | - * |
79 | | - * @since 1.20 |
80 | | - * @var bool |
81 | | - */ |
82 | | - protected $inSummaryMode = false; |
83 | | - |
84 | | - /** |
85 | | - * Constructor. |
86 | | - * |
87 | | - * @since 1.20 |
88 | | - * |
89 | | - * @param DBTable $table |
90 | | - * @param array|null $fields |
91 | | - * @param boolean $loadDefaults |
92 | | - */ |
93 | | - public function __construct( DBTable $table, $fields = null, $loadDefaults = false ) { |
94 | | - $this->table = $table; |
95 | | - |
96 | | - if ( !is_array( $fields ) ) { |
97 | | - $fields = array(); |
98 | | - } |
99 | | - |
100 | | - if ( $loadDefaults ) { |
101 | | - $fields = array_merge( $this->table->getDefaults(), $fields ); |
102 | | - } |
103 | | - |
104 | | - $this->setFields( $fields ); |
105 | | - } |
106 | | - |
107 | | - /** |
108 | | - * Load the specified fields from the database. |
109 | | - * |
110 | | - * @since 1.20 |
111 | | - * |
112 | | - * @param array|null $fields |
113 | | - * @param boolean $override |
114 | | - * @param boolean $skipLoaded |
115 | | - * |
116 | | - * @return bool Success indicator |
117 | | - */ |
118 | | - public function loadFields( $fields = null, $override = true, $skipLoaded = false ) { |
119 | | - if ( is_null( $this->getId() ) ) { |
120 | | - return false; |
121 | | - } |
122 | | - |
123 | | - if ( is_null( $fields ) ) { |
124 | | - $fields = array_keys( $this->table->getFieldTypes() ); |
125 | | - } |
126 | | - |
127 | | - if ( $skipLoaded ) { |
128 | | - $fields = array_diff( $fields, array_keys( $this->fields ) ); |
129 | | - } |
130 | | - |
131 | | - if ( !empty( $fields ) ) { |
132 | | - $result = $this->table->rawSelectRow( |
133 | | - $this->table->getPrefixedFields( $fields ), |
134 | | - array( $this->table->getPrefixedField( 'id' ) => $this->getId() ), |
135 | | - array( 'LIMIT' => 1 ) |
136 | | - ); |
137 | | - |
138 | | - if ( $result !== false ) { |
139 | | - $this->setFields( $this->table->getFieldsFromDBResult( $result ), $override ); |
140 | | - return true; |
141 | | - } |
142 | | - |
143 | | - return false; |
144 | | - } |
145 | | - |
146 | | - return true; |
147 | | - } |
148 | | - |
149 | | - /** |
150 | | - * Gets the value of a field. |
151 | | - * |
152 | | - * @since 1.20 |
153 | | - * |
154 | | - * @param string $name |
155 | | - * @param mixed $default |
156 | | - * |
157 | | - * @throws MWException |
158 | | - * @return mixed |
159 | | - */ |
160 | | - public function getField( $name, $default = null ) { |
161 | | - if ( $this->hasField( $name ) ) { |
162 | | - return $this->fields[$name]; |
163 | | - } elseif ( !is_null( $default ) ) { |
164 | | - return $default; |
165 | | - } else { |
166 | | - throw new MWException( 'Attempted to get not-set field ' . $name ); |
167 | | - } |
168 | | - } |
169 | | - |
170 | | - /** |
171 | | - * Gets the value of a field but first loads it if not done so already. |
172 | | - * |
173 | | - * @since 1.20 |
174 | | - * |
175 | | - * @param string$name |
176 | | - * |
177 | | - * @return mixed |
178 | | - */ |
179 | | - public function loadAndGetField( $name ) { |
180 | | - if ( !$this->hasField( $name ) ) { |
181 | | - $this->loadFields( array( $name ) ); |
182 | | - } |
183 | | - |
184 | | - return $this->getField( $name ); |
185 | | - } |
186 | | - |
187 | | - /** |
188 | | - * Remove a field. |
189 | | - * |
190 | | - * @since 1.20 |
191 | | - * |
192 | | - * @param string $name |
193 | | - */ |
194 | | - public function removeField( $name ) { |
195 | | - unset( $this->fields[$name] ); |
196 | | - } |
197 | | - |
198 | | - /** |
199 | | - * Returns the objects database id. |
200 | | - * |
201 | | - * @since 1.20 |
202 | | - * |
203 | | - * @return integer|null |
204 | | - */ |
205 | | - public function getId() { |
206 | | - return $this->getField( 'id' ); |
207 | | - } |
208 | | - |
209 | | - /** |
210 | | - * Sets the objects database id. |
211 | | - * |
212 | | - * @since 1.20 |
213 | | - * |
214 | | - * @param integer|null $id |
215 | | - */ |
216 | | - public function setId( $id ) { |
217 | | - return $this->setField( 'id', $id ); |
218 | | - } |
219 | | - |
220 | | - /** |
221 | | - * Gets if a certain field is set. |
222 | | - * |
223 | | - * @since 1.20 |
224 | | - * |
225 | | - * @param string $name |
226 | | - * |
227 | | - * @return boolean |
228 | | - */ |
229 | | - public function hasField( $name ) { |
230 | | - return array_key_exists( $name, $this->fields ); |
231 | | - } |
232 | | - |
233 | | - /** |
234 | | - * Gets if the id field is set. |
235 | | - * |
236 | | - * @since 1.20 |
237 | | - * |
238 | | - * @return boolean |
239 | | - */ |
240 | | - public function hasIdField() { |
241 | | - return $this->hasField( 'id' ) |
242 | | - && !is_null( $this->getField( 'id' ) ); |
243 | | - } |
244 | | - |
245 | | - /** |
246 | | - * Sets multiple fields. |
247 | | - * |
248 | | - * @since 1.20 |
249 | | - * |
250 | | - * @param array $fields The fields to set |
251 | | - * @param boolean $override Override already set fields with the provided values? |
252 | | - */ |
253 | | - public function setFields( array $fields, $override = true ) { |
254 | | - foreach ( $fields as $name => $value ) { |
255 | | - if ( $override || !$this->hasField( $name ) ) { |
256 | | - $this->setField( $name, $value ); |
257 | | - } |
258 | | - } |
259 | | - } |
260 | | - |
261 | | - /** |
262 | | - * Gets the fields => values to write to the table. |
263 | | - * |
264 | | - * @since 1.20 |
265 | | - * |
266 | | - * @return array |
267 | | - */ |
268 | | - protected function getWriteValues() { |
269 | | - $values = array(); |
270 | | - |
271 | | - foreach ( $this->table->getFieldTypes() as $name => $type ) { |
272 | | - if ( array_key_exists( $name, $this->fields ) ) { |
273 | | - $value = $this->fields[$name]; |
274 | | - |
275 | | - switch ( $type ) { |
276 | | - case 'array': |
277 | | - $value = (array)$value; |
278 | | - case 'blob': |
279 | | - $value = serialize( $value ); |
280 | | - } |
281 | | - |
282 | | - $values[$this->table->getPrefixedField( $name )] = $value; |
283 | | - } |
284 | | - } |
285 | | - |
286 | | - return $values; |
287 | | - } |
288 | | - |
289 | | - /** |
290 | | - * Serializes the object to an associative array which |
291 | | - * can then easily be converted into JSON or similar. |
292 | | - * |
293 | | - * @since 1.20 |
294 | | - * |
295 | | - * @param null|array $fields |
296 | | - * @param boolean $incNullId |
297 | | - * |
298 | | - * @return array |
299 | | - */ |
300 | | - public function toArray( $fields = null, $incNullId = false ) { |
301 | | - $data = array(); |
302 | | - $setFields = array(); |
303 | | - |
304 | | - if ( !is_array( $fields ) ) { |
305 | | - $setFields = $this->getSetFieldNames(); |
306 | | - } else { |
307 | | - foreach ( $fields as $field ) { |
308 | | - if ( $this->hasField( $field ) ) { |
309 | | - $setFields[] = $field; |
310 | | - } |
311 | | - } |
312 | | - } |
313 | | - |
314 | | - foreach ( $setFields as $field ) { |
315 | | - if ( $incNullId || $field != 'id' || $this->hasIdField() ) { |
316 | | - $data[$field] = $this->getField( $field ); |
317 | | - } |
318 | | - } |
319 | | - |
320 | | - return $data; |
321 | | - } |
322 | | - |
323 | | - /** |
324 | | - * Load the default values, via getDefaults. |
325 | | - * |
326 | | - * @since 1.20 |
327 | | - * |
328 | | - * @param boolean $override |
329 | | - */ |
330 | | - public function loadDefaults( $override = true ) { |
331 | | - $this->setFields( $this->table->getDefaults(), $override ); |
332 | | - } |
333 | | - |
334 | | - /** |
335 | | - * Writes the answer to the database, either updating it |
336 | | - * when it already exists, or inserting it when it doesn't. |
337 | | - * |
338 | | - * @since 1.20 |
339 | | - * |
340 | | - * @param string|null $functionName |
341 | | - * |
342 | | - * @return boolean Success indicator |
343 | | - */ |
344 | | - public function save( $functionName = null ) { |
345 | | - if ( $this->hasIdField() ) { |
346 | | - return $this->saveExisting( $functionName ); |
347 | | - } else { |
348 | | - return $this->insert( $functionName ); |
349 | | - } |
350 | | - } |
351 | | - |
352 | | - /** |
353 | | - * Updates the object in the database. |
354 | | - * |
355 | | - * @since 1.20 |
356 | | - * |
357 | | - * @param string|null $functionName |
358 | | - * |
359 | | - * @return boolean Success indicator |
360 | | - */ |
361 | | - protected function saveExisting( $functionName = null ) { |
362 | | - $dbw = wfGetDB( DB_MASTER ); |
363 | | - |
364 | | - $success = $dbw->update( |
365 | | - $this->table->getDBTable(), |
366 | | - $this->getWriteValues(), |
367 | | - $this->table->getPrefixedValues( $this->getUpdateConditions() ), |
368 | | - is_null( $functionName ) ? __METHOD__ : $functionName |
369 | | - ); |
370 | | - |
371 | | - return $success; |
372 | | - } |
373 | | - |
374 | | - /** |
375 | | - * Returns the WHERE considtions needed to identify this object so |
376 | | - * it can be updated. |
377 | | - * |
378 | | - * @since 1.20 |
379 | | - * |
380 | | - * @return array |
381 | | - */ |
382 | | - protected function getUpdateConditions() { |
383 | | - return array( 'id' => $this->getId() ); |
384 | | - } |
385 | | - |
386 | | - /** |
387 | | - * Inserts the object into the database. |
388 | | - * |
389 | | - * @since 1.20 |
390 | | - * |
391 | | - * @param string|null $functionName |
392 | | - * @param array|null $options |
393 | | - * |
394 | | - * @return boolean Success indicator |
395 | | - */ |
396 | | - protected function insert( $functionName = null, array $options = null ) { |
397 | | - $dbw = wfGetDB( DB_MASTER ); |
398 | | - |
399 | | - $result = $dbw->insert( |
400 | | - $this->table->getDBTable(), |
401 | | - $this->getWriteValues(), |
402 | | - is_null( $functionName ) ? __METHOD__ : $functionName, |
403 | | - is_null( $options ) ? array( 'IGNORE' ) : $options |
404 | | - ); |
405 | | - |
406 | | - if ( $result ) { |
407 | | - $this->setField( 'id', $dbw->insertId() ); |
408 | | - } |
409 | | - |
410 | | - return $result; |
411 | | - } |
412 | | - |
413 | | - /** |
414 | | - * Removes the object from the database. |
415 | | - * |
416 | | - * @since 1.20 |
417 | | - * |
418 | | - * @return boolean Success indicator |
419 | | - */ |
420 | | - public function remove() { |
421 | | - $this->beforeRemove(); |
422 | | - |
423 | | - $success = $this->table->delete( array( 'id' => $this->getId() ) ); |
424 | | - |
425 | | - if ( $success ) { |
426 | | - $this->onRemoved(); |
427 | | - } |
428 | | - |
429 | | - return $success; |
430 | | - } |
431 | | - |
432 | | - /** |
433 | | - * Gets called before an object is removed from the database. |
434 | | - * |
435 | | - * @since 1.20 |
436 | | - */ |
437 | | - protected function beforeRemove() { |
438 | | - $this->loadFields( $this->getBeforeRemoveFields(), false, true ); |
439 | | - } |
440 | | - |
441 | | - /** |
442 | | - * Before removal of an object happens, @see beforeRemove gets called. |
443 | | - * This method loads the fields of which the names have been returned by this one (or all fields if null is returned). |
444 | | - * This allows for loading info needed after removal to get rid of linked data and the like. |
445 | | - * |
446 | | - * @since 1.20 |
447 | | - * |
448 | | - * @return array|null |
449 | | - */ |
450 | | - protected function getBeforeRemoveFields() { |
451 | | - return array(); |
452 | | - } |
453 | | - |
454 | | - /** |
455 | | - * Gets called after successfull removal. |
456 | | - * Can be overriden to get rid of linked data. |
457 | | - * |
458 | | - * @since 1.20 |
459 | | - */ |
460 | | - protected function onRemoved() { |
461 | | - $this->setField( 'id', null ); |
462 | | - } |
463 | | - |
464 | | - /** |
465 | | - * Return the names and values of the fields. |
466 | | - * |
467 | | - * @since 1.20 |
468 | | - * |
469 | | - * @return array |
470 | | - */ |
471 | | - public function getFields() { |
472 | | - return $this->fields; |
473 | | - } |
474 | | - |
475 | | - /** |
476 | | - * Return the names of the fields. |
477 | | - * |
478 | | - * @since 1.20 |
479 | | - * |
480 | | - * @return array |
481 | | - */ |
482 | | - public function getSetFieldNames() { |
483 | | - return array_keys( $this->fields ); |
484 | | - } |
485 | | - |
486 | | - /** |
487 | | - * Sets the value of a field. |
488 | | - * Strings can be provided for other types, |
489 | | - * so this method can be called from unserialization handlers. |
490 | | - * |
491 | | - * @since 1.20 |
492 | | - * |
493 | | - * @param string $name |
494 | | - * @param mixed $value |
495 | | - * |
496 | | - * @throws MWException |
497 | | - */ |
498 | | - public function setField( $name, $value ) { |
499 | | - $fields = $this->table->getFieldTypes(); |
500 | | - |
501 | | - if ( array_key_exists( $name, $fields ) ) { |
502 | | - switch ( $fields[$name] ) { |
503 | | - case 'int': |
504 | | - $value = (int)$value; |
505 | | - break; |
506 | | - case 'float': |
507 | | - $value = (float)$value; |
508 | | - break; |
509 | | - case 'bool': |
510 | | - if ( is_string( $value ) ) { |
511 | | - $value = $value !== '0'; |
512 | | - } elseif ( is_int( $value ) ) { |
513 | | - $value = $value !== 0; |
514 | | - } |
515 | | - break; |
516 | | - case 'array': |
517 | | - if ( is_string( $value ) ) { |
518 | | - $value = unserialize( $value ); |
519 | | - } |
520 | | - |
521 | | - if ( !is_array( $value ) ) { |
522 | | - $value = array(); |
523 | | - } |
524 | | - break; |
525 | | - case 'blob': |
526 | | - if ( is_string( $value ) ) { |
527 | | - $value = unserialize( $value ); |
528 | | - } |
529 | | - break; |
530 | | - case 'id': |
531 | | - if ( is_string( $value ) ) { |
532 | | - $value = (int)$value; |
533 | | - } |
534 | | - break; |
535 | | - } |
536 | | - |
537 | | - $this->fields[$name] = $value; |
538 | | - } else { |
539 | | - throw new MWException( 'Attempted to set unknown field ' . $name ); |
540 | | - } |
541 | | - } |
542 | | - |
543 | | - /** |
544 | | - * Add an amount (can be negative) to the specified field (needs to be numeric). |
545 | | - * |
546 | | - * @since 1.20 |
547 | | - * |
548 | | - * @param string $field |
549 | | - * @param integer $amount |
550 | | - * |
551 | | - * @return boolean Success indicator |
552 | | - */ |
553 | | - public function addToField( $field, $amount ) { |
554 | | - if ( $amount == 0 ) { |
555 | | - return true; |
556 | | - } |
557 | | - |
558 | | - if ( !$this->hasIdField() ) { |
559 | | - return false; |
560 | | - } |
561 | | - |
562 | | - $absoluteAmount = abs( $amount ); |
563 | | - $isNegative = $amount < 0; |
564 | | - |
565 | | - $dbw = wfGetDB( DB_MASTER ); |
566 | | - |
567 | | - $fullField = $this->table->getPrefixedField( $field ); |
568 | | - |
569 | | - $success = $dbw->update( |
570 | | - $this->table->getDBTable(), |
571 | | - array( "$fullField=$fullField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ), |
572 | | - array( $this->table->getPrefixedField( 'id' ) => $this->getId() ), |
573 | | - __METHOD__ |
574 | | - ); |
575 | | - |
576 | | - if ( $success && $this->hasField( $field ) ) { |
577 | | - $this->setField( $field, $this->getField( $field ) + $amount ); |
578 | | - } |
579 | | - |
580 | | - return $success; |
581 | | - } |
582 | | - |
583 | | - /** |
584 | | - * Return the names of the fields. |
585 | | - * |
586 | | - * @since 1.20 |
587 | | - * |
588 | | - * @return array |
589 | | - */ |
590 | | - public function getFieldNames() { |
591 | | - return array_keys( $this->table->getFieldTypes() ); |
592 | | - } |
593 | | - |
594 | | - /** |
595 | | - * Computes and updates the values of the summary fields. |
596 | | - * |
597 | | - * @since 1.20 |
598 | | - * |
599 | | - * @param array|string|null $summaryFields |
600 | | - */ |
601 | | - public function loadSummaryFields( $summaryFields = null ) { |
602 | | - |
603 | | - } |
604 | | - |
605 | | - /** |
606 | | - * Sets the value for the @see $updateSummaries field. |
607 | | - * |
608 | | - * @since 1.20 |
609 | | - * |
610 | | - * @param boolean $update |
611 | | - */ |
612 | | - public function setUpdateSummaries( $update ) { |
613 | | - $this->updateSummaries = $update; |
614 | | - } |
615 | | - |
616 | | - /** |
617 | | - * Sets the value for the @see $inSummaryMode field. |
618 | | - * |
619 | | - * @since 1.20 |
620 | | - * |
621 | | - * @param boolean $summaryMode |
622 | | - */ |
623 | | - public function setSummaryMode( $summaryMode ) { |
624 | | - $this->inSummaryMode = $summaryMode; |
625 | | - } |
626 | | - |
627 | | - /** |
628 | | - * Return if any fields got changed. |
629 | | - * |
630 | | - * @since 1.20 |
631 | | - * |
632 | | - * @param DBDataObject $object |
633 | | - * @param boolean|array $excludeSummaryFields |
634 | | - * When set to true, summary field changes are ignored. |
635 | | - * Can also be an array of fields to ignore. |
636 | | - * |
637 | | - * @return boolean |
638 | | - */ |
639 | | - protected function fieldsChanged( DBDataObject $object, $excludeSummaryFields = false ) { |
640 | | - $exclusionFields = array(); |
641 | | - |
642 | | - if ( $excludeSummaryFields !== false ) { |
643 | | - $exclusionFields = is_array( $excludeSummaryFields ) ? $excludeSummaryFields : $this->table->getSummaryFields(); |
644 | | - } |
645 | | - |
646 | | - foreach ( $this->fields as $name => $value ) { |
647 | | - $excluded = $excludeSummaryFields && in_array( $name, $exclusionFields ); |
648 | | - |
649 | | - if ( !$excluded && $object->getField( $name ) !== $value ) { |
650 | | - return true; |
651 | | - } |
652 | | - } |
653 | | - |
654 | | - return false; |
655 | | - } |
656 | | - |
657 | | - /** |
658 | | - * Returns the table this DBDataObject is a row in. |
659 | | - * |
660 | | - * @since 1.20 |
661 | | - * |
662 | | - * @return DBTable |
663 | | - */ |
664 | | - public function getTable() { |
665 | | - return $this->table; |
666 | | - } |
667 | | - |
668 | | -} |
Index: trunk/extensions/EducationProgram/compat/ORMResult.php |
— | — | @@ -0,0 +1,104 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Result of a ORMTable::select, which returns ORMRow objects. |
| 6 | + * |
| 7 | + * @since 1.20 |
| 8 | + * |
| 9 | + * @file ORMResult.php |
| 10 | + * |
| 11 | + * @licence GNU GPL v2 or later |
| 12 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 13 | + */ |
| 14 | +class ORMResult implements Iterator { |
| 15 | + |
| 16 | + /** |
| 17 | + * @var ResultWrapper |
| 18 | + */ |
| 19 | + protected $res; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var integer |
| 23 | + */ |
| 24 | + protected $key; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var ORMRow |
| 28 | + */ |
| 29 | + protected $current; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ORMTable |
| 33 | + */ |
| 34 | + protected $table; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param ORMTable $table |
| 38 | + * @param ResultWrapper $res |
| 39 | + */ |
| 40 | + public function __construct( ORMTable $table, ResultWrapper $res ) { |
| 41 | + $this->table = $table; |
| 42 | + $this->res = $res; |
| 43 | + $this->key = 0; |
| 44 | + $this->setCurrent( $this->res->current() ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param $row |
| 49 | + */ |
| 50 | + protected function setCurrent( $row ) { |
| 51 | + if ( $row === false ) { |
| 52 | + $this->current = false; |
| 53 | + } else { |
| 54 | + $this->current = $this->table->newFromDBResult( $row ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return integer |
| 60 | + */ |
| 61 | + public function count() { |
| 62 | + return $this->res->numRows(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return boolean |
| 67 | + */ |
| 68 | + public function isEmpty() { |
| 69 | + return $this->res->numRows() === 0; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return ORMRow |
| 74 | + */ |
| 75 | + public function current() { |
| 76 | + return $this->current; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return integer |
| 81 | + */ |
| 82 | + public function key() { |
| 83 | + return $this->key; |
| 84 | + } |
| 85 | + |
| 86 | + public function next() { |
| 87 | + $row = $this->res->next(); |
| 88 | + $this->setCurrent( $row ); |
| 89 | + $this->key++; |
| 90 | + } |
| 91 | + |
| 92 | + public function rewind() { |
| 93 | + $this->res->rewind(); |
| 94 | + $this->key = 0; |
| 95 | + $this->setCurrent( $this->res->current() ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return boolean |
| 100 | + */ |
| 101 | + public function valid() { |
| 102 | + return $this->current !== false; |
| 103 | + } |
| 104 | + |
| 105 | +} |
Index: trunk/extensions/EducationProgram/compat/ORMRow.php |
— | — | @@ -0,0 +1,662 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract base class for representing objects that are stored in some DB table. |
| 6 | + * This is basically an ORM-like wrapper around rows in database tables that |
| 7 | + * aims to be both simple and very flexible. It is centered around an associative |
| 8 | + * array of fields and various methods to do common interaction with the database. |
| 9 | + * |
| 10 | + * These methods are likely candidates for overriding: |
| 11 | + * * getDefaults |
| 12 | + * * remove |
| 13 | + * * insert |
| 14 | + * * saveExisting |
| 15 | + * * loadSummaryFields |
| 16 | + * * getSummaryFields |
| 17 | + * |
| 18 | + * Main instance methods: |
| 19 | + * * getField(s) |
| 20 | + * * setField(s) |
| 21 | + * * save |
| 22 | + * * remove |
| 23 | + * |
| 24 | + * Main static methods: |
| 25 | + * * select |
| 26 | + * * update |
| 27 | + * * delete |
| 28 | + * * count |
| 29 | + * * has |
| 30 | + * * selectRow |
| 31 | + * * selectFields |
| 32 | + * * selectFieldsRow |
| 33 | + * |
| 34 | + * @since 1.20 |
| 35 | + * |
| 36 | + * @file ORMRow.php |
| 37 | + * |
| 38 | + * @licence GNU GPL v2 or later |
| 39 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 40 | + */ |
| 41 | +abstract class ORMRow { |
| 42 | + |
| 43 | + /** |
| 44 | + * The fields of the object. |
| 45 | + * field name (w/o prefix) => value |
| 46 | + * |
| 47 | + * @since 1.20 |
| 48 | + * @var array |
| 49 | + */ |
| 50 | + protected $fields = array( 'id' => null ); |
| 51 | + |
| 52 | + /** |
| 53 | + * @since 1.20 |
| 54 | + * @var ORMTable |
| 55 | + */ |
| 56 | + protected $table; |
| 57 | + |
| 58 | + /** |
| 59 | + * If the object should update summaries of linked items when changed. |
| 60 | + * For example, update the course_count field in universities when a course in courses is deleted. |
| 61 | + * Settings this to false can prevent needless updating work in situations |
| 62 | + * such as deleting a university, which will then delete all it's courses. |
| 63 | + * |
| 64 | + * @since 1.20 |
| 65 | + * @var bool |
| 66 | + */ |
| 67 | + protected $updateSummaries = true; |
| 68 | + |
| 69 | + /** |
| 70 | + * Indicates if the object is in summary mode. |
| 71 | + * This mode indicates that only summary fields got updated, |
| 72 | + * which allows for optimizations. |
| 73 | + * |
| 74 | + * @since 1.20 |
| 75 | + * @var bool |
| 76 | + */ |
| 77 | + protected $inSummaryMode = false; |
| 78 | + |
| 79 | + /** |
| 80 | + * Constructor. |
| 81 | + * |
| 82 | + * @since 1.20 |
| 83 | + * |
| 84 | + * @param ORMTable $table |
| 85 | + * @param array|null $fields |
| 86 | + * @param boolean $loadDefaults |
| 87 | + */ |
| 88 | + public function __construct( ORMTable $table, $fields = null, $loadDefaults = false ) { |
| 89 | + $this->table = $table; |
| 90 | + |
| 91 | + if ( !is_array( $fields ) ) { |
| 92 | + $fields = array(); |
| 93 | + } |
| 94 | + |
| 95 | + if ( $loadDefaults ) { |
| 96 | + $fields = array_merge( $this->table->getDefaults(), $fields ); |
| 97 | + } |
| 98 | + |
| 99 | + $this->setFields( $fields ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Load the specified fields from the database. |
| 104 | + * |
| 105 | + * @since 1.20 |
| 106 | + * |
| 107 | + * @param array|null $fields |
| 108 | + * @param boolean $override |
| 109 | + * @param boolean $skipLoaded |
| 110 | + * |
| 111 | + * @return bool Success indicator |
| 112 | + */ |
| 113 | + public function loadFields( $fields = null, $override = true, $skipLoaded = false ) { |
| 114 | + if ( is_null( $this->getId() ) ) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + if ( is_null( $fields ) ) { |
| 119 | + $fields = array_keys( $this->table->getFields() ); |
| 120 | + } |
| 121 | + |
| 122 | + if ( $skipLoaded ) { |
| 123 | + $fields = array_diff( $fields, array_keys( $this->fields ) ); |
| 124 | + } |
| 125 | + |
| 126 | + if ( !empty( $fields ) ) { |
| 127 | + $result = $this->table->rawSelectRow( |
| 128 | + $this->table->getPrefixedFields( $fields ), |
| 129 | + array( $this->table->getPrefixedField( 'id' ) => $this->getId() ), |
| 130 | + array( 'LIMIT' => 1 ) |
| 131 | + ); |
| 132 | + |
| 133 | + if ( $result !== false ) { |
| 134 | + $this->setFields( $this->table->getFieldsFromDBResult( $result ), $override ); |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Gets the value of a field. |
| 146 | + * |
| 147 | + * @since 1.20 |
| 148 | + * |
| 149 | + * @param string $name |
| 150 | + * @param mixed $default |
| 151 | + * |
| 152 | + * @throws MWException |
| 153 | + * @return mixed |
| 154 | + */ |
| 155 | + public function getField( $name, $default = null ) { |
| 156 | + if ( $this->hasField( $name ) ) { |
| 157 | + return $this->fields[$name]; |
| 158 | + } elseif ( !is_null( $default ) ) { |
| 159 | + return $default; |
| 160 | + } else { |
| 161 | + throw new MWException( 'Attempted to get not-set field ' . $name ); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Gets the value of a field but first loads it if not done so already. |
| 167 | + * |
| 168 | + * @since 1.20 |
| 169 | + * |
| 170 | + * @param string$name |
| 171 | + * |
| 172 | + * @return mixed |
| 173 | + */ |
| 174 | + public function loadAndGetField( $name ) { |
| 175 | + if ( !$this->hasField( $name ) ) { |
| 176 | + $this->loadFields( array( $name ) ); |
| 177 | + } |
| 178 | + |
| 179 | + return $this->getField( $name ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Remove a field. |
| 184 | + * |
| 185 | + * @since 1.20 |
| 186 | + * |
| 187 | + * @param string $name |
| 188 | + */ |
| 189 | + public function removeField( $name ) { |
| 190 | + unset( $this->fields[$name] ); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Returns the objects database id. |
| 195 | + * |
| 196 | + * @since 1.20 |
| 197 | + * |
| 198 | + * @return integer|null |
| 199 | + */ |
| 200 | + public function getId() { |
| 201 | + return $this->getField( 'id' ); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Sets the objects database id. |
| 206 | + * |
| 207 | + * @since 1.20 |
| 208 | + * |
| 209 | + * @param integer|null $id |
| 210 | + */ |
| 211 | + public function setId( $id ) { |
| 212 | + return $this->setField( 'id', $id ); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Gets if a certain field is set. |
| 217 | + * |
| 218 | + * @since 1.20 |
| 219 | + * |
| 220 | + * @param string $name |
| 221 | + * |
| 222 | + * @return boolean |
| 223 | + */ |
| 224 | + public function hasField( $name ) { |
| 225 | + return array_key_exists( $name, $this->fields ); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Gets if the id field is set. |
| 230 | + * |
| 231 | + * @since 1.20 |
| 232 | + * |
| 233 | + * @return boolean |
| 234 | + */ |
| 235 | + public function hasIdField() { |
| 236 | + return $this->hasField( 'id' ) |
| 237 | + && !is_null( $this->getField( 'id' ) ); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Sets multiple fields. |
| 242 | + * |
| 243 | + * @since 1.20 |
| 244 | + * |
| 245 | + * @param array $fields The fields to set |
| 246 | + * @param boolean $override Override already set fields with the provided values? |
| 247 | + */ |
| 248 | + public function setFields( array $fields, $override = true ) { |
| 249 | + foreach ( $fields as $name => $value ) { |
| 250 | + if ( $override || !$this->hasField( $name ) ) { |
| 251 | + $this->setField( $name, $value ); |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Gets the fields => values to write to the table. |
| 258 | + * |
| 259 | + * @since 1.20 |
| 260 | + * |
| 261 | + * @return array |
| 262 | + */ |
| 263 | + protected function getWriteValues() { |
| 264 | + $values = array(); |
| 265 | + |
| 266 | + foreach ( $this->table->getFields() as $name => $type ) { |
| 267 | + if ( array_key_exists( $name, $this->fields ) ) { |
| 268 | + $value = $this->fields[$name]; |
| 269 | + |
| 270 | + switch ( $type ) { |
| 271 | + case 'array': |
| 272 | + $value = (array)$value; |
| 273 | + case 'blob': |
| 274 | + $value = serialize( $value ); |
| 275 | + } |
| 276 | + |
| 277 | + $values[$this->table->getPrefixedField( $name )] = $value; |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + return $values; |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Serializes the object to an associative array which |
| 286 | + * can then easily be converted into JSON or similar. |
| 287 | + * |
| 288 | + * @since 1.20 |
| 289 | + * |
| 290 | + * @param null|array $fields |
| 291 | + * @param boolean $incNullId |
| 292 | + * |
| 293 | + * @return array |
| 294 | + */ |
| 295 | + public function toArray( $fields = null, $incNullId = false ) { |
| 296 | + $data = array(); |
| 297 | + $setFields = array(); |
| 298 | + |
| 299 | + if ( !is_array( $fields ) ) { |
| 300 | + $setFields = $this->getSetFieldNames(); |
| 301 | + } else { |
| 302 | + foreach ( $fields as $field ) { |
| 303 | + if ( $this->hasField( $field ) ) { |
| 304 | + $setFields[] = $field; |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + foreach ( $setFields as $field ) { |
| 310 | + if ( $incNullId || $field != 'id' || $this->hasIdField() ) { |
| 311 | + $data[$field] = $this->getField( $field ); |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + return $data; |
| 316 | + } |
| 317 | + |
| 318 | + /** |
| 319 | + * Load the default values, via getDefaults. |
| 320 | + * |
| 321 | + * @since 1.20 |
| 322 | + * |
| 323 | + * @param boolean $override |
| 324 | + */ |
| 325 | + public function loadDefaults( $override = true ) { |
| 326 | + $this->setFields( $this->table->getDefaults(), $override ); |
| 327 | + } |
| 328 | + |
| 329 | + /** |
| 330 | + * Writes the answer to the database, either updating it |
| 331 | + * when it already exists, or inserting it when it doesn't. |
| 332 | + * |
| 333 | + * @since 1.20 |
| 334 | + * |
| 335 | + * @param string|null $functionName |
| 336 | + * |
| 337 | + * @return boolean Success indicator |
| 338 | + */ |
| 339 | + public function save( $functionName = null ) { |
| 340 | + if ( $this->hasIdField() ) { |
| 341 | + return $this->saveExisting( $functionName ); |
| 342 | + } else { |
| 343 | + return $this->insert( $functionName ); |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + /** |
| 348 | + * Updates the object in the database. |
| 349 | + * |
| 350 | + * @since 1.20 |
| 351 | + * |
| 352 | + * @param string|null $functionName |
| 353 | + * |
| 354 | + * @return boolean Success indicator |
| 355 | + */ |
| 356 | + protected function saveExisting( $functionName = null ) { |
| 357 | + $dbw = wfGetDB( DB_MASTER ); |
| 358 | + |
| 359 | + $success = $dbw->update( |
| 360 | + $this->table->getName(), |
| 361 | + $this->getWriteValues(), |
| 362 | + $this->table->getPrefixedValues( $this->getUpdateConditions() ), |
| 363 | + is_null( $functionName ) ? __METHOD__ : $functionName |
| 364 | + ); |
| 365 | + |
| 366 | + return $success; |
| 367 | + } |
| 368 | + |
| 369 | + /** |
| 370 | + * Returns the WHERE considtions needed to identify this object so |
| 371 | + * it can be updated. |
| 372 | + * |
| 373 | + * @since 1.20 |
| 374 | + * |
| 375 | + * @return array |
| 376 | + */ |
| 377 | + protected function getUpdateConditions() { |
| 378 | + return array( 'id' => $this->getId() ); |
| 379 | + } |
| 380 | + |
| 381 | + /** |
| 382 | + * Inserts the object into the database. |
| 383 | + * |
| 384 | + * @since 1.20 |
| 385 | + * |
| 386 | + * @param string|null $functionName |
| 387 | + * @param array|null $options |
| 388 | + * |
| 389 | + * @return boolean Success indicator |
| 390 | + */ |
| 391 | + protected function insert( $functionName = null, array $options = null ) { |
| 392 | + $dbw = wfGetDB( DB_MASTER ); |
| 393 | + |
| 394 | + $result = $dbw->insert( |
| 395 | + $this->table->getName(), |
| 396 | + $this->getWriteValues(), |
| 397 | + is_null( $functionName ) ? __METHOD__ : $functionName, |
| 398 | + is_null( $options ) ? array( 'IGNORE' ) : $options |
| 399 | + ); |
| 400 | + |
| 401 | + if ( $result ) { |
| 402 | + $this->setField( 'id', $dbw->insertId() ); |
| 403 | + } |
| 404 | + |
| 405 | + return $result; |
| 406 | + } |
| 407 | + |
| 408 | + /** |
| 409 | + * Removes the object from the database. |
| 410 | + * |
| 411 | + * @since 1.20 |
| 412 | + * |
| 413 | + * @return boolean Success indicator |
| 414 | + */ |
| 415 | + public function remove() { |
| 416 | + $this->beforeRemove(); |
| 417 | + |
| 418 | + $success = $this->table->delete( array( 'id' => $this->getId() ) ); |
| 419 | + |
| 420 | + if ( $success ) { |
| 421 | + $this->onRemoved(); |
| 422 | + } |
| 423 | + |
| 424 | + return $success; |
| 425 | + } |
| 426 | + |
| 427 | + /** |
| 428 | + * Gets called before an object is removed from the database. |
| 429 | + * |
| 430 | + * @since 1.20 |
| 431 | + */ |
| 432 | + protected function beforeRemove() { |
| 433 | + $this->loadFields( $this->getBeforeRemoveFields(), false, true ); |
| 434 | + } |
| 435 | + |
| 436 | + /** |
| 437 | + * Before removal of an object happens, @see beforeRemove gets called. |
| 438 | + * This method loads the fields of which the names have been returned by this one (or all fields if null is returned). |
| 439 | + * This allows for loading info needed after removal to get rid of linked data and the like. |
| 440 | + * |
| 441 | + * @since 1.20 |
| 442 | + * |
| 443 | + * @return array|null |
| 444 | + */ |
| 445 | + protected function getBeforeRemoveFields() { |
| 446 | + return array(); |
| 447 | + } |
| 448 | + |
| 449 | + /** |
| 450 | + * Gets called after successfull removal. |
| 451 | + * Can be overriden to get rid of linked data. |
| 452 | + * |
| 453 | + * @since 1.20 |
| 454 | + */ |
| 455 | + protected function onRemoved() { |
| 456 | + $this->setField( 'id', null ); |
| 457 | + } |
| 458 | + |
| 459 | + /** |
| 460 | + * Return the names and values of the fields. |
| 461 | + * |
| 462 | + * @since 1.20 |
| 463 | + * |
| 464 | + * @return array |
| 465 | + */ |
| 466 | + public function getFields() { |
| 467 | + return $this->fields; |
| 468 | + } |
| 469 | + |
| 470 | + /** |
| 471 | + * Return the names of the fields. |
| 472 | + * |
| 473 | + * @since 1.20 |
| 474 | + * |
| 475 | + * @return array |
| 476 | + */ |
| 477 | + public function getSetFieldNames() { |
| 478 | + return array_keys( $this->fields ); |
| 479 | + } |
| 480 | + |
| 481 | + /** |
| 482 | + * Sets the value of a field. |
| 483 | + * Strings can be provided for other types, |
| 484 | + * so this method can be called from unserialization handlers. |
| 485 | + * |
| 486 | + * @since 1.20 |
| 487 | + * |
| 488 | + * @param string $name |
| 489 | + * @param mixed $value |
| 490 | + * |
| 491 | + * @throws MWException |
| 492 | + */ |
| 493 | + public function setField( $name, $value ) { |
| 494 | + $fields = $this->table->getFields(); |
| 495 | + |
| 496 | + if ( array_key_exists( $name, $fields ) ) { |
| 497 | + switch ( $fields[$name] ) { |
| 498 | + case 'int': |
| 499 | + $value = (int)$value; |
| 500 | + break; |
| 501 | + case 'float': |
| 502 | + $value = (float)$value; |
| 503 | + break; |
| 504 | + case 'bool': |
| 505 | + if ( is_string( $value ) ) { |
| 506 | + $value = $value !== '0'; |
| 507 | + } elseif ( is_int( $value ) ) { |
| 508 | + $value = $value !== 0; |
| 509 | + } |
| 510 | + break; |
| 511 | + case 'array': |
| 512 | + if ( is_string( $value ) ) { |
| 513 | + $value = unserialize( $value ); |
| 514 | + } |
| 515 | + |
| 516 | + if ( !is_array( $value ) ) { |
| 517 | + $value = array(); |
| 518 | + } |
| 519 | + break; |
| 520 | + case 'blob': |
| 521 | + if ( is_string( $value ) ) { |
| 522 | + $value = unserialize( $value ); |
| 523 | + } |
| 524 | + break; |
| 525 | + case 'id': |
| 526 | + if ( is_string( $value ) ) { |
| 527 | + $value = (int)$value; |
| 528 | + } |
| 529 | + break; |
| 530 | + } |
| 531 | + |
| 532 | + $this->fields[$name] = $value; |
| 533 | + } else { |
| 534 | + throw new MWException( 'Attempted to set unknown field ' . $name ); |
| 535 | + } |
| 536 | + } |
| 537 | + |
| 538 | + /** |
| 539 | + * Add an amount (can be negative) to the specified field (needs to be numeric). |
| 540 | + * |
| 541 | + * @since 1.20 |
| 542 | + * |
| 543 | + * @param string $field |
| 544 | + * @param integer $amount |
| 545 | + * |
| 546 | + * @return boolean Success indicator |
| 547 | + */ |
| 548 | + public function addToField( $field, $amount ) { |
| 549 | + if ( $amount == 0 ) { |
| 550 | + return true; |
| 551 | + } |
| 552 | + |
| 553 | + if ( !$this->hasIdField() ) { |
| 554 | + return false; |
| 555 | + } |
| 556 | + |
| 557 | + $absoluteAmount = abs( $amount ); |
| 558 | + $isNegative = $amount < 0; |
| 559 | + |
| 560 | + $dbw = wfGetDB( DB_MASTER ); |
| 561 | + |
| 562 | + $fullField = $this->table->getPrefixedField( $field ); |
| 563 | + |
| 564 | + $success = $dbw->update( |
| 565 | + $this->table->getName(), |
| 566 | + array( "$fullField=$fullField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ), |
| 567 | + array( $this->table->getPrefixedField( 'id' ) => $this->getId() ), |
| 568 | + __METHOD__ |
| 569 | + ); |
| 570 | + |
| 571 | + if ( $success && $this->hasField( $field ) ) { |
| 572 | + $this->setField( $field, $this->getField( $field ) + $amount ); |
| 573 | + } |
| 574 | + |
| 575 | + return $success; |
| 576 | + } |
| 577 | + |
| 578 | + /** |
| 579 | + * Return the names of the fields. |
| 580 | + * |
| 581 | + * @since 1.20 |
| 582 | + * |
| 583 | + * @return array |
| 584 | + */ |
| 585 | + public function getFieldNames() { |
| 586 | + return array_keys( $this->table->getFields() ); |
| 587 | + } |
| 588 | + |
| 589 | + /** |
| 590 | + * Computes and updates the values of the summary fields. |
| 591 | + * |
| 592 | + * @since 1.20 |
| 593 | + * |
| 594 | + * @param array|string|null $summaryFields |
| 595 | + */ |
| 596 | + public function loadSummaryFields( $summaryFields = null ) { |
| 597 | + |
| 598 | + } |
| 599 | + |
| 600 | + /** |
| 601 | + * Sets the value for the @see $updateSummaries field. |
| 602 | + * |
| 603 | + * @since 1.20 |
| 604 | + * |
| 605 | + * @param boolean $update |
| 606 | + */ |
| 607 | + public function setUpdateSummaries( $update ) { |
| 608 | + $this->updateSummaries = $update; |
| 609 | + } |
| 610 | + |
| 611 | + /** |
| 612 | + * Sets the value for the @see $inSummaryMode field. |
| 613 | + * |
| 614 | + * @since 1.20 |
| 615 | + * |
| 616 | + * @param boolean $summaryMode |
| 617 | + */ |
| 618 | + public function setSummaryMode( $summaryMode ) { |
| 619 | + $this->inSummaryMode = $summaryMode; |
| 620 | + } |
| 621 | + |
| 622 | + /** |
| 623 | + * Return if any fields got changed. |
| 624 | + * |
| 625 | + * @since 1.20 |
| 626 | + * |
| 627 | + * @param ORMRow $object |
| 628 | + * @param boolean|array $excludeSummaryFields |
| 629 | + * When set to true, summary field changes are ignored. |
| 630 | + * Can also be an array of fields to ignore. |
| 631 | + * |
| 632 | + * @return boolean |
| 633 | + */ |
| 634 | + protected function fieldsChanged( ORMRow $object, $excludeSummaryFields = false ) { |
| 635 | + $exclusionFields = array(); |
| 636 | + |
| 637 | + if ( $excludeSummaryFields !== false ) { |
| 638 | + $exclusionFields = is_array( $excludeSummaryFields ) ? $excludeSummaryFields : $this->table->getSummaryFields(); |
| 639 | + } |
| 640 | + |
| 641 | + foreach ( $this->fields as $name => $value ) { |
| 642 | + $excluded = $excludeSummaryFields && in_array( $name, $exclusionFields ); |
| 643 | + |
| 644 | + if ( !$excluded && $object->getField( $name ) !== $value ) { |
| 645 | + return true; |
| 646 | + } |
| 647 | + } |
| 648 | + |
| 649 | + return false; |
| 650 | + } |
| 651 | + |
| 652 | + /** |
| 653 | + * Returns the table this ORMRow is a row in. |
| 654 | + * |
| 655 | + * @since 1.20 |
| 656 | + * |
| 657 | + * @return ORMTable |
| 658 | + */ |
| 659 | + public function getTable() { |
| 660 | + return $this->table; |
| 661 | + } |
| 662 | + |
| 663 | +} |
Index: trunk/extensions/EducationProgram/compat/ORMTable.php |
— | — | @@ -0,0 +1,658 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract base class for representing a single database table. |
| 6 | + * |
| 7 | + * @since 1.20 |
| 8 | + * |
| 9 | + * @file ORMTable.php |
| 10 | + * |
| 11 | + * @licence GNU GPL v2 or later |
| 12 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 13 | + */ |
| 14 | +abstract class ORMTable { |
| 15 | + |
| 16 | + /** |
| 17 | + * Returns the name of the database table objects of this type are stored in. |
| 18 | + * |
| 19 | + * @since 1.20 |
| 20 | + * |
| 21 | + * @return string |
| 22 | + */ |
| 23 | + public abstract function getName(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Returns the name of a ORMRow deriving class that |
| 27 | + * represents single rows in this table. |
| 28 | + * |
| 29 | + * @since 1.20 |
| 30 | + * |
| 31 | + * @return string |
| 32 | + */ |
| 33 | + public abstract function getRowClass(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the db field prefix. |
| 37 | + * |
| 38 | + * @since 1.20 |
| 39 | + * |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + protected abstract function getFieldPrefix(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns an array with the fields and their types this object contains. |
| 46 | + * This corresponds directly to the fields in the database, without prefix. |
| 47 | + * |
| 48 | + * field name => type |
| 49 | + * |
| 50 | + * Allowed types: |
| 51 | + * * id |
| 52 | + * * str |
| 53 | + * * int |
| 54 | + * * float |
| 55 | + * * bool |
| 56 | + * * array |
| 57 | + * |
| 58 | + * @since 1.20 |
| 59 | + * |
| 60 | + * @return array |
| 61 | + */ |
| 62 | + public abstract function getFields(); |
| 63 | + |
| 64 | + /** |
| 65 | + * The database connection to use for read operations. |
| 66 | + * Can be changed via @see setReadDb. |
| 67 | + * |
| 68 | + * @since 1.20 |
| 69 | + * @var integer DB_ enum |
| 70 | + */ |
| 71 | + protected $readDb = DB_SLAVE; |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns a list of default field values. |
| 75 | + * field name => field value |
| 76 | + * |
| 77 | + * @since 1.20 |
| 78 | + * |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + public function getDefaults() { |
| 82 | + return array(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns a list of the summary fields. |
| 87 | + * These are fields that cache computed values, such as the amount of linked objects of $type. |
| 88 | + * This is relevant as one might not want to do actions such as log changes when these get updated. |
| 89 | + * |
| 90 | + * @since 1.20 |
| 91 | + * |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function getSummaryFields() { |
| 95 | + return array(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Selects the the specified fields of the records matching the provided |
| 100 | + * conditions and returns them as ORMRow. Field names get prefixed. |
| 101 | + * |
| 102 | + * @since 1.20 |
| 103 | + * |
| 104 | + * @param array|string|null $fields |
| 105 | + * @param array $conditions |
| 106 | + * @param array $options |
| 107 | + * @param string|null $functionName |
| 108 | + * |
| 109 | + * @return array of self |
| 110 | + */ |
| 111 | + public function select( $fields = null, array $conditions = array(), array $options = array(), $functionName = null ) { |
| 112 | + $result = $this->selectFields( $fields, $conditions, $options, false, $functionName ); |
| 113 | + |
| 114 | + $objects = array(); |
| 115 | + |
| 116 | + foreach ( $result as $record ) { |
| 117 | + $objects[] = $this->newFromArray( $record ); |
| 118 | + } |
| 119 | + |
| 120 | + return $objects; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Selects the the specified fields of the records matching the provided |
| 125 | + * conditions and returns them as associative arrays. |
| 126 | + * Provided field names get prefixed. |
| 127 | + * Returned field names will not have a prefix. |
| 128 | + * |
| 129 | + * When $collapse is true: |
| 130 | + * If one field is selected, each item in the result array will be this field. |
| 131 | + * If two fields are selected, each item in the result array will have as key |
| 132 | + * the first field and as value the second field. |
| 133 | + * If more then two fields are selected, each item will be an associative array. |
| 134 | + * |
| 135 | + * @since 1.20 |
| 136 | + * |
| 137 | + * @param array|string|null $fields |
| 138 | + * @param array $conditions |
| 139 | + * @param array $options |
| 140 | + * @param boolean $collapse Set to false to always return each result row as associative array. |
| 141 | + * @param string|null $functionName |
| 142 | + * |
| 143 | + * @return array of array |
| 144 | + */ |
| 145 | + public function selectFields( $fields = null, array $conditions = array(), array $options = array(), $collapse = true, $functionName = null ) { |
| 146 | + if ( is_null( $fields ) ) { |
| 147 | + $fields = array_keys( $this->getFields() ); |
| 148 | + } |
| 149 | + else { |
| 150 | + $fields = (array)$fields; |
| 151 | + } |
| 152 | + |
| 153 | + $dbr = wfGetDB( $this->getReadDb() ); |
| 154 | + $result = $dbr->select( |
| 155 | + $this->getName(), |
| 156 | + $this->getPrefixedFields( $fields ), |
| 157 | + $this->getPrefixedValues( $conditions ), |
| 158 | + is_null( $functionName ) ? __METHOD__ : $functionName, |
| 159 | + $options |
| 160 | + ); |
| 161 | + |
| 162 | + $objects = array(); |
| 163 | + |
| 164 | + foreach ( $result as $record ) { |
| 165 | + $objects[] = $this->getFieldsFromDBResult( $record ); |
| 166 | + } |
| 167 | + |
| 168 | + if ( $collapse ) { |
| 169 | + if ( count( $fields ) === 1 ) { |
| 170 | + $objects = array_map( 'array_shift', $objects ); |
| 171 | + } |
| 172 | + elseif ( count( $fields ) === 2 ) { |
| 173 | + $o = array(); |
| 174 | + |
| 175 | + foreach ( $objects as $object ) { |
| 176 | + $o[array_shift( $object )] = array_shift( $object ); |
| 177 | + } |
| 178 | + |
| 179 | + $objects = $o; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return $objects; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Selects the the specified fields of the first matching record. |
| 188 | + * Field names get prefixed. |
| 189 | + * |
| 190 | + * @since 1.20 |
| 191 | + * |
| 192 | + * @param array|string|null $fields |
| 193 | + * @param array $conditions |
| 194 | + * @param array $options |
| 195 | + * @param string|null $functionName |
| 196 | + * |
| 197 | + * @return DBObject|bool False on failure |
| 198 | + */ |
| 199 | + public function selectRow( $fields = null, array $conditions = array(), array $options = array(), $functionName = null ) { |
| 200 | + $options['LIMIT'] = 1; |
| 201 | + |
| 202 | + $objects = $this->select( $fields, $conditions, $options, $functionName ); |
| 203 | + |
| 204 | + return empty( $objects ) ? false : $objects[0]; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Selects the the specified fields of the records matching the provided |
| 209 | + * conditions. Field names do NOT get prefixed. |
| 210 | + * |
| 211 | + * @since 1.20 |
| 212 | + * |
| 213 | + * @param array $fields |
| 214 | + * @param array $conditions |
| 215 | + * @param array $options |
| 216 | + * @param string|null $functionName |
| 217 | + * |
| 218 | + * @return ResultWrapper |
| 219 | + */ |
| 220 | + public function rawSelectRow( array $fields, array $conditions = array(), array $options = array(), $functionName = null ) { |
| 221 | + $dbr = wfGetDB( $this->getReadDb() ); |
| 222 | + |
| 223 | + return $dbr->selectRow( |
| 224 | + $this->getName(), |
| 225 | + $fields, |
| 226 | + $conditions, |
| 227 | + is_null( $functionName ) ? __METHOD__ : $functionName, |
| 228 | + $options |
| 229 | + ); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Selects the the specified fields of the first record matching the provided |
| 234 | + * conditions and returns it as an associative array, or false when nothing matches. |
| 235 | + * This method makes use of selectFields and expects the same parameters and |
| 236 | + * returns the same results (if there are any, if there are none, this method returns false). |
| 237 | + * @see ORMTable::selectFields |
| 238 | + * |
| 239 | + * @since 1.20 |
| 240 | + * |
| 241 | + * @param array|string|null $fields |
| 242 | + * @param array $conditions |
| 243 | + * @param array $options |
| 244 | + * @param boolean $collapse Set to false to always return each result row as associative array. |
| 245 | + * @param string|null $functionName |
| 246 | + * |
| 247 | + * @return mixed|array|bool False on failure |
| 248 | + */ |
| 249 | + public function selectFieldsRow( $fields = null, array $conditions = array(), array $options = array(), $collapse = true, $functionName = null ) { |
| 250 | + $options['LIMIT'] = 1; |
| 251 | + |
| 252 | + $objects = $this->selectFields( $fields, $conditions, $options, $collapse, $functionName ); |
| 253 | + |
| 254 | + return empty( $objects ) ? false : $objects[0]; |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Returns if there is at least one record matching the provided conditions. |
| 259 | + * Condition field names get prefixed. |
| 260 | + * |
| 261 | + * @since 1.20 |
| 262 | + * |
| 263 | + * @param array $conditions |
| 264 | + * |
| 265 | + * @return boolean |
| 266 | + */ |
| 267 | + public function has( array $conditions = array() ) { |
| 268 | + return $this->selectRow( array( 'id' ), $conditions ) !== false; |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Returns the amount of matching records. |
| 273 | + * Condition field names get prefixed. |
| 274 | + * |
| 275 | + * Note that this can be expensive on large tables. |
| 276 | + * In such cases you might want to use DatabaseBase::estimateRowCount instead. |
| 277 | + * |
| 278 | + * @since 1.20 |
| 279 | + * |
| 280 | + * @param array $conditions |
| 281 | + * @param array $options |
| 282 | + * |
| 283 | + * @return integer |
| 284 | + */ |
| 285 | + public function count( array $conditions = array(), array $options = array() ) { |
| 286 | + $res = $this->rawSelectRow( |
| 287 | + array( 'COUNT(*) AS rowcount' ), |
| 288 | + $this->getPrefixedValues( $conditions ), |
| 289 | + $options |
| 290 | + ); |
| 291 | + |
| 292 | + return $res->rowcount; |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * Removes the object from the database. |
| 297 | + * |
| 298 | + * @since 1.20 |
| 299 | + * |
| 300 | + * @param array $conditions |
| 301 | + * @param string|null $functionName |
| 302 | + * |
| 303 | + * @return boolean Success indicator |
| 304 | + */ |
| 305 | + public function delete( array $conditions, $functionName = null ) { |
| 306 | + return wfGetDB( DB_MASTER )->delete( |
| 307 | + $this->getName(), |
| 308 | + $this->getPrefixedValues( $conditions ), |
| 309 | + $functionName |
| 310 | + ); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * Get API parameters for the fields supported by this object. |
| 315 | + * |
| 316 | + * @since 1.20 |
| 317 | + * |
| 318 | + * @param boolean $requireParams |
| 319 | + * @param boolean $setDefaults |
| 320 | + * |
| 321 | + * @return array |
| 322 | + */ |
| 323 | + public function getAPIParams( $requireParams = false, $setDefaults = false ) { |
| 324 | + $typeMap = array( |
| 325 | + 'id' => 'integer', |
| 326 | + 'int' => 'integer', |
| 327 | + 'float' => 'NULL', |
| 328 | + 'str' => 'string', |
| 329 | + 'bool' => 'integer', |
| 330 | + 'array' => 'string', |
| 331 | + 'blob' => 'string', |
| 332 | + ); |
| 333 | + |
| 334 | + $params = array(); |
| 335 | + $defaults = $this->getDefaults(); |
| 336 | + |
| 337 | + foreach ( $this->getFields() as $field => $type ) { |
| 338 | + if ( $field == 'id' ) { |
| 339 | + continue; |
| 340 | + } |
| 341 | + |
| 342 | + $hasDefault = array_key_exists( $field, $defaults ); |
| 343 | + |
| 344 | + $params[$field] = array( |
| 345 | + ApiBase::PARAM_TYPE => $typeMap[$type], |
| 346 | + ApiBase::PARAM_REQUIRED => $requireParams && !$hasDefault |
| 347 | + ); |
| 348 | + |
| 349 | + if ( $type == 'array' ) { |
| 350 | + $params[$field][ApiBase::PARAM_ISMULTI] = true; |
| 351 | + } |
| 352 | + |
| 353 | + if ( $setDefaults && $hasDefault ) { |
| 354 | + $default = is_array( $defaults[$field] ) ? implode( '|', $defaults[$field] ) : $defaults[$field]; |
| 355 | + $params[$field][ApiBase::PARAM_DFLT] = $default; |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + return $params; |
| 360 | + } |
| 361 | + |
| 362 | + /** |
| 363 | + * Returns an array with the fields and their descriptions. |
| 364 | + * |
| 365 | + * field name => field description |
| 366 | + * |
| 367 | + * @since 1.20 |
| 368 | + * |
| 369 | + * @return array |
| 370 | + */ |
| 371 | + public function getFieldDescriptions() { |
| 372 | + return array(); |
| 373 | + } |
| 374 | + |
| 375 | + /** |
| 376 | + * Get the database type used for read operations. |
| 377 | + * |
| 378 | + * @since 1.20 |
| 379 | + * |
| 380 | + * @return integer DB_ enum |
| 381 | + */ |
| 382 | + public function getReadDb() { |
| 383 | + return $this->readDb; |
| 384 | + } |
| 385 | + |
| 386 | + /** |
| 387 | + * Set the database type to use for read operations. |
| 388 | + * |
| 389 | + * @param integer $db |
| 390 | + * |
| 391 | + * @since 1.20 |
| 392 | + */ |
| 393 | + public function setReadDb( $db ) { |
| 394 | + $this->readDb = $db; |
| 395 | + } |
| 396 | + |
| 397 | + /** |
| 398 | + * Update the records matching the provided conditions by |
| 399 | + * setting the fields that are keys in the $values param to |
| 400 | + * their corresponding values. |
| 401 | + * |
| 402 | + * @since 1.20 |
| 403 | + * |
| 404 | + * @param array $values |
| 405 | + * @param array $conditions |
| 406 | + * |
| 407 | + * @return boolean Success indicator |
| 408 | + */ |
| 409 | + public function update( array $values, array $conditions = array() ) { |
| 410 | + $dbw = wfGetDB( DB_MASTER ); |
| 411 | + |
| 412 | + return $dbw->update( |
| 413 | + $this->getName(), |
| 414 | + $this->getPrefixedValues( $values ), |
| 415 | + $this->getPrefixedValues( $conditions ), |
| 416 | + __METHOD__ |
| 417 | + ); |
| 418 | + } |
| 419 | + |
| 420 | + /** |
| 421 | + * Computes the values of the summary fields of the objects matching the provided conditions. |
| 422 | + * |
| 423 | + * @since 1.20 |
| 424 | + * |
| 425 | + * @param array|string|null $summaryFields |
| 426 | + * @param array $conditions |
| 427 | + */ |
| 428 | + public function updateSummaryFields( $summaryFields = null, array $conditions = array() ) { |
| 429 | + $this->setReadDb( DB_MASTER ); |
| 430 | + |
| 431 | + foreach ( $this->select( null, $conditions ) as /* ORMRow */ $item ) { |
| 432 | + $item->loadSummaryFields( $summaryFields ); |
| 433 | + $item->setSummaryMode( true ); |
| 434 | + $item->save(); |
| 435 | + } |
| 436 | + |
| 437 | + $this->setReadDb( DB_SLAVE ); |
| 438 | + } |
| 439 | + |
| 440 | + /** |
| 441 | + * Takes in an associative array with field names as keys and |
| 442 | + * their values as value. The field names are prefixed with the |
| 443 | + * db field prefix. |
| 444 | + * |
| 445 | + * Field names can also be provided as an array with as first element a table name, such as |
| 446 | + * $conditions = array( |
| 447 | + * array( array( 'tablename', 'fieldname' ), $value ), |
| 448 | + * ); |
| 449 | + * |
| 450 | + * @since 1.20 |
| 451 | + * |
| 452 | + * @param array $values |
| 453 | + * |
| 454 | + * @return array |
| 455 | + */ |
| 456 | + public function getPrefixedValues( array $values ) { |
| 457 | + $prefixedValues = array(); |
| 458 | + |
| 459 | + foreach ( $values as $field => $value ) { |
| 460 | + if ( is_integer( $field ) ) { |
| 461 | + if ( is_array( $value ) ) { |
| 462 | + $field = $value[0]; |
| 463 | + $value = $value[1]; |
| 464 | + } |
| 465 | + else { |
| 466 | + $value = explode( ' ', $value, 2 ); |
| 467 | + $value[0] = $this->getPrefixedField( $value[0] ); |
| 468 | + $prefixedValues[] = implode( ' ', $value ); |
| 469 | + continue; |
| 470 | + } |
| 471 | + } |
| 472 | + |
| 473 | + $prefixedValues[$this->getPrefixedField( $field )] = $value; |
| 474 | + } |
| 475 | + |
| 476 | + return $prefixedValues; |
| 477 | + } |
| 478 | + |
| 479 | + /** |
| 480 | + * Takes in a field or array of fields and returns an |
| 481 | + * array with their prefixed versions, ready for db usage. |
| 482 | + * |
| 483 | + * @since 1.20 |
| 484 | + * |
| 485 | + * @param array|string $fields |
| 486 | + * |
| 487 | + * @return array |
| 488 | + */ |
| 489 | + public function getPrefixedFields( array $fields ) { |
| 490 | + foreach ( $fields as &$field ) { |
| 491 | + $field = $this->getPrefixedField( $field ); |
| 492 | + } |
| 493 | + |
| 494 | + return $fields; |
| 495 | + } |
| 496 | + |
| 497 | + /** |
| 498 | + * Takes in a field and returns an it's prefixed version, ready for db usage. |
| 499 | + * |
| 500 | + * @since 1.20 |
| 501 | + * |
| 502 | + * @param string|array $field |
| 503 | + * |
| 504 | + * @return string |
| 505 | + */ |
| 506 | + public function getPrefixedField( $field ) { |
| 507 | + return $this->getFieldPrefix() . $field; |
| 508 | + } |
| 509 | + |
| 510 | + /** |
| 511 | + * Takes an array of field names with prefix and returns the unprefixed equivalent. |
| 512 | + * |
| 513 | + * @since 1.20 |
| 514 | + * |
| 515 | + * @param array $fieldNames |
| 516 | + * |
| 517 | + * @return array |
| 518 | + */ |
| 519 | + public function unprefixFieldNames( array $fieldNames ) { |
| 520 | + return array_map( array( $this, 'unprefixFieldName' ), $fieldNames ); |
| 521 | + } |
| 522 | + |
| 523 | + /** |
| 524 | + * Takes a field name with prefix and returns the unprefixed equivalent. |
| 525 | + * |
| 526 | + * @since 1.20 |
| 527 | + * |
| 528 | + * @param string $fieldName |
| 529 | + * |
| 530 | + * @return string |
| 531 | + */ |
| 532 | + public function unprefixFieldName( $fieldName ) { |
| 533 | + return substr( $fieldName, strlen( $this->getFieldPrefix() ) ); |
| 534 | + } |
| 535 | + |
| 536 | + public function __construct() { |
| 537 | + |
| 538 | + } |
| 539 | + |
| 540 | + /** |
| 541 | + * Get an instance of this class. |
| 542 | + * |
| 543 | + * @since 1.20 |
| 544 | + * |
| 545 | + * @return ORMTable |
| 546 | + */ |
| 547 | + public static function singleton() { |
| 548 | + static $instance; |
| 549 | + |
| 550 | + if ( !isset( $instance ) ) { |
| 551 | + $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); |
| 552 | + $instance = new $class; |
| 553 | + } |
| 554 | + |
| 555 | + return $instance; |
| 556 | + } |
| 557 | + |
| 558 | + /** |
| 559 | + * Compatibility fallback function so the singleton method works on PHP < 5.3. |
| 560 | + * Code borrowed from http://www.php.net/manual/en/function.get-called-class.php#107445 |
| 561 | + * |
| 562 | + * @since 1.20 |
| 563 | + * |
| 564 | + * @return string |
| 565 | + */ |
| 566 | + protected static function get_called_class() { |
| 567 | + $bt = debug_backtrace(); |
| 568 | + $l = count($bt) - 1; |
| 569 | + $matches = array(); |
| 570 | + while(empty($matches) && $l > -1){ |
| 571 | + $lines = file($bt[$l]['file']); |
| 572 | + $callerLine = $lines[$bt[$l]['line']-1]; |
| 573 | + preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l--]['function'].'/', |
| 574 | + $callerLine, |
| 575 | + $matches); |
| 576 | + } |
| 577 | + if (!isset($matches[1])) $matches[1]=NULL; //for notices |
| 578 | + if ($matches[1] == 'self') { |
| 579 | + $line = $bt[$l]['line']-1; |
| 580 | + while ($line > 0 && strpos($lines[$line], 'class') === false) { |
| 581 | + $line--; |
| 582 | + } |
| 583 | + preg_match('/class[\s]+(.+?)[\s]+/si', $lines[$line], $matches); |
| 584 | + } |
| 585 | + return $matches[1]; |
| 586 | + } |
| 587 | + |
| 588 | + /** |
| 589 | + * Get an array with fields from a database result, |
| 590 | + * that can be fed directly to the constructor or |
| 591 | + * to setFields. |
| 592 | + * |
| 593 | + * @since 1.20 |
| 594 | + * |
| 595 | + * @param stdClass $result |
| 596 | + * |
| 597 | + * @return array |
| 598 | + */ |
| 599 | + public function getFieldsFromDBResult( stdClass $result ) { |
| 600 | + $result = (array)$result; |
| 601 | + return array_combine( |
| 602 | + $this->unprefixFieldNames( array_keys( $result ) ), |
| 603 | + array_values( $result ) |
| 604 | + ); |
| 605 | + } |
| 606 | + |
| 607 | + /** |
| 608 | + * Get a new instance of the class from a database result. |
| 609 | + * |
| 610 | + * @since 1.20 |
| 611 | + * |
| 612 | + * @param stdClass $result |
| 613 | + * |
| 614 | + * @return ORMRow |
| 615 | + */ |
| 616 | + public function newFromDBResult( stdClass $result ) { |
| 617 | + return $this->newFromArray( $this->getFieldsFromDBResult( $result ) ); |
| 618 | + } |
| 619 | + |
| 620 | + /** |
| 621 | + * Get a new instance of the class from an array. |
| 622 | + * |
| 623 | + * @since 1.20 |
| 624 | + * |
| 625 | + * @param array $data |
| 626 | + * @param boolean $loadDefaults |
| 627 | + * |
| 628 | + * @return ORMRow |
| 629 | + */ |
| 630 | + public function newFromArray( array $data, $loadDefaults = false ) { |
| 631 | + $class = $this->getRowClass(); |
| 632 | + return new $class( $this, $data, $loadDefaults ); |
| 633 | + } |
| 634 | + |
| 635 | + /** |
| 636 | + * Return the names of the fields. |
| 637 | + * |
| 638 | + * @since 1.20 |
| 639 | + * |
| 640 | + * @return array |
| 641 | + */ |
| 642 | + public function getFieldNames() { |
| 643 | + return array_keys( $this->getFields() ); |
| 644 | + } |
| 645 | + |
| 646 | + /** |
| 647 | + * Gets if the object can take a certain field. |
| 648 | + * |
| 649 | + * @since 1.20 |
| 650 | + * |
| 651 | + * @param string $name |
| 652 | + * |
| 653 | + * @return boolean |
| 654 | + */ |
| 655 | + public function canHaveField( $name ) { |
| 656 | + return array_key_exists( $name, $this->getFields() ); |
| 657 | + } |
| 658 | + |
| 659 | +} |
Index: trunk/extensions/EducationProgram/docs/schema.txt |
— | — | @@ -21,7 +21,7 @@ |
22 | 22 | * Revision storage is the ep_revisions table which contains blobs with the |
23 | 23 | relational data for one particular object. |
24 | 24 | |
25 | | -The extension uses the DBDataObject and DBTable classes for virtually all |
| 25 | +The extension uses the ORMRow and ORMTable classes for virtually all |
26 | 26 | it's database interaction. These have mechanisms for distinguishing between |
27 | 27 | relational and denormalized data (referred to as "summary data" in their docs). |
28 | 28 | All revisioning work is done through EPRevision and EPRevisions using EPRevisionedObject. |
Index: trunk/extensions/EducationProgram/EducationProgram.php |
— | — | @@ -146,8 +146,9 @@ |
147 | 147 | |
148 | 148 | // Compat classes |
149 | 149 | foreach ( array( |
150 | | - 'DBDataObject' => 'DBDataObject.php', |
151 | | - 'DBTable' => 'DBTable.php', |
| 150 | + 'ORMResult' => 'ORMResult.php', |
| 151 | + 'ORMRow' => 'ORMRow.php', |
| 152 | + 'ORMTable' => 'ORMTable.php', |
152 | 153 | 'CacheHelper' => 'CacheHelper.php', |
153 | 154 | 'ICacheHelper' => 'CacheHelper.php', |
154 | 155 | 'CachedAction' => 'CachedAction.php', |
Index: trunk/extensions/EducationProgram/sql/EducationProgram.sql |
— | — | @@ -18,7 +18,7 @@ |
19 | 19 | org_ca_count INT unsigned NOT NULL, -- Amount of campus ambassadors |
20 | 20 | org_student_count INT unsigned NOT NULL, -- Amount of students |
21 | 21 | org_courses BLOB NOT NULL -- The ids of the courses (linking ep_courses.course_id) |
22 | | -) /*$wgDBTableOptions*/; |
| 22 | +) /*$wgORMTableOptions*/; |
23 | 23 | |
24 | 24 | CREATE UNIQUE INDEX /*i*/ep_org_name ON /*_*/ep_orgs (org_name); |
25 | 25 | CREATE INDEX /*i*/ep_org_city ON /*_*/ep_orgs (org_city); |
— | — | @@ -57,7 +57,7 @@ |
58 | 58 | course_oa_count SMALLINT unsigned NOT NULL, -- Amount of online ambassadors |
59 | 59 | course_ca_count SMALLINT unsigned NOT NULL, -- Amount of campus ambassadors |
60 | 60 | course_student_count SMALLINT unsigned NOT NULL -- Amount of students |
61 | | -) /*$wgDBTableOptions*/; |
| 61 | +) /*$wgORMTableOptions*/; |
62 | 62 | |
63 | 63 | CREATE INDEX /*i*/ep_course_org_id ON /*_*/ep_courses (course_org_id); |
64 | 64 | CREATE INDEX /*i*/ep_course_name ON /*_*/ep_courses (course_name); |
— | — | @@ -86,7 +86,7 @@ |
87 | 87 | article_page_title varchar(255) binary NOT NULL, -- Full title of the page, to allow for associating non-existing pages |
88 | 88 | |
89 | 89 | article_reviewers BLOB NOT NULL -- List of reviewers for this article (linking user.user_id) |
90 | | -) /*$wgDBTableOptions*/; |
| 90 | +) /*$wgORMTableOptions*/; |
91 | 91 | |
92 | 92 | CREATE INDEX /*i*/ep_articles_user_id ON /*_*/ep_articles (article_user_id); |
93 | 93 | CREATE INDEX /*i*/ep_articles_course_id ON /*_*/ep_articles (article_course_id); |
— | — | @@ -102,7 +102,7 @@ |
103 | 103 | upc_course_id INT unsigned NOT NULL, -- Foreign key on ep_courses.course_id |
104 | 104 | upc_role TINYINT unsigned NOT NULL, -- The role the user has for the course |
105 | 105 | upc_time varbinary(14) NOT NULL -- Time at which the link was made |
106 | | -) /*$wgDBTableOptions*/; |
| 106 | +) /*$wgORMTableOptions*/; |
107 | 107 | |
108 | 108 | CREATE UNIQUE INDEX /*i*/ep_users_per_course ON /*_*/ep_users_per_course (upc_user_id, upc_course_id, upc_role); |
109 | 109 | CREATE INDEX /*i*/ep_upc_course_id ON /*_*/ep_users_per_course (upc_course_id); |
— | — | @@ -123,7 +123,7 @@ |
124 | 124 | student_last_course INT unsigned NOT NULL, -- Last course the user enrolled in |
125 | 125 | student_last_active varbinary(14) NOT NULL, -- Time of last activity in article NS |
126 | 126 | student_active_enroll TINYINT unsigned NOT NULL -- If the student is enrolled in any active courses |
127 | | -) /*$wgDBTableOptions*/; |
| 127 | +) /*$wgORMTableOptions*/; |
128 | 128 | |
129 | 129 | CREATE UNIQUE INDEX /*i*/ep_students_user_id ON /*_*/ep_students (student_user_id); |
130 | 130 | CREATE INDEX /*i*/ep_students_first_enroll ON /*_*/ep_students (student_first_enroll); |
— | — | @@ -139,7 +139,7 @@ |
140 | 140 | CREATE TABLE IF NOT EXISTS /*_*/ep_instructors ( |
141 | 141 | instructor_id INT unsigned NOT NULL auto_increment PRIMARY KEY, |
142 | 142 | instructor_user_id INT unsigned NOT NULL -- Foreign key on user.user_id |
143 | | -) /*$wgDBTableOptions*/; |
| 143 | +) /*$wgORMTableOptions*/; |
144 | 144 | |
145 | 145 | CREATE UNIQUE INDEX /*i*/ep_instructors_user_id ON /*_*/ep_instructors (instructor_user_id); |
146 | 146 | |
— | — | @@ -153,7 +153,7 @@ |
154 | 154 | ca_visible TINYINT unsigned NOT NULL, -- If the profile should be public |
155 | 155 | ca_bio TEXT NOT NULL, -- Bio of the ambassador |
156 | 156 | ca_photo VARCHAR(255) NOT NULL -- Name of a photo of the ambassador on commons |
157 | | -) /*$wgDBTableOptions*/; |
| 157 | +) /*$wgORMTableOptions*/; |
158 | 158 | |
159 | 159 | CREATE UNIQUE INDEX /*i*/ep_cas_user_id ON /*_*/ep_cas (ca_user_id); |
160 | 160 | CREATE INDEX /*i*/ep_cas_visible ON /*_*/ep_cas (ca_visible); |
— | — | @@ -168,7 +168,7 @@ |
169 | 169 | oa_visible TINYINT unsigned NOT NULL, -- If the profile should be public |
170 | 170 | oa_bio TEXT NOT NULL, -- Bio of the ambassador |
171 | 171 | oa_photo VARCHAR(255) NOT NULL -- Name of a photo of the ambassador on commons |
172 | | -) /*$wgDBTableOptions*/; |
| 172 | +) /*$wgORMTableOptions*/; |
173 | 173 | |
174 | 174 | CREATE UNIQUE INDEX /*i*/ep_oas_user_id ON /*_*/ep_oas (oa_user_id); |
175 | 175 | CREATE INDEX /*i*/ep_oas_visible ON /*_*/ep_oas (oa_visible); |
— | — | @@ -215,10 +215,10 @@ |
216 | 216 | |
217 | 217 | -- The actual revision content. This is a blob containing the fields |
218 | 218 | -- of the object (array) passed to PHPs serialize(). |
219 | | - -- A new DBDataObject of it's type can be constructed by passing |
| 219 | + -- A new ORMRow of it's type can be constructed by passing |
220 | 220 | -- it the result of unserialize on this blob. |
221 | 221 | rev_data BLOB NOT NULL |
222 | | -) /*$wgDBTableOptions*/; |
| 222 | +) /*$wgORMTableOptions*/; |
223 | 223 | |
224 | 224 | CREATE INDEX /*i*/ep_revision_object_id ON /*_*/ep_revisions (rev_object_id); |
225 | 225 | CREATE INDEX /*i*/ep_revision_type ON /*_*/ep_revisions (rev_type); |
Index: trunk/extensions/EducationProgram/actions/ViewCourseAction.php |
— | — | @@ -50,7 +50,7 @@ |
51 | 51 | * (non-PHPdoc) |
52 | 52 | * @see EPViewAction::getPageHTML() |
53 | 53 | */ |
54 | | - public function getPageHTML( DBDataObject $course ) { |
| 54 | + public function getPageHTML( ORMRow $course ) { |
55 | 55 | $html = parent::getPageHTML( $course ); |
56 | 56 | |
57 | 57 | $html .= Html::element( 'h2', array(), wfMsg( 'ep-course-description' ) ); |
— | — | @@ -93,7 +93,7 @@ |
94 | 94 | * |
95 | 95 | * @return array |
96 | 96 | */ |
97 | | - protected function getSummaryData( DBDataObject $course ) { |
| 97 | + protected function getSummaryData( ORMRow $course ) { |
98 | 98 | $stats = array(); |
99 | 99 | |
100 | 100 | $orgName = EPOrgs::singleton()->selectFieldsRow( 'name', array( 'id' => $course->getField( 'org_id' ) ) ); |
Index: trunk/extensions/EducationProgram/actions/EPViewAction.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Abstract action for viewing DBDataObject items. |
| 5 | + * Abstract action for viewing ORMRow items. |
6 | 6 | * |
7 | 7 | * @since 0.1 |
8 | 8 | * |
— | — | @@ -27,7 +27,7 @@ |
28 | 28 | * |
29 | 29 | * @param Page $page |
30 | 30 | * @param IContextSource $context |
31 | | - * @param DBTable $table |
| 31 | + * @param ORMTable $table |
32 | 32 | */ |
33 | 33 | protected function __construct( Page $page, IContextSource $context = null, EPPageTable $table ) { |
34 | 34 | $this->table = $table; |
— | — | @@ -143,11 +143,11 @@ |
144 | 144 | * |
145 | 145 | * @since 0.1 |
146 | 146 | * |
147 | | - * @param DBDataObject $object |
| 147 | + * @param ORMRow $object |
148 | 148 | * |
149 | 149 | * @return string |
150 | 150 | */ |
151 | | - public function getPageHTML( DBDataObject $object ) { |
| 151 | + public function getPageHTML( ORMRow $object ) { |
152 | 152 | return $this->getSummary( $object ); |
153 | 153 | } |
154 | 154 | |
— | — | @@ -181,13 +181,13 @@ |
182 | 182 | * |
183 | 183 | * @since 0.1 |
184 | 184 | * |
185 | | - * @param DBDataObject $item |
| 185 | + * @param ORMRow $item |
186 | 186 | * @param boolean $collapsed |
187 | 187 | * @param array $summaryData |
188 | 188 | * |
189 | 189 | * @return string |
190 | 190 | */ |
191 | | - protected function getSummary( DBDataObject $item, $collapsed = false, array $summaryData = null ) { |
| 191 | + protected function getSummary( ORMRow $item, $collapsed = false, array $summaryData = null ) { |
192 | 192 | $html = ''; |
193 | 193 | |
194 | 194 | $class = 'wikitable ep-summary mw-collapsible'; |
— | — | @@ -231,11 +231,11 @@ |
232 | 232 | * |
233 | 233 | * @since 0.1 |
234 | 234 | * |
235 | | - * @param DBDataObject $item |
| 235 | + * @param ORMRow $item |
236 | 236 | * |
237 | 237 | * @return array |
238 | 238 | */ |
239 | | - protected function getSummaryData( DBDataObject $item ) { |
| 239 | + protected function getSummaryData( ORMRow $item ) { |
240 | 240 | return array(); |
241 | 241 | } |
242 | 242 | |
Index: trunk/extensions/EducationProgram/actions/EditCourseAction.php |
— | — | @@ -121,7 +121,7 @@ |
122 | 122 | protected function getFormFields() { |
123 | 123 | $fields = parent::getFormFields(); |
124 | 124 | |
125 | | - $orgOptions = EPOrgs::singleton()->getOrgOptions(); |
| 125 | + $orgOptions = EPOrgs::singleton()->selectFields( array( 'name', 'id' ) ); |
126 | 126 | |
127 | 127 | $fields['name'] = array ( |
128 | 128 | 'type' => 'text', |
Index: trunk/extensions/EducationProgram/actions/EPEditAction.php |
— | — | @@ -43,7 +43,7 @@ |
44 | 44 | * |
45 | 45 | * @param Page $page |
46 | 46 | * @param IContextSource $context |
47 | | - * @param DBTable $table |
| 47 | + * @param ORMTable $table |
48 | 48 | */ |
49 | 49 | protected function __construct( Page $page, IContextSource $context = null, EPPageTable $table ) { |
50 | 50 | $this->table = $table; |
— | — | @@ -432,11 +432,11 @@ |
433 | 433 | *$title = SpecialPage::getTitleFor( $this->itemPage, $this->subPage )->getLocalURL(); |
434 | 434 | * @since 0.1 |
435 | 435 | * |
436 | | - * @param DBDataObject $item |
| 436 | + * @param ORMRow $item |
437 | 437 | * @param string $name |
438 | 438 | * @param string $value This is a string, since it comes from request data, but might be a number or other type. |
439 | 439 | */ |
440 | | - protected function handleUnknownField( DBDataObject $item, $name, $value ) { |
| 440 | + protected function handleUnknownField( ORMRow $item, $name, $value ) { |
441 | 441 | // Override to use. |
442 | 442 | } |
443 | 443 | |
Index: trunk/extensions/EducationProgram/actions/ViewOrgAction.php |
— | — | @@ -51,7 +51,7 @@ |
52 | 52 | * @see EPViewAction::getPageHTML() |
53 | 53 | * @return string |
54 | 54 | */ |
55 | | - public function getPageHTML( DBDataObject $org ) { |
| 55 | + public function getPageHTML( ORMRow $org ) { |
56 | 56 | $html = parent::getPageHTML( $org ); |
57 | 57 | |
58 | 58 | $html .= Html::element( 'h2', array(), wfMsg( 'ep-institution-courses' ) ); |
— | — | @@ -75,7 +75,7 @@ |
76 | 76 | * |
77 | 77 | * @return array |
78 | 78 | */ |
79 | | - protected function getSummaryData( DBDataObject $org ) { |
| 79 | + protected function getSummaryData( ORMRow $org ) { |
80 | 80 | $stats = array(); |
81 | 81 | |
82 | 82 | $stats['name'] = $org->getField( 'name' ); |
Index: trunk/extensions/EducationProgram/actions/EPUndeleteAction.php |
— | — | @@ -50,7 +50,7 @@ |
51 | 51 | if ( $object === false ) { |
52 | 52 | $revision = EPRevisions::singleton()->getLatestRevision( array( |
53 | 53 | 'object_identifier' => $this->getTitle()->getText(), |
54 | | - 'type' => $this->page->getTable()->getDataObjectClass(), |
| 54 | + 'type' => $this->page->getTable()->getRowClass(), |
55 | 55 | ) ); |
56 | 56 | |
57 | 57 | if ( $revision === false ) { |
Index: trunk/extensions/EducationProgram/specials/SpecialEPPage.php |
— | — | @@ -145,11 +145,11 @@ |
146 | 146 | * |
147 | 147 | * @since 0.1 |
148 | 148 | * |
149 | | - * @param DBDataObject $item |
| 149 | + * @param ORMRow $item |
150 | 150 | * @param boolean $collapsed |
151 | 151 | * @param array $summaryData |
152 | 152 | */ |
153 | | - protected function displaySummary( DBDataObject $item, $collapsed = false, array $summaryData = null ) { |
| 153 | + protected function displaySummary( ORMRow $item, $collapsed = false, array $summaryData = null ) { |
154 | 154 | $this->getOutput()->addHTML( $item, $collapsed, $summaryData ); |
155 | 155 | } |
156 | 156 | |
— | — | @@ -158,13 +158,13 @@ |
159 | 159 | * |
160 | 160 | * @since 0.1 |
161 | 161 | * |
162 | | - * @param DBDataObject $item |
| 162 | + * @param ORMRow $item |
163 | 163 | * @param boolean $collapsed |
164 | 164 | * @param array $summaryData |
165 | 165 | * |
166 | 166 | * @return string |
167 | 167 | */ |
168 | | - protected function getSummary( DBDataObject $item, $collapsed = false, array $summaryData = null ) { |
| 168 | + protected function getSummary( ORMRow $item, $collapsed = false, array $summaryData = null ) { |
169 | 169 | $html = ''; |
170 | 170 | |
171 | 171 | $class = 'wikitable ep-summary mw-collapsible'; |
— | — | @@ -208,11 +208,11 @@ |
209 | 209 | * |
210 | 210 | * @since 0.1 |
211 | 211 | * |
212 | | - * @param DBDataObject $item |
| 212 | + * @param ORMRow $item |
213 | 213 | * |
214 | 214 | * @return array |
215 | 215 | */ |
216 | | - protected function getSummaryData( DBDataObject $item ) { |
| 216 | + protected function getSummaryData( ORMRow $item ) { |
217 | 217 | return array(); |
218 | 218 | } |
219 | 219 | |
Index: trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php |
— | — | @@ -260,7 +260,7 @@ |
261 | 261 | $orgs = array_unique( $orgs ); |
262 | 262 | |
263 | 263 | $term = array( |
264 | | - 'courses' => count( $courses ), |
| 264 | + 'courses' => $courses->count(), |
265 | 265 | 'students' => count( $students ), |
266 | 266 | 'instructors' => count( $instructors ), |
267 | 267 | 'oas' => count( $oas ), |
Index: trunk/extensions/EducationProgram/specials/SpecialStudent.php |
— | — | @@ -141,7 +141,7 @@ |
142 | 142 | * |
143 | 143 | * @return array |
144 | 144 | */ |
145 | | - protected function getSummaryData( DBDataObject $student ) { |
| 145 | + protected function getSummaryData( ORMRow $student ) { |
146 | 146 | $stats = array(); |
147 | 147 | |
148 | 148 | $id = $student->getUser()->getId(); |
Index: trunk/extensions/EducationProgram/includes/EPOrgPager.php |
— | — | @@ -136,7 +136,7 @@ |
137 | 137 | * (non-PHPdoc) |
138 | 138 | * @see EPPager::getControlLinks() |
139 | 139 | */ |
140 | | - protected function getControlLinks( DBDataObject $item ) { |
| 140 | + protected function getControlLinks( ORMRow $item ) { |
141 | 141 | $links = parent::getControlLinks( $item ); |
142 | 142 | |
143 | 143 | $links[] = $item->getLink( 'view', wfMsgHtml( 'view' ) ); |
Index: trunk/extensions/EducationProgram/includes/EPArticles.php |
— | — | @@ -11,21 +11,21 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class EPArticles extends DBTable { |
| 15 | +class EPArticles extends ORMTable { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_articles'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPArticle'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
— | — | @@ -63,7 +63,7 @@ |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * (non-PHPdoc) |
67 | | - * @see DBTable::getDefaults() |
| 67 | + * @see ORMTable::getDefaults() |
68 | 68 | * @since 0.1 |
69 | 69 | * @return array |
70 | 70 | */ |
Index: trunk/extensions/EducationProgram/includes/EPOrgs.php |
— | — | @@ -15,17 +15,17 @@ |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_orgs'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPOrg'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
— | — | @@ -69,7 +69,7 @@ |
70 | 70 | |
71 | 71 | /** |
72 | 72 | * (non-PHPdoc) |
73 | | - * @see DBTable::getDefaults() |
| 73 | + * @see ORMTable::getDefaults() |
74 | 74 | * @since 0.1 |
75 | 75 | * @return array |
76 | 76 | */ |
— | — | @@ -95,14 +95,14 @@ |
96 | 96 | */ |
97 | 97 | public function getRevertableFields() { |
98 | 98 | return array_diff( |
99 | | - array_keys( $this->getFieldTypes() ), |
| 99 | + array_keys( $this->getFields() ), |
100 | 100 | array_merge( array( 'id', $this->getSummaryFields() ) ) |
101 | 101 | ); |
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
105 | 105 | * (non-PHPdoc) |
106 | | - * @see DBTable::getSummaryFields() |
| 106 | + * @see ORMTable::getSummaryFields() |
107 | 107 | * @since 0.1 |
108 | 108 | * @return array |
109 | 109 | */ |
— | — | @@ -119,29 +119,6 @@ |
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
123 | | - * Returns a list of orgs in an array that can be fed to select inputs. |
124 | | - * |
125 | | - * @since 0.1 |
126 | | - * |
127 | | - * @param array|null $orgs |
128 | | - * |
129 | | - * @return array |
130 | | - */ |
131 | | - public function getOrgOptions( array /* EPOrg */ $orgs = null ) { |
132 | | - $options = array(); |
133 | | - |
134 | | - if ( is_null( $orgs ) ) { |
135 | | - $orgs = $this->select( array( 'name', 'id' ) ); |
136 | | - } |
137 | | - |
138 | | - foreach ( $orgs as /* EPOrg */ $org ) { |
139 | | - $options[$org->getField( 'name' )] = $org->getId(); |
140 | | - } |
141 | | - |
142 | | - return $options; |
143 | | - } |
144 | | - |
145 | | - /** |
146 | 123 | * (non-PHPdoc) |
147 | 124 | * @see EPPageObject::getIdentifierField() |
148 | 125 | */ |
Index: trunk/extensions/EducationProgram/includes/EPPageTable.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Abstract base class DBDataObjects that have associated page views. |
| 5 | + * Abstract base class ORMRows that have associated page views. |
6 | 6 | * |
7 | 7 | * @since 0.1 |
8 | 8 | * |
— | — | @@ -11,7 +11,7 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -abstract class EPPageTable extends DBTable { |
| 15 | +abstract class EPPageTable extends ORMTable { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Returns the field use to identify this object, ie the part used as page title. |
Index: trunk/extensions/EducationProgram/includes/EPRoleObject.php |
— | — | @@ -11,7 +11,7 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -abstract class EPRoleObject extends DBDataObject implements EPIRole { |
| 15 | +abstract class EPRoleObject extends ORMRow implements EPIRole { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Field for caching the linked user. |
— | — | @@ -294,7 +294,7 @@ |
295 | 295 | } |
296 | 296 | |
297 | 297 | /** |
298 | | - * @see DBDataObject::getUpdateConditions() |
| 298 | + * @see ORMRow::getUpdateConditions() |
299 | 299 | * |
300 | 300 | * Always adding the user ID to the list of consitions, |
301 | 301 | * even when not loaded yet (a new query will be done), |
Index: trunk/extensions/EducationProgram/includes/EPInstructors.php |
— | — | @@ -11,21 +11,21 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class EPInstructors extends DBTable { |
| 15 | +class EPInstructors extends ORMTable { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_instructors'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPInstructor'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
Index: trunk/extensions/EducationProgram/includes/EPPageObject.php |
— | — | @@ -56,7 +56,7 @@ |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * (non-PHPdoc) |
60 | | - * @see DBDataObject::save() |
| 60 | + * @see ORMRow::save() |
61 | 61 | */ |
62 | 62 | public function save( $functionName = null ) { |
63 | 63 | if ( $this->hasField( $this->table->getIdentifierField() ) && is_null( $this->getTitle() ) ) { |
— | — | @@ -87,7 +87,7 @@ |
88 | 88 | |
89 | 89 | /** |
90 | 90 | * (non-PHPdoc) |
91 | | - * @see DBDataObject::setField() |
| 91 | + * @see ORMRow::setField() |
92 | 92 | */ |
93 | 93 | public function setField( $name, $value ) { |
94 | 94 | if ( $name === $this->table->getIdentifierField() ) { |
Index: trunk/extensions/EducationProgram/includes/EPCAs.php |
— | — | @@ -11,21 +11,21 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class EPCAs extends DBTable { |
| 15 | +class EPCAs extends ORMTable { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_cas'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPCA'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
— | — | @@ -63,7 +63,7 @@ |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * (non-PHPdoc) |
67 | | - * @see DBTable::getDefaults() |
| 67 | + * @see ORMTable::getDefaults() |
68 | 68 | * @since 0.1 |
69 | 69 | * @return array |
70 | 70 | */ |
Index: trunk/extensions/EducationProgram/includes/EPStudents.php |
— | — | @@ -11,21 +11,21 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class EPStudents extends DBTable { |
| 15 | +class EPStudents extends ORMTable { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_students'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPStudent'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
— | — | @@ -65,7 +65,7 @@ |
66 | 66 | |
67 | 67 | /** |
68 | 68 | * (non-PHPdoc) |
69 | | - * @see DBTable::getSummaryFields() |
| 69 | + * @see ORMTable::getSummaryFields() |
70 | 70 | * @since 0.1 |
71 | 71 | * @return array |
72 | 72 | */ |
Index: trunk/extensions/EducationProgram/includes/EPRevisionPager.php |
— | — | @@ -162,10 +162,10 @@ |
163 | 163 | function getQueryInfo() { |
164 | 164 | $table = EPRevisions::singleton(); |
165 | 165 | return array( |
166 | | - 'tables' => $table->getDBTable(), |
| 166 | + 'tables' => $table->getName(), |
167 | 167 | 'fields' => $table->getPrefixedFields( $table->getFieldNames() ), |
168 | 168 | 'conds' => $table->getPrefixedValues( $this->conds ), |
169 | | - 'options' => array( 'USE INDEX' => array( $table->getDBTable() => $table->getPrefixedField( 'time' ) ) ), |
| 169 | + 'options' => array( 'USE INDEX' => array( $table->getName() => $table->getPrefixedField( 'time' ) ) ), |
170 | 170 | ); |
171 | 171 | } |
172 | 172 | |
Index: trunk/extensions/EducationProgram/includes/EPOAPager.php |
— | — | @@ -18,9 +18,9 @@ |
19 | 19 | * |
20 | 20 | * @param IContextSource $context |
21 | 21 | * @param array $conds |
22 | | - * @param DBTable|null $table |
| 22 | + * @param ORMTable|null $table |
23 | 23 | */ |
24 | | - public function __construct( IContextSource $context, array $conds = array(), DBTable $table = null ) { |
| 24 | + public function __construct( IContextSource $context, array $conds = array(), ORMTable $table = null ) { |
25 | 25 | $this->mDefaultDirection = true; |
26 | 26 | |
27 | 27 | $conds = array_merge( |
Index: trunk/extensions/EducationProgram/includes/EPCoursePager.php |
— | — | @@ -155,7 +155,7 @@ |
156 | 156 | 'type' => 'select', |
157 | 157 | 'options' => array_merge( |
158 | 158 | array( '' => '' ), |
159 | | - $orgs->getOrgOptions( $orgs->select( array( 'name', 'id' ) ) ) |
| 159 | + $orgs->selectFields( array( 'name', 'id' ) ) |
160 | 160 | ), |
161 | 161 | 'value' => '', |
162 | 162 | 'datatype' => 'int', |
— | — | @@ -195,7 +195,7 @@ |
196 | 196 | * (non-PHPdoc) |
197 | 197 | * @see EPPager::getControlLinks() |
198 | 198 | */ |
199 | | - protected function getControlLinks( DBDataObject $item ) { |
| 199 | + protected function getControlLinks( ORMRow $item ) { |
200 | 200 | $links = parent::getControlLinks( $item ); |
201 | 201 | |
202 | 202 | $links[] = $item->getLink( 'view', wfMsgHtml( 'view' ) ); |
Index: trunk/extensions/EducationProgram/includes/EPRevisions.php |
— | — | @@ -11,21 +11,21 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class EPRevisions extends DBTable { |
| 15 | +class EPRevisions extends ORMTable { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_revisions'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPRevision'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
— | — | @@ -72,7 +72,7 @@ |
73 | 73 | * |
74 | 74 | * @since 0.1 |
75 | 75 | * |
76 | | - * @param DBDataObject $object |
| 76 | + * @param ORMRow $object |
77 | 77 | * @param EPRevisionAction $revAction |
78 | 78 | * |
79 | 79 | * @return EPRevision |
Index: trunk/extensions/EducationProgram/includes/EPOAs.php |
— | — | @@ -11,21 +11,21 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class EPOAs extends DBTable { |
| 15 | +class EPOAs extends ORMTable { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_oas'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPOA'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
— | — | @@ -63,7 +63,7 @@ |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * (non-PHPdoc) |
67 | | - * @see DBTable::getDefaults() |
| 67 | + * @see ORMTable::getDefaults() |
68 | 68 | * @since 0.1 |
69 | 69 | * @return array |
70 | 70 | */ |
Index: trunk/extensions/EducationProgram/includes/EPCourses.php |
— | — | @@ -15,17 +15,17 @@ |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * (non-PHPdoc) |
19 | | - * @see DBTable::getDBTable() |
| 19 | + * @see ORMTable::getName() |
20 | 20 | * @since 0.1 |
21 | 21 | * @return string |
22 | 22 | */ |
23 | | - public function getDBTable() { |
| 23 | + public function getName() { |
24 | 24 | return 'ep_courses'; |
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * (non-PHPdoc) |
29 | | - * @see DBTable::getFieldPrefix() |
| 29 | + * @see ORMTable::getFieldPrefix() |
30 | 30 | * @since 0.1 |
31 | 31 | * @return string |
32 | 32 | */ |
— | — | @@ -35,21 +35,21 @@ |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * (non-PHPdoc) |
39 | | - * @see DBTable::getDataObjectClass() |
| 39 | + * @see ORMTable::getRowClass() |
40 | 40 | * @since 0.1 |
41 | 41 | * @return string |
42 | 42 | */ |
43 | | - public function getDataObjectClass() { |
| 43 | + public function getRowClass() { |
44 | 44 | return 'EPCourse'; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * (non-PHPdoc) |
49 | | - * @see DBTable::getFieldTypes() |
| 49 | + * @see ORMTable::getFields() |
50 | 50 | * @since 0.1 |
51 | 51 | * @return array |
52 | 52 | */ |
53 | | - public function getFieldTypes() { |
| 53 | + public function getFields() { |
54 | 54 | return array( |
55 | 55 | 'id' => 'id', |
56 | 56 | |
— | — | @@ -78,7 +78,7 @@ |
79 | 79 | |
80 | 80 | /** |
81 | 81 | * (non-PHPdoc) |
82 | | - * @see DBTable::getDefaults() |
| 82 | + * @see ORMTable::getDefaults() |
83 | 83 | * @since 0.1 |
84 | 84 | * @return array |
85 | 85 | */ |
— | — | @@ -108,7 +108,7 @@ |
109 | 109 | |
110 | 110 | /** |
111 | 111 | * (non-PHPdoc) |
112 | | - * @see DBTable::getSummaryFields() |
| 112 | + * @see ORMTable::getSummaryFields() |
113 | 113 | * @since 0.1 |
114 | 114 | * @return array |
115 | 115 | */ |
Index: trunk/extensions/EducationProgram/includes/EPArticle.php |
— | — | @@ -12,7 +12,7 @@ |
13 | 13 | * @licence GNU GPL v3 or later |
14 | 14 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
15 | 15 | */ |
16 | | -class EPArticle extends DBDataObject { |
| 16 | +class EPArticle extends ORMRow { |
17 | 17 | |
18 | 18 | /** |
19 | 19 | * Cached user object for this article. |
Index: trunk/extensions/EducationProgram/includes/EPStudent.php |
— | — | @@ -50,7 +50,7 @@ |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * (non-PHPdoc) |
54 | | - * @see DBDataObject::loadSummaryFields() |
| 54 | + * @see ORMRow::loadSummaryFields() |
55 | 55 | */ |
56 | 56 | public function loadSummaryFields( $summaryFields = null ) { |
57 | 57 | if ( is_null( $summaryFields ) ) { |
Index: trunk/extensions/EducationProgram/includes/EPOrg.php |
— | — | @@ -23,7 +23,7 @@ |
24 | 24 | |
25 | 25 | /** |
26 | 26 | * (non-PHPdoc) |
27 | | - * @see DBDataObject::loadSummaryFields() |
| 27 | + * @see ORMRow::loadSummaryFields() |
28 | 28 | */ |
29 | 29 | public function loadSummaryFields( $summaryFields = null ) { |
30 | 30 | if ( is_null( $summaryFields ) ) { |
— | — | @@ -96,7 +96,7 @@ |
97 | 97 | |
98 | 98 | /** |
99 | 99 | * (non-PHPdoc) |
100 | | - * @see DBDataObject::save() |
| 100 | + * @see ORMRow::save() |
101 | 101 | */ |
102 | 102 | public function save( $functionName = null ) { |
103 | 103 | if ( $this->hasField( 'name' ) ) { |
— | — | @@ -225,7 +225,7 @@ |
226 | 226 | */ |
227 | 227 | public function getCourses( array $fields = null ) { |
228 | 228 | if ( $this->courses === false ) { |
229 | | - $courses = EPCourses::singleton()->select( $fields, array( 'org_id' => $this->getId() ) ); |
| 229 | + $courses = EPCourses::singleton()->selectObjects( $fields, array( 'org_id' => $this->getId() ) ); |
230 | 230 | |
231 | 231 | if ( is_null( $fields ) ) { |
232 | 232 | $this->courses = $courses; |
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Abstract base class for DBDataObjects with revision history and logging support. |
| 5 | + * Abstract base class for ORMRows with revision history and logging support. |
6 | 6 | * |
7 | 7 | * @since 0.1 |
8 | 8 | * |
— | — | @@ -11,7 +11,7 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -abstract class EPRevisionedObject extends DBDataObject { |
| 15 | +abstract class EPRevisionedObject extends ORMRow { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * If the object should log changes. |
— | — | @@ -132,7 +132,7 @@ |
133 | 133 | |
134 | 134 | /** |
135 | 135 | * (non-PHPdoc) |
136 | | - * @see DBDataObject::saveExisting() |
| 136 | + * @see ORMRow::saveExisting() |
137 | 137 | */ |
138 | 138 | protected function saveExisting( $functionName = null ) { |
139 | 139 | if ( !$this->inSummaryMode ) { |
— | — | @@ -173,7 +173,7 @@ |
174 | 174 | |
175 | 175 | /** |
176 | 176 | * (non-PHPdoc) |
177 | | - * @see DBDataObject::insert() |
| 177 | + * @see ORMRow::insert() |
178 | 178 | */ |
179 | 179 | protected function insert( $functionName = null, array $options = null ) { |
180 | 180 | $result = parent::insert( $functionName, $options ); |
— | — | @@ -188,7 +188,7 @@ |
189 | 189 | |
190 | 190 | /** |
191 | 191 | * Do logging and revision storage after a removal. |
192 | | - * @see DBDataObject::onRemoved() |
| 192 | + * @see ORMRow::onRemoved() |
193 | 193 | * |
194 | 194 | * @since 0.1 |
195 | 195 | */ |
— | — | @@ -238,7 +238,7 @@ |
239 | 239 | |
240 | 240 | /** |
241 | 241 | * (non-PHPdoc) |
242 | | - * @see DBDataObject::getBeforeRemoveFields() |
| 242 | + * @see ORMRow::getBeforeRemoveFields() |
243 | 243 | */ |
244 | 244 | protected function getBeforeRemoveFields() { |
245 | 245 | return null; |
— | — | @@ -260,7 +260,7 @@ |
261 | 261 | array( 'LIMIT' => 1 ) |
262 | 262 | ); |
263 | 263 | |
264 | | - return empty( $objects ) ? false : $objects[0]; |
| 264 | + return $objects->isEmpty() ? false : $objects->current(); |
265 | 265 | } |
266 | 266 | |
267 | 267 | /** |
— | — | @@ -272,7 +272,7 @@ |
273 | 273 | * @param array $conditions |
274 | 274 | * @param array $options |
275 | 275 | * |
276 | | - * @return array of EPRevision |
| 276 | + * @return ORMResult |
277 | 277 | */ |
278 | 278 | public function getRevisions( array $conditions = array(), array $options = array() ) { |
279 | 279 | return EPRevisions::singleton()->select( null, array_merge( |
Index: trunk/extensions/EducationProgram/includes/EPRevision.php |
— | — | @@ -11,7 +11,7 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class EPRevision extends DBDataObject { |
| 15 | +class EPRevision extends ORMRow { |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Cached user object for this revision. |
— | — | @@ -23,7 +23,7 @@ |
24 | 24 | |
25 | 25 | /** |
26 | 26 | * (non-PHPdoc) |
27 | | - * @see DBDataObject::getTable() |
| 27 | + * @see ORMRow::getTable() |
28 | 28 | */ |
29 | 29 | public function getTable() { |
30 | 30 | return EPRevisions::singleton(); |
Index: trunk/extensions/EducationProgram/includes/EPPager.php |
— | — | @@ -2,7 +2,7 @@ |
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Abstract class extending the TablePager with common functions |
6 | | - * for pagers listing DBDataObject deriving classes and some compatibility helpers. |
| 6 | + * for pagers listing ORMRow deriving classes and some compatibility helpers. |
7 | 7 | * |
8 | 8 | * @since 0.1 |
9 | 9 | * |
— | — | @@ -23,14 +23,14 @@ |
24 | 24 | |
25 | 25 | /** |
26 | 26 | * @since 0.1 |
27 | | - * @var DBTable |
| 27 | + * @var ORMTable |
28 | 28 | */ |
29 | 29 | protected $table; |
30 | 30 | |
31 | 31 | /** |
32 | | - * DBDataObject object constructed from $this->currentRow. |
| 32 | + * ORMRow object constructed from $this->currentRow. |
33 | 33 | * @since 0.1 |
34 | | - * @var DBDataObject |
| 34 | + * @var ORMRow |
35 | 35 | */ |
36 | 36 | protected $currentObject; |
37 | 37 | |
— | — | @@ -53,9 +53,9 @@ |
54 | 54 | * |
55 | 55 | * @param IContextSource $context |
56 | 56 | * @param array $conds |
57 | | - * @param DBTable $table |
| 57 | + * @param ORMTable $table |
58 | 58 | */ |
59 | | - public function __construct( IContextSource $context, array $conds, DBTable $table ) { |
| 59 | + public function __construct( IContextSource $context, array $conds, ORMTable $table ) { |
60 | 60 | $this->conds = $conds; |
61 | 61 | $this->table = $table; |
62 | 62 | $this->context = $context; |
— | — | @@ -227,7 +227,7 @@ |
228 | 228 | */ |
229 | 229 | function getQueryInfo() { |
230 | 230 | return array( |
231 | | - 'tables' => array( $this->table->getDBTable() ), |
| 231 | + 'tables' => array( $this->table->getName() ), |
232 | 232 | 'fields' => $this->table->getPrefixedFields( $this->table->getFieldNames() ), |
233 | 233 | 'conds' => $this->table->getPrefixedValues( $this->getConditions() ), |
234 | 234 | ); |
— | — | @@ -446,7 +446,7 @@ |
447 | 447 | * @return string |
448 | 448 | */ |
449 | 449 | protected function getMsg( $messageKey ) { |
450 | | - return wfMsg( strtolower( $this->table->getDataObjectClass() ) . 'pager-' . str_replace( '_', '-', $messageKey ) ); |
| 450 | + return wfMsg( strtolower( $this->table->getRowClass() ) . 'pager-' . str_replace( '_', '-', $messageKey ) ); |
451 | 451 | } |
452 | 452 | |
453 | 453 | /** |
— | — | @@ -475,11 +475,11 @@ |
476 | 476 | * |
477 | 477 | * @since 0.1 |
478 | 478 | * |
479 | | - * @param DBDataObject $item |
| 479 | + * @param ORMRow $item |
480 | 480 | * |
481 | 481 | * @return array |
482 | 482 | */ |
483 | | - protected function getControlLinks( DBDataObject $item ) { |
| 483 | + protected function getControlLinks( ORMRow $item ) { |
484 | 484 | return array(); |
485 | 485 | } |
486 | 486 | |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -97,7 +97,7 @@ |
98 | 98 | |
99 | 99 | /** |
100 | 100 | * (non-PHPdoc) |
101 | | - * @see DBDataObject::loadSummaryFields() |
| 101 | + * @see ORMRow::loadSummaryFields() |
102 | 102 | */ |
103 | 103 | public function loadSummaryFields( $summaryFields = null ) { |
104 | 104 | if ( is_null( $summaryFields ) ) { |
— | — | @@ -118,7 +118,7 @@ |
119 | 119 | |
120 | 120 | /** |
121 | 121 | * (non-PHPdoc) |
122 | | - * @see DBDataObject::insert() |
| 122 | + * @see ORMRow::insert() |
123 | 123 | */ |
124 | 124 | protected function insert( $functionName = null, array $options = null ) { |
125 | 125 | $success = parent::insert( $functionName, $options ); |
— | — | @@ -216,7 +216,7 @@ |
217 | 217 | |
218 | 218 | /** |
219 | 219 | * (non-PHPdoc) |
220 | | - * @see DBDataObject::save() |
| 220 | + * @see ORMRow::save() |
221 | 221 | */ |
222 | 222 | public function save( $functionName = null ) { |
223 | 223 | if ( $this->hasField( 'name' ) ) { |
— | — | @@ -325,7 +325,7 @@ |
326 | 326 | array_key_exists( 'org', $args ) ? $args['org'] : false |
327 | 327 | ); |
328 | 328 | |
329 | | - $select->addOptions( EPOrgs::singleton()->getOrgOptions() ); |
| 329 | + $select->addOptions( EPOrgs::singleton()->selectFields( array( 'name', 'id' ) ) ); |
330 | 330 | $html .= $select->getHTML(); |
331 | 331 | |
332 | 332 | $html .= ' ' . Xml::inputLabel( |
— | — | @@ -545,7 +545,7 @@ |
546 | 546 | |
547 | 547 | /** |
548 | 548 | * (non-PHPdoc) |
549 | | - * @see DBDataObject::setField() |
| 549 | + * @see ORMRow::setField() |
550 | 550 | */ |
551 | 551 | public function setField( $name, $value ) { |
552 | 552 | switch ( $name ) { |