PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/phase3/includes/api/ApiQueryLogEvents.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 461 lines | 368 code | 48 blank | 45 comment | 61 complexity | e4210fce373dc0c3deccd67e4e3edaea MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Oct 16, 2006
  6. *
  7. * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. if ( !defined( 'MEDIAWIKI' ) ) {
  27. // Eclipse helper - will be ignored in production
  28. require_once( 'ApiQueryBase.php' );
  29. }
  30. /**
  31. * Query action to List the log events, with optional filtering by various parameters.
  32. *
  33. * @ingroup API
  34. */
  35. class ApiQueryLogEvents extends ApiQueryBase {
  36. public function __construct( $query, $moduleName ) {
  37. parent::__construct( $query, $moduleName, 'le' );
  38. }
  39. private $fld_ids = false, $fld_title = false, $fld_type = false,
  40. $fld_action = false, $fld_user = false, $fld_userid = false,
  41. $fld_timestamp = false, $fld_comment = false, $fld_parsedcomment = false,
  42. $fld_details = false, $fld_tags = false;
  43. public function execute() {
  44. $params = $this->extractRequestParams();
  45. $db = $this->getDB();
  46. $prop = array_flip( $params['prop'] );
  47. $this->fld_ids = isset( $prop['ids'] );
  48. $this->fld_title = isset( $prop['title'] );
  49. $this->fld_type = isset( $prop['type'] );
  50. $this->fld_action = isset ( $prop['action'] );
  51. $this->fld_user = isset( $prop['user'] );
  52. $this->fld_userid = isset( $prop['userid'] );
  53. $this->fld_timestamp = isset( $prop['timestamp'] );
  54. $this->fld_comment = isset( $prop['comment'] );
  55. $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
  56. $this->fld_details = isset( $prop['details'] );
  57. $this->fld_tags = isset( $prop['tags'] );
  58. $hideLogs = LogEventsList::getExcludeClause( $db );
  59. if ( $hideLogs !== false ) {
  60. $this->addWhere( $hideLogs );
  61. }
  62. // Order is significant here
  63. $this->addTables( array( 'logging', 'user', 'page' ) );
  64. $this->addOption( 'STRAIGHT_JOIN' );
  65. $this->addJoinConds( array(
  66. 'user' => array( 'JOIN',
  67. 'user_id=log_user' ),
  68. 'page' => array( 'LEFT JOIN',
  69. array( 'log_namespace=page_namespace',
  70. 'log_title=page_title' ) ) ) );
  71. $index = array( 'logging' => 'times' ); // default, may change
  72. $this->addFields( array(
  73. 'log_type',
  74. 'log_action',
  75. 'log_timestamp',
  76. 'log_deleted',
  77. ) );
  78. $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids );
  79. $this->addFieldsIf( array( 'log_user', 'user_name' ), $this->fld_user );
  80. $this->addFieldsIf( 'user_id', $this->fld_userid );
  81. $this->addFieldsIf( array( 'log_namespace', 'log_title' ), $this->fld_title || $this->fld_parsedcomment );
  82. $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
  83. $this->addFieldsIf( 'log_params', $this->fld_details );
  84. if ( $this->fld_tags ) {
  85. $this->addTables( 'tag_summary' );
  86. $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
  87. $this->addFields( 'ts_tags' );
  88. }
  89. if ( !is_null( $params['tag'] ) ) {
  90. $this->addTables( 'change_tag' );
  91. $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
  92. $this->addWhereFld( 'ct_tag', $params['tag'] );
  93. global $wgOldChangeTagsIndex;
  94. $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
  95. }
  96. if ( !is_null( $params['action'] ) ) {
  97. list( $type, $action ) = explode( '/', $params['action'] );
  98. $this->addWhereFld( 'log_type', $type );
  99. $this->addWhereFld( 'log_action', $action );
  100. } elseif ( !is_null( $params['type'] ) ) {
  101. $this->addWhereFld( 'log_type', $params['type'] );
  102. $index['logging'] = 'type_time';
  103. }
  104. $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
  105. $limit = $params['limit'];
  106. $this->addOption( 'LIMIT', $limit + 1 );
  107. $user = $params['user'];
  108. if ( !is_null( $user ) ) {
  109. $userid = User::idFromName( $user );
  110. if ( !$userid ) {
  111. $this->dieUsage( "User name $user not found", 'param_user' );
  112. }
  113. $this->addWhereFld( 'log_user', $userid );
  114. $index['logging'] = 'user_time';
  115. }
  116. $title = $params['title'];
  117. if ( !is_null( $title ) ) {
  118. $titleObj = Title::newFromText( $title );
  119. if ( is_null( $titleObj ) ) {
  120. $this->dieUsage( "Bad title value '$title'", 'param_title' );
  121. }
  122. $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
  123. $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
  124. // Use the title index in preference to the user index if there is a conflict
  125. $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
  126. }
  127. $prefix = $params['prefix'];
  128. if ( !is_null( $prefix ) ) {
  129. global $wgMiserMode;
  130. if ( $wgMiserMode ) {
  131. $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
  132. }
  133. $title = Title::newFromText( $prefix );
  134. if ( is_null( $title ) ) {
  135. $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
  136. }
  137. $this->addWhereFld( 'log_namespace', $title->getNamespace() );
  138. $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
  139. }
  140. $this->addOption( 'USE INDEX', $index );
  141. // Paranoia: avoid brute force searches (bug 17342)
  142. if ( !is_null( $title ) ) {
  143. $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
  144. }
  145. if ( !is_null( $user ) ) {
  146. $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
  147. }
  148. $count = 0;
  149. $res = $this->select( __METHOD__ );
  150. $result = $this->getResult();
  151. foreach ( $res as $row ) {
  152. if ( ++ $count > $limit ) {
  153. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  154. $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
  155. break;
  156. }
  157. $vals = $this->extractRowInfo( $row );
  158. if ( !$vals ) {
  159. continue;
  160. }
  161. $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
  162. if ( !$fit ) {
  163. $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
  164. break;
  165. }
  166. }
  167. $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
  168. }
  169. /**
  170. * @param $result ApiResult
  171. * @param $vals array
  172. * @param $params string
  173. * @param $type string
  174. * @param $action string
  175. * @param $ts
  176. * @return array
  177. */
  178. public static function addLogParams( $result, &$vals, $params, $type, $action, $ts ) {
  179. $params = explode( "\n", $params );
  180. switch ( $type ) {
  181. case 'move':
  182. if ( isset( $params[0] ) ) {
  183. $title = Title::newFromText( $params[0] );
  184. if ( $title ) {
  185. $vals2 = array();
  186. ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
  187. $vals[$type] = $vals2;
  188. }
  189. }
  190. if ( isset( $params[1] ) && $params[1] ) {
  191. $vals[$type]['suppressedredirect'] = '';
  192. }
  193. $params = null;
  194. break;
  195. case 'patrol':
  196. $vals2 = array();
  197. list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
  198. $vals[$type] = $vals2;
  199. $params = null;
  200. break;
  201. case 'rights':
  202. $vals2 = array();
  203. list( $vals2['old'], $vals2['new'] ) = $params;
  204. $vals[$type] = $vals2;
  205. $params = null;
  206. break;
  207. case 'block':
  208. if ( $action == 'unblock' ) {
  209. break;
  210. }
  211. $vals2 = array();
  212. list( $vals2['duration'], $vals2['flags'] ) = $params;
  213. // Indefinite blocks have no expiry time
  214. if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
  215. $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
  216. strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
  217. }
  218. $vals[$type] = $vals2;
  219. $params = null;
  220. break;
  221. }
  222. if ( !is_null( $params ) ) {
  223. $result->setIndexedTagName( $params, 'param' );
  224. $vals = array_merge( $vals, $params );
  225. }
  226. return $vals;
  227. }
  228. private function extractRowInfo( $row ) {
  229. $vals = array();
  230. if ( $this->fld_ids ) {
  231. $vals['logid'] = intval( $row->log_id );
  232. $vals['pageid'] = intval( $row->page_id );
  233. }
  234. if ( $this->fld_title || $this->fld_parsedcomment ) {
  235. $title = Title::makeTitle( $row->log_namespace, $row->log_title );
  236. }
  237. if ( $this->fld_title ) {
  238. if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
  239. $vals['actionhidden'] = '';
  240. } else {
  241. ApiQueryBase::addTitleInfo( $vals, $title );
  242. }
  243. }
  244. if ( $this->fld_type || $this->fld_action ) {
  245. $vals['type'] = $row->log_type;
  246. $vals['action'] = $row->log_action;
  247. }
  248. if ( $this->fld_details && $row->log_params !== '' ) {
  249. if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
  250. $vals['actionhidden'] = '';
  251. } else {
  252. self::addLogParams(
  253. $this->getResult(),
  254. $vals,
  255. $row->log_params,
  256. $row->log_type,
  257. $row->log_action,
  258. $row->log_timestamp
  259. );
  260. }
  261. }
  262. if ( $this->fld_user || $this->fld_userid ) {
  263. if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
  264. $vals['userhidden'] = '';
  265. } else {
  266. if ( $this->fld_user ) {
  267. $vals['user'] = $row->user_name;
  268. }
  269. if ( $this->fld_userid ) {
  270. $vals['userid'] = $row->user_id;
  271. }
  272. if ( !$row->log_user ) {
  273. $vals['anon'] = '';
  274. }
  275. }
  276. }
  277. if ( $this->fld_timestamp ) {
  278. $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
  279. }
  280. if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
  281. if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
  282. $vals['commenthidden'] = '';
  283. } else {
  284. if ( $this->fld_comment ) {
  285. $vals['comment'] = $row->log_comment;
  286. }
  287. if ( $this->fld_parsedcomment ) {
  288. global $wgUser;
  289. $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment, $title );
  290. }
  291. }
  292. }
  293. if ( $this->fld_tags ) {
  294. if ( $row->ts_tags ) {
  295. $tags = explode( ',', $row->ts_tags );
  296. $this->getResult()->setIndexedTagName( $tags, 'tag' );
  297. $vals['tags'] = $tags;
  298. } else {
  299. $vals['tags'] = array();
  300. }
  301. }
  302. return $vals;
  303. }
  304. public function getCacheMode( $params ) {
  305. if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
  306. // formatComment() calls wfMsg() among other things
  307. return 'anon-public-user-private';
  308. } else {
  309. return 'public';
  310. }
  311. }
  312. public function getAllowedParams() {
  313. global $wgLogTypes, $wgLogActions;
  314. return array(
  315. 'prop' => array(
  316. ApiBase::PARAM_ISMULTI => true,
  317. ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
  318. ApiBase::PARAM_TYPE => array(
  319. 'ids',
  320. 'title',
  321. 'type',
  322. 'user',
  323. 'userid',
  324. 'timestamp',
  325. 'comment',
  326. 'parsedcomment',
  327. 'details',
  328. 'tags'
  329. )
  330. ),
  331. 'type' => array(
  332. ApiBase::PARAM_TYPE => $wgLogTypes
  333. ),
  334. 'action' => array(
  335. ApiBase::PARAM_TYPE => array_keys( $wgLogActions )
  336. ),
  337. 'start' => array(
  338. ApiBase::PARAM_TYPE => 'timestamp'
  339. ),
  340. 'end' => array(
  341. ApiBase::PARAM_TYPE => 'timestamp'
  342. ),
  343. 'dir' => array(
  344. ApiBase::PARAM_DFLT => 'older',
  345. ApiBase::PARAM_TYPE => array(
  346. 'newer',
  347. 'older'
  348. )
  349. ),
  350. 'user' => null,
  351. 'title' => null,
  352. 'prefix' => null,
  353. 'tag' => null,
  354. 'limit' => array(
  355. ApiBase::PARAM_DFLT => 10,
  356. ApiBase::PARAM_TYPE => 'limit',
  357. ApiBase::PARAM_MIN => 1,
  358. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  359. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  360. )
  361. );
  362. }
  363. public function getParamDescription() {
  364. $p = $this->getModulePrefix();
  365. return array(
  366. 'prop' => array(
  367. 'Which properties to get',
  368. ' ids - Adds the ID of the log event',
  369. ' title - Adds the title of the page for the log event',
  370. ' type - Adds the type of log event',
  371. ' user - Adds the user responsible for the log event',
  372. ' userid - Adds the user ID who was responsible for the log event',
  373. ' timestamp - Adds the timestamp for the event',
  374. ' comment - Adds the comment of the event',
  375. ' parsedcomment - Adds the parsed comment of the event',
  376. ' details - Lists addtional details about the event',
  377. ' tags - Lists tags for the event',
  378. ),
  379. 'type' => 'Filter log entries to only this type',
  380. 'action' => "Filter log actions to only this type. Overrides {$p}type",
  381. 'start' => 'The timestamp to start enumerating from',
  382. 'end' => 'The timestamp to end enumerating',
  383. 'dir' => $this->getDirectionDescription( $p ),
  384. 'user' => 'Filter entries to those made by the given user',
  385. 'title' => 'Filter entries to those related to a page',
  386. 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
  387. 'limit' => 'How many total event entries to return',
  388. 'tag' => 'Only list event entries tagged with this tag',
  389. );
  390. }
  391. public function getDescription() {
  392. return 'Get events from logs';
  393. }
  394. public function getPossibleErrors() {
  395. return array_merge( parent::getPossibleErrors(), array(
  396. array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
  397. array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
  398. array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
  399. array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
  400. ) );
  401. }
  402. public function getExamples() {
  403. return array(
  404. 'api.php?action=query&list=logevents'
  405. );
  406. }
  407. public function getHelpUrls() {
  408. return 'http://www.mediawiki.org/wiki/API:Logevents';
  409. }
  410. public function getVersion() {
  411. return __CLASS__ . ': $Id$';
  412. }
  413. }