/extensions/Favorites/FavParser.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 195 lines · 116 code · 31 blank · 48 comment · 17 complexity · a8eca438f28d1d5386909fbf8fae0382 MD5 · raw file

  1. <?php
  2. class FavParser {
  3. function wfSpecialFavoritelist($argv, $parser) {
  4. global $wgUser, $wgOut, $wgLang, $wgRequest;
  5. global $wgRCShowFavoritingUsers, $wgEnotifFavoritelist, $wgShowUpdatedMarker;
  6. $output = '';
  7. $skin = $wgUser->getSkin();
  8. $specialTitle = SpecialPage::getTitleFor( 'Favoritelist' );
  9. //$wgOut->setRobotPolicy( 'noindex,nofollow' );
  10. $this->mTitle = $parser->getTitle();
  11. if ($this->mTitle->getNamespace() == NS_USER && array_key_exists('userpage', $argv) && $argv['userpage']) {
  12. $parts = explode( '/', $this->mTitle->getText() );
  13. $rootPart = $parts[0];
  14. $user = User::newFromName( $rootPart, true /* don't allow IP users*/ );
  15. //echo "Userpage: $user";
  16. $output = $this->viewFavList($user, $output, $wgRequest, $argv);
  17. $output .= $this->editlink($argv, $skin);
  18. return $output ;
  19. } else {
  20. $user = $wgUser;
  21. }
  22. # Anons don't get a favoritelist
  23. if( $wgUser->isAnon() ) {
  24. //$wgOut->setPageTitle( wfMsg( 'favoritenologin' ) );
  25. $llink = $skin->linkKnown(
  26. SpecialPage::getTitleFor( 'Userlogin' ),
  27. wfMsg( 'loginreqlink' ),
  28. array(),
  29. array( 'returnto' => $specialTitle->getPrefixedText() )
  30. );
  31. $output = wfMsgHtml( 'favoritelistanontext', $llink ) ;
  32. return $output ;
  33. }
  34. $output = $this->viewFavList($user, $output, $wgRequest, $argv);
  35. $output .= $this->editlink($argv, $skin);
  36. return $output ;
  37. }
  38. private function viewFavList ($user, $output, $request, $argv) {
  39. global $wgOut, $wgLang, $wgRequest;
  40. $output = $this->showNormalForm( $output, $user );
  41. return $output;
  42. }
  43. /**
  44. * Does the user want to display an editlink?
  45. *
  46. * @param $argv Array of values from the parser
  47. * $param $skin User skin
  48. * @return Output
  49. */
  50. private function editlink($argv, $skin) {
  51. $output='';
  52. if ( array_key_exists('editlink', $argv) && $argv['editlink']) {
  53. # Add an edit link if you want it:
  54. $output = "<div id='contentSub'><br>" .
  55. $skin->link(
  56. SpecialPage::getTitleFor( 'Favoritelist', 'edit' ),
  57. wfMsgHtml( "favoritelisttools-edit" ),
  58. array(),
  59. array(),
  60. array( 'known', 'noclasses' )
  61. ) . "</div>";
  62. }
  63. return $output;
  64. }
  65. /**
  66. * Count the number of titles on a user's favoritelist, excluding talk pages
  67. *
  68. * @param $user User
  69. * @return int
  70. */
  71. private function countFavoritelist( $user ) {
  72. $dbr = wfGetDB( DB_MASTER );
  73. $res = $dbr->select( 'favoritelist', 'COUNT(fl_user) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ );
  74. $row = $dbr->fetchObject( $res );
  75. return ceil( $row->count);
  76. }
  77. /**
  78. * Get a list of titles on a user's favoritelist, excluding talk pages,
  79. * and return as a two-dimensional array with namespace, title and
  80. * redirect status
  81. *
  82. * @param $user User
  83. * @return array
  84. */
  85. private function getFavoritelistInfo( $user ) {
  86. $titles = array();
  87. $dbr = wfGetDB( DB_MASTER );
  88. $uid = intval( $user->getId() );
  89. list( $favoritelist, $page ) = $dbr->tableNamesN( 'favoritelist', 'page' );
  90. $sql = "SELECT fl_namespace, fl_title, page_id, page_len, page_is_redirect
  91. FROM {$favoritelist} LEFT JOIN {$page} ON ( fl_namespace = page_namespace
  92. AND fl_title = page_title ) WHERE fl_user = {$uid}";
  93. $res = $dbr->query( $sql, __METHOD__ );
  94. if( $res && $dbr->numRows( $res ) > 0 ) {
  95. $cache = LinkCache::singleton();
  96. while( $row = $dbr->fetchObject( $res ) ) {
  97. $title = Title::makeTitleSafe( $row->fl_namespace, $row->fl_title );
  98. if( $title instanceof Title ) {
  99. // Update the link cache while we're at it
  100. if( $row->page_id ) {
  101. $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
  102. } else {
  103. $cache->addBadLinkObj( $title );
  104. }
  105. // Ignore non-talk
  106. if( !$title->isTalkPage() )
  107. $titles[$row->fl_namespace][$row->fl_title] = $row->page_is_redirect;
  108. }
  109. }
  110. }
  111. return $titles;
  112. }
  113. /**
  114. * Show the standard favoritelist
  115. *
  116. * @param $output OutputPage
  117. * @param $user User
  118. */
  119. private function showNormalForm( $output, $user ) {
  120. global $wgOut;
  121. $skin = $user->getSkin();
  122. if ( $this->countFavoritelist($user ) > 0 ) {
  123. $form = $this->buildRemoveList( $user, $skin );
  124. $output .= $form ;
  125. return $output;
  126. } else {
  127. $output = wfmsg('nofavoritelist');
  128. return $output;
  129. }
  130. }
  131. /**
  132. * Build part of the standard favoritelist
  133. *
  134. * @param $user User
  135. * @param $skin Skin (really, Linker)
  136. */
  137. private function buildRemoveList( $user, $skin ) {
  138. $list = "";
  139. $list .= "<ul>\n";
  140. foreach( $this->getFavoritelistInfo( $user ) as $namespace => $pages ) {
  141. foreach( $pages as $dbkey => $redirect ) {
  142. $title = Title::makeTitleSafe( $namespace, $dbkey );
  143. $list .= $this->buildRemoveLine( $title, $redirect, $skin );
  144. }
  145. }
  146. $list .= "</ul>\n";
  147. return $list;
  148. }
  149. /**
  150. * Build a single list item containing a link
  151. *
  152. * @param $title Title
  153. * @param $redirect bool
  154. * @param $skin Skin
  155. * @return string
  156. */
  157. private function buildRemoveLine( $title, $redirect, $skin ) {
  158. global $wgLang;
  159. $link = $skin->link( $title );
  160. if( $redirect )
  161. $link = '<span class="favoritelistredir">' . $link . '</span>';
  162. return "<li>" . $link . "</li>\n";
  163. }
  164. }