MediaWiki  1.23.2
ORMRow.php
Go to the documentation of this file.
1 <?php
34 class ORMRow implements IORMRow {
42  protected $fields = array( 'id' => null );
43 
54  protected $updateSummaries = true;
55 
65  protected $inSummaryMode = false;
66 
72  protected $table;
73 
83  public function __construct( IORMTable $table = null, $fields = null, $loadDefaults = false ) {
84  $this->table = $table;
85 
86  if ( !is_array( $fields ) ) {
87  $fields = array();
88  }
89 
90  if ( $loadDefaults ) {
91  $fields = array_merge( $this->table->getDefaults(), $fields );
92  }
93 
94  $this->setFields( $fields );
95  }
96 
109  public function loadFields( $fields = null, $override = true, $skipLoaded = false ) {
110  if ( is_null( $this->getId() ) ) {
111  return false;
112  }
113 
114  if ( is_null( $fields ) ) {
115  $fields = array_keys( $this->table->getFields() );
116  }
117 
118  if ( $skipLoaded ) {
119  $fields = array_diff( $fields, array_keys( $this->fields ) );
120  }
121 
122  if ( !empty( $fields ) ) {
123  $result = $this->table->rawSelectRow(
124  $this->table->getPrefixedFields( $fields ),
125  array( $this->table->getPrefixedField( 'id' ) => $this->getId() ),
126  array( 'LIMIT' => 1 ),
127  __METHOD__
128  );
129 
130  if ( $result !== false ) {
131  $this->setFields( $this->table->getFieldsFromDBResult( $result ), $override );
132 
133  return true;
134  }
135 
136  return false;
137  }
138 
139  return true;
140  }
141 
154  public function getField( $name, $default = null ) {
155  if ( $this->hasField( $name ) ) {
156  return $this->fields[$name];
157  } elseif ( !is_null( $default ) ) {
158  return $default;
159  } else {
160  throw new MWException( 'Attempted to get not-set field ' . $name );
161  }
162  }
163 
174  public function loadAndGetField( $name ) {
175  if ( !$this->hasField( $name ) ) {
176  $this->loadFields( array( $name ) );
177  }
178 
179  return $this->getField( $name );
180  }
181 
189  public function removeField( $name ) {
190  unset( $this->fields[$name] );
191  }
192 
200  public function getId() {
201  return $this->getField( 'id' );
202  }
203 
211  public function setId( $id ) {
212  $this->setField( 'id', $id );
213  }
214 
224  public function hasField( $name ) {
225  return array_key_exists( $name, $this->fields );
226  }
227 
235  public function hasIdField() {
236  return $this->hasField( 'id' ) && !is_null( $this->getField( 'id' ) );
237  }
238 
247  protected function getWriteValues() {
248  $values = array();
249 
250  foreach ( $this->table->getFields() as $name => $type ) {
251  if ( array_key_exists( $name, $this->fields ) ) {
252  $value = $this->fields[$name];
253 
254  // Skip null id fields so that the DBMS can set the default.
255  if ( $name === 'id' && is_null( $value ) ) {
256  continue;
257  }
258 
259  switch ( $type ) {
260  case 'array':
261  $value = (array)$value;
262  // fall-through!
263  case 'blob':
264  $value = serialize( $value );
265  // fall-through!
266  }
267 
268  $values[$this->table->getPrefixedField( $name )] = $value;
269  }
270  }
271 
272  return $values;
273  }
274 
283  public function setFields( array $fields, $override = true ) {
284  foreach ( $fields as $name => $value ) {
285  if ( $override || !$this->hasField( $name ) ) {
286  $this->setField( $name, $value );
287  }
288  }
289  }
290 
302  public function toArray( $fields = null, $incNullId = false ) {
303  $data = array();
304  $setFields = array();
305 
306  if ( !is_array( $fields ) ) {
307  $setFields = $this->getSetFieldNames();
308  } else {
309  foreach ( $fields as $field ) {
310  if ( $this->hasField( $field ) ) {
311  $setFields[] = $field;
312  }
313  }
314  }
315 
316  foreach ( $setFields as $field ) {
317  if ( $incNullId || $field != 'id' || $this->hasIdField() ) {
318  $data[$field] = $this->getField( $field );
319  }
320  }
321 
322  return $data;
323  }
324 
333  public function loadDefaults( $override = true ) {
334  $this->setFields( $this->table->getDefaults(), $override );
335  }
336 
348  public function save( $functionName = null ) {
349  if ( $this->hasIdField() ) {
350  return $this->table->updateRow( $this, $functionName );
351  } else {
352  return $this->table->insertRow( $this, $functionName );
353  }
354  }
355 
366  protected function saveExisting( $functionName = null ) {
367  $dbw = $this->table->getWriteDbConnection();
368 
369  $success = $dbw->update(
370  $this->table->getName(),
371  $this->getWriteValues(),
372  $this->table->getPrefixedValues( $this->getUpdateConditions() ),
373  is_null( $functionName ) ? __METHOD__ : $functionName
374  );
375 
376  $this->table->releaseConnection( $dbw );
377 
378  // DatabaseBase::update does not always return true for success as documented...
379  return $success !== false;
380  }
381 
390  protected function getUpdateConditions() {
391  return array( 'id' => $this->getId() );
392  }
393 
405  protected function insert( $functionName = null, array $options = null ) {
406  $dbw = $this->table->getWriteDbConnection();
407 
408  $success = $dbw->insert(
409  $this->table->getName(),
410  $this->getWriteValues(),
411  is_null( $functionName ) ? __METHOD__ : $functionName,
412  $options
413  );
414 
415  // DatabaseBase::insert does not always return true for success as documented...
416  $success = $success !== false;
417 
418  if ( $success ) {
419  $this->setField( 'id', $dbw->insertId() );
420  }
421 
422  $this->table->releaseConnection( $dbw );
423 
424  return $success;
425  }
426 
435  public function remove() {
436  $this->beforeRemove();
437 
438  $success = $this->table->removeRow( $this, __METHOD__ );
439 
440  if ( $success ) {
441  $this->onRemoved();
442  }
443 
444  return $success;
445  }
446 
453  protected function beforeRemove() {
454  $this->loadFields( $this->getBeforeRemoveFields(), false, true );
455  }
456 
467  protected function getBeforeRemoveFields() {
468  return array();
469  }
470 
478  protected function onRemoved() {
479  $this->setField( 'id', null );
480  }
481 
489  public function getFields() {
490  return $this->fields;
491  }
492 
500  public function getSetFieldNames() {
501  return array_keys( $this->fields );
502  }
503 
516  public function setField( $name, $value ) {
517  $this->fields[$name] = $value;
518  }
519 
531  public function addToField( $field, $amount ) {
532  return $this->table->addToField( $this->getUpdateConditions(), $field, $amount );
533  }
534 
543  public function getFieldNames() {
544  return array_keys( $this->table->getFields() );
545  }
546 
555  public function loadSummaryFields( $summaryFields = null ) {
556  }
557 
566  public function setUpdateSummaries( $update ) {
567  $this->updateSummaries = $update;
568  }
569 
578  public function setSummaryMode( $summaryMode ) {
579  $this->inSummaryMode = $summaryMode;
580  }
581 
590  public function getTable() {
591  return $this->table;
592  }
593 }
ORMRow\$table
ORMTable null $table
Definition: ORMRow.php:68
ORMRow\$fields
array $fields
The fields of the object.
Definition: ORMRow.php:41
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
ORMRow\getWriteValues
getWriteValues()
Gets the fields => values to write to the table.
Definition: ORMRow.php:243
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ORMRow\hasField
hasField( $name)
Gets if a certain field is set.
Definition: ORMRow.php:220
IORMRow
Definition: IORMRow.php:34
ORMRow\loadDefaults
loadDefaults( $override=true)
Load the default values, via getDefaults.
Definition: ORMRow.php:329
ORMTable
Definition: ORMTable.php:31
ORMRow\getTable
getTable()
Returns the table this IORMRow is a row in.
Definition: ORMRow.php:586
ORMRow\save
save( $functionName=null)
Writes the answer to the database, either updating it when it already exists, or inserting it when it...
Definition: ORMRow.php:344
IORMTable
Definition: IORMTable.php:30
ORMRow\loadSummaryFields
loadSummaryFields( $summaryFields=null)
Computes and updates the values of the summary fields.
Definition: ORMRow.php:551
ORMRow\$updateSummaries
bool $updateSummaries
If the object should update summaries of linked items when changed.
Definition: ORMRow.php:52
$success
$success
Definition: Utf8Test.php:91
ORMRow\loadFields
loadFields( $fields=null, $override=true, $skipLoaded=false)
Load the specified fields from the database.
Definition: ORMRow.php:105
MWException
MediaWiki exception.
Definition: MWException.php:26
table
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs table
Definition: deferred.txt:11
ORMRow\getUpdateConditions
getUpdateConditions()
Returns the WHERE considtions needed to identify this object so it can be updated.
Definition: ORMRow.php:386
ORMRow\onRemoved
onRemoved()
Gets called after successful removal.
Definition: ORMRow.php:474
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ORMRow\beforeRemove
beforeRemove()
Gets called before an object is removed from the database.
Definition: ORMRow.php:449
ORMRow\toArray
toArray( $fields=null, $incNullId=false)
Serializes the object to an associative array which can then easily be converted into JSON or similar...
Definition: ORMRow.php:298
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
ORMRow\getId
getId()
Returns the objects database id.
Definition: ORMRow.php:196
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
ORMRow\getField
getField( $name, $default=null)
Gets the value of a field.
Definition: ORMRow.php:150
ORMRow\setSummaryMode
setSummaryMode( $summaryMode)
Sets the value for the.
Definition: ORMRow.php:574
ORMRow\insert
insert( $functionName=null, array $options=null)
Inserts the object into the database.
Definition: ORMRow.php:401
ORMRow\removeField
removeField( $name)
Remove a field.
Definition: ORMRow.php:185
ORMRow\setFields
setFields(array $fields, $override=true)
Sets multiple fields.
Definition: ORMRow.php:279
ORMRow\addToField
addToField( $field, $amount)
Add an amount (can be negative) to the specified field (needs to be numeric).
Definition: ORMRow.php:527
ORMRow\hasIdField
hasIdField()
Gets if the id field is set.
Definition: ORMRow.php:231
ORMRow
Definition: ORMRow.php:34
ORMRow\getBeforeRemoveFields
getBeforeRemoveFields()
Before removal of an object happens,.
Definition: ORMRow.php:463
ORMRow\setField
setField( $name, $value)
Sets the value of a field.
Definition: ORMRow.php:512
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ORMRow\getFieldNames
getFieldNames()
Return the names of the fields.
Definition: ORMRow.php:539
ORMRow\loadAndGetField
loadAndGetField( $name)
Gets the value of a field but first loads it if not done so already.
Definition: ORMRow.php:170
ORMRow\__construct
__construct(IORMTable $table=null, $fields=null, $loadDefaults=false)
Constructor.
Definition: ORMRow.php:79
ORMRow\getSetFieldNames
getSetFieldNames()
Return the names of the fields.
Definition: ORMRow.php:496
ORMRow\setId
setId( $id)
Sets the objects database id.
Definition: ORMRow.php:207
ORMRow\$inSummaryMode
bool $inSummaryMode
Indicates if the object is in summary mode.
Definition: ORMRow.php:62
ORMRow\setUpdateSummaries
setUpdateSummaries( $update)
Sets the value for the.
Definition: ORMRow.php:562
ORMRow\saveExisting
saveExisting( $functionName=null)
Updates the object in the database.
Definition: ORMRow.php:362
ORMRow\getFields
getFields()
Return the names and values of the fields.
Definition: ORMRow.php:485
$type
$type
Definition: testCompression.php:46