RevisionList.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. /**
  3. * Holders of revision list for a single page
  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. */
  22. use MediaWiki\MediaWikiServices;
  23. use Wikimedia\Rdbms\ResultWrapper;
  24. use Wikimedia\Rdbms\IDatabase;
  25. /**
  26. * List for revision table items for a single page
  27. */
  28. abstract class RevisionListBase extends ContextSource implements Iterator {
  29. /** @var Title */
  30. public $title;
  31. /** @var array */
  32. protected $ids;
  33. /** @var ResultWrapper|bool */
  34. protected $res;
  35. /** @var bool|Revision */
  36. protected $current;
  37. /**
  38. * Construct a revision list for a given title
  39. * @param IContextSource $context
  40. * @param Title $title
  41. */
  42. function __construct( IContextSource $context, Title $title ) {
  43. $this->setContext( $context );
  44. $this->title = $title;
  45. }
  46. /**
  47. * Select items only where the ID is any of the specified values
  48. * @param array $ids
  49. */
  50. function filterByIds( array $ids ) {
  51. $this->ids = $ids;
  52. }
  53. /**
  54. * Get the internal type name of this list. Equal to the table name.
  55. * Override this function.
  56. * @return null
  57. */
  58. public function getType() {
  59. return null;
  60. }
  61. /**
  62. * Initialise the current iteration pointer
  63. */
  64. protected function initCurrent() {
  65. $row = $this->res->current();
  66. if ( $row ) {
  67. $this->current = $this->newItem( $row );
  68. } else {
  69. $this->current = false;
  70. }
  71. }
  72. /**
  73. * Start iteration. This must be called before current() or next().
  74. * @return Revision First list item
  75. */
  76. public function reset() {
  77. if ( !$this->res ) {
  78. $this->res = $this->doQuery( wfGetDB( DB_REPLICA ) );
  79. } else {
  80. $this->res->rewind();
  81. }
  82. $this->initCurrent();
  83. return $this->current;
  84. }
  85. public function rewind() {
  86. $this->reset();
  87. }
  88. /**
  89. * Get the current list item, or false if we are at the end
  90. * @return Revision
  91. */
  92. public function current() {
  93. return $this->current;
  94. }
  95. /**
  96. * Move the iteration pointer to the next list item, and return it.
  97. * @return Revision
  98. */
  99. public function next() {
  100. $this->res->next();
  101. $this->initCurrent();
  102. return $this->current;
  103. }
  104. public function key() {
  105. return $this->res ? $this->res->key() : 0;
  106. }
  107. public function valid() {
  108. return $this->res ? $this->res->valid() : false;
  109. }
  110. /**
  111. * Get the number of items in the list.
  112. * @return int
  113. */
  114. public function length() {
  115. if ( !$this->res ) {
  116. return 0;
  117. } else {
  118. return $this->res->numRows();
  119. }
  120. }
  121. /**
  122. * Do the DB query to iterate through the objects.
  123. * @param IDatabase $db DB object to use for the query
  124. */
  125. abstract public function doQuery( $db );
  126. /**
  127. * Create an item object from a DB result row
  128. * @param object $row
  129. */
  130. abstract public function newItem( $row );
  131. }
  132. /**
  133. * Abstract base class for revision items
  134. */
  135. abstract class RevisionItemBase {
  136. /** @var RevisionListBase The parent */
  137. protected $list;
  138. /** The database result row */
  139. protected $row;
  140. /**
  141. * @param RevisionListBase $list
  142. * @param object $row DB result row
  143. */
  144. public function __construct( $list, $row ) {
  145. $this->list = $list;
  146. $this->row = $row;
  147. }
  148. /**
  149. * Get the DB field name associated with the ID list.
  150. * Override this function.
  151. * @return null
  152. */
  153. public function getIdField() {
  154. return null;
  155. }
  156. /**
  157. * Get the DB field name storing timestamps.
  158. * Override this function.
  159. * @return bool
  160. */
  161. public function getTimestampField() {
  162. return false;
  163. }
  164. /**
  165. * Get the DB field name storing user ids.
  166. * Override this function.
  167. * @return bool
  168. */
  169. public function getAuthorIdField() {
  170. return false;
  171. }
  172. /**
  173. * Get the DB field name storing user names.
  174. * Override this function.
  175. * @return bool
  176. */
  177. public function getAuthorNameField() {
  178. return false;
  179. }
  180. /**
  181. * Get the DB field name storing actor ids.
  182. * Override this function.
  183. * @since 1.31
  184. * @return bool
  185. */
  186. public function getAuthorActorField() {
  187. return false;
  188. }
  189. /**
  190. * Get the ID, as it would appear in the ids URL parameter
  191. * @return int
  192. */
  193. public function getId() {
  194. $field = $this->getIdField();
  195. return $this->row->$field;
  196. }
  197. /**
  198. * Get the date, formatted in user's language
  199. * @return string
  200. */
  201. public function formatDate() {
  202. return $this->list->getLanguage()->userDate( $this->getTimestamp(),
  203. $this->list->getUser() );
  204. }
  205. /**
  206. * Get the time, formatted in user's language
  207. * @return string
  208. */
  209. public function formatTime() {
  210. return $this->list->getLanguage()->userTime( $this->getTimestamp(),
  211. $this->list->getUser() );
  212. }
  213. /**
  214. * Get the timestamp in MW 14-char form
  215. * @return mixed
  216. */
  217. public function getTimestamp() {
  218. $field = $this->getTimestampField();
  219. return wfTimestamp( TS_MW, $this->row->$field );
  220. }
  221. /**
  222. * Get the author user ID
  223. * @return int
  224. */
  225. public function getAuthorId() {
  226. $field = $this->getAuthorIdField();
  227. return intval( $this->row->$field );
  228. }
  229. /**
  230. * Get the author user name
  231. * @return string
  232. */
  233. public function getAuthorName() {
  234. $field = $this->getAuthorNameField();
  235. return strval( $this->row->$field );
  236. }
  237. /**
  238. * Get the author actor ID
  239. * @since 1.31
  240. * @return string
  241. */
  242. public function getAuthorActor() {
  243. $field = $this->getAuthorActorField();
  244. return strval( $this->row->$field );
  245. }
  246. /**
  247. * Returns true if the current user can view the item
  248. */
  249. abstract public function canView();
  250. /**
  251. * Returns true if the current user can view the item text/file
  252. */
  253. abstract public function canViewContent();
  254. /**
  255. * Get the HTML of the list item. Should be include "<li></li>" tags.
  256. * This is used to show the list in HTML form, by the special page.
  257. */
  258. abstract public function getHTML();
  259. /**
  260. * Returns an instance of LinkRenderer
  261. * @return \MediaWiki\Linker\LinkRenderer
  262. */
  263. protected function getLinkRenderer() {
  264. return MediaWikiServices::getInstance()->getLinkRenderer();
  265. }
  266. }
  267. class RevisionList extends RevisionListBase {
  268. public function getType() {
  269. return 'revision';
  270. }
  271. /**
  272. * @param IDatabase $db
  273. * @return mixed
  274. */
  275. public function doQuery( $db ) {
  276. $conds = [ 'rev_page' => $this->title->getArticleID() ];
  277. if ( $this->ids !== null ) {
  278. $conds['rev_id'] = array_map( 'intval', $this->ids );
  279. }
  280. $revQuery = Revision::getQueryInfo( [ 'page', 'user' ] );
  281. return $db->select(
  282. $revQuery['tables'],
  283. $revQuery['fields'],
  284. $conds,
  285. __METHOD__,
  286. [ 'ORDER BY' => 'rev_id DESC' ],
  287. $revQuery['joins']
  288. );
  289. }
  290. public function newItem( $row ) {
  291. return new RevisionItem( $this, $row );
  292. }
  293. }
  294. /**
  295. * Item class for a live revision table row
  296. */
  297. class RevisionItem extends RevisionItemBase {
  298. /** @var Revision */
  299. protected $revision;
  300. /** @var RequestContext */
  301. protected $context;
  302. public function __construct( $list, $row ) {
  303. parent::__construct( $list, $row );
  304. $this->revision = new Revision( $row );
  305. $this->context = $list->getContext();
  306. }
  307. public function getIdField() {
  308. return 'rev_id';
  309. }
  310. public function getTimestampField() {
  311. return 'rev_timestamp';
  312. }
  313. public function getAuthorIdField() {
  314. return 'rev_user';
  315. }
  316. public function getAuthorNameField() {
  317. return 'rev_user_text';
  318. }
  319. public function canView() {
  320. return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
  321. }
  322. public function canViewContent() {
  323. return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
  324. }
  325. public function isDeleted() {
  326. return $this->revision->isDeleted( Revision::DELETED_TEXT );
  327. }
  328. /**
  329. * Get the HTML link to the revision text.
  330. * @todo Essentially a copy of RevDelRevisionItem::getRevisionLink. That class
  331. * should inherit from this one, and implement an appropriate interface instead
  332. * of extending RevDelItem
  333. * @return string
  334. */
  335. protected function getRevisionLink() {
  336. $date = $this->list->getLanguage()->userTimeAndDate(
  337. $this->revision->getTimestamp(), $this->list->getUser() );
  338. if ( $this->isDeleted() && !$this->canViewContent() ) {
  339. return htmlspecialchars( $date );
  340. }
  341. $linkRenderer = $this->getLinkRenderer();
  342. return $linkRenderer->makeKnownLink(
  343. $this->list->title,
  344. $date,
  345. [],
  346. [
  347. 'oldid' => $this->revision->getId(),
  348. 'unhide' => 1
  349. ]
  350. );
  351. }
  352. /**
  353. * Get the HTML link to the diff.
  354. * @todo Essentially a copy of RevDelRevisionItem::getDiffLink. That class
  355. * should inherit from this one, and implement an appropriate interface instead
  356. * of extending RevDelItem
  357. * @return string
  358. */
  359. protected function getDiffLink() {
  360. if ( $this->isDeleted() && !$this->canViewContent() ) {
  361. return $this->context->msg( 'diff' )->escaped();
  362. } else {
  363. $linkRenderer = $this->getLinkRenderer();
  364. return $linkRenderer->makeKnownLink(
  365. $this->list->title,
  366. $this->list->msg( 'diff' )->text(),
  367. [],
  368. [
  369. 'diff' => $this->revision->getId(),
  370. 'oldid' => 'prev',
  371. 'unhide' => 1
  372. ]
  373. );
  374. }
  375. }
  376. /**
  377. * @todo Essentially a copy of RevDelRevisionItem::getHTML. That class
  378. * should inherit from this one, and implement an appropriate interface instead
  379. * of extending RevDelItem
  380. * @return string
  381. */
  382. public function getHTML() {
  383. $difflink = $this->context->msg( 'parentheses' )
  384. ->rawParams( $this->getDiffLink() )->escaped();
  385. $revlink = $this->getRevisionLink();
  386. $userlink = Linker::revUserLink( $this->revision );
  387. $comment = Linker::revComment( $this->revision );
  388. if ( $this->isDeleted() ) {
  389. $revlink = "<span class=\"history-deleted\">$revlink</span>";
  390. }
  391. return "<li>$difflink $revlink $userlink $comment</li>";
  392. }
  393. }