MediaWiki
REL1_22
|
00001 <?php 00034 class ORMRow implements IORMRow { 00035 00043 protected $fields = array( 'id' => null ); 00044 00055 protected $updateSummaries = true; 00056 00066 protected $inSummaryMode = false; 00067 00073 protected $table; 00074 00084 public function __construct( IORMTable $table = null, $fields = null, $loadDefaults = false ) { 00085 $this->table = $table; 00086 00087 if ( !is_array( $fields ) ) { 00088 $fields = array(); 00089 } 00090 00091 if ( $loadDefaults ) { 00092 $fields = array_merge( $this->table->getDefaults(), $fields ); 00093 } 00094 00095 $this->setFields( $fields ); 00096 } 00097 00110 public function loadFields( $fields = null, $override = true, $skipLoaded = false ) { 00111 if ( is_null( $this->getId() ) ) { 00112 return false; 00113 } 00114 00115 if ( is_null( $fields ) ) { 00116 $fields = array_keys( $this->table->getFields() ); 00117 } 00118 00119 if ( $skipLoaded ) { 00120 $fields = array_diff( $fields, array_keys( $this->fields ) ); 00121 } 00122 00123 if ( !empty( $fields ) ) { 00124 $result = $this->table->rawSelectRow( 00125 $this->table->getPrefixedFields( $fields ), 00126 array( $this->table->getPrefixedField( 'id' ) => $this->getId() ), 00127 array( 'LIMIT' => 1 ), 00128 __METHOD__ 00129 ); 00130 00131 if ( $result !== false ) { 00132 $this->setFields( $this->table->getFieldsFromDBResult( $result ), $override ); 00133 return true; 00134 } 00135 return false; 00136 } 00137 00138 return true; 00139 } 00140 00153 public function getField( $name, $default = null ) { 00154 if ( $this->hasField( $name ) ) { 00155 return $this->fields[$name]; 00156 } elseif ( !is_null( $default ) ) { 00157 return $default; 00158 } else { 00159 throw new MWException( 'Attempted to get not-set field ' . $name ); 00160 } 00161 } 00162 00173 public function loadAndGetField( $name ) { 00174 if ( !$this->hasField( $name ) ) { 00175 $this->loadFields( array( $name ) ); 00176 } 00177 00178 return $this->getField( $name ); 00179 } 00180 00188 public function removeField( $name ) { 00189 unset( $this->fields[$name] ); 00190 } 00191 00199 public function getId() { 00200 return $this->getField( 'id' ); 00201 } 00202 00210 public function setId( $id ) { 00211 $this->setField( 'id', $id ); 00212 } 00213 00223 public function hasField( $name ) { 00224 return array_key_exists( $name, $this->fields ); 00225 } 00226 00234 public function hasIdField() { 00235 return $this->hasField( 'id' ) 00236 && !is_null( $this->getField( 'id' ) ); 00237 } 00238 00247 protected function getWriteValues() { 00248 $values = array(); 00249 00250 foreach ( $this->table->getFields() as $name => $type ) { 00251 if ( array_key_exists( $name, $this->fields ) ) { 00252 $value = $this->fields[$name]; 00253 00254 // Skip null id fields so that the DBMS can set the default. 00255 if ( $name === 'id' && is_null ( $value ) ) { 00256 continue; 00257 } 00258 00259 switch ( $type ) { 00260 case 'array': 00261 $value = (array)$value; 00262 // fall-through! 00263 case 'blob': 00264 $value = serialize( $value ); 00265 // fall-through! 00266 } 00267 00268 $values[$this->table->getPrefixedField( $name )] = $value; 00269 } 00270 } 00271 00272 return $values; 00273 } 00274 00283 public function setFields( array $fields, $override = true ) { 00284 foreach ( $fields as $name => $value ) { 00285 if ( $override || !$this->hasField( $name ) ) { 00286 $this->setField( $name, $value ); 00287 } 00288 } 00289 } 00290 00302 public function toArray( $fields = null, $incNullId = false ) { 00303 $data = array(); 00304 $setFields = array(); 00305 00306 if ( !is_array( $fields ) ) { 00307 $setFields = $this->getSetFieldNames(); 00308 } else { 00309 foreach ( $fields as $field ) { 00310 if ( $this->hasField( $field ) ) { 00311 $setFields[] = $field; 00312 } 00313 } 00314 } 00315 00316 foreach ( $setFields as $field ) { 00317 if ( $incNullId || $field != 'id' || $this->hasIdField() ) { 00318 $data[$field] = $this->getField( $field ); 00319 } 00320 } 00321 00322 return $data; 00323 } 00324 00333 public function loadDefaults( $override = true ) { 00334 $this->setFields( $this->table->getDefaults(), $override ); 00335 } 00336 00348 public function save( $functionName = null ) { 00349 if ( $this->hasIdField() ) { 00350 return $this->table->updateRow( $this, $functionName ); 00351 } else { 00352 return $this->table->insertRow( $this, $functionName ); 00353 } 00354 } 00355 00366 protected function saveExisting( $functionName = null ) { 00367 $dbw = $this->table->getWriteDbConnection(); 00368 00369 $success = $dbw->update( 00370 $this->table->getName(), 00371 $this->getWriteValues(), 00372 $this->table->getPrefixedValues( $this->getUpdateConditions() ), 00373 is_null( $functionName ) ? __METHOD__ : $functionName 00374 ); 00375 00376 $this->table->releaseConnection( $dbw ); 00377 00378 // DatabaseBase::update does not always return true for success as documented... 00379 return $success !== false; 00380 } 00381 00390 protected function getUpdateConditions() { 00391 return array( 'id' => $this->getId() ); 00392 } 00393 00405 protected function insert( $functionName = null, array $options = null ) { 00406 $dbw = $this->table->getWriteDbConnection(); 00407 00408 $success = $dbw->insert( 00409 $this->table->getName(), 00410 $this->getWriteValues(), 00411 is_null( $functionName ) ? __METHOD__ : $functionName, 00412 $options 00413 ); 00414 00415 // DatabaseBase::insert does not always return true for success as documented... 00416 $success = $success !== false; 00417 00418 if ( $success ) { 00419 $this->setField( 'id', $dbw->insertId() ); 00420 } 00421 00422 $this->table->releaseConnection( $dbw ); 00423 00424 return $success; 00425 } 00426 00435 public function remove() { 00436 $this->beforeRemove(); 00437 00438 $success = $this->table->removeRow( $this, __METHOD__ ); 00439 00440 if ( $success ) { 00441 $this->onRemoved(); 00442 } 00443 00444 return $success; 00445 } 00446 00453 protected function beforeRemove() { 00454 $this->loadFields( $this->getBeforeRemoveFields(), false, true ); 00455 } 00456 00466 protected function getBeforeRemoveFields() { 00467 return array(); 00468 } 00469 00477 protected function onRemoved() { 00478 $this->setField( 'id', null ); 00479 } 00480 00488 public function getFields() { 00489 return $this->fields; 00490 } 00491 00499 public function getSetFieldNames() { 00500 return array_keys( $this->fields ); 00501 } 00502 00515 public function setField( $name, $value ) { 00516 $this->fields[$name] = $value; 00517 } 00518 00530 public function addToField( $field, $amount ) { 00531 return $this->table->addToField( $this->getUpdateConditions(), $field, $amount ); 00532 } 00533 00542 public function getFieldNames() { 00543 return array_keys( $this->table->getFields() ); 00544 } 00545 00554 public function loadSummaryFields( $summaryFields = null ) { 00555 00556 } 00557 00566 public function setUpdateSummaries( $update ) { 00567 $this->updateSummaries = $update; 00568 } 00569 00578 public function setSummaryMode( $summaryMode ) { 00579 $this->inSummaryMode = $summaryMode; 00580 } 00581 00590 public function getTable() { 00591 return $this->table; 00592 } 00593 00594 }