r63114 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63113‎ | r63114 | r63115 >
Date:09:52, 1 March 2010
Author:nad
Status:deferred
Tags:
Comment:
getRecordArgs static function
Modified paths:
  • /trunk/extensions/RecordAdmin/RecordAdmin.php (modified) (history)
  • /trunk/extensions/RecordAdmin/RecordAdmin_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RecordAdmin/RecordAdmin_body.php
@@ -416,65 +416,6 @@
417417 }
418418
419419 /**
420 - * Return a list of title objects of a specified record type
421 - */
422 - static function getRecordsByType( $type ) {
423 - $records = array();
424 - $dbr = wfGetDB( DB_SLAVE );
425 - $tbl = $dbr->tableName( 'templatelinks' );
426 - $ty = $dbr->addQuotes( $type );
427 - $res = $dbr->select( $tbl, 'tl_from', "tl_namespace = 10 AND tl_title = $ty", __METHOD__ );
428 - while ( $row = $dbr->fetchRow( $res ) ) $records[] = Title::newFromID( $row[0] );
429 - $dbr->freeResult( $res );
430 - return $records;
431 - }
432 -
433 - /**
434 - * Get a field value from a record
435 - */
436 - static function getFieldValue( &$args ) {
437 - $result = '';
438 -
439 - # Build SQL condition from the supplied args, if any
440 - $regexp = '';
441 - foreach ( $args as $k => $v ) {
442 - if ( $k == 'type' ) $type = $v;
443 - elseif ( $k == 'record' ) $record = $v;
444 - elseif ( $k == 'field' ) $field = $v;
445 - else $regexp .= "AND old_text REGEXP('[|] *{$k} *= *{$v}[[:space:]]*[|}]')";
446 - }
447 -
448 - # If a record and field name are specified, return the field value
449 - if ( isset( $type ) && isset( $record ) && isset( $field ) ) {
450 - if ( is_object( $record ) ) $title = $record; else $title = Title::newFromText( $record );
451 - if ( is_object( $title ) ) {
452 - $article = new Article( $title );
453 - $text = $article->getContent();
454 - $braces = false;
455 - foreach ( self::examineBraces( $text ) as $brace ) if ( $brace['NAME'] == $type ) $braces = $brace;
456 - if ( $braces ) {
457 - $values = self::valuesFromText( substr( $text, $braces['OFFSET'], $braces['LENGTH'] ) );
458 - $result = isset( $values[$field] ) ? $values[$field] : '';
459 - }
460 - }
461 - }
462 -
463 - # If record is not set, find first record matching the supplied field values
464 - if ( isset( $type ) && !isset( $record ) ) {
465 - $dbr = wfGetDB( DB_SLAVE );
466 - $row = $dbr->selectRow(
467 - array( 'page', 'revision', 'text', 'templatelinks' ),
468 - 'page_id',
469 - "rev_id=page_latest AND old_id=rev_text_id AND tl_from=page_id AND tl_title='$type' $regexp",
470 - __METHOD__
471 - );
472 - if ( $row ) $result = Title::newFromId( $row->page_id )->getPrefixedText();
473 - }
474 -
475 - return $result;
476 - }
477 -
478 - /**
479420 * Compares a field value according to its operator
480421 * - $a is the field value for the current row
481422 * - $b is the expression from the recordtable query
@@ -873,6 +814,76 @@
874815 }
875816
876817 /**
 818+ * Return a list of title objects of a specified record type
 819+ */
 820+ static function getRecordsByType( $type ) {
 821+ $records = array();
 822+ $dbr = wfGetDB( DB_SLAVE );
 823+ $tbl = $dbr->tableName( 'templatelinks' );
 824+ $ty = $dbr->addQuotes( $type );
 825+ $res = $dbr->select( $tbl, 'tl_from', "tl_namespace = 10 AND tl_title = $ty", __METHOD__ );
 826+ while ( $row = $dbr->fetchRow( $res ) ) $records[] = Title::newFromID( $row[0] );
 827+ $dbr->freeResult( $res );
 828+ return $records;
 829+ }
 830+
 831+ /**
 832+ * Get a field value from a record
 833+ */
 834+ static function getFieldValue( &$args ) {
 835+ $result = '';
 836+
 837+ # Build SQL condition from the supplied args, if any
 838+ $regexp = '';
 839+ foreach ( $args as $k => $v ) {
 840+ if ( $k == 'type' ) $type = $v;
 841+ elseif ( $k == 'record' ) $record = $v;
 842+ elseif ( $k == 'field' ) $field = $v;
 843+ else $regexp .= "AND old_text REGEXP('[|] *{$k} *= *{$v}[[:space:]]*[|}]')";
 844+ }
 845+
 846+ # If a record and field name are specified, return the field value
 847+ if ( isset( $type ) && isset( $record ) && isset( $field ) ) {
 848+ $values = self::getRecordArgs( $record, $type );
 849+ $result = isset( $values[$field] ) ? $values[$field] : '';
 850+ }
 851+
 852+ # If record is not set, find first record matching the supplied field values
 853+ if ( isset( $type ) && !isset( $record ) ) {
 854+ $dbr = wfGetDB( DB_SLAVE );
 855+ $row = $dbr->selectRow(
 856+ array( 'page', 'revision', 'text', 'templatelinks' ),
 857+ 'page_id',
 858+ "rev_id=page_latest AND old_id=rev_text_id AND tl_from=page_id AND tl_title='$type' $regexp",
 859+ __METHOD__
 860+ );
 861+ if ( $row ) $result = Title::newFromId( $row->page_id )->getPrefixedText();
 862+ }
 863+
 864+ return $result;
 865+ }
 866+
 867+ /**
 868+ * Get args from a record article
 869+ * - if type not specified, first template is used
 870+ */
 871+ static function getRecordArgs( &$record, $type = false ) {
 872+ if ( is_object( $record ) ) $title = $record; else $title = Title::newFromText( $record );
 873+ if ( is_object( $title ) ) {
 874+ $article = new Article( $title );
 875+ $text = $article->getContent();
 876+ $eb = self::examineBraces( $text );
 877+ $braces = false;
 878+ if ( $type ) {
 879+ foreach ( $eb as $brace ) if ( $brace['NAME'] == $type ) $braces = $brace;
 880+ } elseif ( count( $eb ) > 0 ) $braces = $eb[0];
 881+ if ( $braces ) {
 882+ $values = self::valuesFromText( substr( $text, $braces['OFFSET'], $braces['LENGTH'] ) );
 883+ }
 884+ }
 885+ }
 886+
 887+ /**
877888 * Return array of args represented by passed template syntax
878889 */
879890 static function valuesFromText( $text ) {
Index: trunk/extensions/RecordAdmin/RecordAdmin.php
@@ -11,7 +11,7 @@
1212 * @licence GNU General Public Licence 2.0 or later
1313 */
1414
15 -define( 'RECORDADMIN_VERSION', '0.11.3, 2010-03-01' );
 15+define( 'RECORDADMIN_VERSION', '0.11.4, 2010-03-01' );
1616
1717 $wgRecordAdminUseNamespaces = false; # Whether record articles should be in a namespace of the same name as their type
1818 $wgRecordAdminCategory = 'Records'; # Category containing record types

Follow-up revisions

RevisionCommit summaryAuthorDate
r63149MFT r60652-r63114demon23:11, 1 March 2010

Status & tagging log