SpecialNewpages.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <?php
  2. /**
  3. * Implements Special:Newpages
  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. use MediaWiki\MediaWikiServices;
  24. /**
  25. * A special page that list newly created pages
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialNewpages extends IncludableSpecialPage {
  30. /**
  31. * @var FormOptions
  32. */
  33. protected $opts;
  34. protected $customFilters;
  35. protected $showNavigation = false;
  36. public function __construct() {
  37. parent::__construct( 'Newpages' );
  38. }
  39. /**
  40. * @param string|null $par
  41. */
  42. protected function setup( $par ) {
  43. $opts = new FormOptions();
  44. $this->opts = $opts; // bind
  45. $opts->add( 'hideliu', false );
  46. $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
  47. $opts->add( 'hidebots', false );
  48. $opts->add( 'hideredirs', true );
  49. $opts->add( 'limit', $this->getUser()->getIntOption( 'rclimit' ) );
  50. $opts->add( 'offset', '' );
  51. $opts->add( 'namespace', '0' );
  52. $opts->add( 'username', '' );
  53. $opts->add( 'feed', '' );
  54. $opts->add( 'tagfilter', '' );
  55. $opts->add( 'invert', false );
  56. $opts->add( 'associated', false );
  57. $opts->add( 'size-mode', 'max' );
  58. $opts->add( 'size', 0 );
  59. $this->customFilters = [];
  60. Hooks::run( 'SpecialNewPagesFilters', [ $this, &$this->customFilters ] );
  61. foreach ( $this->customFilters as $key => $params ) {
  62. $opts->add( $key, $params['default'] );
  63. }
  64. $opts->fetchValuesFromRequest( $this->getRequest() );
  65. if ( $par ) {
  66. $this->parseParams( $par );
  67. }
  68. $opts->validateIntBounds( 'limit', 0, 5000 );
  69. }
  70. /**
  71. * @param string $par
  72. */
  73. protected function parseParams( $par ) {
  74. $bits = preg_split( '/\s*,\s*/', trim( $par ) );
  75. foreach ( $bits as $bit ) {
  76. if ( $bit === 'shownav' ) {
  77. $this->showNavigation = true;
  78. }
  79. if ( $bit === 'hideliu' ) {
  80. $this->opts->setValue( 'hideliu', true );
  81. }
  82. if ( $bit === 'hidepatrolled' ) {
  83. $this->opts->setValue( 'hidepatrolled', true );
  84. }
  85. if ( $bit === 'hidebots' ) {
  86. $this->opts->setValue( 'hidebots', true );
  87. }
  88. if ( $bit === 'showredirs' ) {
  89. $this->opts->setValue( 'hideredirs', false );
  90. }
  91. if ( is_numeric( $bit ) ) {
  92. $this->opts->setValue( 'limit', intval( $bit ) );
  93. }
  94. $m = [];
  95. if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
  96. $this->opts->setValue( 'limit', intval( $m[1] ) );
  97. }
  98. // PG offsets not just digits!
  99. if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
  100. $this->opts->setValue( 'offset', intval( $m[1] ) );
  101. }
  102. if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
  103. $this->opts->setValue( 'username', $m[1] );
  104. }
  105. if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
  106. $ns = $this->getLanguage()->getNsIndex( $m[1] );
  107. if ( $ns !== false ) {
  108. $this->opts->setValue( 'namespace', $ns );
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * Show a form for filtering namespace and username
  115. *
  116. * @param string|null $par
  117. */
  118. public function execute( $par ) {
  119. $out = $this->getOutput();
  120. $this->setHeaders();
  121. $this->outputHeader();
  122. $this->showNavigation = !$this->including(); // Maybe changed in setup
  123. $this->setup( $par );
  124. $this->addHelpLink( 'Help:New pages' );
  125. if ( !$this->including() ) {
  126. // Settings
  127. $this->form();
  128. $feedType = $this->opts->getValue( 'feed' );
  129. if ( $feedType ) {
  130. $this->feed( $feedType );
  131. return;
  132. }
  133. $allValues = $this->opts->getAllValues();
  134. unset( $allValues['feed'] );
  135. $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
  136. }
  137. $pager = new NewPagesPager( $this, $this->opts );
  138. $pager->mLimit = $this->opts->getValue( 'limit' );
  139. $pager->mOffset = $this->opts->getValue( 'offset' );
  140. if ( $pager->getNumRows() ) {
  141. $navigation = '';
  142. if ( $this->showNavigation ) {
  143. $navigation = $pager->getNavigationBar();
  144. }
  145. $out->addHTML( $navigation . $pager->getBody() . $navigation );
  146. // Add styles for change tags
  147. $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
  148. } else {
  149. $out->addWikiMsg( 'specialpage-empty' );
  150. }
  151. }
  152. protected function filterLinks() {
  153. // show/hide links
  154. $showhide = [ $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() ];
  155. // Option value -> message mapping
  156. $filters = [
  157. 'hideliu' => 'rcshowhideliu',
  158. 'hidepatrolled' => 'rcshowhidepatr',
  159. 'hidebots' => 'rcshowhidebots',
  160. 'hideredirs' => 'whatlinkshere-hideredirs'
  161. ];
  162. foreach ( $this->customFilters as $key => $params ) {
  163. $filters[$key] = $params['msg'];
  164. }
  165. // Disable some if needed
  166. if ( !MediaWikiServices::getInstance()->getPermissionManager()
  167. ->groupHasPermission( '*', 'createpage' )
  168. ) {
  169. unset( $filters['hideliu'] );
  170. }
  171. if ( !$this->getUser()->useNPPatrol() ) {
  172. unset( $filters['hidepatrolled'] );
  173. }
  174. $links = [];
  175. $changed = $this->opts->getChangedValues();
  176. unset( $changed['offset'] ); // Reset offset if query type changes
  177. // wfArrayToCgi(), called from LinkRenderer/Title, will not output null and false values
  178. // to the URL, which would omit some options (T158504). Fix it by explicitly setting them
  179. // to 0 or 1.
  180. // Also do this only for boolean options, not eg. namespace or tagfilter
  181. foreach ( $changed as $key => $value ) {
  182. if ( array_key_exists( $key, $filters ) ) {
  183. $changed[$key] = $changed[$key] ? '1' : '0';
  184. }
  185. }
  186. $self = $this->getPageTitle();
  187. $linkRenderer = $this->getLinkRenderer();
  188. foreach ( $filters as $key => $msg ) {
  189. $onoff = 1 - $this->opts->getValue( $key );
  190. $link = $linkRenderer->makeLink(
  191. $self,
  192. new HtmlArmor( $showhide[$onoff] ),
  193. [],
  194. [ $key => $onoff ] + $changed
  195. );
  196. $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
  197. }
  198. return $this->getLanguage()->pipeList( $links );
  199. }
  200. protected function form() {
  201. $out = $this->getOutput();
  202. // Consume values
  203. $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
  204. $namespace = $this->opts->consumeValue( 'namespace' );
  205. $username = $this->opts->consumeValue( 'username' );
  206. $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
  207. $nsinvert = $this->opts->consumeValue( 'invert' );
  208. $nsassociated = $this->opts->consumeValue( 'associated' );
  209. $size = $this->opts->consumeValue( 'size' );
  210. $max = $this->opts->consumeValue( 'size-mode' ) === 'max';
  211. // Check username input validity
  212. $ut = Title::makeTitleSafe( NS_USER, $username );
  213. $userText = $ut ? $ut->getText() : '';
  214. $formDescriptor = [
  215. 'namespace' => [
  216. 'type' => 'namespaceselect',
  217. 'name' => 'namespace',
  218. 'label-message' => 'namespace',
  219. 'default' => $namespace,
  220. ],
  221. 'nsinvert' => [
  222. 'type' => 'check',
  223. 'name' => 'invert',
  224. 'label-message' => 'invert',
  225. 'default' => $nsinvert,
  226. 'tooltip' => 'invert',
  227. ],
  228. 'nsassociated' => [
  229. 'type' => 'check',
  230. 'name' => 'associated',
  231. 'label-message' => 'namespace_association',
  232. 'default' => $nsassociated,
  233. 'tooltip' => 'namespace_association',
  234. ],
  235. 'tagFilter' => [
  236. 'type' => 'tagfilter',
  237. 'name' => 'tagfilter',
  238. 'label-raw' => $this->msg( 'tag-filter' )->parse(),
  239. 'default' => $tagFilterVal,
  240. ],
  241. 'username' => [
  242. 'type' => 'user',
  243. 'name' => 'username',
  244. 'label-message' => 'newpages-username',
  245. 'default' => $userText,
  246. 'id' => 'mw-np-username',
  247. 'size' => 30,
  248. ],
  249. 'size' => [
  250. 'type' => 'sizefilter',
  251. 'name' => 'size',
  252. 'default' => -$max * $size,
  253. ],
  254. ];
  255. $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
  256. // Store query values in hidden fields so that form submission doesn't lose them
  257. foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
  258. $htmlForm->addHiddenField( $key, $value );
  259. }
  260. $htmlForm
  261. ->setMethod( 'get' )
  262. ->setFormIdentifier( 'newpagesform' )
  263. // The form should be visible on each request (inclusive requests with submitted forms), so
  264. // return always false here.
  265. ->setSubmitCallback(
  266. function () {
  267. return false;
  268. }
  269. )
  270. ->setSubmitText( $this->msg( 'newpages-submit' )->text() )
  271. ->setWrapperLegend( $this->msg( 'newpages' )->text() )
  272. ->addFooterText( Html::rawElement(
  273. 'div',
  274. null,
  275. $this->filterLinks()
  276. ) )
  277. ->show();
  278. $out->addModuleStyles( 'mediawiki.special' );
  279. }
  280. /**
  281. * @param stdClass $result Result row from recent changes
  282. * @param Title $title
  283. * @return bool|Revision
  284. */
  285. protected function revisionFromRcResult( stdClass $result, Title $title ) {
  286. return new Revision( [
  287. 'comment' => CommentStore::getStore()->getComment( 'rc_comment', $result )->text,
  288. 'deleted' => $result->rc_deleted,
  289. 'user_text' => $result->rc_user_text,
  290. 'user' => $result->rc_user,
  291. 'actor' => $result->rc_actor,
  292. ], 0, $title );
  293. }
  294. /**
  295. * Format a row, providing the timestamp, links to the page/history,
  296. * size, user links, and a comment
  297. *
  298. * @param object $result Result row
  299. * @return string
  300. */
  301. public function formatRow( $result ) {
  302. $title = Title::newFromRow( $result );
  303. // Revision deletion works on revisions,
  304. // so cast our recent change row to a revision row.
  305. $rev = $this->revisionFromRcResult( $result, $title );
  306. $classes = [];
  307. $attribs = [ 'data-mw-revid' => $result->rev_id ];
  308. $lang = $this->getLanguage();
  309. $dm = $lang->getDirMark();
  310. $spanTime = Html::element( 'span', [ 'class' => 'mw-newpages-time' ],
  311. $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
  312. );
  313. $linkRenderer = $this->getLinkRenderer();
  314. $time = $linkRenderer->makeKnownLink(
  315. $title,
  316. new HtmlArmor( $spanTime ),
  317. [],
  318. [ 'oldid' => $result->rc_this_oldid ]
  319. );
  320. $query = $title->isRedirect() ? [ 'redirect' => 'no' ] : [];
  321. $plink = $linkRenderer->makeKnownLink(
  322. $title,
  323. null,
  324. [ 'class' => 'mw-newpages-pagename' ],
  325. $query
  326. );
  327. $histLink = $linkRenderer->makeKnownLink(
  328. $title,
  329. $this->msg( 'hist' )->text(),
  330. [],
  331. [ 'action' => 'history' ]
  332. );
  333. $hist = Html::rawElement( 'span', [ 'class' => 'mw-newpages-history' ],
  334. $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
  335. $length = Html::rawElement(
  336. 'span',
  337. [ 'class' => 'mw-newpages-length' ],
  338. $this->msg( 'brackets' )->rawParams(
  339. $this->msg( 'nbytes' )->numParams( $result->length )->escaped()
  340. )->escaped()
  341. );
  342. $ulink = Linker::revUserTools( $rev );
  343. $comment = Linker::revComment( $rev );
  344. if ( $this->patrollable( $result ) ) {
  345. $classes[] = 'not-patrolled';
  346. }
  347. # Add a class for zero byte pages
  348. if ( $result->length == 0 ) {
  349. $classes[] = 'mw-newpages-zero-byte-page';
  350. }
  351. # Tags, if any.
  352. if ( isset( $result->ts_tags ) ) {
  353. list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
  354. $result->ts_tags,
  355. 'newpages',
  356. $this->getContext()
  357. );
  358. $classes = array_merge( $classes, $newClasses );
  359. } else {
  360. $tagDisplay = '';
  361. }
  362. # Display the old title if the namespace/title has been changed
  363. $oldTitleText = '';
  364. $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
  365. if ( !$title->equals( $oldTitle ) ) {
  366. $oldTitleText = $oldTitle->getPrefixedText();
  367. $oldTitleText = Html::rawElement(
  368. 'span',
  369. [ 'class' => 'mw-newpages-oldtitle' ],
  370. $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped()
  371. );
  372. }
  373. $ret = "{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} "
  374. . "{$tagDisplay} {$oldTitleText}";
  375. // Let extensions add data
  376. Hooks::run( 'NewPagesLineEnding', [ $this, &$ret, $result, &$classes, &$attribs ] );
  377. $attribs = array_filter( $attribs,
  378. [ Sanitizer::class, 'isReservedDataAttribute' ],
  379. ARRAY_FILTER_USE_KEY
  380. );
  381. if ( count( $classes ) ) {
  382. $attribs['class'] = implode( ' ', $classes );
  383. }
  384. return Html::rawElement( 'li', $attribs, $ret ) . "\n";
  385. }
  386. /**
  387. * Should a specific result row provide "patrollable" links?
  388. *
  389. * @param object $result Result row
  390. * @return bool
  391. */
  392. protected function patrollable( $result ) {
  393. return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
  394. }
  395. /**
  396. * Output a subscription feed listing recent edits to this page.
  397. *
  398. * @param string $type
  399. */
  400. protected function feed( $type ) {
  401. if ( !$this->getConfig()->get( 'Feed' ) ) {
  402. $this->getOutput()->addWikiMsg( 'feed-unavailable' );
  403. return;
  404. }
  405. $feedClasses = $this->getConfig()->get( 'FeedClasses' );
  406. if ( !isset( $feedClasses[$type] ) ) {
  407. $this->getOutput()->addWikiMsg( 'feed-invalid' );
  408. return;
  409. }
  410. $feed = new $feedClasses[$type](
  411. $this->feedTitle(),
  412. $this->msg( 'tagline' )->text(),
  413. $this->getPageTitle()->getFullURL()
  414. );
  415. $pager = new NewPagesPager( $this, $this->opts );
  416. $limit = $this->opts->getValue( 'limit' );
  417. $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
  418. $feed->outHeader();
  419. if ( $pager->getNumRows() > 0 ) {
  420. foreach ( $pager->mResult as $row ) {
  421. $feed->outItem( $this->feedItem( $row ) );
  422. }
  423. }
  424. $feed->outFooter();
  425. }
  426. protected function feedTitle() {
  427. $desc = $this->getDescription();
  428. $code = $this->getConfig()->get( 'LanguageCode' );
  429. $sitename = $this->getConfig()->get( 'Sitename' );
  430. return "$sitename - $desc [$code]";
  431. }
  432. protected function feedItem( $row ) {
  433. $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
  434. if ( $title ) {
  435. $date = $row->rc_timestamp;
  436. $comments = $title->getTalkPage()->getFullURL();
  437. return new FeedItem(
  438. $title->getPrefixedText(),
  439. $this->feedItemDesc( $row ),
  440. $title->getFullURL(),
  441. $date,
  442. $this->feedItemAuthor( $row ),
  443. $comments
  444. );
  445. } else {
  446. return null;
  447. }
  448. }
  449. protected function feedItemAuthor( $row ) {
  450. return $row->rc_user_text ?? '';
  451. }
  452. protected function feedItemDesc( $row ) {
  453. $revision = Revision::newFromId( $row->rev_id );
  454. if ( !$revision ) {
  455. return '';
  456. }
  457. $content = $revision->getContent();
  458. if ( $content === null ) {
  459. return '';
  460. }
  461. // XXX: include content model/type in feed item?
  462. return '<p>' . htmlspecialchars( $revision->getUserText() ) .
  463. $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
  464. htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
  465. "</p>\n<hr />\n<div>" .
  466. nl2br( htmlspecialchars( $content->serialize() ) ) . "</div>";
  467. }
  468. protected function getGroupName() {
  469. return 'changes';
  470. }
  471. protected function getCacheTTL() {
  472. return 60 * 5;
  473. }
  474. }