/extensions/SemanticGlossary/SemanticGlossaryBackend.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 99 lines · 47 code · 21 blank · 31 comment · 12 complexity · 7ed68bcd6dad394738668a44f216344e MD5 · raw file

  1. <?php
  2. /**
  3. * File holding the SemanticGlossaryBackend class
  4. *
  5. * @author Stephan Gambke
  6. * @file
  7. * @ingroup SemanticGlossary
  8. */
  9. if ( !defined( 'SG_VERSION' ) ) {
  10. die( 'This file is part of the SemanticGlossary extension, it is not a valid entry point.' );
  11. }
  12. /**
  13. * The SemanticGlossaryBackend class.
  14. *
  15. * @ingroup SemanticGlossary
  16. */
  17. class SemanticGlossaryBackend extends LingoBackend {
  18. protected $mQueryResult;
  19. public function __construct( LingoMessageLog &$messages = null ) {
  20. parent::__construct( $messages );
  21. // get the store
  22. $store = smwfGetStore();
  23. // Create query
  24. $desc = new SMWSomeProperty( new SMWDIProperty( '___glt' ), new SMWThingDescription() );
  25. $desc->addPrintRequest( new SMWPrintRequest( SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___glt' ) ) );
  26. $desc->addPrintRequest( new SMWPrintRequest( SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___gld' ) ) );
  27. $desc->addPrintRequest( new SMWPrintRequest( SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___gll' ) ) );
  28. $query = new SMWQuery( $desc, false, false );
  29. $query->sort = true;
  30. $query->sortkeys['___glt'] = 'ASC';
  31. // get the query result
  32. $this->mQueryResult = $store->getQueryResult( $query );
  33. }
  34. /**
  35. * This function returns the next element. The element is an array of four
  36. * strings: Term, Definition, Link, Source. If there is no next element the
  37. * function returns null.
  38. *
  39. * @return the next element or null
  40. */
  41. public function next() {
  42. $ret = null;
  43. // find next line
  44. while ( !$ret && ( $resultline = $this->mQueryResult->getNext() ) ) {
  45. $term = $resultline[0]->getNextText( SMW_OUTPUT_HTML );
  46. $definition = $resultline[1]->getNextText( SMW_OUTPUT_HTML );
  47. $link = $resultline[2]->getNextText( SMW_OUTPUT_HTML );
  48. // FIXME: By not checking for 2nd term defined on the same page some
  49. // time could be saved. However, no message could then be generated.
  50. // Introduce a setting?
  51. $nextTerm = $resultline[0]->getNextText( SMW_OUTPUT_HTML );
  52. $nextDefinition = $resultline[1]->getNextText( SMW_OUTPUT_HTML );
  53. $nextLink = $resultline[2]->getNextText( SMW_OUTPUT_HTML );
  54. // FIXME: SMW has a bug that right after storing data this data
  55. // might be available twice. The workaround here is to compare the
  56. // first and second result and if they are identical assume that
  57. // it is because of the bug. (2nd condition in the if below)
  58. // skip if more then one term or more than one definition present
  59. if ( ( $nextTerm || $nextDefinition || $nextLink ) &&
  60. !( $nextTerm == $term && $nextDefinition == $definition && $nextLink == $link ) ) {
  61. if ( $ml = $this->getMessageLog() ) {
  62. $ml->addMessage(
  63. wfMsg( 'semanticglossary-termdefinedtwice',
  64. array($resultline[0]->getResultSubject()->getTitle()->getPrefixedText()) ),
  65. LingoMessageLog::MESSAGE_WARNING );
  66. }
  67. continue;
  68. }
  69. $ret = array(
  70. LingoElement::ELEMENT_TERM => $term,
  71. LingoElement::ELEMENT_DEFINITION => $definition,
  72. LingoElement::ELEMENT_LINK => $link,
  73. LingoElement::ELEMENT_SOURCE => $resultline[0]->getResultSubject()
  74. );
  75. }
  76. return $ret;
  77. }
  78. }