PageRenderTime 45ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/specials/SpecialRecentchanges.php

https://bitbucket.org/brunodefraine/mediawiki
PHP | 869 lines | 575 code | 95 blank | 199 comment | 90 complexity | 6db27001efadcd9658041f236ae3f926 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Implements Special:Recentchanges
  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. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. /**
  24. * A special page that lists last changes made to the wiki
  25. *
  26. * @ingroup SpecialPage
  27. */
  28. class SpecialRecentChanges extends IncludableSpecialPage {
  29. var $rcOptions, $rcSubpage;
  30. protected $customFilters;
  31. public function __construct( $name = 'Recentchanges' ) {
  32. parent::__construct( $name );
  33. }
  34. /**
  35. * Get a FormOptions object containing the default options
  36. *
  37. * @return FormOptions
  38. */
  39. public function getDefaultOptions() {
  40. $opts = new FormOptions();
  41. $opts->add( 'days', (int)$this->getUser()->getOption( 'rcdays' ) );
  42. $opts->add( 'limit', (int)$this->getUser()->getOption( 'rclimit' ) );
  43. $opts->add( 'from', '' );
  44. $opts->add( 'hideminor', $this->getUser()->getBoolOption( 'hideminor' ) );
  45. $opts->add( 'hidebots', true );
  46. $opts->add( 'hideanons', false );
  47. $opts->add( 'hideliu', false );
  48. $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'hidepatrolled' ) );
  49. $opts->add( 'hidemyself', false );
  50. $opts->add( 'namespace', '', FormOptions::INTNULL );
  51. $opts->add( 'invert', false );
  52. $opts->add( 'associated', false );
  53. $opts->add( 'categories', '' );
  54. $opts->add( 'categories_any', false );
  55. $opts->add( 'tagfilter', '' );
  56. return $opts;
  57. }
  58. /**
  59. * Create a FormOptions object with options as specified by the user
  60. *
  61. * @param $parameters array
  62. *
  63. * @return FormOptions
  64. */
  65. public function setup( $parameters ) {
  66. $opts = $this->getDefaultOptions();
  67. foreach( $this->getCustomFilters() as $key => $params ) {
  68. $opts->add( $key, $params['default'] );
  69. }
  70. $opts->fetchValuesFromRequest( $this->getRequest() );
  71. // Give precedence to subpage syntax
  72. if( $parameters !== null ) {
  73. $this->parseParameters( $parameters, $opts );
  74. }
  75. $opts->validateIntBounds( 'limit', 0, 5000 );
  76. return $opts;
  77. }
  78. /**
  79. * Get custom show/hide filters
  80. *
  81. * @return Array Map of filter URL param names to properties (msg/default)
  82. */
  83. protected function getCustomFilters() {
  84. if ( $this->customFilters === null ) {
  85. $this->customFilters = array();
  86. wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters ) );
  87. }
  88. return $this->customFilters;
  89. }
  90. /**
  91. * Create a FormOptions object specific for feed requests and return it
  92. *
  93. * @return FormOptions
  94. */
  95. public function feedSetup() {
  96. global $wgFeedLimit;
  97. $opts = $this->getDefaultOptions();
  98. # Feed is cached on limit,hideminor,namespace; other params would randomly not work
  99. $opts->fetchValuesFromRequest( $this->getRequest(), array( 'limit', 'hideminor', 'namespace' ) );
  100. $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
  101. return $opts;
  102. }
  103. /**
  104. * Get the current FormOptions for this request
  105. */
  106. public function getOptions() {
  107. if ( $this->rcOptions === null ) {
  108. if ( $this->including() ) {
  109. $isFeed = false;
  110. } else {
  111. $isFeed = (bool)$this->getRequest()->getVal( 'feed' );
  112. }
  113. $this->rcOptions = $isFeed ? $this->feedSetup() : $this->setup( $this->rcSubpage );
  114. }
  115. return $this->rcOptions;
  116. }
  117. /**
  118. * Main execution point
  119. *
  120. * @param $subpage String
  121. */
  122. public function execute( $subpage ) {
  123. $this->rcSubpage = $subpage;
  124. $feedFormat = $this->including() ? null : $this->getRequest()->getVal( 'feed' );
  125. # 10 seconds server-side caching max
  126. $this->getOutput()->setSquidMaxage( 10 );
  127. # Check if the client has a cached version
  128. $lastmod = $this->checkLastModified( $feedFormat );
  129. if( $lastmod === false ) {
  130. return;
  131. }
  132. $opts = $this->getOptions();
  133. $this->setHeaders();
  134. $this->outputHeader();
  135. $this->addRecentChangesJS();
  136. // Fetch results, prepare a batch link existence check query
  137. $conds = $this->buildMainQueryConds( $opts );
  138. $rows = $this->doMainQuery( $conds, $opts );
  139. if( $rows === false ){
  140. if( !$this->including() ) {
  141. $this->doHeader( $opts );
  142. }
  143. return;
  144. }
  145. if( !$feedFormat ) {
  146. $batch = new LinkBatch;
  147. foreach( $rows as $row ) {
  148. $batch->add( NS_USER, $row->rc_user_text );
  149. $batch->add( NS_USER_TALK, $row->rc_user_text );
  150. $batch->add( $row->rc_namespace, $row->rc_title );
  151. }
  152. $batch->execute();
  153. }
  154. if( $feedFormat ) {
  155. list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
  156. $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
  157. } else {
  158. $this->webOutput( $rows, $opts );
  159. }
  160. $rows->free();
  161. }
  162. /**
  163. * Return an array with a ChangesFeed object and ChannelFeed object
  164. *
  165. * @return Array
  166. */
  167. public function getFeedObject( $feedFormat ){
  168. $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
  169. $formatter = $changesFeed->getFeedObject(
  170. wfMsgForContent( 'recentchanges' ),
  171. wfMsgForContent( 'recentchanges-feed-description' ),
  172. $this->getTitle()->getFullURL()
  173. );
  174. return array( $changesFeed, $formatter );
  175. }
  176. /**
  177. * Process $par and put options found if $opts
  178. * Mainly used when including the page
  179. *
  180. * @param $par String
  181. * @param $opts FormOptions
  182. */
  183. public function parseParameters( $par, FormOptions $opts ) {
  184. $bits = preg_split( '/\s*,\s*/', trim( $par ) );
  185. foreach( $bits as $bit ) {
  186. if( 'hidebots' === $bit ) {
  187. $opts['hidebots'] = true;
  188. }
  189. if( 'bots' === $bit ) {
  190. $opts['hidebots'] = false;
  191. }
  192. if( 'hideminor' === $bit ) {
  193. $opts['hideminor'] = true;
  194. }
  195. if( 'minor' === $bit ) {
  196. $opts['hideminor'] = false;
  197. }
  198. if( 'hideliu' === $bit ) {
  199. $opts['hideliu'] = true;
  200. }
  201. if( 'hidepatrolled' === $bit ) {
  202. $opts['hidepatrolled'] = true;
  203. }
  204. if( 'hideanons' === $bit ) {
  205. $opts['hideanons'] = true;
  206. }
  207. if( 'hidemyself' === $bit ) {
  208. $opts['hidemyself'] = true;
  209. }
  210. if( is_numeric( $bit ) ) {
  211. $opts['limit'] = $bit;
  212. }
  213. $m = array();
  214. if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
  215. $opts['limit'] = $m[1];
  216. }
  217. if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
  218. $opts['days'] = $m[1];
  219. }
  220. if( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
  221. $opts['namespace'] = $m[1];
  222. }
  223. }
  224. }
  225. /**
  226. * Get last modified date, for client caching
  227. * Don't use this if we are using the patrol feature, patrol changes don't
  228. * update the timestamp
  229. *
  230. * @param $feedFormat String
  231. * @return String or false
  232. */
  233. public function checkLastModified( $feedFormat ) {
  234. $dbr = wfGetDB( DB_SLAVE );
  235. $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
  236. if( $feedFormat || !$this->getUser()->useRCPatrol() ) {
  237. if( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
  238. # Client cache fresh and headers sent, nothing more to do.
  239. return false;
  240. }
  241. }
  242. return $lastmod;
  243. }
  244. /**
  245. * Return an array of conditions depending of options set in $opts
  246. *
  247. * @param $opts FormOptions
  248. * @return array
  249. */
  250. public function buildMainQueryConds( FormOptions $opts ) {
  251. $dbr = wfGetDB( DB_SLAVE );
  252. $conds = array();
  253. # It makes no sense to hide both anons and logged-in users
  254. # Where this occurs, force anons to be shown
  255. $forcebot = false;
  256. if( $opts['hideanons'] && $opts['hideliu'] ){
  257. # Check if the user wants to show bots only
  258. if( $opts['hidebots'] ){
  259. $opts['hideanons'] = false;
  260. } else {
  261. $forcebot = true;
  262. $opts['hidebots'] = false;
  263. }
  264. }
  265. // Calculate cutoff
  266. $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
  267. $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
  268. $cutoff = $dbr->timestamp( $cutoff_unixtime );
  269. $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
  270. if( $fromValid && $opts['from'] > wfTimestamp(TS_MW,$cutoff) ) {
  271. $cutoff = $dbr->timestamp($opts['from']);
  272. } else {
  273. $opts->reset( 'from' );
  274. }
  275. $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
  276. $hidePatrol = $this->getUser()->useRCPatrol() && $opts['hidepatrolled'];
  277. $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
  278. $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
  279. if( $opts['hideminor'] ) {
  280. $conds['rc_minor'] = 0;
  281. }
  282. if( $opts['hidebots'] ) {
  283. $conds['rc_bot'] = 0;
  284. }
  285. if( $hidePatrol ) {
  286. $conds['rc_patrolled'] = 0;
  287. }
  288. if( $forcebot ) {
  289. $conds['rc_bot'] = 1;
  290. }
  291. if( $hideLoggedInUsers ) {
  292. $conds[] = 'rc_user = 0';
  293. }
  294. if( $hideAnonymousUsers ) {
  295. $conds[] = 'rc_user != 0';
  296. }
  297. if( $opts['hidemyself'] ) {
  298. if( $this->getUser()->getId() ) {
  299. $conds[] = 'rc_user != ' . $dbr->addQuotes( $this->getUser()->getId() );
  300. } else {
  301. $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $this->getUser()->getName() );
  302. }
  303. }
  304. # Namespace filtering
  305. if( $opts['namespace'] !== '' ) {
  306. $selectedNS = $dbr->addQuotes( $opts['namespace'] );
  307. $operator = $opts['invert'] ? '!=' : '=';
  308. $boolean = $opts['invert'] ? 'AND' : 'OR';
  309. # namespace association (bug 2429)
  310. if( !$opts['associated'] ) {
  311. $condition = "rc_namespace $operator $selectedNS";
  312. } else {
  313. # Also add the associated namespace
  314. $associatedNS = $dbr->addQuotes(
  315. MWNamespace::getAssociated( $opts['namespace'] )
  316. );
  317. $condition = "(rc_namespace $operator $selectedNS "
  318. . $boolean
  319. . " rc_namespace $operator $associatedNS)";
  320. }
  321. $conds[] = $condition;
  322. }
  323. return $conds;
  324. }
  325. /**
  326. * Process the query
  327. *
  328. * @param $conds Array
  329. * @param $opts FormOptions
  330. * @return database result or false (for Recentchangeslinked only)
  331. */
  332. public function doMainQuery( $conds, $opts ) {
  333. $tables = array( 'recentchanges' );
  334. $join_conds = array();
  335. $query_options = array(
  336. 'USE INDEX' => array( 'recentchanges' => 'rc_timestamp' )
  337. );
  338. $uid = $this->getUser()->getId();
  339. $dbr = wfGetDB( DB_SLAVE );
  340. $limit = $opts['limit'];
  341. $namespace = $opts['namespace'];
  342. $invert = $opts['invert'];
  343. $associated = $opts['associated'];
  344. $fields = array( $dbr->tableName( 'recentchanges' ) . '.*' ); // all rc columns
  345. // JOIN on watchlist for users
  346. if ( $uid ) {
  347. $tables[] = 'watchlist';
  348. $fields[] = 'wl_user';
  349. $fields[] = 'wl_notificationtimestamp';
  350. $join_conds['watchlist'] = array('LEFT JOIN',
  351. "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace");
  352. }
  353. if ( $this->getUser()->isAllowed( 'rollback' ) ) {
  354. $tables[] = 'page';
  355. $fields[] = 'page_latest';
  356. $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
  357. }
  358. if ( !$this->including() ) {
  359. // Tag stuff.
  360. // Doesn't work when transcluding. See bug 23293
  361. ChangeTags::modifyDisplayQuery(
  362. $tables, $fields, $conds, $join_conds, $query_options,
  363. $opts['tagfilter']
  364. );
  365. }
  366. if ( !wfRunHooks( 'SpecialRecentChangesQuery',
  367. array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) ) )
  368. {
  369. return false;
  370. }
  371. // Don't use the new_namespace_time timestamp index if:
  372. // (a) "All namespaces" selected
  373. // (b) We want pages in more than one namespace (inverted/associated)
  374. // (c) There is a tag to filter on (use tag index instead)
  375. // (d) UNION + sort/limit is not an option for the DBMS
  376. if( $namespace === ''
  377. || ( $invert || $associated )
  378. || $opts['tagfilter'] != ''
  379. || !$dbr->unionSupportsOrderAndLimit() )
  380. {
  381. $res = $dbr->select( $tables, $fields, $conds, __METHOD__,
  382. array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
  383. $query_options,
  384. $join_conds );
  385. // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
  386. } else {
  387. // New pages
  388. $sqlNew = $dbr->selectSQLText(
  389. $tables,
  390. $fields,
  391. array( 'rc_new' => 1 ) + $conds,
  392. __METHOD__,
  393. array(
  394. 'ORDER BY' => 'rc_timestamp DESC',
  395. 'LIMIT' => $limit,
  396. 'USE INDEX' => array( 'recentchanges' => 'new_name_timestamp' )
  397. ),
  398. $join_conds
  399. );
  400. // Old pages
  401. $sqlOld = $dbr->selectSQLText(
  402. $tables,
  403. $fields,
  404. array( 'rc_new' => 0 ) + $conds,
  405. __METHOD__,
  406. array(
  407. 'ORDER BY' => 'rc_timestamp DESC',
  408. 'LIMIT' => $limit,
  409. 'USE INDEX' => array( 'recentchanges' => 'new_name_timestamp' )
  410. ),
  411. $join_conds
  412. );
  413. # Join the two fast queries, and sort the result set
  414. $sql = $dbr->unionQueries( array( $sqlNew, $sqlOld ), false ) .
  415. ' ORDER BY rc_timestamp DESC';
  416. $sql = $dbr->limitResult( $sql, $limit, false );
  417. $res = $dbr->query( $sql, __METHOD__ );
  418. }
  419. return $res;
  420. }
  421. /**
  422. * Send output to the OutputPage object, only called if not used feeds
  423. *
  424. * @param $rows Array of database rows
  425. * @param $opts FormOptions
  426. */
  427. public function webOutput( $rows, $opts ) {
  428. global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
  429. $limit = $opts['limit'];
  430. if( !$this->including() ) {
  431. // Output options box
  432. $this->doHeader( $opts );
  433. }
  434. // And now for the content
  435. $this->getOutput()->setFeedAppendQuery( $this->getFeedQuery() );
  436. if( $wgAllowCategorizedRecentChanges ) {
  437. $this->filterByCategories( $rows, $opts );
  438. }
  439. $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
  440. $watcherCache = array();
  441. $dbr = wfGetDB( DB_SLAVE );
  442. $counter = 1;
  443. $list = ChangesList::newFromContext( $this->getContext() );
  444. $s = $list->beginRecentChangesList();
  445. foreach( $rows as $obj ) {
  446. if( $limit == 0 ) {
  447. break;
  448. }
  449. $rc = RecentChange::newFromRow( $obj );
  450. $rc->counter = $counter++;
  451. # Check if the page has been updated since the last visit
  452. if( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp ) ) {
  453. $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
  454. } else {
  455. $rc->notificationtimestamp = false; // Default
  456. }
  457. # Check the number of users watching the page
  458. $rc->numberofWatchingusers = 0; // Default
  459. if( $showWatcherCount && $obj->rc_namespace >= 0 ) {
  460. if( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
  461. $watcherCache[$obj->rc_namespace][$obj->rc_title] =
  462. $dbr->selectField(
  463. 'watchlist',
  464. 'COUNT(*)',
  465. array(
  466. 'wl_namespace' => $obj->rc_namespace,
  467. 'wl_title' => $obj->rc_title,
  468. ),
  469. __METHOD__ . '-watchers'
  470. );
  471. }
  472. $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
  473. }
  474. $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
  475. --$limit;
  476. }
  477. $s .= $list->endRecentChangesList();
  478. $this->getOutput()->addHTML( $s );
  479. }
  480. /**
  481. * Get the query string to append to feed link URLs.
  482. * This is overridden by RCL to add the target parameter
  483. */
  484. public function getFeedQuery() {
  485. return false;
  486. }
  487. /**
  488. * Return the text to be displayed above the changes
  489. *
  490. * @param $opts FormOptions
  491. * @return String: XHTML
  492. */
  493. public function doHeader( $opts ) {
  494. global $wgScript;
  495. $this->setTopText( $opts );
  496. $defaults = $opts->getAllValues();
  497. $nondefaults = $opts->getChangedValues();
  498. $opts->consumeValues( array(
  499. 'namespace', 'invert', 'associated', 'tagfilter',
  500. 'categories', 'categories_any'
  501. ) );
  502. $panel = array();
  503. $panel[] = $this->optionsPanel( $defaults, $nondefaults );
  504. $panel[] = '<hr />';
  505. $extraOpts = $this->getExtraOptions( $opts );
  506. $extraOptsCount = count( $extraOpts );
  507. $count = 0;
  508. $submit = ' ' . Xml::submitbutton( wfMsg( 'allpagessubmit' ) );
  509. $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
  510. foreach( $extraOpts as $optionRow ) {
  511. # Add submit button to the last row only
  512. ++$count;
  513. $addSubmit = $count === $extraOptsCount ? $submit : '';
  514. $out .= Xml::openElement( 'tr' );
  515. if( is_array( $optionRow ) ) {
  516. $out .= Xml::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
  517. $out .= Xml::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
  518. } else {
  519. $out .= Xml::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
  520. }
  521. $out .= Xml::closeElement( 'tr' );
  522. }
  523. $out .= Xml::closeElement( 'table' );
  524. $unconsumed = $opts->getUnconsumedValues();
  525. foreach( $unconsumed as $key => $value ) {
  526. $out .= Html::hidden( $key, $value );
  527. }
  528. $t = $this->getTitle();
  529. $out .= Html::hidden( 'title', $t->getPrefixedText() );
  530. $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
  531. $panel[] = $form;
  532. $panelString = implode( "\n", $panel );
  533. $this->getOutput()->addHTML(
  534. Xml::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
  535. );
  536. $this->setBottomText( $opts );
  537. }
  538. /**
  539. * Get options to be displayed in a form
  540. *
  541. * @param $opts FormOptions
  542. * @return Array
  543. */
  544. function getExtraOptions( $opts ) {
  545. $extraOpts = array();
  546. $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
  547. global $wgAllowCategorizedRecentChanges;
  548. if( $wgAllowCategorizedRecentChanges ) {
  549. $extraOpts['category'] = $this->categoryFilterForm( $opts );
  550. }
  551. $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
  552. if ( count( $tagFilter ) ) {
  553. $extraOpts['tagfilter'] = $tagFilter;
  554. }
  555. wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
  556. return $extraOpts;
  557. }
  558. /**
  559. * Send the text to be displayed above the options
  560. *
  561. * @param $opts FormOptions
  562. */
  563. function setTopText( FormOptions $opts ) {
  564. global $wgContLang;
  565. $this->getOutput()->addWikiText(
  566. Html::rawElement( 'p',
  567. array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
  568. "\n" . wfMsgForContentNoTrans( 'recentchangestext' ) . "\n"
  569. ),
  570. /* $lineStart */ false,
  571. /* $interface */ false
  572. );
  573. }
  574. /**
  575. * Send the text to be displayed after the options, for use in
  576. * Recentchangeslinked
  577. *
  578. * @param $opts FormOptions
  579. */
  580. function setBottomText( FormOptions $opts ) {}
  581. /**
  582. * Creates the choose namespace selection
  583. *
  584. * @todo Uses radio buttons (HASHAR)
  585. * @param $opts FormOptions
  586. * @return String
  587. */
  588. protected function namespaceFilterForm( FormOptions $opts ) {
  589. $nsSelect = Html::namespaceSelector(
  590. array( 'selected' => $opts['namespace'], 'all' => '' ),
  591. array( 'name' => 'namespace', 'id' => 'namespace' )
  592. );
  593. $nsLabel = Xml::label( wfMsg( 'namespace' ), 'namespace' );
  594. $invert = Xml::checkLabel(
  595. wfMsg( 'invert' ), 'invert', 'nsinvert',
  596. $opts['invert'],
  597. array( 'title' => wfMsg( 'tooltip-invert' ) )
  598. );
  599. $associated = Xml::checkLabel(
  600. wfMsg( 'namespace_association' ), 'associated', 'nsassociated',
  601. $opts['associated'],
  602. array( 'title' => wfMsg( 'tooltip-namespace_association' ) )
  603. );
  604. return array( $nsLabel, "$nsSelect $invert $associated" );
  605. }
  606. /**
  607. * Create a input to filter changes by categories
  608. *
  609. * @param $opts FormOptions
  610. * @return Array
  611. */
  612. protected function categoryFilterForm( FormOptions $opts ) {
  613. list( $label, $input ) = Xml::inputLabelSep( wfMsg( 'rc_categories' ),
  614. 'categories', 'mw-categories', false, $opts['categories'] );
  615. $input .= ' ' . Xml::checkLabel( wfMsg( 'rc_categories_any' ),
  616. 'categories_any', 'mw-categories_any', $opts['categories_any'] );
  617. return array( $label, $input );
  618. }
  619. /**
  620. * Filter $rows by categories set in $opts
  621. *
  622. * @param $rows Array of database rows
  623. * @param $opts FormOptions
  624. */
  625. function filterByCategories( &$rows, FormOptions $opts ) {
  626. $categories = array_map( 'trim', explode( '|' , $opts['categories'] ) );
  627. if( !count( $categories ) ) {
  628. return;
  629. }
  630. # Filter categories
  631. $cats = array();
  632. foreach( $categories as $cat ) {
  633. $cat = trim( $cat );
  634. if( $cat == '' ) {
  635. continue;
  636. }
  637. $cats[] = $cat;
  638. }
  639. # Filter articles
  640. $articles = array();
  641. $a2r = array();
  642. $rowsarr = array();
  643. foreach( $rows as $k => $r ) {
  644. $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
  645. $id = $nt->getArticleID();
  646. if( $id == 0 ) {
  647. continue; # Page might have been deleted...
  648. }
  649. if( !in_array( $id, $articles ) ) {
  650. $articles[] = $id;
  651. }
  652. if( !isset( $a2r[$id] ) ) {
  653. $a2r[$id] = array();
  654. }
  655. $a2r[$id][] = $k;
  656. $rowsarr[$k] = $r;
  657. }
  658. # Shortcut?
  659. if( !count( $articles ) || !count( $cats ) ) {
  660. return;
  661. }
  662. # Look up
  663. $c = new Categoryfinder;
  664. $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
  665. $match = $c->run();
  666. # Filter
  667. $newrows = array();
  668. foreach( $match as $id ) {
  669. foreach( $a2r[$id] as $rev ) {
  670. $k = $rev;
  671. $newrows[$k] = $rowsarr[$k];
  672. }
  673. }
  674. $rows = $newrows;
  675. }
  676. /**
  677. * Makes change an option link which carries all the other options
  678. *
  679. * @param $title Title
  680. * @param $override Array: options to override
  681. * @param $options Array: current options
  682. * @param $active Boolean: whether to show the link in bold
  683. */
  684. function makeOptionsLink( $title, $override, $options, $active = false ) {
  685. $params = $override + $options;
  686. $text = htmlspecialchars( $title );
  687. if ( $active ) {
  688. $text = '<strong>' . $text . '</strong>';
  689. }
  690. return Linker::linkKnown( $this->getTitle(), $text, array(), $params );
  691. }
  692. /**
  693. * Creates the options panel.
  694. *
  695. * @param $defaults Array
  696. * @param $nondefaults Array
  697. */
  698. function optionsPanel( $defaults, $nondefaults ) {
  699. global $wgRCLinkLimits, $wgRCLinkDays;
  700. $options = $nondefaults + $defaults;
  701. $note = '';
  702. if( !wfEmptyMsg( 'rclegend' ) ) {
  703. $note .= '<div class="mw-rclegend">' .
  704. wfMsgExt( 'rclegend', array( 'parseinline' ) ) . "</div>\n";
  705. }
  706. if( $options['from'] ) {
  707. $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
  708. $this->getLanguage()->formatNum( $options['limit'] ),
  709. $this->getLanguage()->timeanddate( $options['from'], true ),
  710. $this->getLanguage()->date( $options['from'], true ),
  711. $this->getLanguage()->time( $options['from'], true ) ) . '<br />';
  712. }
  713. # Sort data for display and make sure it's unique after we've added user data.
  714. $wgRCLinkLimits[] = $options['limit'];
  715. $wgRCLinkDays[] = $options['days'];
  716. sort( $wgRCLinkLimits );
  717. sort( $wgRCLinkDays );
  718. $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
  719. $wgRCLinkDays = array_unique( $wgRCLinkDays );
  720. // limit links
  721. foreach( $wgRCLinkLimits as $value ) {
  722. $cl[] = $this->makeOptionsLink( $this->getLanguage()->formatNum( $value ),
  723. array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
  724. }
  725. $cl = $this->getLanguage()->pipeList( $cl );
  726. // day links, reset 'from' to none
  727. foreach( $wgRCLinkDays as $value ) {
  728. $dl[] = $this->makeOptionsLink( $this->getLanguage()->formatNum( $value ),
  729. array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
  730. }
  731. $dl = $this->getLanguage()->pipeList( $dl );
  732. // show/hide links
  733. $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
  734. $filters = array(
  735. 'hideminor' => 'rcshowhideminor',
  736. 'hidebots' => 'rcshowhidebots',
  737. 'hideanons' => 'rcshowhideanons',
  738. 'hideliu' => 'rcshowhideliu',
  739. 'hidepatrolled' => 'rcshowhidepatr',
  740. 'hidemyself' => 'rcshowhidemine'
  741. );
  742. foreach ( $this->getCustomFilters() as $key => $params ) {
  743. $filters[$key] = $params['msg'];
  744. }
  745. // Disable some if needed
  746. if ( !$this->getUser()->useRCPatrol() ) {
  747. unset( $filters['hidepatrolled'] );
  748. }
  749. $links = array();
  750. foreach ( $filters as $key => $msg ) {
  751. $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
  752. array( $key => 1-$options[$key] ), $nondefaults );
  753. $links[] = wfMsgHtml( $msg, $link );
  754. }
  755. // show from this onward link
  756. $timestamp = wfTimestampNow();
  757. $now = $this->getLanguage()->timeanddate( $timestamp, true );
  758. $tl = $this->makeOptionsLink(
  759. $now, array( 'from' => $timestamp ), $nondefaults
  760. );
  761. $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
  762. $cl, $dl, $this->getLanguage()->pipeList( $links ) );
  763. $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
  764. return "{$note}$rclinks<br />$rclistfrom";
  765. }
  766. /**
  767. * add javascript specific to the [[Special:RecentChanges]] page
  768. */
  769. function addRecentChangesJS() {
  770. $this->getOutput()->addModules( array(
  771. 'mediawiki.special.recentchanges',
  772. ) );
  773. }
  774. }