Advertisement
Repton33333

Untitled

Jan 30th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. <?php
  2.  
  3. class Editcount extends IncludableSpecialPage {
  4. /**
  5. * Constructor
  6. */
  7. public function __construct() {
  8. parent::__construct( 'Editcount' );
  9. }
  10.  
  11. /**
  12. * Show the special page
  13. *
  14. * @param string|null $par User name, optionally followed by a namespace, or null
  15. */
  16. public function execute( $par ) {
  17. global $wgContLang;
  18.  
  19. $target = isset( $par ) ? $par : $this->getRequest()->getText( 'username' );
  20.  
  21. list( $username, $namespace ) = $this->extractParamaters( $target );
  22.  
  23. $user = User::newFromName( $username );
  24. $username = is_object( $user ) ? $user->getName() : '';
  25.  
  26. $uid = $user->getId();
  27.  
  28. if ( $this->including() ) {
  29. if ( $namespace === null ) {
  30. if ( $uid != 0 ) {
  31. $out = $wgContLang->formatNum( $user->getEditCount() );
  32. } else {
  33. $out = '';
  34. }
  35. } else {
  36. $out = $wgContLang->formatNum( $this->editsInNs( $uid, $namespace ) );
  37. }
  38. $this->getOutput()->addHTML( $out );
  39. } else {
  40. if ( $uid != 0 ) {
  41. $nscount = $this->editsByNs( $uid );
  42. $total = $this->getTotal( $nscount );
  43. }
  44. $html = new EditcountHTML;
  45. $html->setContext( $this->getContext() );
  46. // @fixme don't use @
  47. $html->outputHTML( $username, $uid, @$nscount, @$total );
  48. }
  49. }
  50.  
  51. /**
  52. * Parse the username and namespace parts of the input and return them
  53. *
  54. * @access private
  55. *
  56. * @param string $par
  57. * @return array
  58. */
  59. function extractParamaters( $par ) {
  60. global $wgContLang;
  61.  
  62. // @fixme don't use @
  63. @list( $user, $namespace ) = explode( '/', $par, 2 );
  64.  
  65. // str*cmp sucks
  66. if ( isset( $namespace ) ) {
  67. $namespace = $wgContLang->getNsIndex( $namespace );
  68. }
  69.  
  70. return array( $user, $namespace );
  71. }
  72.  
  73. /**
  74. * Compute and return the total edits in all namespaces
  75. *
  76. * @access private
  77. *
  78. * @param array $nscount An associative array
  79. * @return int
  80. */
  81. function getTotal( $nscount ) {
  82. $total = 0;
  83. foreach ( array_values( $nscount ) as $i ) {
  84. $total += $i;
  85. }
  86.  
  87. return $total;
  88. }
  89.  
  90. /**
  91. * Count the number of edits of a user by namespace
  92. *
  93. * @param int $uid The user ID to check
  94. * @return array
  95. */
  96. function editsByNs( $uid ) {
  97. $nscount = array();
  98.  
  99. $dbr = wfGetDB( DB_SLAVE );
  100. $res = $dbr->select(
  101. array( 'user', 'revision', 'page' ),
  102. array( 'page_namespace', 'COUNT(*) AS count' ),
  103. array(
  104. 'user_id' => $uid,
  105. 'rev_user = user_id',
  106. 'rev_page = page_id'
  107. ),
  108. __METHOD__,
  109. array( 'GROUP BY' => 'page_namespace' )
  110. );
  111.  
  112. foreach ( $res as $row ) {
  113. $nscount[$row->page_namespace] = $row->count;
  114. }
  115.  
  116. return $nscount;
  117. }
  118.  
  119. /**
  120. * Count the number of edits of a user in a given namespace
  121. *
  122. * @param int $uid The user ID to check
  123. * @param int $ns The namespace to check
  124. * @return string
  125. */
  126. function editsInNs( $uid, $ns ) {
  127. $dbr = wfGetDB( DB_SLAVE );
  128. $res = $dbr->selectField(
  129. array( 'user', 'revision', 'page' ),
  130. array( 'COUNT(*) AS count' ),
  131. array(
  132. 'user_id' => $uid,
  133. 'page_namespace' => $ns,
  134. 'rev_user = user_id',
  135. 'rev_page = page_id'
  136. ),
  137. __METHOD__,
  138. array( 'GROUP BY' => 'page_namespace' )
  139. );
  140.  
  141. return $res;
  142. }
  143. }
  144.  
  145. class EditcountHTML extends Editcount {
  146. /**
  147. * @var array
  148. */
  149. private $nscount;
  150.  
  151. /**
  152. * @var int
  153. */
  154. private $total;
  155.  
  156. /**
  157. * Output the HTML form on Special:Editcount
  158. *
  159. * @param string $username
  160. * @param int $uid
  161. * @param array $nscount
  162. * @param int $total
  163. */
  164. function outputHTML( $username, $uid, $nscount, $total ) {
  165. $this->nscount = $nscount;
  166. $this->total = $total;
  167.  
  168. $this->setHeaders();
  169.  
  170. $action = htmlspecialchars( $this->getPageTitle()->getLocalURL() );
  171. $user = $this->msg( 'editcount_username' )->escaped();
  172. $submit = $this->msg( 'editcount_submit' )->escaped();
  173. $out = "
  174. <form id='editcount' method='post' action=\"$action\">
  175. <table>
  176. <tr>
  177. <td>$user</td>
  178. <td><input tabindex='1' type='text' size='20' name='username' value=\"" . htmlspecialchars( $username ) . "\"/></td>
  179. <td><input type='submit' name='submit' value=\"$submit\"/></td>
  180. </tr>";
  181. if ( $username != null && $uid != 0 ) {
  182. $editcounttable = $this->makeTable();
  183. $out .= "
  184. <tr>
  185. <td>&#160;</td>
  186. <td>$editcounttable</td>
  187. <td>&#160;</td>
  188. </tr>";
  189. }
  190. $out .= '
  191. </table>
  192. </form>';
  193. $this->getOutput()->addHTML( $out );
  194. }
  195.  
  196. /**
  197. * Make the editcount-by-namespaces HTML table
  198. *
  199. * @access private
  200. */
  201. function makeTable() {
  202. $lang = $this->getLanguage();
  203.  
  204. $total = $this->msg( 'editcount_total' )->escaped();
  205. $ftotal = $lang->formatNum( $this->total );
  206. $percent = $this->total > 0 ? wfPercent( $this->total / $this->total * 100 , 2 ) : wfPercent( 0 ); // @bug 4400
  207. // @fixme don't use inline styles
  208. $ret = "<table border='1' style='background-color: #fff; border: 1px #aaa solid; border-collapse: collapse;'>
  209. <tr>
  210. <th>$total</th>
  211. <th>$ftotal</th>
  212. <th>$percent</th>
  213. </tr>
  214. ";
  215.  
  216. foreach ( $this->nscount as $ns => $edits ) {
  217. $fedits = $lang->formatNum( $edits );
  218. $fns = ( $ns == NS_MAIN ) ? $this->msg( 'blanknamespace' ) : $lang->getFormattedNsText( $ns );
  219. $percent = wfPercent( $edits / $this->total * 100 );
  220. $fpercent = $lang->formatNum( $percent );
  221. $ret .= "
  222. <tr>
  223. <td>$fns</td>
  224. <td>$fedits</td>
  225. <td>$fpercent</td>
  226. </tr>
  227. ";
  228. }
  229. $ret .= '</table>
  230. ';
  231.  
  232. return $ret;
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement