ChangesListSpecialPage.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. <?php
  2. /**
  3. * Special page which uses a ChangesList to show query results.
  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\Logger\LoggerFactory;
  24. use Wikimedia\Rdbms\DBQueryTimeoutError;
  25. use Wikimedia\Rdbms\IResultWrapper;
  26. use Wikimedia\Rdbms\FakeResultWrapper;
  27. use Wikimedia\Rdbms\IDatabase;
  28. /**
  29. * Special page which uses a ChangesList to show query results.
  30. * @todo Way too many public functions, most of them should be protected
  31. *
  32. * @ingroup SpecialPage
  33. */
  34. abstract class ChangesListSpecialPage extends SpecialPage {
  35. /**
  36. * Maximum length of a tag description in UTF-8 characters.
  37. * Longer descriptions will be truncated.
  38. */
  39. const TAG_DESC_CHARACTER_LIMIT = 120;
  40. /**
  41. * Preference name for saved queries. Subclasses that use saved queries should override this.
  42. * @var string
  43. */
  44. protected static $savedQueriesPreferenceName;
  45. /**
  46. * Preference name for 'days'. Subclasses should override this.
  47. * @var string
  48. */
  49. protected static $daysPreferenceName;
  50. /**
  51. * Preference name for 'limit'. Subclasses should override this.
  52. * @var string
  53. */
  54. protected static $limitPreferenceName;
  55. /** @var string */
  56. protected $rcSubpage;
  57. /** @var FormOptions */
  58. protected $rcOptions;
  59. /** @var array */
  60. protected $customFilters;
  61. // Order of both groups and filters is significant; first is top-most priority,
  62. // descending from there.
  63. // 'showHideSuffix' is a shortcut to and avoid spelling out
  64. // details specific to subclasses here.
  65. /**
  66. * Definition information for the filters and their groups
  67. *
  68. * The value is $groupDefinition, a parameter to the ChangesListFilterGroup constructor.
  69. * However, priority is dynamically added for the core groups, to ease maintenance.
  70. *
  71. * Groups are displayed to the user in the structured UI. However, if necessary,
  72. * all of the filters in a group can be configured to only display on the
  73. * unstuctured UI, in which case you don't need a group title. This is done in
  74. * getFilterGroupDefinitionFromLegacyCustomFilters, for example.
  75. *
  76. * @var array $filterGroupDefinitions
  77. */
  78. private $filterGroupDefinitions;
  79. // Same format as filterGroupDefinitions, but for a single group (reviewStatus)
  80. // that is registered conditionally.
  81. private $legacyReviewStatusFilterGroupDefinition;
  82. // Single filter group registered conditionally
  83. private $reviewStatusFilterGroupDefinition;
  84. // Single filter group registered conditionally
  85. private $hideCategorizationFilterDefinition;
  86. /**
  87. * Filter groups, and their contained filters
  88. * This is an associative array (with group name as key) of ChangesListFilterGroup objects.
  89. *
  90. * @var array $filterGroups
  91. */
  92. protected $filterGroups = [];
  93. public function __construct( $name, $restriction ) {
  94. parent::__construct( $name, $restriction );
  95. $nonRevisionTypes = [ RC_LOG ];
  96. Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
  97. $this->filterGroupDefinitions = [
  98. [
  99. 'name' => 'registration',
  100. 'title' => 'rcfilters-filtergroup-registration',
  101. 'class' => ChangesListBooleanFilterGroup::class,
  102. 'filters' => [
  103. [
  104. 'name' => 'hideliu',
  105. // rcshowhideliu-show, rcshowhideliu-hide,
  106. // wlshowhideliu
  107. 'showHideSuffix' => 'showhideliu',
  108. 'default' => false,
  109. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  110. &$query_options, &$join_conds
  111. ) {
  112. $actorMigration = ActorMigration::newMigration();
  113. $actorQuery = $actorMigration->getJoin( 'rc_user' );
  114. $tables += $actorQuery['tables'];
  115. $join_conds += $actorQuery['joins'];
  116. $conds[] = $actorMigration->isAnon( $actorQuery['fields']['rc_user'] );
  117. },
  118. 'isReplacedInStructuredUi' => true,
  119. ],
  120. [
  121. 'name' => 'hideanons',
  122. // rcshowhideanons-show, rcshowhideanons-hide,
  123. // wlshowhideanons
  124. 'showHideSuffix' => 'showhideanons',
  125. 'default' => false,
  126. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  127. &$query_options, &$join_conds
  128. ) {
  129. $actorMigration = ActorMigration::newMigration();
  130. $actorQuery = $actorMigration->getJoin( 'rc_user' );
  131. $tables += $actorQuery['tables'];
  132. $join_conds += $actorQuery['joins'];
  133. $conds[] = $actorMigration->isNotAnon( $actorQuery['fields']['rc_user'] );
  134. },
  135. 'isReplacedInStructuredUi' => true,
  136. ]
  137. ],
  138. ],
  139. [
  140. 'name' => 'userExpLevel',
  141. 'title' => 'rcfilters-filtergroup-userExpLevel',
  142. 'class' => ChangesListStringOptionsFilterGroup::class,
  143. 'isFullCoverage' => true,
  144. 'filters' => [
  145. [
  146. 'name' => 'unregistered',
  147. 'label' => 'rcfilters-filter-user-experience-level-unregistered-label',
  148. 'description' => 'rcfilters-filter-user-experience-level-unregistered-description',
  149. 'cssClassSuffix' => 'user-unregistered',
  150. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  151. return !$rc->getAttribute( 'rc_user' );
  152. }
  153. ],
  154. [
  155. 'name' => 'registered',
  156. 'label' => 'rcfilters-filter-user-experience-level-registered-label',
  157. 'description' => 'rcfilters-filter-user-experience-level-registered-description',
  158. 'cssClassSuffix' => 'user-registered',
  159. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  160. return $rc->getAttribute( 'rc_user' );
  161. }
  162. ],
  163. [
  164. 'name' => 'newcomer',
  165. 'label' => 'rcfilters-filter-user-experience-level-newcomer-label',
  166. 'description' => 'rcfilters-filter-user-experience-level-newcomer-description',
  167. 'cssClassSuffix' => 'user-newcomer',
  168. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  169. $performer = $rc->getPerformer();
  170. return $performer && $performer->isLoggedIn() &&
  171. $performer->getExperienceLevel() === 'newcomer';
  172. }
  173. ],
  174. [
  175. 'name' => 'learner',
  176. 'label' => 'rcfilters-filter-user-experience-level-learner-label',
  177. 'description' => 'rcfilters-filter-user-experience-level-learner-description',
  178. 'cssClassSuffix' => 'user-learner',
  179. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  180. $performer = $rc->getPerformer();
  181. return $performer && $performer->isLoggedIn() &&
  182. $performer->getExperienceLevel() === 'learner';
  183. },
  184. ],
  185. [
  186. 'name' => 'experienced',
  187. 'label' => 'rcfilters-filter-user-experience-level-experienced-label',
  188. 'description' => 'rcfilters-filter-user-experience-level-experienced-description',
  189. 'cssClassSuffix' => 'user-experienced',
  190. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  191. $performer = $rc->getPerformer();
  192. return $performer && $performer->isLoggedIn() &&
  193. $performer->getExperienceLevel() === 'experienced';
  194. },
  195. ]
  196. ],
  197. 'default' => ChangesListStringOptionsFilterGroup::NONE,
  198. 'queryCallable' => [ $this, 'filterOnUserExperienceLevel' ],
  199. ],
  200. [
  201. 'name' => 'authorship',
  202. 'title' => 'rcfilters-filtergroup-authorship',
  203. 'class' => ChangesListBooleanFilterGroup::class,
  204. 'filters' => [
  205. [
  206. 'name' => 'hidemyself',
  207. 'label' => 'rcfilters-filter-editsbyself-label',
  208. 'description' => 'rcfilters-filter-editsbyself-description',
  209. // rcshowhidemine-show, rcshowhidemine-hide,
  210. // wlshowhidemine
  211. 'showHideSuffix' => 'showhidemine',
  212. 'default' => false,
  213. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  214. &$query_options, &$join_conds
  215. ) {
  216. $actorQuery = ActorMigration::newMigration()->getWhere( $dbr, 'rc_user', $ctx->getUser() );
  217. $tables += $actorQuery['tables'];
  218. $join_conds += $actorQuery['joins'];
  219. $conds[] = 'NOT(' . $actorQuery['conds'] . ')';
  220. },
  221. 'cssClassSuffix' => 'self',
  222. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  223. return $ctx->getUser()->equals( $rc->getPerformer() );
  224. },
  225. ],
  226. [
  227. 'name' => 'hidebyothers',
  228. 'label' => 'rcfilters-filter-editsbyother-label',
  229. 'description' => 'rcfilters-filter-editsbyother-description',
  230. 'default' => false,
  231. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  232. &$query_options, &$join_conds
  233. ) {
  234. $actorQuery = ActorMigration::newMigration()
  235. ->getWhere( $dbr, 'rc_user', $ctx->getUser(), false );
  236. $tables += $actorQuery['tables'];
  237. $join_conds += $actorQuery['joins'];
  238. $conds[] = $actorQuery['conds'];
  239. },
  240. 'cssClassSuffix' => 'others',
  241. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  242. return !$ctx->getUser()->equals( $rc->getPerformer() );
  243. },
  244. ]
  245. ]
  246. ],
  247. [
  248. 'name' => 'automated',
  249. 'title' => 'rcfilters-filtergroup-automated',
  250. 'class' => ChangesListBooleanFilterGroup::class,
  251. 'filters' => [
  252. [
  253. 'name' => 'hidebots',
  254. 'label' => 'rcfilters-filter-bots-label',
  255. 'description' => 'rcfilters-filter-bots-description',
  256. // rcshowhidebots-show, rcshowhidebots-hide,
  257. // wlshowhidebots
  258. 'showHideSuffix' => 'showhidebots',
  259. 'default' => false,
  260. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  261. &$query_options, &$join_conds
  262. ) {
  263. $conds['rc_bot'] = 0;
  264. },
  265. 'cssClassSuffix' => 'bot',
  266. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  267. return $rc->getAttribute( 'rc_bot' );
  268. },
  269. ],
  270. [
  271. 'name' => 'hidehumans',
  272. 'label' => 'rcfilters-filter-humans-label',
  273. 'description' => 'rcfilters-filter-humans-description',
  274. 'default' => false,
  275. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  276. &$query_options, &$join_conds
  277. ) {
  278. $conds['rc_bot'] = 1;
  279. },
  280. 'cssClassSuffix' => 'human',
  281. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  282. return !$rc->getAttribute( 'rc_bot' );
  283. },
  284. ]
  285. ]
  286. ],
  287. // significance (conditional)
  288. [
  289. 'name' => 'significance',
  290. 'title' => 'rcfilters-filtergroup-significance',
  291. 'class' => ChangesListBooleanFilterGroup::class,
  292. 'priority' => -6,
  293. 'filters' => [
  294. [
  295. 'name' => 'hideminor',
  296. 'label' => 'rcfilters-filter-minor-label',
  297. 'description' => 'rcfilters-filter-minor-description',
  298. // rcshowhideminor-show, rcshowhideminor-hide,
  299. // wlshowhideminor
  300. 'showHideSuffix' => 'showhideminor',
  301. 'default' => false,
  302. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  303. &$query_options, &$join_conds
  304. ) {
  305. $conds[] = 'rc_minor = 0';
  306. },
  307. 'cssClassSuffix' => 'minor',
  308. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  309. return $rc->getAttribute( 'rc_minor' );
  310. }
  311. ],
  312. [
  313. 'name' => 'hidemajor',
  314. 'label' => 'rcfilters-filter-major-label',
  315. 'description' => 'rcfilters-filter-major-description',
  316. 'default' => false,
  317. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  318. &$query_options, &$join_conds
  319. ) {
  320. $conds[] = 'rc_minor = 1';
  321. },
  322. 'cssClassSuffix' => 'major',
  323. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  324. return !$rc->getAttribute( 'rc_minor' );
  325. }
  326. ]
  327. ]
  328. ],
  329. [
  330. 'name' => 'lastRevision',
  331. 'title' => 'rcfilters-filtergroup-lastRevision',
  332. 'class' => ChangesListBooleanFilterGroup::class,
  333. 'priority' => -7,
  334. 'filters' => [
  335. [
  336. 'name' => 'hidelastrevision',
  337. 'label' => 'rcfilters-filter-lastrevision-label',
  338. 'description' => 'rcfilters-filter-lastrevision-description',
  339. 'default' => false,
  340. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  341. &$query_options, &$join_conds ) use ( $nonRevisionTypes ) {
  342. $conds[] = $dbr->makeList(
  343. [
  344. 'rc_this_oldid <> page_latest',
  345. 'rc_type' => $nonRevisionTypes,
  346. ],
  347. LIST_OR
  348. );
  349. },
  350. 'cssClassSuffix' => 'last',
  351. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  352. return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
  353. }
  354. ],
  355. [
  356. 'name' => 'hidepreviousrevisions',
  357. 'label' => 'rcfilters-filter-previousrevision-label',
  358. 'description' => 'rcfilters-filter-previousrevision-description',
  359. 'default' => false,
  360. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  361. &$query_options, &$join_conds ) use ( $nonRevisionTypes ) {
  362. $conds[] = $dbr->makeList(
  363. [
  364. 'rc_this_oldid = page_latest',
  365. 'rc_type' => $nonRevisionTypes,
  366. ],
  367. LIST_OR
  368. );
  369. },
  370. 'cssClassSuffix' => 'previous',
  371. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  372. return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
  373. }
  374. ]
  375. ]
  376. ],
  377. // With extensions, there can be change types that will not be hidden by any of these.
  378. [
  379. 'name' => 'changeType',
  380. 'title' => 'rcfilters-filtergroup-changetype',
  381. 'class' => ChangesListBooleanFilterGroup::class,
  382. 'priority' => -8,
  383. 'filters' => [
  384. [
  385. 'name' => 'hidepageedits',
  386. 'label' => 'rcfilters-filter-pageedits-label',
  387. 'description' => 'rcfilters-filter-pageedits-description',
  388. 'default' => false,
  389. 'priority' => -2,
  390. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  391. &$query_options, &$join_conds
  392. ) {
  393. $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT );
  394. },
  395. 'cssClassSuffix' => 'src-mw-edit',
  396. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  397. return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_EDIT;
  398. },
  399. ],
  400. [
  401. 'name' => 'hidenewpages',
  402. 'label' => 'rcfilters-filter-newpages-label',
  403. 'description' => 'rcfilters-filter-newpages-description',
  404. 'default' => false,
  405. 'priority' => -3,
  406. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  407. &$query_options, &$join_conds
  408. ) {
  409. $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW );
  410. },
  411. 'cssClassSuffix' => 'src-mw-new',
  412. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  413. return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_NEW;
  414. },
  415. ],
  416. // hidecategorization
  417. [
  418. 'name' => 'hidelog',
  419. 'label' => 'rcfilters-filter-logactions-label',
  420. 'description' => 'rcfilters-filter-logactions-description',
  421. 'default' => false,
  422. 'priority' => -5,
  423. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  424. &$query_options, &$join_conds
  425. ) {
  426. $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG );
  427. },
  428. 'cssClassSuffix' => 'src-mw-log',
  429. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  430. return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_LOG;
  431. }
  432. ],
  433. ],
  434. ],
  435. ];
  436. $this->legacyReviewStatusFilterGroupDefinition = [
  437. [
  438. 'name' => 'legacyReviewStatus',
  439. 'title' => 'rcfilters-filtergroup-reviewstatus',
  440. 'class' => ChangesListBooleanFilterGroup::class,
  441. 'filters' => [
  442. [
  443. 'name' => 'hidepatrolled',
  444. // rcshowhidepatr-show, rcshowhidepatr-hide
  445. // wlshowhidepatr
  446. 'showHideSuffix' => 'showhidepatr',
  447. 'default' => false,
  448. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  449. &$query_options, &$join_conds
  450. ) {
  451. $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
  452. },
  453. 'isReplacedInStructuredUi' => true,
  454. ],
  455. [
  456. 'name' => 'hideunpatrolled',
  457. 'default' => false,
  458. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  459. &$query_options, &$join_conds
  460. ) {
  461. $conds[] = 'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED;
  462. },
  463. 'isReplacedInStructuredUi' => true,
  464. ],
  465. ],
  466. ]
  467. ];
  468. $this->reviewStatusFilterGroupDefinition = [
  469. [
  470. 'name' => 'reviewStatus',
  471. 'title' => 'rcfilters-filtergroup-reviewstatus',
  472. 'class' => ChangesListStringOptionsFilterGroup::class,
  473. 'isFullCoverage' => true,
  474. 'priority' => -5,
  475. 'filters' => [
  476. [
  477. 'name' => 'unpatrolled',
  478. 'label' => 'rcfilters-filter-reviewstatus-unpatrolled-label',
  479. 'description' => 'rcfilters-filter-reviewstatus-unpatrolled-description',
  480. 'cssClassSuffix' => 'reviewstatus-unpatrolled',
  481. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  482. return $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_UNPATROLLED;
  483. },
  484. ],
  485. [
  486. 'name' => 'manual',
  487. 'label' => 'rcfilters-filter-reviewstatus-manual-label',
  488. 'description' => 'rcfilters-filter-reviewstatus-manual-description',
  489. 'cssClassSuffix' => 'reviewstatus-manual',
  490. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  491. return $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_PATROLLED;
  492. },
  493. ],
  494. [
  495. 'name' => 'auto',
  496. 'label' => 'rcfilters-filter-reviewstatus-auto-label',
  497. 'description' => 'rcfilters-filter-reviewstatus-auto-description',
  498. 'cssClassSuffix' => 'reviewstatus-auto',
  499. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  500. return $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_AUTOPATROLLED;
  501. },
  502. ],
  503. ],
  504. 'default' => ChangesListStringOptionsFilterGroup::NONE,
  505. 'queryCallable' => function ( $specialPageClassName, $ctx, $dbr,
  506. &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selected
  507. ) {
  508. if ( $selected === [] ) {
  509. return;
  510. }
  511. $rcPatrolledValues = [
  512. 'unpatrolled' => RecentChange::PRC_UNPATROLLED,
  513. 'manual' => RecentChange::PRC_PATROLLED,
  514. 'auto' => RecentChange::PRC_AUTOPATROLLED,
  515. ];
  516. // e.g. rc_patrolled IN (0, 2)
  517. $conds['rc_patrolled'] = array_map( function ( $s ) use ( $rcPatrolledValues ) {
  518. return $rcPatrolledValues[ $s ];
  519. }, $selected );
  520. }
  521. ]
  522. ];
  523. $this->hideCategorizationFilterDefinition = [
  524. 'name' => 'hidecategorization',
  525. 'label' => 'rcfilters-filter-categorization-label',
  526. 'description' => 'rcfilters-filter-categorization-description',
  527. // rcshowhidecategorization-show, rcshowhidecategorization-hide.
  528. // wlshowhidecategorization
  529. 'showHideSuffix' => 'showhidecategorization',
  530. 'default' => false,
  531. 'priority' => -4,
  532. 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
  533. &$query_options, &$join_conds
  534. ) {
  535. $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
  536. },
  537. 'cssClassSuffix' => 'src-mw-categorize',
  538. 'isRowApplicableCallable' => function ( $ctx, $rc ) {
  539. return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_CATEGORIZE;
  540. },
  541. ];
  542. }
  543. /**
  544. * Check if filters are in conflict and guaranteed to return no results.
  545. *
  546. * @return bool
  547. */
  548. protected function areFiltersInConflict() {
  549. $opts = $this->getOptions();
  550. /** @var ChangesListFilterGroup $group */
  551. foreach ( $this->getFilterGroups() as $group ) {
  552. if ( $group->getConflictingGroups() ) {
  553. wfLogWarning(
  554. $group->getName() .
  555. " specifies conflicts with other groups but these are not supported yet."
  556. );
  557. }
  558. /** @var ChangesListFilter $conflictingFilter */
  559. foreach ( $group->getConflictingFilters() as $conflictingFilter ) {
  560. if ( $conflictingFilter->activelyInConflictWithGroup( $group, $opts ) ) {
  561. return true;
  562. }
  563. }
  564. /** @var ChangesListFilter $filter */
  565. foreach ( $group->getFilters() as $filter ) {
  566. /** @var ChangesListFilter $conflictingFilter */
  567. foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
  568. if (
  569. $conflictingFilter->activelyInConflictWithFilter( $filter, $opts ) &&
  570. $filter->activelyInConflictWithFilter( $conflictingFilter, $opts )
  571. ) {
  572. return true;
  573. }
  574. }
  575. }
  576. }
  577. return false;
  578. }
  579. /**
  580. * Main execution point
  581. *
  582. * @param string $subpage
  583. */
  584. public function execute( $subpage ) {
  585. $this->rcSubpage = $subpage;
  586. $this->considerActionsForDefaultSavedQuery( $subpage );
  587. $opts = $this->getOptions();
  588. try {
  589. $rows = $this->getRows();
  590. if ( $rows === false ) {
  591. $rows = new FakeResultWrapper( [] );
  592. }
  593. // Used by Structured UI app to get results without MW chrome
  594. if ( $this->getRequest()->getVal( 'action' ) === 'render' ) {
  595. $this->getOutput()->setArticleBodyOnly( true );
  596. }
  597. // Used by "live update" and "view newest" to check
  598. // if there's new changes with minimal data transfer
  599. if ( $this->getRequest()->getBool( 'peek' ) ) {
  600. $code = $rows->numRows() > 0 ? 200 : 204;
  601. $this->getOutput()->setStatusCode( $code );
  602. if ( $this->getUser()->isAnon() !==
  603. $this->getRequest()->getFuzzyBool( 'isAnon' )
  604. ) {
  605. $this->getOutput()->setStatusCode( 205 );
  606. }
  607. return;
  608. }
  609. $batch = new LinkBatch;
  610. foreach ( $rows as $row ) {
  611. $batch->add( NS_USER, $row->rc_user_text );
  612. $batch->add( NS_USER_TALK, $row->rc_user_text );
  613. $batch->add( $row->rc_namespace, $row->rc_title );
  614. if ( $row->rc_source === RecentChange::SRC_LOG ) {
  615. $formatter = LogFormatter::newFromRow( $row );
  616. foreach ( $formatter->getPreloadTitles() as $title ) {
  617. $batch->addObj( $title );
  618. }
  619. }
  620. }
  621. $batch->execute();
  622. $this->setHeaders();
  623. $this->outputHeader();
  624. $this->addModules();
  625. $this->webOutput( $rows, $opts );
  626. $rows->free();
  627. } catch ( DBQueryTimeoutError $timeoutException ) {
  628. MWExceptionHandler::logException( $timeoutException );
  629. $this->setHeaders();
  630. $this->outputHeader();
  631. $this->addModules();
  632. $this->getOutput()->setStatusCode( 500 );
  633. $this->webOutputHeader( 0, $opts );
  634. $this->outputTimeout();
  635. }
  636. if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
  637. // Clean up any bad page entries for titles showing up in RC
  638. DeferredUpdates::addUpdate( new WANCacheReapUpdate(
  639. $this->getDB(),
  640. LoggerFactory::getInstance( 'objectcache' )
  641. ) );
  642. }
  643. $this->includeRcFiltersApp();
  644. }
  645. /**
  646. * Check whether or not the page should load defaults, and if so, whether
  647. * a default saved query is relevant to be redirected to. If it is relevant,
  648. * redirect properly with all necessary query parameters.
  649. *
  650. * @param string $subpage
  651. */
  652. protected function considerActionsForDefaultSavedQuery( $subpage ) {
  653. if ( !$this->isStructuredFilterUiEnabled() || $this->including() ) {
  654. return;
  655. }
  656. $knownParams = call_user_func_array(
  657. [ $this->getRequest(), 'getValues' ],
  658. array_keys( $this->getOptions()->getAllValues() )
  659. );
  660. // HACK: Temporarily until we can properly define "sticky" filters and parameters,
  661. // we need to exclude several parameters we know should not be counted towards preventing
  662. // the loading of defaults.
  663. $excludedParams = [ 'limit' => '', 'days' => '', 'enhanced' => '', 'from' => '' ];
  664. $knownParams = array_diff_key( $knownParams, $excludedParams );
  665. if (
  666. // If there are NO known parameters in the URL request
  667. // (that are not excluded) then we need to check into loading
  668. // the default saved query
  669. count( $knownParams ) === 0
  670. ) {
  671. // Get the saved queries data and parse it
  672. $savedQueries = FormatJson::decode(
  673. $this->getUser()->getOption( static::$savedQueriesPreferenceName ),
  674. true
  675. );
  676. if ( $savedQueries && isset( $savedQueries[ 'default' ] ) ) {
  677. // Only load queries that are 'version' 2, since those
  678. // have parameter representation
  679. if ( isset( $savedQueries[ 'version' ] ) && $savedQueries[ 'version' ] === '2' ) {
  680. $savedQueryDefaultID = $savedQueries[ 'default' ];
  681. $defaultQuery = $savedQueries[ 'queries' ][ $savedQueryDefaultID ][ 'data' ];
  682. // Build the entire parameter list
  683. $query = array_merge(
  684. $defaultQuery[ 'params' ],
  685. $defaultQuery[ 'highlights' ],
  686. [
  687. 'urlversion' => '2',
  688. ]
  689. );
  690. // Add to the query any parameters that we may have ignored before
  691. // but are still valid and requested in the URL
  692. $query = array_merge( $this->getRequest()->getValues(), $query );
  693. unset( $query[ 'title' ] );
  694. $this->getOutput()->redirect( $this->getPageTitle( $subpage )->getCanonicalURL( $query ) );
  695. } else {
  696. // There's a default, but the version is not 2, and the server can't
  697. // actually recognize the query itself. This happens if it is before
  698. // the conversion, so we need to tell the UI to reload saved query as
  699. // it does the conversion to version 2
  700. $this->getOutput()->addJsConfigVars(
  701. 'wgStructuredChangeFiltersDefaultSavedQueryExists',
  702. true
  703. );
  704. // Add the class that tells the frontend it is still loading
  705. // another query
  706. $this->getOutput()->addBodyClasses( 'mw-rcfilters-ui-loading' );
  707. }
  708. }
  709. }
  710. }
  711. /**
  712. * Include the modules and configuration for the RCFilters app.
  713. * Conditional on the user having the feature enabled.
  714. *
  715. * If it is disabled, add a <body> class marking that
  716. */
  717. protected function includeRcFiltersApp() {
  718. $out = $this->getOutput();
  719. if ( $this->isStructuredFilterUiEnabled() && !$this->including() ) {
  720. $jsData = $this->getStructuredFilterJsData();
  721. $messages = [];
  722. foreach ( $jsData['messageKeys'] as $key ) {
  723. $messages[$key] = $this->msg( $key )->plain();
  724. }
  725. $out->addBodyClasses( 'mw-rcfilters-enabled' );
  726. $out->addHTML(
  727. ResourceLoader::makeInlineScript(
  728. ResourceLoader::makeMessageSetScript( $messages )
  729. )
  730. );
  731. $out->addJsConfigVars( 'wgStructuredChangeFilters', $jsData['groups'] );
  732. $out->addJsConfigVars(
  733. 'wgRCFiltersChangeTags',
  734. $this->getChangeTagList()
  735. );
  736. $out->addJsConfigVars(
  737. 'StructuredChangeFiltersDisplayConfig',
  738. [
  739. 'maxDays' => (int)$this->getConfig()->get( 'RCMaxAge' ) / ( 24 * 3600 ), // Translate to days
  740. 'limitArray' => $this->getConfig()->get( 'RCLinkLimits' ),
  741. 'limitDefault' => $this->getDefaultLimit(),
  742. 'daysArray' => $this->getConfig()->get( 'RCLinkDays' ),
  743. 'daysDefault' => $this->getDefaultDays(),
  744. ]
  745. );
  746. $out->addJsConfigVars(
  747. 'wgStructuredChangeFiltersSavedQueriesPreferenceName',
  748. static::$savedQueriesPreferenceName
  749. );
  750. $out->addJsConfigVars(
  751. 'wgStructuredChangeFiltersLimitPreferenceName',
  752. static::$limitPreferenceName
  753. );
  754. $out->addJsConfigVars(
  755. 'wgStructuredChangeFiltersDaysPreferenceName',
  756. static::$daysPreferenceName
  757. );
  758. $out->addJsConfigVars(
  759. 'StructuredChangeFiltersLiveUpdatePollingRate',
  760. $this->getConfig()->get( 'StructuredChangeFiltersLiveUpdatePollingRate' )
  761. );
  762. } else {
  763. $out->addBodyClasses( 'mw-rcfilters-disabled' );
  764. }
  765. }
  766. /**
  767. * Fetch the change tags list for the front end
  768. *
  769. * @return Array Tag data
  770. */
  771. protected function getChangeTagList() {
  772. $cache = ObjectCache::getMainWANInstance();
  773. $context = $this->getContext();
  774. return $cache->getWithSetCallback(
  775. $cache->makeKey( 'changeslistspecialpage-changetags', $context->getLanguage()->getCode() ),
  776. $cache::TTL_MINUTE * 10,
  777. function () use ( $context ) {
  778. $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
  779. $softwareActivatedTags = array_fill_keys( ChangeTags::listSoftwareActivatedTags(), 0 );
  780. // Hit counts disabled for perf reasons, see T169997
  781. /*
  782. $tagStats = ChangeTags::tagUsageStatistics();
  783. $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags, $tagStats );
  784. // Sort by hits
  785. arsort( $tagHitCounts );
  786. */
  787. $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags );
  788. // Build the list and data
  789. $result = [];
  790. foreach ( $tagHitCounts as $tagName => $hits ) {
  791. if (
  792. // Only get active tags
  793. isset( $explicitlyDefinedTags[ $tagName ] ) ||
  794. isset( $softwareActivatedTags[ $tagName ] )
  795. ) {
  796. $result[] = [
  797. 'name' => $tagName,
  798. 'label' => Sanitizer::stripAllTags(
  799. ChangeTags::tagDescription( $tagName, $context )
  800. ),
  801. 'description' =>
  802. ChangeTags::truncateTagDescription(
  803. $tagName, self::TAG_DESC_CHARACTER_LIMIT, $context
  804. ),
  805. 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
  806. 'hits' => $hits,
  807. ];
  808. }
  809. }
  810. // Instead of sorting by hit count (disabled, see above), sort by display name
  811. usort( $result, function ( $a, $b ) {
  812. return strcasecmp( $a['label'], $b['label'] );
  813. } );
  814. return $result;
  815. },
  816. [
  817. 'lockTSE' => 30
  818. ]
  819. );
  820. }
  821. /**
  822. * Add the "no results" message to the output
  823. */
  824. protected function outputNoResults() {
  825. $this->getOutput()->addHTML(
  826. '<div class="mw-changeslist-empty">' .
  827. $this->msg( 'recentchanges-noresult' )->parse() .
  828. '</div>'
  829. );
  830. }
  831. /**
  832. * Add the "timeout" message to the output
  833. */
  834. protected function outputTimeout() {
  835. $this->getOutput()->addHTML(
  836. '<div class="mw-changeslist-empty mw-changeslist-timeout">' .
  837. $this->msg( 'recentchanges-timeout' )->parse() .
  838. '</div>'
  839. );
  840. }
  841. /**
  842. * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
  843. *
  844. * @return bool|IResultWrapper Result or false
  845. */
  846. public function getRows() {
  847. $opts = $this->getOptions();
  848. $tables = [];
  849. $fields = [];
  850. $conds = [];
  851. $query_options = [];
  852. $join_conds = [];
  853. $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
  854. return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
  855. }
  856. /**
  857. * Get the current FormOptions for this request
  858. *
  859. * @return FormOptions
  860. */
  861. public function getOptions() {
  862. if ( $this->rcOptions === null ) {
  863. $this->rcOptions = $this->setup( $this->rcSubpage );
  864. }
  865. return $this->rcOptions;
  866. }
  867. /**
  868. * Register all filters and their groups (including those from hooks), plus handle
  869. * conflicts and defaults.
  870. *
  871. * You might want to customize these in the same method, in subclasses. You can
  872. * call getFilterGroup to access a group, and (on the group) getFilter to access a
  873. * filter, then make necessary modfications to the filter or group (e.g. with
  874. * setDefault).
  875. */
  876. protected function registerFilters() {
  877. $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions );
  878. // Make sure this is not being transcluded (we don't want to show this
  879. // information to all users just because the user that saves the edit can
  880. // patrol or is logged in)
  881. if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
  882. $this->registerFiltersFromDefinitions( $this->legacyReviewStatusFilterGroupDefinition );
  883. $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition );
  884. }
  885. $changeTypeGroup = $this->getFilterGroup( 'changeType' );
  886. if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
  887. $transformedHideCategorizationDef = $this->transformFilterDefinition(
  888. $this->hideCategorizationFilterDefinition
  889. );
  890. $transformedHideCategorizationDef['group'] = $changeTypeGroup;
  891. $hideCategorization = new ChangesListBooleanFilter(
  892. $transformedHideCategorizationDef
  893. );
  894. }
  895. Hooks::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
  896. $unstructuredGroupDefinition =
  897. $this->getFilterGroupDefinitionFromLegacyCustomFilters(
  898. $this->getCustomFilters()
  899. );
  900. $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
  901. $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
  902. $registered = $userExperienceLevel->getFilter( 'registered' );
  903. $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'newcomer' ) );
  904. $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'learner' ) );
  905. $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'experienced' ) );
  906. $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
  907. $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
  908. $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
  909. $significanceTypeGroup = $this->getFilterGroup( 'significance' );
  910. $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
  911. // categoryFilter is conditional; see registerFilters
  912. if ( $categoryFilter !== null ) {
  913. $hideMinorFilter->conflictsWith(
  914. $categoryFilter,
  915. 'rcfilters-hideminor-conflicts-typeofchange-global',
  916. 'rcfilters-hideminor-conflicts-typeofchange',
  917. 'rcfilters-typeofchange-conflicts-hideminor'
  918. );
  919. }
  920. $hideMinorFilter->conflictsWith(
  921. $logactionsFilter,
  922. 'rcfilters-hideminor-conflicts-typeofchange-global',
  923. 'rcfilters-hideminor-conflicts-typeofchange',
  924. 'rcfilters-typeofchange-conflicts-hideminor'
  925. );
  926. $hideMinorFilter->conflictsWith(
  927. $pagecreationFilter,
  928. 'rcfilters-hideminor-conflicts-typeofchange-global',
  929. 'rcfilters-hideminor-conflicts-typeofchange',
  930. 'rcfilters-typeofchange-conflicts-hideminor'
  931. );
  932. }
  933. /**
  934. * Transforms filter definition to prepare it for constructor.
  935. *
  936. * See overrides of this method as well.
  937. *
  938. * @param array $filterDefinition Original filter definition
  939. *
  940. * @return array Transformed definition
  941. */
  942. protected function transformFilterDefinition( array $filterDefinition ) {
  943. return $filterDefinition;
  944. }
  945. /**
  946. * Register filters from a definition object
  947. *
  948. * Array specifying groups and their filters; see Filter and
  949. * ChangesListFilterGroup constructors.
  950. *
  951. * There is light processing to simplify core maintenance.
  952. * @param array $definition
  953. */
  954. protected function registerFiltersFromDefinitions( array $definition ) {
  955. $autoFillPriority = -1;
  956. foreach ( $definition as $groupDefinition ) {
  957. if ( !isset( $groupDefinition['priority'] ) ) {
  958. $groupDefinition['priority'] = $autoFillPriority;
  959. } else {
  960. // If it's explicitly specified, start over the auto-fill
  961. $autoFillPriority = $groupDefinition['priority'];
  962. }
  963. $autoFillPriority--;
  964. $className = $groupDefinition['class'];
  965. unset( $groupDefinition['class'] );
  966. foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
  967. $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
  968. }
  969. $this->registerFilterGroup( new $className( $groupDefinition ) );
  970. }
  971. }
  972. /**
  973. * Get filter group definition from legacy custom filters
  974. *
  975. * @param array $customFilters Custom filters from legacy hooks
  976. * @return array Group definition
  977. */
  978. protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
  979. // Special internal unstructured group
  980. $unstructuredGroupDefinition = [
  981. 'name' => 'unstructured',
  982. 'class' => ChangesListBooleanFilterGroup::class,
  983. 'priority' => -1, // Won't display in structured
  984. 'filters' => [],
  985. ];
  986. foreach ( $customFilters as $name => $params ) {
  987. $unstructuredGroupDefinition['filters'][] = [
  988. 'name' => $name,
  989. 'showHide' => $params['msg'],
  990. 'default' => $params['default'],
  991. ];
  992. }
  993. return $unstructuredGroupDefinition;
  994. }
  995. /**
  996. * @return array The legacy show/hide toggle filters
  997. */
  998. protected function getLegacyShowHideFilters() {
  999. $filters = [];
  1000. foreach ( $this->filterGroups as $group ) {
  1001. if ( $group instanceof ChangesListBooleanFilterGroup ) {
  1002. foreach ( $group->getFilters() as $key => $filter ) {
  1003. if ( $filter->displaysOnUnstructuredUi( $this ) ) {
  1004. $filters[ $key ] = $filter;
  1005. }
  1006. }
  1007. }
  1008. }
  1009. return $filters;
  1010. }
  1011. /**
  1012. * Register all the filters, including legacy hook-driven ones.
  1013. * Then create a FormOptions object with options as specified by the user
  1014. *
  1015. * @param array $parameters
  1016. *
  1017. * @return FormOptions
  1018. */
  1019. public function setup( $parameters ) {
  1020. $this->registerFilters();
  1021. $opts = $this->getDefaultOptions();
  1022. $opts = $this->fetchOptionsFromRequest( $opts );
  1023. // Give precedence to subpage syntax
  1024. if ( $parameters !== null ) {
  1025. $this->parseParameters( $parameters, $opts );
  1026. }
  1027. $this->validateOptions( $opts );
  1028. return $opts;
  1029. }
  1030. /**
  1031. * Get a FormOptions object containing the default options. By default, returns
  1032. * some basic options. The filters listed explicitly here are overriden in this
  1033. * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
  1034. * and more) are structured. Structured filters are overriden in registerFilters.
  1035. * not here.
  1036. *
  1037. * @return FormOptions
  1038. */
  1039. public function getDefaultOptions() {
  1040. $opts = new FormOptions();
  1041. $structuredUI = $this->isStructuredFilterUiEnabled();
  1042. // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
  1043. $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
  1044. /** @var ChangesListFilterGroup $filterGroup */
  1045. foreach ( $this->filterGroups as $filterGroup ) {
  1046. $filterGroup->addOptions( $opts, $useDefaults, $structuredUI );
  1047. }
  1048. $opts->add( 'namespace', '', FormOptions::STRING );
  1049. $opts->add( 'invert', false );
  1050. $opts->add( 'associated', false );
  1051. $opts->add( 'urlversion', 1 );
  1052. $opts->add( 'tagfilter', '' );
  1053. $opts->add( 'days', $this->getDefaultDays(), FormOptions::FLOAT );
  1054. $opts->add( 'limit', $this->getDefaultLimit(), FormOptions::INT );
  1055. $opts->add( 'from', '' );
  1056. return $opts;
  1057. }
  1058. /**
  1059. * Register a structured changes list filter group
  1060. *
  1061. * @param ChangesListFilterGroup $group
  1062. */
  1063. public function registerFilterGroup( ChangesListFilterGroup $group ) {
  1064. $groupName = $group->getName();
  1065. $this->filterGroups[$groupName] = $group;
  1066. }
  1067. /**
  1068. * Gets the currently registered filters groups
  1069. *
  1070. * @return array Associative array of ChangesListFilterGroup objects, with group name as key
  1071. */
  1072. protected function getFilterGroups() {
  1073. return $this->filterGroups;
  1074. }
  1075. /**
  1076. * Gets a specified ChangesListFilterGroup by name
  1077. *
  1078. * @param string $groupName Name of group
  1079. *
  1080. * @return ChangesListFilterGroup|null Group, or null if not registered
  1081. */
  1082. public function getFilterGroup( $groupName ) {
  1083. return isset( $this->filterGroups[$groupName] ) ?
  1084. $this->filterGroups[$groupName] :
  1085. null;
  1086. }
  1087. // Currently, this intentionally only includes filters that display
  1088. // in the structured UI. This can be changed easily, though, if we want
  1089. // to include data on filters that use the unstructured UI. messageKeys is a
  1090. // special top-level value, with the value being an array of the message keys to
  1091. // send to the client.
  1092. /**
  1093. * Gets structured filter information needed by JS
  1094. *
  1095. * @return array Associative array
  1096. * * array $return['groups'] Group data
  1097. * * array $return['messageKeys'] Array of message keys
  1098. */
  1099. public function getStructuredFilterJsData() {
  1100. $output = [
  1101. 'groups' => [],
  1102. 'messageKeys' => [],
  1103. ];
  1104. usort( $this->filterGroups, function ( $a, $b ) {
  1105. return $b->getPriority() - $a->getPriority();
  1106. } );
  1107. foreach ( $this->filterGroups as $groupName => $group ) {
  1108. $groupOutput = $group->getJsData( $this );
  1109. if ( $groupOutput !== null ) {
  1110. $output['messageKeys'] = array_merge(
  1111. $output['messageKeys'],
  1112. $groupOutput['messageKeys']
  1113. );
  1114. unset( $groupOutput['messageKeys'] );
  1115. $output['groups'][] = $groupOutput;
  1116. }
  1117. }
  1118. return $output;
  1119. }
  1120. /**
  1121. * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
  1122. * hook.
  1123. *
  1124. * @return array Map of filter URL param names to properties (msg/default)
  1125. */
  1126. protected function getCustomFilters() {
  1127. if ( $this->customFilters === null ) {
  1128. $this->customFilters = [];
  1129. Hooks::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters ], '1.29' );
  1130. }
  1131. return $this->customFilters;
  1132. }
  1133. /**
  1134. * Fetch values for a FormOptions object from the WebRequest associated with this instance.
  1135. *
  1136. * Intended for subclassing, e.g. to add a backwards-compatibility layer.
  1137. *
  1138. * @param FormOptions $opts
  1139. * @return FormOptions
  1140. */
  1141. protected function fetchOptionsFromRequest( $opts ) {
  1142. $opts->fetchValuesFromRequest( $this->getRequest() );
  1143. return $opts;
  1144. }
  1145. /**
  1146. * Process $par and put options found in $opts. Used when including the page.
  1147. *
  1148. * @param string $par
  1149. * @param FormOptions $opts
  1150. */
  1151. public function parseParameters( $par, FormOptions $opts ) {
  1152. $stringParameterNameSet = [];
  1153. $hideParameterNameSet = [];
  1154. // URL parameters can be per-group, like 'userExpLevel',
  1155. // or per-filter, like 'hideminor'.
  1156. foreach ( $this->filterGroups as $filterGroup ) {
  1157. if ( $filterGroup instanceof ChangesListStringOptionsFilterGroup ) {
  1158. $stringParameterNameSet[$filterGroup->getName()] = true;
  1159. } elseif ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
  1160. foreach ( $filterGroup->getFilters() as $filter ) {
  1161. $hideParameterNameSet[$filter->getName()] = true;
  1162. }
  1163. }
  1164. }
  1165. $bits = preg_split( '/\s*,\s*/', trim( $par ) );
  1166. foreach ( $bits as $bit ) {
  1167. $m = [];
  1168. if ( isset( $hideParameterNameSet[$bit] ) ) {
  1169. // hidefoo => hidefoo=true
  1170. $opts[$bit] = true;
  1171. } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
  1172. // foo => hidefoo=false
  1173. $opts["hide$bit"] = false;
  1174. } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
  1175. if ( isset( $stringParameterNameSet[$m[1]] ) ) {
  1176. $opts[$m[1]] = $m[2];
  1177. }
  1178. }
  1179. }
  1180. }
  1181. /**
  1182. * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
  1183. *
  1184. * @param FormOptions $opts
  1185. */
  1186. public function validateOptions( FormOptions $opts ) {
  1187. $isContradictory = $this->fixContradictoryOptions( $opts );
  1188. $isReplaced = $this->replaceOldOptions( $opts );
  1189. if ( $isContradictory || $isReplaced ) {
  1190. $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
  1191. $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
  1192. }
  1193. $opts->validateIntBounds( 'limit', 0, 5000 );
  1194. $opts->validateBounds( 'days', 0, $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
  1195. }
  1196. /**
  1197. * Fix invalid options by resetting pairs that should never appear together.
  1198. *
  1199. * @param FormOptions $opts
  1200. * @return bool True if any option was reset
  1201. */
  1202. private function fixContradictoryOptions( FormOptions $opts ) {
  1203. $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
  1204. foreach ( $this->filterGroups as $filterGroup ) {
  1205. if ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
  1206. $filters = $filterGroup->getFilters();
  1207. if ( count( $filters ) === 1 ) {
  1208. // legacy boolean filters should not be considered
  1209. continue;
  1210. }
  1211. $allInGroupEnabled = array_reduce(
  1212. $filters,
  1213. function ( $carry, $filter ) use ( $opts ) {
  1214. return $carry && $opts[ $filter->getName() ];
  1215. },
  1216. /* initialValue */ count( $filters ) > 0
  1217. );
  1218. if ( $allInGroupEnabled ) {
  1219. foreach ( $filters as $filter ) {
  1220. $opts[ $filter->getName() ] = false;
  1221. }
  1222. $fixed = true;
  1223. }
  1224. }
  1225. }
  1226. return $fixed;
  1227. }
  1228. /**
  1229. * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
  1230. * compatibility.
  1231. *
  1232. * This is deprecated and may be removed.
  1233. *
  1234. * @param FormOptions $opts
  1235. * @return bool True if this change was mode
  1236. */
  1237. private function fixBackwardsCompatibilityOptions( FormOptions $opts ) {
  1238. if ( $opts['hideanons'] && $opts['hideliu'] ) {
  1239. $opts->reset( 'hideanons' );
  1240. if ( !$opts['hidebots'] ) {
  1241. $opts->reset( 'hideliu' );
  1242. $opts['hidehumans'] = 1;
  1243. }
  1244. return true;
  1245. }
  1246. return false;
  1247. }
  1248. /**
  1249. * Replace old options with their structured UI equivalents
  1250. *
  1251. * @param FormOptions $opts
  1252. * @return bool True if the change was made
  1253. */
  1254. public function replaceOldOptions( FormOptions $opts ) {
  1255. if ( !$this->isStructuredFilterUiEnabled() ) {
  1256. return false;
  1257. }
  1258. $changed = false;
  1259. // At this point 'hideanons' and 'hideliu' cannot be both true,
  1260. // because fixBackwardsCompatibilityOptions resets (at least) 'hideanons' in such case
  1261. if ( $opts[ 'hideanons' ] ) {
  1262. $opts->reset( 'hideanons' );
  1263. $opts[ 'userExpLevel' ] = 'registered';
  1264. $changed = true;
  1265. }
  1266. if ( $opts[ 'hideliu' ] ) {
  1267. $opts->reset( 'hideliu' );
  1268. $opts[ 'userExpLevel' ] = 'unregistered';
  1269. $changed = true;
  1270. }
  1271. if ( $this->getFilterGroup( 'legacyReviewStatus' ) ) {
  1272. if ( $opts[ 'hidepatrolled' ] ) {
  1273. $opts->reset( 'hidepatrolled' );
  1274. $opts[ 'reviewStatus' ] = 'unpatrolled';
  1275. $changed = true;
  1276. }
  1277. if ( $opts[ 'hideunpatrolled' ] ) {
  1278. $opts->reset( 'hideunpatrolled' );
  1279. $opts[ 'reviewStatus' ] = implode(
  1280. ChangesListStringOptionsFilterGroup::SEPARATOR,
  1281. [ 'manual', 'auto' ]
  1282. );
  1283. $changed = true;
  1284. }
  1285. }
  1286. return $changed;
  1287. }
  1288. /**
  1289. * Convert parameters values from true/false to 1/0
  1290. * so they are not omitted by wfArrayToCgi()
  1291. * Bug 36524
  1292. *
  1293. * @param array $params
  1294. * @return array
  1295. */
  1296. protected function convertParamsForLink( $params ) {
  1297. foreach ( $params as &$value ) {
  1298. if ( $value === false ) {
  1299. $value = '0';
  1300. }
  1301. }
  1302. unset( $value );
  1303. return $params;
  1304. }
  1305. /**
  1306. * Sets appropriate tables, fields, conditions, etc. depending on which filters
  1307. * the user requested.
  1308. *
  1309. * @param array &$tables Array of tables; see IDatabase::select $table
  1310. * @param array &$fields Array of fields; see IDatabase::select $vars
  1311. * @param array &$conds Array of conditions; see IDatabase::select $conds
  1312. * @param array &$query_options Array of query options; see IDatabase::select $options
  1313. * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
  1314. * @param FormOptions $opts
  1315. */
  1316. protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
  1317. &$join_conds, FormOptions $opts
  1318. ) {
  1319. $dbr = $this->getDB();
  1320. $isStructuredUI = $this->isStructuredFilterUiEnabled();
  1321. /** @var ChangesListFilterGroup $filterGroup */
  1322. foreach ( $this->filterGroups as $filterGroup ) {
  1323. $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
  1324. $query_options, $join_conds, $opts, $isStructuredUI );
  1325. }
  1326. // Namespace filtering
  1327. if ( $opts[ 'namespace' ] !== '' ) {
  1328. $namespaces = explode( ';', $opts[ 'namespace' ] );
  1329. if ( $opts[ 'associated' ] ) {
  1330. $associatedNamespaces = array_map(
  1331. function ( $ns ) {
  1332. return MWNamespace::getAssociated( $ns );
  1333. },
  1334. $namespaces
  1335. );
  1336. $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
  1337. }
  1338. if ( count( $namespaces ) === 1 ) {
  1339. $operator = $opts[ 'invert' ] ? '!=' : '=';
  1340. $value = $dbr->addQuotes( reset( $namespaces ) );
  1341. } else {
  1342. $operator = $opts[ 'invert' ] ? 'NOT IN' : 'IN';
  1343. sort( $namespaces );
  1344. $value = '(' . $dbr->makeList( $namespaces ) . ')';
  1345. }
  1346. $conds[] = "rc_namespace $operator $value";
  1347. }
  1348. // Calculate cutoff
  1349. $cutoff_unixtime = time() - $opts['days'] * 3600 * 24;
  1350. $cutoff = $dbr->timestamp( $cutoff_unixtime );
  1351. $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
  1352. if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW, $cutoff ) ) {
  1353. $cutoff = $dbr->timestamp( $opts['from'] );
  1354. } else {
  1355. $opts->reset( 'from' );
  1356. }
  1357. $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
  1358. }
  1359. /**
  1360. * Process the query
  1361. *
  1362. * @param array $tables Array of tables; see IDatabase::select $table
  1363. * @param array $fields Array of fields; see IDatabase::select $vars
  1364. * @param array $conds Array of conditions; see IDatabase::select $conds
  1365. * @param array $query_options Array of query options; see IDatabase::select $options
  1366. * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
  1367. * @param FormOptions $opts
  1368. * @return bool|IResultWrapper Result or false
  1369. */
  1370. protected function doMainQuery( $tables, $fields, $conds,
  1371. $query_options, $join_conds, FormOptions $opts
  1372. ) {
  1373. $rcQuery = RecentChange::getQueryInfo();
  1374. $tables = array_merge( $tables, $rcQuery['tables'] );
  1375. $fields = array_merge( $rcQuery['fields'], $fields );
  1376. $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
  1377. ChangeTags::modifyDisplayQuery(
  1378. $tables,
  1379. $fields,
  1380. $conds,
  1381. $join_conds,
  1382. $query_options,
  1383. ''
  1384. );
  1385. if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
  1386. $opts )
  1387. ) {
  1388. return false;
  1389. }
  1390. $dbr = $this->getDB();
  1391. return $dbr->select(
  1392. $tables,
  1393. $fields,
  1394. $conds,
  1395. __METHOD__,
  1396. $query_options,
  1397. $join_conds
  1398. );
  1399. }
  1400. protected function runMainQueryHook( &$tables, &$fields, &$conds,
  1401. &$query_options, &$join_conds, $opts
  1402. ) {
  1403. return Hooks::run(
  1404. 'ChangesListSpecialPageQuery',
  1405. [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
  1406. );
  1407. }
  1408. /**
  1409. * Return a IDatabase object for reading
  1410. *
  1411. * @return IDatabase
  1412. */
  1413. protected function getDB() {
  1414. return wfGetDB( DB_REPLICA );
  1415. }
  1416. /**
  1417. * Send header output to the OutputPage object, only called if not using feeds
  1418. *
  1419. * @param int $rowCount Number of database rows
  1420. * @param FormOptions $opts
  1421. */
  1422. private function webOutputHeader( $rowCount, $opts ) {
  1423. if ( !$this->including() ) {
  1424. $this->outputFeedLinks();
  1425. $this->doHeader( $opts, $rowCount );
  1426. }
  1427. }
  1428. /**
  1429. * Send output to the OutputPage object, only called if not used feeds
  1430. *
  1431. * @param IResultWrapper $rows Database rows
  1432. * @param FormOptions $opts
  1433. */
  1434. public function webOutput( $rows, $opts ) {
  1435. $this->webOutputHeader( $rows->numRows(), $opts );
  1436. $this->outputChangesList( $rows, $opts );
  1437. }
  1438. /**
  1439. * Output feed links.
  1440. */
  1441. public function outputFeedLinks() {
  1442. // nothing by default
  1443. }
  1444. /**
  1445. * Build and output the actual changes list.
  1446. *
  1447. * @param IResultWrapper $rows Database rows
  1448. * @param FormOptions $opts
  1449. */
  1450. abstract public function outputChangesList( $rows, $opts );
  1451. /**
  1452. * Set the text to be displayed above the changes
  1453. *
  1454. * @param FormOptions $opts
  1455. * @param int $numRows Number of rows in the result to show after this header
  1456. */
  1457. public function doHeader( $opts, $numRows ) {
  1458. $this->setTopText( $opts );
  1459. // @todo Lots of stuff should be done here.
  1460. $this->setBottomText( $opts );
  1461. }
  1462. /**
  1463. * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
  1464. * or similar methods to print the text.
  1465. *
  1466. * @param FormOptions $opts
  1467. */
  1468. public function setTopText( FormOptions $opts ) {
  1469. // nothing by default
  1470. }
  1471. /**
  1472. * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
  1473. * or similar methods to print the text.
  1474. *
  1475. * @param FormOptions $opts
  1476. */
  1477. public function setBottomText( FormOptions $opts ) {
  1478. // nothing by default
  1479. }
  1480. /**
  1481. * Get options to be displayed in a form
  1482. * @todo This should handle options returned by getDefaultOptions().
  1483. * @todo Not called by anything in this class (but is in subclasses), should be
  1484. * called by something… doHeader() maybe?
  1485. *
  1486. * @param FormOptions $opts
  1487. * @return array
  1488. */
  1489. public function getExtraOptions( $opts ) {
  1490. return [];
  1491. }
  1492. /**
  1493. * Return the legend displayed within the fieldset
  1494. *
  1495. * @return string
  1496. */
  1497. public function makeLegend() {
  1498. $context = $this->getContext();
  1499. $user = $context->getUser();
  1500. # The legend showing what the letters and stuff mean
  1501. $legend = Html::openElement( 'dl' ) . "\n";
  1502. # Iterates through them and gets the messages for both letter and tooltip
  1503. $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
  1504. if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
  1505. unset( $legendItems['unpatrolled'] );
  1506. }
  1507. foreach ( $legendItems as $key => $item ) { # generate items of the legend
  1508. $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
  1509. $letter = $item['letter'];
  1510. $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
  1511. $legend .= Html::element( 'dt',
  1512. [ 'class' => $cssClass ], $context->msg( $letter )->text()
  1513. ) . "\n" .
  1514. Html::rawElement( 'dd',
  1515. [ 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ],
  1516. $context->msg( $label )->parse()
  1517. ) . "\n";
  1518. }
  1519. # (+-123)
  1520. $legend .= Html::rawElement( 'dt',
  1521. [ 'class' => 'mw-plusminus-pos' ],
  1522. $context->msg( 'recentchanges-legend-plusminus' )->parse()
  1523. ) . "\n";
  1524. $legend .= Html::element(
  1525. 'dd',
  1526. [ 'class' => 'mw-changeslist-legend-plusminus' ],
  1527. $context->msg( 'recentchanges-label-plusminus' )->text()
  1528. ) . "\n";
  1529. $legend .= Html::closeElement( 'dl' ) . "\n";
  1530. $legendHeading = $this->isStructuredFilterUiEnabled() ?
  1531. $context->msg( 'rcfilters-legend-heading' )->parse() :
  1532. $context->msg( 'recentchanges-legend-heading' )->parse();
  1533. # Collapsible
  1534. $collapsedState = $this->getRequest()->getCookie( 'changeslist-state' );
  1535. $collapsedClass = $collapsedState === 'collapsed' ? ' mw-collapsed' : '';
  1536. $legend =
  1537. '<div class="mw-changeslist-legend mw-collapsible' . $collapsedClass . '">' .
  1538. $legendHeading .
  1539. '<div class="mw-collapsible-content">' . $legend . '</div>' .
  1540. '</div>';
  1541. return $legend;
  1542. }
  1543. /**
  1544. * Add page-specific modules.
  1545. */
  1546. protected function addModules() {
  1547. $out = $this->getOutput();
  1548. // Styles and behavior for the legend box (see makeLegend())
  1549. $out->addModuleStyles( [
  1550. 'mediawiki.special.changeslist.legend',
  1551. 'mediawiki.special.changeslist',
  1552. ] );
  1553. $out->addModules( 'mediawiki.special.changeslist.legend.js' );
  1554. if ( $this->isStructuredFilterUiEnabled() && !$this->including() ) {
  1555. $out->addModules( 'mediawiki.rcfilters.filters.ui' );
  1556. $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
  1557. }
  1558. }
  1559. protected function getGroupName() {
  1560. return 'changes';
  1561. }
  1562. /**
  1563. * Filter on users' experience levels; this will not be called if nothing is
  1564. * selected.
  1565. *
  1566. * @param string $specialPageClassName Class name of current special page
  1567. * @param IContextSource $context Context, for e.g. user
  1568. * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
  1569. * @param array &$tables Array of tables; see IDatabase::select $table
  1570. * @param array &$fields Array of fields; see IDatabase::select $vars
  1571. * @param array &$conds Array of conditions; see IDatabase::select $conds
  1572. * @param array &$query_options Array of query options; see IDatabase::select $options
  1573. * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
  1574. * @param array $selectedExpLevels The allowed active values, sorted
  1575. * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
  1576. * (optional)
  1577. */
  1578. public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
  1579. &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
  1580. ) {
  1581. global $wgLearnerEdits,
  1582. $wgExperiencedUserEdits,
  1583. $wgLearnerMemberSince,
  1584. $wgExperiencedUserMemberSince;
  1585. $LEVEL_COUNT = 5;
  1586. // If all levels are selected, don't filter
  1587. if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
  1588. return;
  1589. }
  1590. // both 'registered' and 'unregistered', experience levels, if any, are included in 'registered'
  1591. if (
  1592. in_array( 'registered', $selectedExpLevels ) &&
  1593. in_array( 'unregistered', $selectedExpLevels )
  1594. ) {
  1595. return;
  1596. }
  1597. $actorMigration = ActorMigration::newMigration();
  1598. $actorQuery = $actorMigration->getJoin( 'rc_user' );
  1599. $tables += $actorQuery['tables'];
  1600. $join_conds += $actorQuery['joins'];
  1601. // 'registered' but not 'unregistered', experience levels, if any, are included in 'registered'
  1602. if (
  1603. in_array( 'registered', $selectedExpLevels ) &&
  1604. !in_array( 'unregistered', $selectedExpLevels )
  1605. ) {
  1606. $conds[] = $actorMigration->isNotAnon( $actorQuery['fields']['rc_user'] );
  1607. return;
  1608. }
  1609. if ( $selectedExpLevels === [ 'unregistered' ] ) {
  1610. $conds[] = $actorMigration->isAnon( $actorQuery['fields']['rc_user'] );
  1611. return;
  1612. }
  1613. $tables[] = 'user';
  1614. $join_conds['user'] = [ 'LEFT JOIN', $actorQuery['fields']['rc_user'] . ' = user_id' ];
  1615. if ( $now === 0 ) {
  1616. $now = time();
  1617. }
  1618. $secondsPerDay = 86400;
  1619. $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
  1620. $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
  1621. $aboveNewcomer = $dbr->makeList(
  1622. [
  1623. 'user_editcount >= ' . intval( $wgLearnerEdits ),
  1624. 'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
  1625. ],
  1626. IDatabase::LIST_AND
  1627. );
  1628. $aboveLearner = $dbr->makeList(
  1629. [
  1630. 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
  1631. 'user_registration <= ' .
  1632. $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
  1633. ],
  1634. IDatabase::LIST_AND
  1635. );
  1636. $conditions = [];
  1637. if ( in_array( 'unregistered', $selectedExpLevels ) ) {
  1638. $selectedExpLevels = array_diff( $selectedExpLevels, [ 'unregistered' ] );
  1639. $conditions[] = $actorMigration->isAnon( $actorQuery['fields']['rc_user'] );
  1640. }
  1641. if ( $selectedExpLevels === [ 'newcomer' ] ) {
  1642. $conditions[] = "NOT ( $aboveNewcomer )";
  1643. } elseif ( $selectedExpLevels === [ 'learner' ] ) {
  1644. $conditions[] = $dbr->makeList(
  1645. [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
  1646. IDatabase::LIST_AND
  1647. );
  1648. } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
  1649. $conditions[] = $aboveLearner;
  1650. } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
  1651. $conditions[] = "NOT ( $aboveLearner )";
  1652. } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
  1653. $conditions[] = $dbr->makeList(
  1654. [ "NOT ( $aboveNewcomer )", $aboveLearner ],
  1655. IDatabase::LIST_OR
  1656. );
  1657. } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
  1658. $conditions[] = $aboveNewcomer;
  1659. } elseif ( $selectedExpLevels === [ 'experienced', 'learner', 'newcomer' ] ) {
  1660. $conditions[] = $actorMigration->isNotAnon( $actorQuery['fields']['rc_user'] );
  1661. }
  1662. if ( count( $conditions ) > 1 ) {
  1663. $conds[] = $dbr->makeList( $conditions, IDatabase::LIST_OR );
  1664. } elseif ( count( $conditions ) === 1 ) {
  1665. $conds[] = reset( $conditions );
  1666. }
  1667. }
  1668. /**
  1669. * Check whether the structured filter UI is enabled
  1670. *
  1671. * @return bool
  1672. */
  1673. public function isStructuredFilterUiEnabled() {
  1674. if ( $this->getRequest()->getBool( 'rcfilters' ) ) {
  1675. return true;
  1676. }
  1677. return static::checkStructuredFilterUiEnabled(
  1678. $this->getConfig(),
  1679. $this->getUser()
  1680. );
  1681. }
  1682. /**
  1683. * Check whether the structured filter UI is enabled by default (regardless of
  1684. * this particular user's setting)
  1685. *
  1686. * @return bool
  1687. */
  1688. public function isStructuredFilterUiEnabledByDefault() {
  1689. if ( $this->getConfig()->get( 'StructuredChangeFiltersShowPreference' ) ) {
  1690. return !$this->getUser()->getDefaultOption( 'rcenhancedfilters-disable' );
  1691. } else {
  1692. return $this->getUser()->getDefaultOption( 'rcenhancedfilters' );
  1693. }
  1694. }
  1695. /**
  1696. * Static method to check whether StructuredFilter UI is enabled for the given user
  1697. *
  1698. * @since 1.31
  1699. * @param Config $config
  1700. * @param User $user
  1701. * @return bool
  1702. */
  1703. public static function checkStructuredFilterUiEnabled( Config $config, User $user ) {
  1704. if ( $config->get( 'StructuredChangeFiltersShowPreference' ) ) {
  1705. return !$user->getOption( 'rcenhancedfilters-disable' );
  1706. } else {
  1707. return $user->getOption( 'rcenhancedfilters' );
  1708. }
  1709. }
  1710. /**
  1711. * Get the default value of the number of changes to display when loading
  1712. * the result set.
  1713. *
  1714. * @since 1.30
  1715. * @return int
  1716. */
  1717. public function getDefaultLimit() {
  1718. return $this->getUser()->getIntOption( static::$limitPreferenceName );
  1719. }
  1720. /**
  1721. * Get the default value of the number of days to display when loading
  1722. * the result set.
  1723. * Supports fractional values, and should be cast to a float.
  1724. *
  1725. * @since 1.30
  1726. * @return float
  1727. */
  1728. public function getDefaultDays() {
  1729. return floatval( $this->getUser()->getOption( static::$daysPreferenceName ) );
  1730. }
  1731. }