revision.php
来自「php 开发的内容管理系统」· PHP 代码 · 共 800 行 · 第 1/2 页
PHP
800 行
<?php/** * @package MediaWiki * @todo document *//** */require_once( 'Database.php' );/** * @package MediaWiki * @todo document */class Revision { const DELETED_TEXT = 1; const DELETED_COMMENT = 2; const DELETED_USER = 4; const DELETED_RESTRICTED = 8; /** * Load a page revision from a given revision ID number. * Returns null if no such revision can be found. * * @param int $id * @static * @access public */ function newFromId( $id ) { return Revision::newFromConds( array( 'page_id=rev_page', 'rev_id' => intval( $id ) ) ); } /** * Load either the current, or a specified, revision * that's attached to a given title. If not attached * to that title, will return null. * * @param Title $title * @param int $id * @return Revision * @access public * @static */ function newFromTitle( &$title, $id = 0 ) { if( $id ) { $matchId = intval( $id ); } else { $matchId = 'page_latest'; } return Revision::newFromConds( array( "rev_id=$matchId", 'page_id=rev_page', 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDbkey() ) ); } /** * Load either the current, or a specified, revision * that's attached to a given page. If not attached * to that page, will return null. * * @param Database $db * @param int $pageid * @param int $id * @return Revision * @access public */ function loadFromPageId( &$db, $pageid, $id = 0 ) { $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid )); if( $id ) { $conds['rev_id']=intval($id); } else { $conds[]='rev_id=page_latest'; } return Revision::loadFromConds( $db, $conds ); } /** * Load either the current, or a specified, revision * that's attached to a given page. If not attached * to that page, will return null. * * @param Database $db * @param Title $title * @param int $id * @return Revision * @access public */ function loadFromTitle( &$db, $title, $id = 0 ) { if( $id ) { $matchId = intval( $id ); } else { $matchId = 'page_latest'; } return Revision::loadFromConds( $db, array( "rev_id=$matchId", 'page_id=rev_page', 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDbkey() ) ); } /** * Load the revision for the given title with the given timestamp. * WARNING: Timestamps may in some circumstances not be unique, * so this isn't the best key to use. * * @param Database $db * @param Title $title * @param string $timestamp * @return Revision * @access public * @static */ function loadFromTimestamp( &$db, &$title, $timestamp ) { return Revision::loadFromConds( $db, array( 'rev_timestamp' => $db->timestamp( $timestamp ), 'page_id=rev_page', 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDbkey() ) ); } /** * Given a set of conditions, fetch a revision. * * @param array $conditions * @return Revision * @static * @access private */ function newFromConds( $conditions ) { $db =& wfGetDB( DB_SLAVE ); $row = Revision::loadFromConds( $db, $conditions ); if( is_null( $row ) ) { $dbw =& wfGetDB( DB_MASTER ); $row = Revision::loadFromConds( $dbw, $conditions ); } return $row; } /** * Given a set of conditions, fetch a revision from * the given database connection. * * @param Database $db * @param array $conditions * @return Revision * @static * @access private */ function loadFromConds( &$db, $conditions ) { $res = Revision::fetchFromConds( $db, $conditions ); if( $res ) { $row = $res->fetchObject(); $res->free(); if( $row ) { $ret = new Revision( $row ); return $ret; } } $ret = null; return $ret; } /** * Return a wrapper for a series of database rows to * fetch all of a given page's revisions in turn. * Each row can be fed to the constructor to get objects. * * @param Title $title * @return ResultWrapper * @static * @access public */ function fetchAllRevisions( &$title ) { return Revision::fetchFromConds( wfGetDB( DB_SLAVE ), array( 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDbkey(), 'page_id=rev_page' ) ); } /** * Return a wrapper for a series of database rows to * fetch all of a given page's revisions in turn. * Each row can be fed to the constructor to get objects. * * @param Title $title * @return ResultWrapper * @static * @access public */ function fetchRevision( &$title ) { return Revision::fetchFromConds( wfGetDB( DB_SLAVE ), array( 'rev_id=page_latest', 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDbkey(), 'page_id=rev_page' ) ); } /** * Given a set of conditions, return a ResultWrapper * which will return matching database rows with the * fields necessary to build Revision objects. * * @param Database $db * @param array $conditions * @return ResultWrapper * @static * @access private */ function fetchFromConds( &$db, $conditions ) { $res = $db->select( array( 'page', 'revision' ), array( 'page_namespace', 'page_title', 'page_latest', 'rev_id', 'rev_page', 'rev_text_id', 'rev_comment', 'rev_user_text', 'rev_user', 'rev_minor_edit', 'rev_timestamp', 'rev_deleted' ), $conditions, 'Revision::fetchRow', array( 'LIMIT' => 1 ) ); $ret = $db->resultObject( $res ); return $ret; } /** * @param object $row * @access private */ function Revision( $row ) { if( is_object( $row ) ) { $this->mId = intval( $row->rev_id ); $this->mPage = intval( $row->rev_page ); $this->mTextId = intval( $row->rev_text_id ); $this->mComment = $row->rev_comment; $this->mUserText = $row->rev_user_text; $this->mUser = intval( $row->rev_user ); $this->mMinorEdit = intval( $row->rev_minor_edit ); $this->mTimestamp = $row->rev_timestamp; $this->mDeleted = intval( $row->rev_deleted ); if( isset( $row->page_latest ) ) { $this->mCurrent = ( $row->rev_id == $row->page_latest ); $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title ); } else { $this->mCurrent = false; $this->mTitle = null; } if( isset( $row->old_text ) ) { $this->mText = $this->getRevisionText( $row ); } else { $this->mText = null; } } elseif( is_array( $row ) ) { // Build a new revision to be saved... global $wgUser; $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null; $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null; $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null; $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName(); $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId(); $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0; $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW ); $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0; // Enforce spacing trimming on supplied text $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null; $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null; $this->mTitle = null; # Load on demand if needed $this->mCurrent = false; } else { throw new MWException( 'Revision constructor passed invalid row format.' ); } } /**#@+ * @access public */ /** * @return int */ function getId() { return $this->mId; } /** * @return int */ function getTextId() { return $this->mTextId; } /** * Returns the title of the page associated with this entry. * @return Title */ function getTitle() { if( isset( $this->mTitle ) ) { return $this->mTitle; } $dbr =& wfGetDB( DB_SLAVE ); $row = $dbr->selectRow( array( 'page', 'revision' ), array( 'page_namespace', 'page_title' ), array( 'page_id=rev_page', 'rev_id' => $this->mId ), 'Revision::getTitle' ); if( $row ) { $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title ); } return $this->mTitle; } /** * Set the title of the revision * @param Title $title */ function setTitle( $title ) { $this->mTitle = $title; } /** * @return int */ function getPage() { return $this->mPage; } /** * Fetch revision's user id if it's available to all users * @return int */ function getUser() { if( $this->isDeleted( self::DELETED_USER ) ) { return 0; } else { return $this->mUser; } } /** * Fetch revision's user id without regard for the current user's permissions * @return string */ function getRawUser() { return $this->mUser; } /** * Fetch revision's username if it's available to all users * @return string */ function getUserText() { if( $this->isDeleted( self::DELETED_USER ) ) { return ""; } else { return $this->mUserText; } } /** * Fetch revision's username without regard for view restrictions * @return string */ function getRawUserText() { return $this->mUserText; } /** * Fetch revision comment if it's available to all users * @return string */ function getComment() { if( $this->isDeleted( self::DELETED_COMMENT ) ) { return ""; } else { return $this->mComment; } } /** * Fetch revision comment without regard for the current user's permissions * @return string
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?