PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/SemanticSocialProfile/SemanticSocialProfile_body.php

http://semantic-social-profile.googlecode.com/
PHP | 167 lines | 136 code | 18 blank | 13 comment | 8 complexity | bcb4b8d0b45e3acfac6030ca0bb421fc MD5 | raw file
  1. <?php
  2. /**
  3. * Class for the setup page
  4. */
  5. class SpecialSemanticSocialProfile extends SpecialPage {
  6. private $_user;
  7. private $_output;
  8. function __construct() {
  9. parent::__construct( 'SemanticSocialProfile', 'editinterface' );
  10. wfLoadExtensionMessages('SemanticSocialProfile');
  11. $wiki = SpecialWikiVersion::getVersion($this);
  12. $this->_output = $wiki->out();
  13. $this->_user = $wiki->user();
  14. }
  15. function execute( $par ) {
  16. global $wgRequest;
  17. //checks if the user has a right to access the page
  18. if ( !$this->userCanExecute($this->_user) ) {
  19. $this->displayRestrictionError();
  20. return;
  21. }
  22. $this->setHeaders();
  23. //FIXME everytime the user goes to the special page the button for initial installation will appear.
  24. //TODO add a flag that SSP has been installed
  25. if ($wgRequest->wasPosted()) {
  26. $hid = $wgRequest->getText('hiddenform');
  27. if($hid=='1'){
  28. //display results of the 1st step
  29. $this->_output->addWikiText("''' ".wfMsg('ssp-setupdone')." '''");
  30. //first setup the vocabulariess then properties
  31. $this->wfsetUp('vocabularies');
  32. $this->wfsetUp('properties');
  33. //prepare form to do step 2
  34. $form = '<form name="syncssp" action="" method="POST">' . "\n".
  35. '<input type = "hidden" name = "hiddenform" value = "2">'. "\n".
  36. '<input type="submit" value="'.wfMsg('ssp-syncbutton').'"/>' . "\n".
  37. '</form>';
  38. $this->_output->addWikiText( "=== ".wfMsg('ssp-syncdesc')." ===");
  39. $this->_output->addWikiText( wfMsg('ssp-syncabout'));
  40. $this->_output->addHtml($form);
  41. }
  42. if($hid=='2'){
  43. if($this->wfSynchronize())
  44. $this->_output->addWikiText( "\n '''".wfMsg('ssp-done')." '''" );
  45. $gotomain = '<form name="setupssp" action="'.Title::newMainPage()->getFullURL().'" method="POST">' . "\n".
  46. '<input type = "submit" value = "'.wfMsg('main-page').'">'. "\n".
  47. '</form>';
  48. $this->_output->addHtml($gotomain);
  49. }
  50. }
  51. else{
  52. $html = '<form name="setupssp" action="" method="POST">' . "\n".
  53. '<input type = "hidden" name = "hiddenform" value = "1">'. "\n".
  54. '<input type="submit" value="'.wfMsg('ssp-setupsspbutton').'"/>' . "\n".
  55. '</form>';
  56. $this->_output->addWikiText( "=== ".wfMsg('ssp-setupdesc')." ===");
  57. $this->_output->addWikiText( wfMsg('ssp-setupabout') );
  58. $this->_output->addHtml( $html);
  59. }
  60. }
  61. /**
  62. * This function adds mapping to RDF to the property pages
  63. */
  64. function wfsetUp($folder){
  65. $directory = dirname(__FILE__) . '/setup/' . $folder;
  66. $filenames = scandir($directory);
  67. $summary = 'Semantic Social Profile installation procedure';
  68. $text = array();
  69. foreach ($filenames as $filename) {
  70. if(is_file($directory.'/'.$filename)) {
  71. $fn = explode('#',$filename,2);
  72. $pageTitle = Title::makeTitle($fn[0],$fn[1]);
  73. $text[] = $pageTitle->getFullURL();
  74. $page = new Article($pageTitle);
  75. $page->doEdit(file_get_contents($directory.'/'.$filename), $summary);
  76. }
  77. }
  78. $this->_output->addWikiText(implode(', ', $text)."\n");
  79. return true;
  80. }
  81. function wfSynchronize(){
  82. $dbr = wfGetDB( DB_SLAVE );
  83. $res = $dbr->select(
  84. 'user',
  85. array( 'user_name' ),
  86. 'user_id > 0',
  87. __METHOD__
  88. );
  89. $text = array();
  90. foreach( $res as $row ){
  91. $nm = $row->user_name;
  92. try{
  93. $au = SSPAdmin::getProfile($nm);
  94. $au->syncWithDB();
  95. $au->syncRelationshipList();
  96. $au->save();
  97. }
  98. catch(SocProfException $e){
  99. $this->_output->addWikiText($e->__toString());
  100. return false;
  101. }
  102. $text[] = Title::makeTitle( NS_USER, $nm);
  103. }
  104. $this->_output->addWikiText("''' ".wfMsg('ssp-syncdone')." '''");
  105. $list = '[['.implode(']], [[', $text).']]';
  106. $this->_output->addWikiText($list);
  107. return true;
  108. }
  109. }
  110. class SocProfException extends Exception {
  111. private $url = 'http://www.mediawiki.org/wiki/Extension:SocialProfile';
  112. public function __toString(){
  113. $out = "''' ".wfMsg('ssp-nosp')." ''' \n $this->url";
  114. return $out;
  115. }
  116. }
  117. abstract class SpecialWikiVersion{
  118. abstract public function out();
  119. abstract public function user();
  120. public static function getVersion(SpecialPage &$sp){
  121. global $wgVersion;
  122. if ( version_compare( $wgVersion, '1.18', '<' ))
  123. return new VersionPre1_18();
  124. else return new VersionPost1_18($sp);
  125. }
  126. }
  127. class VersionPre1_18 extends SpecialWikiVersion{
  128. public function out(){
  129. global $wgOut;
  130. return $wgOut;
  131. }
  132. public function user(){
  133. global $wgUser;
  134. return $wgUser;
  135. }
  136. }
  137. //this should work but NEEDS TESTING NOT TESTED YET
  138. class VersionPost1_18 extends SpecialWikiVersion{
  139. private $spec;
  140. public function __construct(SpecialPage &$sp){
  141. $this->spec = $sp;
  142. }
  143. public function out(){
  144. return $this->spec->getOutput();
  145. }
  146. public function user(){
  147. return $this->spec->getUser();
  148. }
  149. }