Original: https://www.mediawiki.org/wiki/Extension:Comments Das hier ist eine an https://znil.net angepasste Version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentFunctions.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. class CommentFunctions {
  3. /**
  4. * The following four functions are borrowed
  5. * from includes/wikia/GlobalFunctionsNY.php
  6. */
  7. static function dateDiff( $date1, $date2 ) {
  8. $dtDiff = $date1 - $date2;
  9. $totalDays = intval( $dtDiff / ( 24 * 60 * 60 ) );
  10. $totalSecs = $dtDiff - ( $totalDays * 24 * 60 * 60 );
  11. $dif['mo'] = intval( $totalDays / 30 );
  12. $dif['d'] = $totalDays;
  13. $dif['h'] = $h = intval( $totalSecs / ( 60 * 60 ) );
  14. $dif['m'] = $m = intval( ( $totalSecs - ( $h * 60 * 60 ) ) / 60 );
  15. $dif['s'] = $totalSecs - ( $h * 60 * 60 ) - ( $m * 60 );
  16. return $dif;
  17. }
  18. static function getTimeOffset( $time, $timeabrv, $timename ) {
  19. $timeStr = ''; // misza: initialize variables, DUMB FUCKS!
  20. if ( $time[$timeabrv] > 0 ) {
  21. // Give grep a chance to find the usages:
  22. // comments-time-days, comments-time-hours, comments-time-minutes, comments-time-seconds, comments-time-months
  23. $timeStr = wfMessage( "comments-time-{$timename}", $time[$timeabrv] )->parse();
  24. }
  25. if ( $timeStr ) {
  26. $timeStr .= ' ';
  27. }
  28. return $timeStr;
  29. }
  30. static function getTimeAgo( $time ) {
  31. $timeArray = self::dateDiff( time(), $time );
  32. $timeStr = '';
  33. $timeStrMo = self::getTimeOffset( $timeArray, 'mo', 'months' );
  34. $timeStrD = self::getTimeOffset( $timeArray, 'd', 'days' );
  35. $timeStrH = self::getTimeOffset( $timeArray, 'h', 'hours' );
  36. $timeStrM = self::getTimeOffset( $timeArray, 'm', 'minutes' );
  37. $timeStrS = self::getTimeOffset( $timeArray, 's', 'seconds' );
  38. if ( $timeStrMo ) {
  39. $timeStr = $timeStrMo;
  40. } else {
  41. $timeStr = $timeStrD;
  42. if ( $timeStr < 2 ) {
  43. $timeStr .= $timeStrH;
  44. $timeStr .= $timeStrM;
  45. if ( !$timeStr ) {
  46. $timeStr .= $timeStrS;
  47. }
  48. }
  49. }
  50. if ( !$timeStr ) {
  51. $timeStr = wfMessage( 'comments-time-seconds', 1 )->parse();
  52. }
  53. return $timeStr;
  54. }
  55. /**
  56. * Makes sure that link text is not too long by changing too long links to
  57. * <a href=#>http://www.abc....xyz.html</a>
  58. *
  59. * @param $matches Array
  60. * @return String shortened URL
  61. */
  62. public static function cutCommentLinkText( $matches ) {
  63. $tagOpen = $matches[1];
  64. $linkText = $matches[2];
  65. $tagClose = $matches[3];
  66. $image = preg_match( "/<img src=/i", $linkText );
  67. $isURL = ( preg_match( '%^(?:http|https|ftp)://(?:www\.)?.*$%i', $linkText ) ? true : false );
  68. if ( $isURL && !$image && strlen( $linkText ) > 30 ) {
  69. $start = substr( $linkText, 0, ( 30 / 2 ) - 3 );
  70. $end = substr( $linkText, strlen( $linkText ) - ( 30 / 2 ) + 3, ( 30 / 2 ) - 3 );
  71. $linkText = trim( $start ) . wfMessage( 'ellipsis' )->escaped() . trim( $end );
  72. }
  73. return $tagOpen . $linkText . $tagClose;
  74. }
  75. /**
  76. * Simple spam check -- checks the supplied text against MediaWiki's
  77. * built-in regex-based spam filters
  78. *
  79. * @param $text String: text to check for spam patterns
  80. * @return Boolean true if it contains spam, otherwise false
  81. */
  82. public static function isSpam( $text ) {
  83. global $wgSpamRegex, $wgSummarySpamRegex;
  84. $retVal = false;
  85. // Allow to hook other anti-spam extensions so that sites that use,
  86. // for example, AbuseFilter, Phalanx or SpamBlacklist can add additional
  87. // checks
  88. Hooks::run( 'Comments::isSpam', [ &$text, &$retVal ] );
  89. if ( $retVal ) {
  90. // Should only be true here...
  91. return $retVal;
  92. }
  93. // Run text through $wgSpamRegex (and $wgSummarySpamRegex if it has been specified)
  94. if ( $wgSpamRegex && preg_match( $wgSpamRegex, $text ) ) {
  95. return true;
  96. }
  97. if ( $wgSummarySpamRegex && is_array( $wgSummarySpamRegex ) ) {
  98. foreach ( $wgSummarySpamRegex as $spamRegex ) {
  99. if ( preg_match( $spamRegex, $text ) ) {
  100. return true;
  101. }
  102. }
  103. }
  104. return $retVal;
  105. }
  106. /**
  107. * Checks the supplied text for links
  108. *
  109. * @param $text String: text to check
  110. * @return Boolean true if it contains links, otherwise false
  111. */
  112. public static function haveLinks( $text ) {
  113. $linkPatterns = [
  114. '/(https?)|(ftp):\/\//',
  115. '/=\\s*[\'"]?\\s*mailto:/',
  116. ];
  117. foreach ( $linkPatterns as $linkPattern ) {
  118. if ( preg_match( $linkPattern, $text ) ) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. /**
  125. * Blocks comments from a user
  126. *
  127. * @param User $blocker The user who is blocking someone else's comments
  128. * @param int $userId User ID of the guy whose comments we want to block
  129. * @param mixed $userName User name of the same guy
  130. */
  131. public static function blockUser( $blocker, $userId, $userName ) {
  132. $dbw = wfGetDB( DB_MASTER );
  133. Wikimedia\suppressWarnings(); // E_STRICT bitching
  134. $date = date( 'Y-m-d H:i:s' );
  135. Wikimedia\restoreWarnings();
  136. $dbw->insert(
  137. 'Comments_block',
  138. [
  139. 'cb_user_id' => $blocker->getId(),
  140. 'cb_user_name' => $blocker->getName(),
  141. 'cb_user_id_blocked' => $userId,
  142. 'cb_user_name_blocked' => $userName,
  143. 'cb_date' => $date
  144. ],
  145. __METHOD__
  146. );
  147. }
  148. /**
  149. * Fetches the list of blocked users from the database
  150. *
  151. * @param int $userId User ID for whom we're getting the blocks(?)
  152. * @return array List of comment-blocked users
  153. */
  154. static function getBlockList( $userId ) {
  155. $blockList = [];
  156. $dbr = wfGetDB( DB_REPLICA );
  157. $res = $dbr->select(
  158. 'Comments_block',
  159. 'cb_user_name_blocked',
  160. [ 'cb_user_id' => $userId ],
  161. __METHOD__
  162. );
  163. foreach ( $res as $row ) {
  164. $blockList[] = $row->cb_user_name_blocked;
  165. }
  166. return $blockList;
  167. }
  168. static function isUserCommentBlocked( $userId, $userIdBlocked ) {
  169. $dbr = wfGetDB( DB_REPLICA );
  170. $s = $dbr->selectRow(
  171. 'Comments_block',
  172. [ 'cb_id' ],
  173. [
  174. 'cb_user_id' => $userId,
  175. 'cb_user_id_blocked' => $userIdBlocked
  176. ],
  177. __METHOD__
  178. );
  179. if ( $s !== false ) {
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. /**
  186. * Deletes a user from your personal comment-block list.
  187. *
  188. * @param int $userId Your user ID
  189. * @param int $userIdBlocked User ID of the blocked user
  190. */
  191. public static function deleteBlock( $userId, $userIdBlocked ) {
  192. $dbw = wfGetDB( DB_MASTER );
  193. $dbw->delete(
  194. 'Comments_block',
  195. [
  196. 'cb_user_id' => $userId,
  197. 'cb_user_id_blocked' => $userIdBlocked
  198. ],
  199. __METHOD__
  200. );
  201. }
  202. /**
  203. * Sort threads ascending
  204. *
  205. * @param $x
  206. * @param $y
  207. * @return int
  208. */
  209. public static function sortAsc( $x, $y ) {
  210. // return -1 - x goes above y
  211. // return 1 - x goes below y
  212. // return 0 - order irrelevant (only when x == y)
  213. if ( $x[0]->timestamp < $y[0]->timestamp ) {
  214. return -1;
  215. } else {
  216. return 1;
  217. }
  218. }
  219. /**
  220. * Sort threads descending
  221. *
  222. * @param $x
  223. * @param $y
  224. * @return int
  225. */
  226. public static function sortDesc( $x, $y ) {
  227. // return -1 - x goes above y
  228. // return 1 - x goes below y
  229. // return 0 - order irrelevant (only when x == y)
  230. if ( $x[0]->timestamp > $y[0]->timestamp ) {
  231. return -1;
  232. } else {
  233. return 1;
  234. }
  235. }
  236. /**
  237. * Sort threads by score
  238. *
  239. * @param $x
  240. * @param $y
  241. */
  242. public static function sortScore( $x, $y ) {
  243. // return -1 - x goes above y
  244. // return 1 - x goes below y
  245. // return 0 - order irrelevant (only when x == y)
  246. if ( $x[0]->currentScore > $y[0]->currentScore ) {
  247. return -1;
  248. } else {
  249. return 1;
  250. }
  251. }
  252. /**
  253. * Sort the comments purely by the time, from earliest to latest
  254. *
  255. * @param $x
  256. * @param $y
  257. * @return int
  258. */
  259. public static function sortTime( $x, $y ) {
  260. // return -1 - x goes above y
  261. // return 1 - x goes below y
  262. // return 0 - order irrelevant (only when x == y)
  263. if ( $x->timestamp == $y->timestamp ) {
  264. return 0;
  265. } elseif ( $x->timestamp < $y->timestamp ) {
  266. return -1;
  267. } else {
  268. return 1;
  269. }
  270. }
  271. }