MediaWiki
REL1_21
|
00001 <?php 00045 abstract class LockManager { 00047 protected $lockTypeMap = array( 00048 self::LOCK_SH => self::LOCK_SH, 00049 self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH 00050 self::LOCK_EX => self::LOCK_EX 00051 ); 00052 00054 protected $locksHeld = array(); 00055 00056 protected $domain; // string; domain (usually wiki ID) 00057 protected $lockTTL; // integer; maximum time locks can be held 00058 00059 /* Lock types; stronger locks have higher values */ 00060 const LOCK_SH = 1; // shared lock (for reads) 00061 const LOCK_UW = 2; // shared lock (for reads used to write elsewhere) 00062 const LOCK_EX = 3; // exclusive lock (for writes) 00063 00074 public function __construct( array $config ) { 00075 $this->domain = isset( $config['domain'] ) ? $config['domain'] : wfWikiID(); 00076 if ( isset( $config['lockTTL'] ) ) { 00077 $this->lockTTL = max( 1, $config['lockTTL'] ); 00078 } elseif ( PHP_SAPI === 'cli' ) { 00079 $this->lockTTL = 2*3600; 00080 } else { 00081 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode 00082 $this->lockTTL = max( 5*60, 2*(int)$met ); 00083 } 00084 } 00085 00093 final public function lock( array $paths, $type = self::LOCK_EX ) { 00094 wfProfileIn( __METHOD__ ); 00095 $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap[$type] ); 00096 wfProfileOut( __METHOD__ ); 00097 return $status; 00098 } 00099 00107 final public function unlock( array $paths, $type = self::LOCK_EX ) { 00108 wfProfileIn( __METHOD__ ); 00109 $status = $this->doUnlock( array_unique( $paths ), $this->lockTypeMap[$type] ); 00110 wfProfileOut( __METHOD__ ); 00111 return $status; 00112 } 00113 00122 final protected function sha1Base36Absolute( $path ) { 00123 return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 ); 00124 } 00125 00134 final protected function sha1Base16Absolute( $path ) { 00135 return sha1( "{$this->domain}:{$path}" ); 00136 } 00137 00145 abstract protected function doLock( array $paths, $type ); 00146 00154 abstract protected function doUnlock( array $paths, $type ); 00155 } 00156 00161 class NullLockManager extends LockManager { 00168 protected function doLock( array $paths, $type ) { 00169 return Status::newGood(); 00170 } 00171 00178 protected function doUnlock( array $paths, $type ) { 00179 return Status::newGood(); 00180 } 00181 }