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