merged master
[lhc/web/wiklou.git] / includes / Revision.php
1 <?php
2 /**
3 * Representation of a page version.
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
23 /**
24 * @todo document
25 */
26 class Revision implements IDBAccessObject {
27 protected $mId;
28 protected $mPage;
29 protected $mUserText;
30 protected $mOrigUserText;
31 protected $mUser;
32 protected $mMinorEdit;
33 protected $mTimestamp;
34 protected $mDeleted;
35 protected $mSize;
36 protected $mSha1;
37 protected $mParentId;
38 protected $mComment;
39 protected $mText;
40 protected $mTextRow;
41 protected $mTitle;
42 protected $mCurrent;
43 protected $mContentModel;
44 protected $mContentFormat;
45 protected $mContent;
46 protected $mContentHandler;
47
48 const DELETED_TEXT = 1;
49 const DELETED_COMMENT = 2;
50 const DELETED_USER = 4;
51 const DELETED_RESTRICTED = 8;
52 // Convenience field
53 const SUPPRESSED_USER = 12;
54 // Audience options for accessors
55 const FOR_PUBLIC = 1;
56 const FOR_THIS_USER = 2;
57 const RAW = 3;
58
59 /**
60 * Load a page revision from a given revision ID number.
61 * Returns null if no such revision can be found.
62 *
63 * $flags include:
64 * IDBAccessObject::LATEST_READ : Select the data from the master
65 * IDBAccessObject::LOCKING_READ : Select & lock the data from the master
66 * IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
67 *
68 * @param $id Integer
69 * @param $flags Integer (optional)
70 * @return Revision or null
71 */
72 public static function newFromId( $id, $flags = 0 ) {
73 return self::newFromConds( array( 'rev_id' => intval( $id ) ), $flags );
74 }
75
76 /**
77 * Load either the current, or a specified, revision
78 * that's attached to a given title. If not attached
79 * to that title, will return null.
80 *
81 * $flags include:
82 * IDBAccessObject::LATEST_READ : Select the data from the master
83 * IDBAccessObject::LOCKING_READ : Select & lock the data from the master
84 * IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
85 *
86 * @param $title Title
87 * @param $id Integer (optional)
88 * @param $flags Integer Bitfield (optional)
89 * @return Revision or null
90 */
91 public static function newFromTitle( $title, $id = 0, $flags = 0 ) {
92 $conds = array(
93 'page_namespace' => $title->getNamespace(),
94 'page_title' => $title->getDBkey()
95 );
96 if ( $id ) {
97 // Use the specified ID
98 $conds['rev_id'] = $id;
99 } elseif ( !( $flags & self::AVOID_MASTER ) && wfGetLB()->getServerCount() > 1 ) {
100 // Get the latest revision ID from the master
101 $dbw = wfGetDB( DB_MASTER );
102 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
103 if ( $latest === false ) {
104 return null; // page does not exist
105 }
106 $conds['rev_id'] = $latest;
107 } else {
108 // Use a join to get the latest revision
109 $conds[] = 'rev_id=page_latest';
110 }
111 return self::newFromConds( $conds, $flags );
112 }
113
114 /**
115 * Load either the current, or a specified, revision
116 * that's attached to a given page ID.
117 * Returns null if no such revision can be found.
118 *
119 * $flags include:
120 * IDBAccessObject::LATEST_READ : Select the data from the master
121 * IDBAccessObject::LOCKING_READ : Select & lock the data from the master
122 * IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
123 *
124 * @param $revId Integer
125 * @param $pageId Integer (optional)
126 * @param $flags Integer Bitfield (optional)
127 * @return Revision or null
128 */
129 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
130 $conds = array( 'page_id' => $pageId );
131 if ( $revId ) {
132 $conds['rev_id'] = $revId;
133 } elseif ( !( $flags & self::AVOID_MASTER ) && wfGetLB()->getServerCount() > 1 ) {
134 // Get the latest revision ID from the master
135 $dbw = wfGetDB( DB_MASTER );
136 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
137 if ( $latest === false ) {
138 return null; // page does not exist
139 }
140 $conds['rev_id'] = $latest;
141 } else {
142 $conds[] = 'rev_id = page_latest';
143 }
144 return self::newFromConds( $conds, $flags );
145 }
146
147 /**
148 * Make a fake revision object from an archive table row. This is queried
149 * for permissions or even inserted (as in Special:Undelete)
150 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
151 *
152 * @param $row
153 * @param $overrides array
154 *
155 * @return Revision
156 */
157 public static function newFromArchiveRow( $row, $overrides = array() ) {
158 global $wgContentHandlerUseDB;
159
160 $attribs = $overrides + array(
161 'page' => isset( $row->ar_page_id ) ? $row->ar_page_id : null,
162 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
163 'comment' => $row->ar_comment,
164 'user' => $row->ar_user,
165 'user_text' => $row->ar_user_text,
166 'timestamp' => $row->ar_timestamp,
167 'minor_edit' => $row->ar_minor_edit,
168 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
169 'deleted' => $row->ar_deleted,
170 'len' => $row->ar_len,
171 'sha1' => isset( $row->ar_sha1 ) ? $row->ar_sha1 : null,
172 'content_model' => isset( $row->ar_content_model ) ? $row->ar_content_model : null,
173 'content_format' => isset( $row->ar_content_format ) ? $row->ar_content_format : null,
174 );
175
176 if ( !$wgContentHandlerUseDB ) {
177 unset( $attribs['content_model'] );
178 unset( $attribs['content_format'] );
179 }
180
181 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
182 // Pre-1.5 ar_text row
183 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
184 if ( $attribs['text'] === false ) {
185 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
186 }
187 }
188 return new self( $attribs );
189 }
190
191 /**
192 * @since 1.19
193 *
194 * @param $row
195 * @return Revision
196 */
197 public static function newFromRow( $row ) {
198 return new self( $row );
199 }
200
201 /**
202 * Load a page revision from a given revision ID number.
203 * Returns null if no such revision can be found.
204 *
205 * @param $db DatabaseBase
206 * @param $id Integer
207 * @return Revision or null
208 */
209 public static function loadFromId( $db, $id ) {
210 return self::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
211 }
212
213 /**
214 * Load either the current, or a specified, revision
215 * that's attached to a given page. If not attached
216 * to that page, will return null.
217 *
218 * @param $db DatabaseBase
219 * @param $pageid Integer
220 * @param $id Integer
221 * @return Revision or null
222 */
223 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
224 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
225 if( $id ) {
226 $conds['rev_id'] = intval( $id );
227 } else {
228 $conds[] = 'rev_id=page_latest';
229 }
230 return self::loadFromConds( $db, $conds );
231 }
232
233 /**
234 * Load either the current, or a specified, revision
235 * that's attached to a given page. If not attached
236 * to that page, will return null.
237 *
238 * @param $db DatabaseBase
239 * @param $title Title
240 * @param $id Integer
241 * @return Revision or null
242 */
243 public static function loadFromTitle( $db, $title, $id = 0 ) {
244 if( $id ) {
245 $matchId = intval( $id );
246 } else {
247 $matchId = 'page_latest';
248 }
249 return self::loadFromConds( $db,
250 array( "rev_id=$matchId",
251 'page_namespace' => $title->getNamespace(),
252 'page_title' => $title->getDBkey() )
253 );
254 }
255
256 /**
257 * Load the revision for the given title with the given timestamp.
258 * WARNING: Timestamps may in some circumstances not be unique,
259 * so this isn't the best key to use.
260 *
261 * @param $db DatabaseBase
262 * @param $title Title
263 * @param $timestamp String
264 * @return Revision or null
265 */
266 public static function loadFromTimestamp( $db, $title, $timestamp ) {
267 return self::loadFromConds( $db,
268 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
269 'page_namespace' => $title->getNamespace(),
270 'page_title' => $title->getDBkey() )
271 );
272 }
273
274 /**
275 * Given a set of conditions, fetch a revision.
276 *
277 * @param $conditions Array
278 * @param $flags integer (optional)
279 * @return Revision or null
280 */
281 private static function newFromConds( $conditions, $flags = 0 ) {
282 $db = wfGetDB( ( $flags & self::LATEST_READ ) ? DB_MASTER : DB_SLAVE );
283 $rev = self::loadFromConds( $db, $conditions, $flags );
284 if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
285 if ( !( $flags & self::LATEST_READ ) && !( $flags & self::AVOID_MASTER ) ) {
286 $dbw = wfGetDB( DB_MASTER );
287 $rev = self::loadFromConds( $dbw, $conditions, $flags );
288 }
289 }
290 return $rev;
291 }
292
293 /**
294 * Given a set of conditions, fetch a revision from
295 * the given database connection.
296 *
297 * @param $db DatabaseBase
298 * @param $conditions Array
299 * @param $flags integer (optional)
300 * @return Revision or null
301 */
302 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
303 $res = self::fetchFromConds( $db, $conditions, $flags );
304 if( $res ) {
305 $row = $res->fetchObject();
306 if( $row ) {
307 $ret = new Revision( $row );
308 return $ret;
309 }
310 }
311 $ret = null;
312 return $ret;
313 }
314
315 /**
316 * Return a wrapper for a series of database rows to
317 * fetch all of a given page's revisions in turn.
318 * Each row can be fed to the constructor to get objects.
319 *
320 * @param $title Title
321 * @return ResultWrapper
322 */
323 public static function fetchRevision( $title ) {
324 return self::fetchFromConds(
325 wfGetDB( DB_SLAVE ),
326 array( 'rev_id=page_latest',
327 'page_namespace' => $title->getNamespace(),
328 'page_title' => $title->getDBkey() )
329 );
330 }
331
332 /**
333 * Given a set of conditions, return a ResultWrapper
334 * which will return matching database rows with the
335 * fields necessary to build Revision objects.
336 *
337 * @param $db DatabaseBase
338 * @param $conditions Array
339 * @param $flags integer (optional)
340 * @return ResultWrapper
341 */
342 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
343 $fields = array_merge(
344 self::selectFields(),
345 self::selectPageFields(),
346 self::selectUserFields()
347 );
348 $options = array( 'LIMIT' => 1 );
349 if ( $flags & self::FOR_UPDATE ) {
350 $options[] = 'FOR UPDATE';
351 }
352 return $db->select(
353 array( 'revision', 'page', 'user' ),
354 $fields,
355 $conditions,
356 __METHOD__,
357 $options,
358 array( 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() )
359 );
360 }
361
362 /**
363 * Return the value of a select() JOIN conds array for the user table.
364 * This will get user table rows for logged-in users.
365 * @since 1.19
366 * @return Array
367 */
368 public static function userJoinCond() {
369 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
370 }
371
372 /**
373 * Return the value of a select() page conds array for the paeg table.
374 * This will assure that the revision(s) are not orphaned from live pages.
375 * @since 1.19
376 * @return Array
377 */
378 public static function pageJoinCond() {
379 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
380 }
381
382 /**
383 * Return the list of revision fields that should be selected to create
384 * a new revision.
385 * @return array
386 */
387 public static function selectFields() {
388 global $wgContentHandlerUseDB;
389
390 $fields = array(
391 'rev_id',
392 'rev_page',
393 'rev_text_id',
394 'rev_timestamp',
395 'rev_comment',
396 'rev_user_text',
397 'rev_user',
398 'rev_minor_edit',
399 'rev_deleted',
400 'rev_len',
401 'rev_parent_id',
402 'rev_sha1',
403 );
404
405 if ( $wgContentHandlerUseDB ) {
406 $fields[] = 'rev_content_format';
407 $fields[] = 'rev_content_model';
408 }
409
410 return $fields;
411 }
412
413 /**
414 * Return the list of text fields that should be selected to read the
415 * revision text
416 * @return array
417 */
418 public static function selectTextFields() {
419 return array(
420 'old_text',
421 'old_flags'
422 );
423 }
424
425 /**
426 * Return the list of page fields that should be selected from page table
427 * @return array
428 */
429 public static function selectPageFields() {
430 return array(
431 'page_namespace',
432 'page_title',
433 'page_id',
434 'page_latest',
435 'page_is_redirect',
436 'page_len',
437 );
438 }
439
440 /**
441 * Return the list of user fields that should be selected from user table
442 * @return array
443 */
444 public static function selectUserFields() {
445 return array( 'user_name' );
446 }
447
448 /**
449 * Do a batched query to get the parent revision lengths
450 * @param $db DatabaseBase
451 * @param $revIds Array
452 * @return array
453 */
454 public static function getParentLengths( $db, array $revIds ) {
455 $revLens = array();
456 if ( !$revIds ) {
457 return $revLens; // empty
458 }
459 wfProfileIn( __METHOD__ );
460 $res = $db->select( 'revision',
461 array( 'rev_id', 'rev_len' ),
462 array( 'rev_id' => $revIds ),
463 __METHOD__ );
464 foreach ( $res as $row ) {
465 $revLens[$row->rev_id] = $row->rev_len;
466 }
467 wfProfileOut( __METHOD__ );
468 return $revLens;
469 }
470
471 /**
472 * Constructor
473 *
474 * @param $row Mixed: either a database row or an array
475 * @access private
476 */
477 function __construct( $row ) {
478 if( is_object( $row ) ) {
479 $this->mId = intval( $row->rev_id );
480 $this->mPage = intval( $row->rev_page );
481 $this->mTextId = intval( $row->rev_text_id );
482 $this->mComment = $row->rev_comment;
483 $this->mUser = intval( $row->rev_user );
484 $this->mMinorEdit = intval( $row->rev_minor_edit );
485 $this->mTimestamp = $row->rev_timestamp;
486 $this->mDeleted = intval( $row->rev_deleted );
487
488 if( !isset( $row->rev_parent_id ) ) {
489 $this->mParentId = is_null( $row->rev_parent_id ) ? null : 0;
490 } else {
491 $this->mParentId = intval( $row->rev_parent_id );
492 }
493
494 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) ) {
495 $this->mSize = null;
496 } else {
497 $this->mSize = intval( $row->rev_len );
498 }
499
500 if ( !isset( $row->rev_sha1 ) ) {
501 $this->mSha1 = null;
502 } else {
503 $this->mSha1 = $row->rev_sha1;
504 }
505
506 if( isset( $row->page_latest ) ) {
507 $this->mCurrent = ( $row->rev_id == $row->page_latest );
508 $this->mTitle = Title::newFromRow( $row );
509 } else {
510 $this->mCurrent = false;
511 $this->mTitle = null;
512 }
513
514 if( !isset( $row->rev_content_model ) || is_null( $row->rev_content_model ) ) {
515 $this->mContentModel = null; # determine on demand if needed
516 } else {
517 $this->mContentModel = strval( $row->rev_content_model );
518 }
519
520 if( !isset( $row->rev_content_format ) || is_null( $row->rev_content_format ) ) {
521 $this->mContentFormat = null; # determine on demand if needed
522 } else {
523 $this->mContentFormat = strval( $row->rev_content_format );
524 }
525
526 // Lazy extraction...
527 $this->mText = null;
528 if( isset( $row->old_text ) ) {
529 $this->mTextRow = $row;
530 } else {
531 // 'text' table row entry will be lazy-loaded
532 $this->mTextRow = null;
533 }
534
535 // Use user_name for users and rev_user_text for IPs...
536 $this->mUserText = null; // lazy load if left null
537 if ( $this->mUser == 0 ) {
538 $this->mUserText = $row->rev_user_text; // IP user
539 } elseif ( isset( $row->user_name ) ) {
540 $this->mUserText = $row->user_name; // logged-in user
541 }
542 $this->mOrigUserText = $row->rev_user_text;
543 } elseif( is_array( $row ) ) {
544 // Build a new revision to be saved...
545 global $wgUser; // ugh
546
547
548 # if we have a content object, use it to set the model and type
549 if ( !empty( $row['content'] ) ) {
550 if ( !empty( $row['text_id'] ) ) { //@todo: when is that set? test with external store setup! check out insertOn() [dk]
551 throw new MWException( "Text already stored in external store (id {$row['text_id']}), can't serialize content object" );
552 }
553
554 $row['content_model'] = $row['content']->getModel();
555 # note: mContentFormat is initializes later accordingly
556 # note: content is serialized later in this method!
557 # also set text to null?
558 }
559
560 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
561 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
562 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
563 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
564 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
565 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
566 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestampNow();
567 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
568 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
569 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
570 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
571
572 $this->mContentModel = isset( $row['content_model'] ) ? strval( $row['content_model'] ) : null;
573 $this->mContentFormat = isset( $row['content_format'] ) ? strval( $row['content_format'] ) : null;
574
575 // Enforce spacing trimming on supplied text
576 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
577 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
578 $this->mTextRow = null;
579
580 $this->mTitle = isset( $row['title'] ) ? $row['title'] : null;
581
582 // if we have a Content object, override mText and mContentModel
583 if ( !empty( $row['content'] ) ) {
584 $handler = $this->getContentHandler();
585 $this->mContent = $row['content'];
586
587 $this->mContentModel = $this->mContent->getModel();
588 $this->mContentHandler = null;
589
590 $this->mText = $handler->serializeContent( $row['content'], $this->getContentFormat() );
591 } elseif ( !is_null( $this->mText ) ) {
592 $handler = $this->getContentHandler();
593 $this->mContent = $handler->unserializeContent( $this->mText );
594 }
595
596 // if we have a Title object, override mPage. Useful for testing and convenience.
597 if ( isset( $row['title'] ) ) {
598 $this->mTitle = $row['title'];
599 $this->mPage = $this->mTitle->getArticleID();
600 } else {
601 $this->mTitle = null; // Load on demand if needed
602 }
603
604 $this->mCurrent = false; // @todo: XXX: really? we are about to create a revision. it will usually then be the current one.
605
606 // If we still have no length, see it we have the text to figure it out
607 if ( !$this->mSize ) {
608 if ( !is_null( $this->mContent ) ) {
609 $this->mSize = $this->mContent->getSize();
610 } else {
611 #NOTE: this should never happen if we have either text or content object!
612 $this->mSize = null;
613 }
614 }
615
616 // Same for sha1
617 if ( $this->mSha1 === null ) {
618 $this->mSha1 = is_null( $this->mText ) ? null : self::base36Sha1( $this->mText );
619 }
620
621 // force lazy init
622 $this->getContentModel();
623 $this->getContentFormat();
624 } else {
625 throw new MWException( 'Revision constructor passed invalid row format.' );
626 }
627 $this->mUnpatrolled = null;
628 }
629
630 /**
631 * Get revision ID
632 *
633 * @return Integer|null
634 */
635 public function getId() {
636 return $this->mId;
637 }
638
639 /**
640 * Set the revision ID
641 *
642 * @since 1.19
643 * @param $id Integer
644 */
645 public function setId( $id ) {
646 $this->mId = $id;
647 }
648
649 /**
650 * Get text row ID
651 *
652 * @return Integer|null
653 */
654 public function getTextId() {
655 return $this->mTextId;
656 }
657
658 /**
659 * Get parent revision ID (the original previous page revision)
660 *
661 * @return Integer|null
662 */
663 public function getParentId() {
664 return $this->mParentId;
665 }
666
667 /**
668 * Returns the length of the text in this revision, or null if unknown.
669 *
670 * @return Integer|null
671 */
672 public function getSize() {
673 return $this->mSize;
674 }
675
676 /**
677 * Returns the base36 sha1 of the text in this revision, or null if unknown.
678 *
679 * @return String|null
680 */
681 public function getSha1() {
682 return $this->mSha1;
683 }
684
685 /**
686 * Returns the title of the page associated with this entry or null.
687 *
688 * Will do a query, when title is not set and id is given.
689 *
690 * @return Title|null
691 */
692 public function getTitle() {
693 if( isset( $this->mTitle ) ) {
694 return $this->mTitle;
695 }
696 if( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
697 $dbr = wfGetDB( DB_SLAVE );
698 $row = $dbr->selectRow(
699 array( 'page', 'revision' ),
700 self::selectPageFields(),
701 array( 'page_id=rev_page',
702 'rev_id' => $this->mId ),
703 __METHOD__ );
704 if ( $row ) {
705 $this->mTitle = Title::newFromRow( $row );
706 }
707 }
708
709 if ( !$this->mTitle && !is_null( $this->mPage ) && $this->mPage > 0 ) {
710 $this->mTitle = Title::newFromID( $this->mPage );
711 }
712
713 return $this->mTitle;
714 }
715
716 /**
717 * Set the title of the revision
718 *
719 * @param $title Title
720 */
721 public function setTitle( $title ) {
722 $this->mTitle = $title;
723 }
724
725 /**
726 * Get the page ID
727 *
728 * @return Integer|null
729 */
730 public function getPage() {
731 return $this->mPage;
732 }
733
734 /**
735 * Fetch revision's user id if it's available to the specified audience.
736 * If the specified audience does not have access to it, zero will be
737 * returned.
738 *
739 * @param $audience Integer: one of:
740 * Revision::FOR_PUBLIC to be displayed to all users
741 * Revision::FOR_THIS_USER to be displayed to the given user
742 * Revision::RAW get the ID regardless of permissions
743 * @param $user User object to check for, only if FOR_THIS_USER is passed
744 * to the $audience parameter
745 * @return Integer
746 */
747 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
748 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
749 return 0;
750 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
751 return 0;
752 } else {
753 return $this->mUser;
754 }
755 }
756
757 /**
758 * Fetch revision's user id without regard for the current user's permissions
759 *
760 * @return String
761 */
762 public function getRawUser() {
763 return $this->mUser;
764 }
765
766 /**
767 * Fetch revision's username if it's available to the specified audience.
768 * If the specified audience does not have access to the username, an
769 * empty string will be returned.
770 *
771 * @param $audience Integer: one of:
772 * Revision::FOR_PUBLIC to be displayed to all users
773 * Revision::FOR_THIS_USER to be displayed to the given user
774 * Revision::RAW get the text regardless of permissions
775 * @param $user User object to check for, only if FOR_THIS_USER is passed
776 * to the $audience parameter
777 * @return string
778 */
779 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
780 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
781 return '';
782 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
783 return '';
784 } else {
785 return $this->getRawUserText();
786 }
787 }
788
789 /**
790 * Fetch revision's username without regard for view restrictions
791 *
792 * @return String
793 */
794 public function getRawUserText() {
795 if ( $this->mUserText === null ) {
796 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
797 if ( $this->mUserText === false ) {
798 # This shouldn't happen, but it can if the wiki was recovered
799 # via importing revs and there is no user table entry yet.
800 $this->mUserText = $this->mOrigUserText;
801 }
802 }
803 return $this->mUserText;
804 }
805
806 /**
807 * Fetch revision comment if it's available to the specified audience.
808 * If the specified audience does not have access to the comment, an
809 * empty string will be returned.
810 *
811 * @param $audience Integer: one of:
812 * Revision::FOR_PUBLIC to be displayed to all users
813 * Revision::FOR_THIS_USER to be displayed to the given user
814 * Revision::RAW get the text regardless of permissions
815 * @param $user User object to check for, only if FOR_THIS_USER is passed
816 * to the $audience parameter
817 * @return String
818 */
819 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
820 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
821 return '';
822 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
823 return '';
824 } else {
825 return $this->mComment;
826 }
827 }
828
829 /**
830 * Fetch revision comment without regard for the current user's permissions
831 *
832 * @return String
833 */
834 public function getRawComment() {
835 return $this->mComment;
836 }
837
838 /**
839 * @return Boolean
840 */
841 public function isMinor() {
842 return (bool)$this->mMinorEdit;
843 }
844
845 /**
846 * @return Integer rcid of the unpatrolled row, zero if there isn't one
847 */
848 public function isUnpatrolled() {
849 if( $this->mUnpatrolled !== null ) {
850 return $this->mUnpatrolled;
851 }
852 $dbr = wfGetDB( DB_SLAVE );
853 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
854 'rc_id',
855 array( // Add redundant user,timestamp condition so we can use the existing index
856 'rc_user_text' => $this->getRawUserText(),
857 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
858 'rc_this_oldid' => $this->getId(),
859 'rc_patrolled' => 0
860 ),
861 __METHOD__
862 );
863 return (int)$this->mUnpatrolled;
864 }
865
866 /**
867 * @param $field int one of DELETED_* bitfield constants
868 *
869 * @return Boolean
870 */
871 public function isDeleted( $field ) {
872 return ( $this->mDeleted & $field ) == $field;
873 }
874
875 /**
876 * Get the deletion bitfield of the revision
877 *
878 * @return int
879 */
880 public function getVisibility() {
881 return (int)$this->mDeleted;
882 }
883
884 /**
885 * Fetch revision text if it's available to the specified audience.
886 * If the specified audience does not have the ability to view this
887 * revision, an empty string will be returned.
888 *
889 * @param $audience Integer: one of:
890 * Revision::FOR_PUBLIC to be displayed to all users
891 * Revision::FOR_THIS_USER to be displayed to the given user
892 * Revision::RAW get the text regardless of permissions
893 * @param $user User object to check for, only if FOR_THIS_USER is passed
894 * to the $audience parameter
895 * @return String
896 * @deprecated in 1.WD, use getContent() instead
897 * @todo: replace usage in core
898 */
899 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
900 wfDeprecated( __METHOD__, '1.WD' );
901
902 $content = $this->getContent( $audience, $user );
903 return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
904 }
905
906 /**
907 * Fetch revision content if it's available to the specified audience.
908 * If the specified audience does not have the ability to view this
909 * revision, null will be returned.
910 *
911 * @param $audience Integer: one of:
912 * Revision::FOR_PUBLIC to be displayed to all users
913 * Revision::FOR_THIS_USER to be displayed to $wgUser
914 * Revision::RAW get the text regardless of permissions
915 * @param $user User object to check for, only if FOR_THIS_USER is passed
916 * to the $audience parameter
917 * @return Content
918 *
919 * @since 1.WD
920 */
921 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
922 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
923 return null;
924 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
925 return null;
926 } else {
927 return $this->getContentInternal();
928 }
929 }
930
931 /**
932 * Alias for getText(Revision::FOR_THIS_USER)
933 *
934 * @deprecated since 1.17
935 * @return String
936 */
937 public function revText() {
938 wfDeprecated( __METHOD__, '1.17' );
939 return $this->getText( self::FOR_THIS_USER );
940 }
941
942 /**
943 * Fetch revision text without regard for view restrictions
944 *
945 * @return String
946 *
947 * @deprecated since 1.WD. Instead, use Revision::getContent( Revision::RAW ) or Revision::getSerializedData() as appropriate.
948 */
949 public function getRawText() {
950 wfDeprecated( __METHOD__, "1.WD" );
951
952 return $this->getText( self::RAW );
953 }
954
955 /**
956 * Fetch original serialized data without regard for view restrictions
957 *
958 * @return String
959 *
960 * @since 1.WD
961 */
962 public function getSerializedData() {
963 return $this->mText;
964 }
965
966 protected function getContentInternal() {
967 if( is_null( $this->mContent ) ) {
968 // Revision is immutable. Load on demand:
969
970 $handler = $this->getContentHandler();
971 $format = $this->getContentFormat();
972 $title = $this->getTitle();
973
974 if( is_null( $this->mText ) ) {
975 // Load text on demand:
976 $this->mText = $this->loadText();
977 }
978
979 $this->mContent = is_null( $this->mText ) ? null : $handler->unserializeContent( $this->mText, $format );
980 }
981
982 return $this->mContent;
983 }
984
985 /**
986 * Returns the content model for this revision.
987 *
988 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
989 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
990 * is used as a last resort.
991 *
992 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
993 **/
994 public function getContentModel() {
995 if ( !$this->mContentModel ) {
996 $title = $this->getTitle();
997 $this->mContentModel = ( $title ? $title->getContentModel() : CONTENT_MODEL_WIKITEXT );
998
999 assert( !empty( $this->mContentModel ) );
1000 }
1001
1002 return $this->mContentModel;
1003 }
1004
1005 /**
1006 * Returns the content format for this revision.
1007 *
1008 * If no content format was stored in the database, the default format for this
1009 * revision's content model is returned.
1010 *
1011 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
1012 **/
1013 public function getContentFormat() {
1014 if ( !$this->mContentFormat ) {
1015 $handler = $this->getContentHandler();
1016 $this->mContentFormat = $handler->getDefaultFormat();
1017
1018 assert( !empty( $this->mContentFormat ) );
1019 }
1020
1021 return $this->mContentFormat;
1022 }
1023
1024 /**
1025 * Returns the content handler appropriate for this revision's content model.
1026 *
1027 * @return ContentHandler
1028 */
1029 public function getContentHandler() {
1030 if ( !$this->mContentHandler ) {
1031 $model = $this->getContentModel();
1032 $this->mContentHandler = ContentHandler::getForModelID( $model );
1033
1034 $format = $this->getContentFormat();
1035
1036 if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
1037 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1038 }
1039 }
1040
1041 return $this->mContentHandler;
1042 }
1043
1044 /**
1045 * @return String
1046 */
1047 public function getTimestamp() {
1048 return wfTimestamp( TS_MW, $this->mTimestamp );
1049 }
1050
1051 /**
1052 * @return Boolean
1053 */
1054 public function isCurrent() {
1055 return $this->mCurrent;
1056 }
1057
1058 /**
1059 * Get previous revision for this title
1060 *
1061 * @return Revision or null
1062 */
1063 public function getPrevious() {
1064 if( $this->getTitle() ) {
1065 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1066 if( $prev ) {
1067 return self::newFromTitle( $this->getTitle(), $prev );
1068 }
1069 }
1070 return null;
1071 }
1072
1073 /**
1074 * Get next revision for this title
1075 *
1076 * @return Revision or null
1077 */
1078 public function getNext() {
1079 if( $this->getTitle() ) {
1080 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1081 if ( $next ) {
1082 return self::newFromTitle( $this->getTitle(), $next );
1083 }
1084 }
1085 return null;
1086 }
1087
1088 /**
1089 * Get previous revision Id for this page_id
1090 * This is used to populate rev_parent_id on save
1091 *
1092 * @param $db DatabaseBase
1093 * @return Integer
1094 */
1095 private function getPreviousRevisionId( $db ) {
1096 if( is_null( $this->mPage ) ) {
1097 return 0;
1098 }
1099 # Use page_latest if ID is not given
1100 if( !$this->mId ) {
1101 $prevId = $db->selectField( 'page', 'page_latest',
1102 array( 'page_id' => $this->mPage ),
1103 __METHOD__ );
1104 } else {
1105 $prevId = $db->selectField( 'revision', 'rev_id',
1106 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
1107 __METHOD__,
1108 array( 'ORDER BY' => 'rev_id DESC' ) );
1109 }
1110 return intval( $prevId );
1111 }
1112
1113 /**
1114 * Get revision text associated with an old or archive row
1115 * $row is usually an object from wfFetchRow(), both the flags and the text
1116 * field must be included
1117 *
1118 * @param $row Object: the text data
1119 * @param $prefix String: table prefix (default 'old_')
1120 * @return String: text the text requested or false on failure
1121 */
1122 public static function getRevisionText( $row, $prefix = 'old_' ) {
1123 wfProfileIn( __METHOD__ );
1124
1125 # Get data
1126 $textField = $prefix . 'text';
1127 $flagsField = $prefix . 'flags';
1128
1129 if( isset( $row->$flagsField ) ) {
1130 $flags = explode( ',', $row->$flagsField );
1131 } else {
1132 $flags = array();
1133 }
1134
1135 if( isset( $row->$textField ) ) {
1136 $text = $row->$textField;
1137 } else {
1138 wfProfileOut( __METHOD__ );
1139 return false;
1140 }
1141
1142 # Use external methods for external objects, text in table is URL-only then
1143 if ( in_array( 'external', $flags ) ) {
1144 $url = $text;
1145 $parts = explode( '://', $url, 2 );
1146 if( count( $parts ) == 1 || $parts[1] == '' ) {
1147 wfProfileOut( __METHOD__ );
1148 return false;
1149 }
1150 $text = ExternalStore::fetchFromURL( $url );
1151 }
1152
1153 // If the text was fetched without an error, convert it
1154 if ( $text !== false ) {
1155 if( in_array( 'gzip', $flags ) ) {
1156 # Deal with optional compression of archived pages.
1157 # This can be done periodically via maintenance/compressOld.php, and
1158 # as pages are saved if $wgCompressRevisions is set.
1159 $text = gzinflate( $text );
1160 }
1161
1162 if( in_array( 'object', $flags ) ) {
1163 # Generic compressed storage
1164 $obj = unserialize( $text );
1165 if ( !is_object( $obj ) ) {
1166 // Invalid object
1167 wfProfileOut( __METHOD__ );
1168 return false;
1169 }
1170 $text = $obj->getText();
1171 }
1172
1173 global $wgLegacyEncoding;
1174 if( $text !== false && $wgLegacyEncoding
1175 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1176 {
1177 # Old revisions kept around in a legacy encoding?
1178 # Upconvert on demand.
1179 # ("utf8" checked for compatibility with some broken
1180 # conversion scripts 2008-12-30)
1181 global $wgContLang;
1182 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1183 }
1184 }
1185 wfProfileOut( __METHOD__ );
1186 return $text;
1187 }
1188
1189 /**
1190 * If $wgCompressRevisions is enabled, we will compress data.
1191 * The input string is modified in place.
1192 * Return value is the flags field: contains 'gzip' if the
1193 * data is compressed, and 'utf-8' if we're saving in UTF-8
1194 * mode.
1195 *
1196 * @param $text Mixed: reference to a text
1197 * @return String
1198 */
1199 public static function compressRevisionText( &$text ) {
1200 global $wgCompressRevisions;
1201 $flags = array();
1202
1203 # Revisions not marked this way will be converted
1204 # on load if $wgLegacyCharset is set in the future.
1205 $flags[] = 'utf-8';
1206
1207 if( $wgCompressRevisions ) {
1208 if( function_exists( 'gzdeflate' ) ) {
1209 $text = gzdeflate( $text );
1210 $flags[] = 'gzip';
1211 } else {
1212 wfDebug( __METHOD__ . " -- no zlib support, not compressing\n" );
1213 }
1214 }
1215 return implode( ',', $flags );
1216 }
1217
1218 /**
1219 * Insert a new revision into the database, returning the new revision ID
1220 * number on success and dies horribly on failure.
1221 *
1222 * @param $dbw DatabaseBase: (master connection)
1223 * @return Integer
1224 */
1225 public function insertOn( $dbw ) {
1226 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1227
1228 wfProfileIn( __METHOD__ );
1229
1230 $this->checkContentModel();
1231
1232 $data = $this->mText;
1233 $flags = self::compressRevisionText( $data );
1234
1235 # Write to external storage if required
1236 if( $wgDefaultExternalStore ) {
1237 // Store and get the URL
1238 $data = ExternalStore::insertToDefault( $data );
1239 if( !$data ) {
1240 throw new MWException( "Unable to store text to external storage" );
1241 }
1242 if( $flags ) {
1243 $flags .= ',';
1244 }
1245 $flags .= 'external';
1246 }
1247
1248 # Record the text (or external storage URL) to the text table
1249 if( !isset( $this->mTextId ) ) {
1250 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1251 $dbw->insert( 'text',
1252 array(
1253 'old_id' => $old_id,
1254 'old_text' => $data,
1255 'old_flags' => $flags,
1256 ), __METHOD__
1257 );
1258 $this->mTextId = $dbw->insertId();
1259 }
1260
1261 if ( $this->mComment === null ) $this->mComment = "";
1262
1263 # Record the edit in revisions
1264 $rev_id = isset( $this->mId )
1265 ? $this->mId
1266 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1267 $row = array(
1268 'rev_id' => $rev_id,
1269 'rev_page' => $this->mPage,
1270 'rev_text_id' => $this->mTextId,
1271 'rev_comment' => $this->mComment,
1272 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1273 'rev_user' => $this->mUser,
1274 'rev_user_text' => $this->mUserText,
1275 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1276 'rev_deleted' => $this->mDeleted,
1277 'rev_len' => $this->mSize,
1278 'rev_parent_id' => is_null( $this->mParentId )
1279 ? $this->getPreviousRevisionId( $dbw )
1280 : $this->mParentId,
1281 'rev_sha1' => is_null( $this->mSha1 )
1282 ? Revision::base36Sha1( $this->mText )
1283 : $this->mSha1,
1284 );
1285
1286 if ( $wgContentHandlerUseDB ) {
1287 //NOTE: Store null for the default model and format, to save space.
1288 //XXX: Makes the DB sensitive to changed defaults. Make this behaviour optional? Only in miser mode?
1289
1290 $model = $this->getContentModel();
1291 $format = $this->getContentFormat();
1292
1293 $defaultModel = ContentHandler::getDefaultModelFor( $this->getTitle() );
1294 $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
1295
1296 $row[ 'rev_content_model' ] = ( $model === $defaultModel ) ? null : $model;
1297 $row[ 'rev_content_format' ] = ( $format === $defaultFormat ) ? null : $format;
1298 }
1299
1300 $dbw->insert( 'revision', $row, __METHOD__ );
1301
1302 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
1303
1304 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1305
1306 wfProfileOut( __METHOD__ );
1307 return $this->mId;
1308 }
1309
1310 protected function checkContentModel() {
1311 global $wgContentHandlerUseDB;
1312
1313 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1314
1315 $model = $this->getContentModel();
1316 $format = $this->getContentFormat();
1317 $handler = $this->getContentHandler();
1318
1319 if ( !$handler->isSupportedFormat( $format ) ) {
1320 $t = $title->getPrefixedDBkey();
1321
1322 throw new MWException( "Can't use format $format with content model $model on $t" );
1323 }
1324
1325 if ( !$wgContentHandlerUseDB && $title ) {
1326 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1327
1328 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1329 $defaultHandler = ContentHandler::getForModelID( $defaultModel );
1330 $defaultFormat = $defaultHandler->getDefaultFormat();
1331
1332 if ( $this->getContentModel() != $defaultModel ) {
1333 $t = $title->getPrefixedDBkey();
1334
1335 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: model is $model , default for $t is $defaultModel" );
1336 }
1337
1338 if ( $this->getContentFormat() != $defaultFormat ) {
1339 $t = $title->getPrefixedDBkey();
1340
1341 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: format is $format, default for $t is $defaultFormat" );
1342 }
1343 }
1344
1345 $content = $this->getContent( Revision::RAW );
1346
1347 if ( !$content->isValid() ) {
1348 $t = $title->getPrefixedDBkey();
1349
1350 throw new MWException( "Content of $t is not valid! Content model is $model" );
1351 }
1352 }
1353
1354 /**
1355 * Get the base 36 SHA-1 value for a string of text
1356 * @param $text String
1357 * @return String
1358 */
1359 public static function base36Sha1( $text ) {
1360 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1361 }
1362
1363 /**
1364 * Lazy-load the revision's text.
1365 * Currently hardcoded to the 'text' table storage engine.
1366 *
1367 * @return String
1368 */
1369 protected function loadText() {
1370 wfProfileIn( __METHOD__ );
1371
1372 // Caching may be beneficial for massive use of external storage
1373 global $wgRevisionCacheExpiry, $wgMemc;
1374 $textId = $this->getTextId();
1375 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1376 if( $wgRevisionCacheExpiry ) {
1377 $text = $wgMemc->get( $key );
1378 if( is_string( $text ) ) {
1379 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1380 wfProfileOut( __METHOD__ );
1381 return $text;
1382 }
1383 }
1384
1385 // If we kept data for lazy extraction, use it now...
1386 if ( isset( $this->mTextRow ) ) {
1387 $row = $this->mTextRow;
1388 $this->mTextRow = null;
1389 } else {
1390 $row = null;
1391 }
1392
1393 if( !$row ) {
1394 // Text data is immutable; check slaves first.
1395 $dbr = wfGetDB( DB_SLAVE );
1396 $row = $dbr->selectRow( 'text',
1397 array( 'old_text', 'old_flags' ),
1398 array( 'old_id' => $this->getTextId() ),
1399 __METHOD__ );
1400 }
1401
1402 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1403 // Possible slave lag!
1404 $dbw = wfGetDB( DB_MASTER );
1405 $row = $dbw->selectRow( 'text',
1406 array( 'old_text', 'old_flags' ),
1407 array( 'old_id' => $this->getTextId() ),
1408 __METHOD__ );
1409 }
1410
1411 $text = self::getRevisionText( $row );
1412
1413 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1414 if( $wgRevisionCacheExpiry && $text !== false ) {
1415 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1416 }
1417
1418 wfProfileOut( __METHOD__ );
1419
1420 return $text;
1421 }
1422
1423 /**
1424 * Create a new null-revision for insertion into a page's
1425 * history. This will not re-save the text, but simply refer
1426 * to the text from the previous version.
1427 *
1428 * Such revisions can for instance identify page rename
1429 * operations and other such meta-modifications.
1430 *
1431 * @param $dbw DatabaseBase
1432 * @param $pageId Integer: ID number of the page to read from
1433 * @param $summary String: revision's summary
1434 * @param $minor Boolean: whether the revision should be considered as minor
1435 * @return Revision|null on error
1436 */
1437 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1438 global $wgContentHandlerUseDB;
1439
1440 wfProfileIn( __METHOD__ );
1441
1442 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1443 'rev_text_id', 'rev_len', 'rev_sha1' );
1444
1445 if ( $wgContentHandlerUseDB ) {
1446 $fields[] = 'rev_content_model';
1447 $fields[] = 'rev_content_format';
1448 }
1449
1450 $current = $dbw->selectRow(
1451 array( 'page', 'revision' ),
1452 $fields,
1453 array(
1454 'page_id' => $pageId,
1455 'page_latest=rev_id',
1456 ),
1457 __METHOD__ );
1458
1459 if( $current ) {
1460 $row = array(
1461 'page' => $pageId,
1462 'comment' => $summary,
1463 'minor_edit' => $minor,
1464 'text_id' => $current->rev_text_id,
1465 'parent_id' => $current->page_latest,
1466 'len' => $current->rev_len,
1467 'sha1' => $current->rev_sha1
1468 );
1469
1470 if ( $wgContentHandlerUseDB ) {
1471 $row[ 'content_model' ] = $current->rev_content_model;
1472 $row[ 'content_format' ] = $current->rev_content_format;
1473 }
1474
1475 $revision = new Revision( $row );
1476 $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
1477 } else {
1478 $revision = null;
1479 }
1480
1481 wfProfileOut( __METHOD__ );
1482 return $revision;
1483 }
1484
1485 /**
1486 * Determine if the current user is allowed to view a particular
1487 * field of this revision, if it's marked as deleted.
1488 *
1489 * @param $field Integer:one of self::DELETED_TEXT,
1490 * self::DELETED_COMMENT,
1491 * self::DELETED_USER
1492 * @param $user User object to check, or null to use $wgUser
1493 * @return Boolean
1494 */
1495 public function userCan( $field, User $user = null ) {
1496 return self::userCanBitfield( $this->mDeleted, $field, $user );
1497 }
1498
1499 /**
1500 * Determine if the current user is allowed to view a particular
1501 * field of this revision, if it's marked as deleted. This is used
1502 * by various classes to avoid duplication.
1503 *
1504 * @param $bitfield Integer: current field
1505 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1506 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1507 * self::DELETED_USER = File::DELETED_USER
1508 * @param $user User object to check, or null to use $wgUser
1509 * @return Boolean
1510 */
1511 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1512 if( $bitfield & $field ) { // aspect is deleted
1513 if ( $bitfield & self::DELETED_RESTRICTED ) {
1514 $permission = 'suppressrevision';
1515 } elseif ( $field & self::DELETED_TEXT ) {
1516 $permission = 'deletedtext';
1517 } else {
1518 $permission = 'deletedhistory';
1519 }
1520 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1521 if ( $user === null ) {
1522 global $wgUser;
1523 $user = $wgUser;
1524 }
1525 return $user->isAllowed( $permission );
1526 } else {
1527 return true;
1528 }
1529 }
1530
1531 /**
1532 * Get rev_timestamp from rev_id, without loading the rest of the row
1533 *
1534 * @param $title Title
1535 * @param $id Integer
1536 * @return String
1537 */
1538 static function getTimestampFromId( $title, $id ) {
1539 $dbr = wfGetDB( DB_SLAVE );
1540 // Casting fix for DB2
1541 if ( $id == '' ) {
1542 $id = 0;
1543 }
1544 $conds = array( 'rev_id' => $id );
1545 $conds['rev_page'] = $title->getArticleID();
1546 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1547 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1548 # Not in slave, try master
1549 $dbw = wfGetDB( DB_MASTER );
1550 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1551 }
1552 return wfTimestamp( TS_MW, $timestamp );
1553 }
1554
1555 /**
1556 * Get count of revisions per page...not very efficient
1557 *
1558 * @param $db DatabaseBase
1559 * @param $id Integer: page id
1560 * @return Integer
1561 */
1562 static function countByPageId( $db, $id ) {
1563 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1564 array( 'rev_page' => $id ), __METHOD__ );
1565 if( $row ) {
1566 return $row->revCount;
1567 }
1568 return 0;
1569 }
1570
1571 /**
1572 * Get count of revisions per page...not very efficient
1573 *
1574 * @param $db DatabaseBase
1575 * @param $title Title
1576 * @return Integer
1577 */
1578 static function countByTitle( $db, $title ) {
1579 $id = $title->getArticleID();
1580 if( $id ) {
1581 return self::countByPageId( $db, $id );
1582 }
1583 return 0;
1584 }
1585 }