LogEventsList.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. <?php
  2. # Copyright (C) 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
  3. # http://www.mediawiki.org/
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # http://www.gnu.org/copyleft/gpl.html
  19. class LogEventsList {
  20. const NO_ACTION_LINK = 1;
  21. private $skin;
  22. private $out;
  23. public $flags;
  24. public function __construct( $skin, $out, $flags = 0 ) {
  25. $this->skin = $skin;
  26. $this->out = $out;
  27. $this->flags = $flags;
  28. $this->preCacheMessages();
  29. }
  30. /**
  31. * As we use the same small set of messages in various methods and that
  32. * they are called often, we call them once and save them in $this->message
  33. */
  34. private function preCacheMessages() {
  35. // Precache various messages
  36. if( !isset( $this->message ) ) {
  37. $messages = array( 'revertmerge', 'protect_change', 'unblocklink', 'change-blocklink',
  38. 'revertmove', 'undeletelink', 'revdel-restore', 'rev-delundel', 'hist', 'diff',
  39. 'pipe-separator' );
  40. foreach( $messages as $msg ) {
  41. $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
  42. }
  43. }
  44. }
  45. /**
  46. * Set page title and show header for this log type
  47. * @param $type String
  48. */
  49. public function showHeader( $type ) {
  50. if( LogPage::isLogType( $type ) ) {
  51. $this->out->setPageTitle( LogPage::logName( $type ) );
  52. $this->out->addHTML( LogPage::logHeader( $type ) );
  53. }
  54. }
  55. /**
  56. * Show options for the log list
  57. * @param $type String
  58. * @param $user String
  59. * @param $page String
  60. * @param $pattern String
  61. * @param $year Integer: year
  62. * @param $month Integer: month
  63. * @param $filter: array
  64. * @param $tagFilter: array?
  65. */
  66. public function showOptions( $type = '', $user = '', $page = '', $pattern = '', $year = '',
  67. $month = '', $filter = null, $tagFilter='' )
  68. {
  69. global $wgScript, $wgMiserMode;
  70. $action = htmlspecialchars( $wgScript );
  71. $title = SpecialPage::getTitleFor( 'Log' );
  72. $special = htmlspecialchars( $title->getPrefixedDBkey() );
  73. $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
  74. $this->out->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
  75. Xml::element( 'legend', array(), wfMsg( 'log' ) ) .
  76. Xml::hidden( 'title', $special ) . "\n" .
  77. $this->getTypeMenu( $type ) . "\n" .
  78. $this->getUserInput( $user ) . "\n" .
  79. $this->getTitleInput( $page ) . "\n" .
  80. ( !$wgMiserMode ? ($this->getTitlePattern( $pattern )."\n") : "" ) .
  81. "<p>" . Xml::dateMenu( $year, $month ) . "\n" .
  82. ( $tagSelector ? Xml::tags( 'p', null, implode( '&nbsp;', $tagSelector ) ) :'' ). "\n" .
  83. ( $filter ? "</p><p>".$this->getFilterLinks( $type, $filter )."\n" : "" ) . "\n" .
  84. Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "</p>\n" .
  85. "</fieldset></form>"
  86. );
  87. }
  88. private function getFilterLinks( $logType, $filter ) {
  89. global $wgTitle, $wgLang;
  90. // show/hide links
  91. $messages = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
  92. // Option value -> message mapping
  93. $links = array();
  94. $hiddens = ''; // keep track for "go" button
  95. foreach( $filter as $type => $val ) {
  96. $hideVal = 1 - intval($val);
  97. $link = $this->skin->makeKnownLinkObj( $wgTitle, $messages[$hideVal],
  98. wfArrayToCGI( array( "hide_{$type}_log" => $hideVal ), $this->getDefaultQuery() )
  99. );
  100. $links[$type] = wfMsgHtml( "log-show-hide-{$type}", $link );
  101. $hiddens .= Xml::hidden( "hide_{$type}_log", $val ) . "\n";
  102. }
  103. // Build links
  104. return '<small>'.$wgLang->pipeList( $links ) . '</small>' . $hiddens;
  105. }
  106. private function getDefaultQuery() {
  107. if ( !isset( $this->mDefaultQuery ) ) {
  108. $this->mDefaultQuery = $_GET;
  109. unset( $this->mDefaultQuery['title'] );
  110. unset( $this->mDefaultQuery['dir'] );
  111. unset( $this->mDefaultQuery['offset'] );
  112. unset( $this->mDefaultQuery['limit'] );
  113. unset( $this->mDefaultQuery['order'] );
  114. unset( $this->mDefaultQuery['month'] );
  115. unset( $this->mDefaultQuery['year'] );
  116. }
  117. return $this->mDefaultQuery;
  118. }
  119. /**
  120. * @param $queryType String
  121. * @return String: Formatted HTML
  122. */
  123. private function getTypeMenu( $queryType ) {
  124. global $wgLogRestrictions, $wgUser;
  125. $html = "<select name='type'>\n";
  126. $validTypes = LogPage::validTypes();
  127. $typesByName = array(); // Temporary array
  128. // First pass to load the log names
  129. foreach( $validTypes as $type ) {
  130. $text = LogPage::logName( $type );
  131. $typesByName[$text] = $type;
  132. }
  133. // Second pass to sort by name
  134. ksort($typesByName);
  135. // Third pass generates sorted XHTML content
  136. foreach( $typesByName as $text => $type ) {
  137. $selected = ($type == $queryType);
  138. // Restricted types
  139. if ( isset($wgLogRestrictions[$type]) ) {
  140. if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
  141. $html .= Xml::option( $text, $type, $selected ) . "\n";
  142. }
  143. } else {
  144. $html .= Xml::option( $text, $type, $selected ) . "\n";
  145. }
  146. }
  147. $html .= '</select>';
  148. return $html;
  149. }
  150. /**
  151. * @param $user String
  152. * @return String: Formatted HTML
  153. */
  154. private function getUserInput( $user ) {
  155. return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'mw-log-user', 15, $user );
  156. }
  157. /**
  158. * @param $title String
  159. * @return String: Formatted HTML
  160. */
  161. private function getTitleInput( $title ) {
  162. return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'mw-log-page', 20, $title );
  163. }
  164. /**
  165. * @return boolean Checkbox
  166. */
  167. private function getTitlePattern( $pattern ) {
  168. return '<span style="white-space: nowrap">' .
  169. Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern ) .
  170. '</span>';
  171. }
  172. public function beginLogEventsList() {
  173. return "<ul>\n";
  174. }
  175. public function endLogEventsList() {
  176. return "</ul>\n";
  177. }
  178. /**
  179. * @param $row Row: a single row from the result set
  180. * @return String: Formatted HTML list item
  181. */
  182. public function logLine( $row ) {
  183. global $wgLang, $wgUser, $wgContLang;
  184. $title = Title::makeTitle( $row->log_namespace, $row->log_title );
  185. $classes = array( "mw-logline-{$row->log_type}" );
  186. $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->log_timestamp), true );
  187. // User links
  188. if( self::isDeleted($row,LogPage::DELETED_USER) ) {
  189. $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
  190. } else {
  191. $userLink = $this->skin->userLink( $row->log_user, $row->user_name ) .
  192. $this->skin->userToolLinks( $row->log_user, $row->user_name, true, 0, $row->user_editcount );
  193. }
  194. // Comment
  195. if( self::isDeleted($row,LogPage::DELETED_COMMENT) ) {
  196. $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
  197. } else {
  198. $comment = $wgContLang->getDirMark() . $this->skin->commentBlock( $row->log_comment );
  199. }
  200. // Extract extra parameters
  201. $paramArray = LogPage::extractParams( $row->log_params );
  202. $revert = $del = '';
  203. // Some user can hide log items and have review links
  204. if( $wgUser->isAllowed( 'deleterevision' ) ) {
  205. $del = $this->getShowHideLinks( $row ) . ' ';
  206. }
  207. // Add review links and such...
  208. if( ($this->flags & self::NO_ACTION_LINK) || ($row->log_deleted & LogPage::DELETED_ACTION) ) {
  209. // Action text is suppressed...
  210. } else if( self::typeAction($row,'move','move','move') && !empty($paramArray[0]) ) {
  211. $destTitle = Title::newFromText( $paramArray[0] );
  212. if( $destTitle ) {
  213. $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Movepage' ),
  214. $this->message['revertmove'],
  215. 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
  216. '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
  217. '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
  218. '&wpMovetalk=0' ) . ')';
  219. }
  220. // Show undelete link
  221. } else if( self::typeAction($row,array('delete','suppress'),'delete','delete') ) {
  222. $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete' ),
  223. $this->message['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
  224. // Show unblock/change block link
  225. } else if( self::typeAction($row,array('block','suppress'),array('block','reblock'),'block') ) {
  226. $revert = '(' .
  227. $this->skin->link( SpecialPage::getTitleFor( 'Ipblocklist' ),
  228. $this->message['unblocklink'],
  229. array(),
  230. array( 'action' => 'unblock', 'ip' => $row->log_title ),
  231. 'known' )
  232. . $this->message['pipe-separator'] .
  233. $this->skin->link( SpecialPage::getTitleFor( 'Blockip', $row->log_title ),
  234. $this->message['change-blocklink'],
  235. array(), array(), 'known' ) .
  236. ')';
  237. // Show change protection link
  238. } else if( self::typeAction( $row, 'protect', array( 'modify', 'protect', 'unprotect' ) ) ) {
  239. $revert .= ' (' .
  240. $this->skin->link( $title,
  241. $this->message['hist'],
  242. array(),
  243. array( 'action' => 'history', 'offset' => $row->log_timestamp ) );
  244. if( $wgUser->isAllowed( 'protect' ) ) {
  245. $revert .= $this->message['pipe-separator'] .
  246. $this->skin->link( $title,
  247. $this->message['protect_change'],
  248. array(),
  249. array( 'action' => 'protect' ),
  250. 'known' );
  251. }
  252. $revert .= ')';
  253. // Show unmerge link
  254. } else if( self::typeAction($row,'merge','merge','mergehistory') ) {
  255. $merge = SpecialPage::getTitleFor( 'Mergehistory' );
  256. $revert = '(' . $this->skin->makeKnownLinkObj( $merge, $this->message['revertmerge'],
  257. wfArrayToCGI( array('target' => $paramArray[0], 'dest' => $title->getPrefixedDBkey(),
  258. 'mergepoint' => $paramArray[1] ) ) ) . ')';
  259. // If an edit was hidden from a page give a review link to the history
  260. } else if( self::typeAction($row,array('delete','suppress'),'revision','deleterevision') ) {
  261. if( count($paramArray) == 2 ) {
  262. $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
  263. // Different revision types use different URL params...
  264. $key = $paramArray[0];
  265. // Link to each hidden object ID, $paramArray[1] is the url param
  266. $Ids = explode( ',', $paramArray[1] );
  267. $revParams = '';
  268. foreach( $Ids as $n => $id ) {
  269. $revParams .= '&' . urlencode($key) . '[]=' . urlencode($id);
  270. }
  271. $revert = array();
  272. // Diff link for single rev deletions
  273. if( $key === 'oldid' && count($Ids) == 1 ) {
  274. $token = urlencode( $wgUser->editToken( intval($Ids[0]) ) );
  275. $revert[] = $this->skin->makeKnownLinkObj( $title, $this->message['diff'],
  276. 'diff='.intval($Ids[0])."&unhide=1&token=$token" );
  277. }
  278. // View/modify link...
  279. $revert[] = $this->skin->makeKnownLinkObj( $revdel, $this->message['revdel-restore'],
  280. 'target=' . $title->getPrefixedUrl() . $revParams );
  281. $revert = '(' . implode(' | ',$revert) . ')';
  282. }
  283. // Hidden log items, give review link
  284. } else if( self::typeAction($row,array('delete','suppress'),'event','deleterevision') ) {
  285. if( count($paramArray) == 1 ) {
  286. $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
  287. $Ids = explode( ',', $paramArray[0] );
  288. // Link to each hidden object ID, $paramArray[1] is the url param
  289. $logParams = '';
  290. foreach( $Ids as $n => $id ) {
  291. $logParams .= '&logid[]=' . intval($id);
  292. }
  293. $revert = '(' . $this->skin->makeKnownLinkObj( $revdel, $this->message['revdel-restore'],
  294. 'target=' . $title->getPrefixedUrl() . $logParams ) . ')';
  295. }
  296. // Self-created users
  297. } else if( self::typeAction($row,'newusers','create2') ) {
  298. if( isset( $paramArray[0] ) ) {
  299. $revert = $this->skin->userToolLinks( $paramArray[0], $title->getDBkey(), true );
  300. } else {
  301. # Fall back to a blue contributions link
  302. $revert = $this->skin->userToolLinks( 1, $title->getDBkey() );
  303. }
  304. if( $time < '20080129000000' ) {
  305. # Suppress $comment from old entries (before 2008-01-29),
  306. # not needed and can contain incorrect links
  307. $comment = '';
  308. }
  309. // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
  310. } else {
  311. wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray,
  312. &$comment, &$revert, $row->log_timestamp ) );
  313. }
  314. // Event description
  315. if( self::isDeleted($row,LogPage::DELETED_ACTION) ) {
  316. $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
  317. } else {
  318. $action = LogPage::actionText( $row->log_type, $row->log_action, $title,
  319. $this->skin, $paramArray, true );
  320. }
  321. // Any tags...
  322. list($tagDisplay, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'logevent' );
  323. $classes = array_merge( $classes, $newClasses );
  324. if( $revert != '' ) {
  325. $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
  326. }
  327. return Xml::tags( 'li', array( "class" => implode( ' ', $classes ) ),
  328. $del . $time . ' ' . $userLink . ' ' . $action . ' ' . $comment . ' ' . $revert . " $tagDisplay" ) . "\n";
  329. }
  330. /**
  331. * @param $row Row
  332. * @return string
  333. */
  334. private function getShowHideLinks( $row ) {
  335. $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
  336. // If event was hidden from sysops
  337. if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) {
  338. $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message['rev-delundel'].')' );
  339. } else if( $row->log_type == 'suppress' ) {
  340. // No one should be hiding from the oversight log
  341. $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message['rev-delundel'].')' );
  342. } else {
  343. $target = SpecialPage::getTitleFor( 'Log', $row->log_type );
  344. $query = array( 'target' => $target->getPrefixedDBkey(),
  345. 'logid[]' => $row->log_id
  346. );
  347. $del = $this->skin->revDeleteLink( $query, self::isDeleted( $row, LogPage::DELETED_RESTRICTED ) );
  348. }
  349. return $del;
  350. }
  351. /**
  352. * @param $row Row
  353. * @param $type Mixed: string/array
  354. * @param $action Mixed: string/array
  355. * @param $right string
  356. * @return bool
  357. */
  358. public static function typeAction( $row, $type, $action, $right='' ) {
  359. $match = is_array($type) ? in_array($row->log_type,$type) : $row->log_type == $type;
  360. if( $match ) {
  361. $match = is_array($action) ?
  362. in_array($row->log_action,$action) : $row->log_action == $action;
  363. if( $match && $right ) {
  364. global $wgUser;
  365. $match = $wgUser->isAllowed( $right );
  366. }
  367. }
  368. return $match;
  369. }
  370. /**
  371. * Determine if the current user is allowed to view a particular
  372. * field of this log row, if it's marked as deleted.
  373. * @param $row Row
  374. * @param $field Integer
  375. * @return Boolean
  376. */
  377. public static function userCan( $row, $field ) {
  378. if( ( $row->log_deleted & $field ) == $field ) {
  379. global $wgUser;
  380. $permission = ( $row->log_deleted & LogPage::DELETED_RESTRICTED ) == LogPage::DELETED_RESTRICTED
  381. ? 'suppressrevision'
  382. : 'deleterevision';
  383. wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
  384. return $wgUser->isAllowed( $permission );
  385. } else {
  386. return true;
  387. }
  388. }
  389. /**
  390. * @param $row Row
  391. * @param $field Integer: one of DELETED_* bitfield constants
  392. * @return Boolean
  393. */
  394. public static function isDeleted( $row, $field ) {
  395. return ($row->log_deleted & $field) == $field;
  396. }
  397. /**
  398. * Quick function to show a short log extract
  399. * @param $out OutputPage
  400. * @param $type String
  401. * @param $page String
  402. * @param $user String
  403. * @param $lim Integer
  404. * @param $conds Array
  405. */
  406. public static function showLogExtract( $out, $type='', $page='', $user='', $lim=0, $conds=array() ) {
  407. global $wgUser;
  408. # Insert list of top 50 or so items
  409. $loglist = new LogEventsList( $wgUser->getSkin(), $out, 0 );
  410. $pager = new LogPager( $loglist, $type, $user, $page, '', $conds );
  411. if( $lim > 0 ) $pager->mLimit = $lim;
  412. $logBody = $pager->getBody();
  413. if( $logBody ) {
  414. $out->addHTML(
  415. $loglist->beginLogEventsList() .
  416. $logBody .
  417. $loglist->endLogEventsList()
  418. );
  419. } else {
  420. $out->addWikiMsg( 'logempty' );
  421. }
  422. return $pager->getNumRows();
  423. }
  424. /**
  425. * SQL clause to skip forbidden log types for this user
  426. * @param $db Database
  427. * @param $audience string, public/user
  428. * @return mixed (string or false)
  429. */
  430. public static function getExcludeClause( $db, $audience = 'public' ) {
  431. global $wgLogRestrictions, $wgUser;
  432. // Reset the array, clears extra "where" clauses when $par is used
  433. $hiddenLogs = array();
  434. // Don't show private logs to unprivileged users
  435. foreach( $wgLogRestrictions as $logType => $right ) {
  436. if( $audience == 'public' || !$wgUser->isAllowed($right) ) {
  437. $safeType = $db->strencode( $logType );
  438. $hiddenLogs[] = $safeType;
  439. }
  440. }
  441. if( count($hiddenLogs) == 1 ) {
  442. return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
  443. } elseif( $hiddenLogs ) {
  444. return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
  445. }
  446. return false;
  447. }
  448. }
  449. /**
  450. * @ingroup Pager
  451. */
  452. class LogPager extends ReverseChronologicalPager {
  453. private $type = '', $user = '', $title = '', $pattern = '';
  454. public $mLogEventsList;
  455. /**
  456. * constructor
  457. * @param $list LogEventsList
  458. * @param $type String
  459. * @param $user String
  460. * @param $title String
  461. * @param $pattern String
  462. * @param $conds Array
  463. * @param $year Integer
  464. * @param $month Integer
  465. */
  466. public function __construct( $list, $type = '', $user = '', $title = '', $pattern = '',
  467. $conds = array(), $year = false, $month = false, $tagFilter = '' )
  468. {
  469. parent::__construct();
  470. $this->mConds = $conds;
  471. $this->mLogEventsList = $list;
  472. $this->limitType( $type ); // also excludes hidden types
  473. $this->limitUser( $user );
  474. $this->limitTitle( $title, $pattern );
  475. $this->getDateCond( $year, $month );
  476. $this->mTagFilter = $tagFilter;
  477. }
  478. public function getDefaultQuery() {
  479. $query = parent::getDefaultQuery();
  480. $query['type'] = $this->type;
  481. $query['user'] = $this->user;
  482. $query['month'] = $this->mMonth;
  483. $query['year'] = $this->mYear;
  484. return $query;
  485. }
  486. public function getFilterParams() {
  487. global $wgFilterLogTypes, $wgUser, $wgRequest;
  488. $filters = array();
  489. if( $this->type ) {
  490. return $filters;
  491. }
  492. foreach( $wgFilterLogTypes as $type => $default ) {
  493. // Avoid silly filtering
  494. if( $type !== 'patrol' || $wgUser->useNPPatrol() ) {
  495. $hide = $wgRequest->getInt( "hide_{$type}_log", $default );
  496. $filters[$type] = $hide;
  497. if( $hide )
  498. $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
  499. }
  500. }
  501. return $filters;
  502. }
  503. /**
  504. * Set the log reader to return only entries of the given type.
  505. * Type restrictions enforced here
  506. * @param $type String: A log type ('upload', 'delete', etc)
  507. */
  508. private function limitType( $type ) {
  509. global $wgLogRestrictions, $wgUser;
  510. // Don't even show header for private logs; don't recognize it...
  511. if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
  512. $type = '';
  513. }
  514. // Don't show private logs to unpriviledged users.
  515. // Also, only show them upon specific request to avoid suprises.
  516. $audience = $type ? 'user' : 'public';
  517. $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience );
  518. if( $hideLogs !== false ) {
  519. $this->mConds[] = $hideLogs;
  520. }
  521. if( $type ) {
  522. $this->type = $type;
  523. $this->mConds['log_type'] = $type;
  524. }
  525. }
  526. /**
  527. * Set the log reader to return only entries by the given user.
  528. * @param $name String: (In)valid user name
  529. */
  530. private function limitUser( $name ) {
  531. if( $name == '' ) {
  532. return false;
  533. }
  534. $usertitle = Title::makeTitleSafe( NS_USER, $name );
  535. if( is_null($usertitle) ) {
  536. return false;
  537. }
  538. /* Fetch userid at first, if known, provides awesome query plan afterwards */
  539. $userid = User::idFromName( $name );
  540. if( !$userid ) {
  541. /* It should be nicer to abort query at all,
  542. but for now it won't pass anywhere behind the optimizer */
  543. $this->mConds[] = "NULL";
  544. } else {
  545. global $wgUser;
  546. $this->mConds['log_user'] = $userid;
  547. // Paranoia: avoid brute force searches (bug 17342)
  548. if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
  549. $this->mConds[] = 'log_deleted & ' . LogPage::DELETED_USER . ' = 0';
  550. }
  551. $this->user = $usertitle->getText();
  552. }
  553. }
  554. /**
  555. * Set the log reader to return only entries affecting the given page.
  556. * (For the block and rights logs, this is a user page.)
  557. * @param $page String: Title name as text
  558. * @param $pattern String
  559. */
  560. private function limitTitle( $page, $pattern ) {
  561. global $wgMiserMode, $wgUser;
  562. $title = Title::newFromText( $page );
  563. if( strlen($page) == 0 || !$title instanceof Title )
  564. return false;
  565. $this->title = $title->getPrefixedText();
  566. $ns = $title->getNamespace();
  567. # Using the (log_namespace, log_title, log_timestamp) index with a
  568. # range scan (LIKE) on the first two parts, instead of simple equality,
  569. # makes it unusable for sorting. Sorted retrieval using another index
  570. # would be possible, but then we might have to scan arbitrarily many
  571. # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
  572. # is on.
  573. #
  574. # This is not a problem with simple title matches, because then we can
  575. # use the page_time index. That should have no more than a few hundred
  576. # log entries for even the busiest pages, so it can be safely scanned
  577. # in full to satisfy an impossible condition on user or similar.
  578. if( $pattern && !$wgMiserMode ) {
  579. # use escapeLike to avoid expensive search patterns like 't%st%'
  580. $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
  581. $this->mConds['log_namespace'] = $ns;
  582. $this->mConds[] = "log_title LIKE '$safetitle%'";
  583. $this->pattern = $pattern;
  584. } else {
  585. $this->mConds['log_namespace'] = $ns;
  586. $this->mConds['log_title'] = $title->getDBkey();
  587. }
  588. // Paranoia: avoid brute force searches (bug 17342)
  589. if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
  590. $this->mConds[] = 'log_deleted & ' . LogPage::DELETED_ACTION . ' = 0';
  591. }
  592. }
  593. public function getQueryInfo() {
  594. $this->mConds[] = 'user_id = log_user';
  595. # Don't use the wrong logging index
  596. if( $this->title || $this->pattern || $this->user ) {
  597. $index = array( 'USE INDEX' => array( 'logging' => array('page_time','user_time') ) );
  598. } else if( $this->type ) {
  599. $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
  600. } else {
  601. $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
  602. }
  603. $info = array(
  604. 'tables' => array( 'logging', 'user' ),
  605. 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title', 'log_params',
  606. 'log_comment', 'log_id', 'log_deleted', 'log_timestamp', 'user_name', 'user_editcount' ),
  607. 'conds' => $this->mConds,
  608. 'options' => $index,
  609. 'join_conds' => array( 'user' => array( 'INNER JOIN', 'user_id=log_user' ) ),
  610. );
  611. ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
  612. $info['join_conds'], $info['options'], $this->mTagFilter );
  613. return $info;
  614. }
  615. function getIndexField() {
  616. return 'log_timestamp';
  617. }
  618. public function getStartBody() {
  619. wfProfileIn( __METHOD__ );
  620. # Do a link batch query
  621. if( $this->getNumRows() > 0 ) {
  622. $lb = new LinkBatch;
  623. while( $row = $this->mResult->fetchObject() ) {
  624. $lb->add( $row->log_namespace, $row->log_title );
  625. $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
  626. $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
  627. }
  628. $lb->execute();
  629. $this->mResult->seek( 0 );
  630. }
  631. wfProfileOut( __METHOD__ );
  632. return '';
  633. }
  634. public function formatRow( $row ) {
  635. return $this->mLogEventsList->logLine( $row );
  636. }
  637. public function getType() {
  638. return $this->type;
  639. }
  640. public function getUser() {
  641. return $this->user;
  642. }
  643. public function getPage() {
  644. return $this->title;
  645. }
  646. public function getPattern() {
  647. return $this->pattern;
  648. }
  649. public function getYear() {
  650. return $this->mYear;
  651. }
  652. public function getMonth() {
  653. return $this->mMonth;
  654. }
  655. public function getTagFilter() {
  656. return $this->mTagFilter;
  657. }
  658. }
  659. /**
  660. * @deprecated
  661. * @ingroup SpecialPage
  662. */
  663. class LogReader {
  664. var $pager;
  665. /**
  666. * @param $request WebRequest: for internal use use a FauxRequest object to pass arbitrary parameters.
  667. */
  668. function __construct( $request ) {
  669. global $wgUser, $wgOut;
  670. wfDeprecated(__METHOD__);
  671. # Get parameters
  672. $type = $request->getVal( 'type' );
  673. $user = $request->getText( 'user' );
  674. $title = $request->getText( 'page' );
  675. $pattern = $request->getBool( 'pattern' );
  676. $year = $request->getIntOrNull( 'year' );
  677. $month = $request->getIntOrNull( 'month' );
  678. $tagFilter = $request->getVal( 'tagfilter' );
  679. # Don't let the user get stuck with a certain date
  680. $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
  681. if( $skip ) {
  682. $year = '';
  683. $month = '';
  684. }
  685. # Use new list class to output results
  686. $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, 0 );
  687. $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern, $year, $month, $tagFilter );
  688. }
  689. /**
  690. * Is there at least one row?
  691. * @return bool
  692. */
  693. public function hasRows() {
  694. return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
  695. }
  696. }
  697. /**
  698. * @deprecated
  699. * @ingroup SpecialPage
  700. */
  701. class LogViewer {
  702. const NO_ACTION_LINK = 1;
  703. /**
  704. * LogReader object
  705. */
  706. var $reader;
  707. /**
  708. * @param &$reader LogReader: where to get our data from
  709. * @param $flags Integer: Bitwise combination of flags:
  710. * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
  711. */
  712. function __construct( &$reader, $flags = 0 ) {
  713. global $wgUser;
  714. wfDeprecated(__METHOD__);
  715. $this->reader =& $reader;
  716. $this->reader->pager->mLogEventsList->flags = $flags;
  717. # Aliases for shorter code...
  718. $this->pager =& $this->reader->pager;
  719. $this->list =& $this->reader->pager->mLogEventsList;
  720. }
  721. /**
  722. * Take over the whole output page in $wgOut with the log display.
  723. */
  724. public function show() {
  725. # Set title and add header
  726. $this->list->showHeader( $pager->getType() );
  727. # Show form options
  728. $this->list->showOptions( $this->pager->getType(), $this->pager->getUser(), $this->pager->getPage(),
  729. $this->pager->getPattern(), $this->pager->getYear(), $this->pager->getMonth() );
  730. # Insert list
  731. $logBody = $this->pager->getBody();
  732. if( $logBody ) {
  733. $wgOut->addHTML(
  734. $this->pager->getNavigationBar() .
  735. $this->list->beginLogEventsList() .
  736. $logBody .
  737. $this->list->endLogEventsList() .
  738. $this->pager->getNavigationBar()
  739. );
  740. } else {
  741. $wgOut->addWikiMsg( 'logempty' );
  742. }
  743. }
  744. /**
  745. * Output just the list of entries given by the linked LogReader,
  746. * with extraneous UI elements. Use for displaying log fragments in
  747. * another page (eg at Special:Undelete)
  748. * @param $out OutputPage: where to send output
  749. */
  750. public function showList( &$out ) {
  751. $logBody = $this->pager->getBody();
  752. if( $logBody ) {
  753. $out->addHTML(
  754. $this->list->beginLogEventsList() .
  755. $logBody .
  756. $this->list->endLogEventsList()
  757. );
  758. } else {
  759. $out->addWikiMsg( 'logempty' );
  760. }
  761. }
  762. }