PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/APC/APCCacheMode.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 299 lines | 252 code | 46 blank | 1 comment | 30 complexity | 940f47c75cf1a87795d0041c453f1b0e MD5 | raw file
  1. <?php
  2. class APCCacheMode {
  3. protected $opts, $title;
  4. protected $userMode = false;
  5. protected $fieldKey;
  6. public function __construct( FormOptions $opts, Title $title ) {
  7. $this->opts = $opts;
  8. $this->title = $title;
  9. $this->userMode = $opts->getValue( 'mode' ) === SpecialAPC::MODE_USER_CACHE;
  10. $this->fieldKey = $this->userMode ? 'info' : ( ini_get( 'apc.stat' ) ? 'inode' : 'filename' );
  11. }
  12. protected $scopes = array(
  13. 'A' => 'cache_list',
  14. 'D' => 'deleted_list'
  15. );
  16. protected function displayObject( $object ) {
  17. global $wgLang;
  18. $cache = apc_cache_info( $this->userMode ? 'user' : 'opcode' );
  19. $s =
  20. Xml::openElement( 'div', array( 'class' => 'mw-apc-listing' ) ) .
  21. Xml::openElement( 'table' ) . Xml::openElement( 'tbody' ) .
  22. Xml::openElement( 'tr' ) .
  23. Xml::element( 'th', null, wfMsg( 'viewapc-display-attribute' ) ) .
  24. Xml::element( 'th', null, wfMsg( 'viewapc-display-value' ) ) .
  25. Xml::closeElement( 'tr' );
  26. $r = 1;
  27. foreach ( $this->scopes as $list ) {
  28. foreach ( $cache[$list] as $entry ) {
  29. if ( md5( $entry[$this->fieldKey] ) !== $object ) continue;
  30. $size = 0;
  31. foreach ( $entry as $key => $value ) {
  32. switch ( $key ) {
  33. case 'num_hits':
  34. $value = $wgLang->formatNum( $value ) .
  35. $wgLang->formatNum( sprintf( " (%.2f%%)", $value * 100 / $cache['num_hits'] ) );
  36. break;
  37. case 'deletion_time':
  38. $value = $this->formatValue( $key, $value );
  39. if ( !$value ) {
  40. $value = wfMsg( 'viewapc-display-no-delete' );
  41. break;
  42. }
  43. case 'mem_size':
  44. $size = $value;
  45. default:
  46. $value = $this->formatValue( $key, $value );
  47. }
  48. $s .= APCUtils::tableRow( $r = 1 - $r,
  49. wfMsgHtml( 'viewapc-display-' . $key ),
  50. htmlspecialchars( $value ) );
  51. }
  52. if ( $this->userMode ) {
  53. if ( $size > 1024 * 1024 ) {
  54. $s .= APCUtils::tableRow( $r = 1 - $r,
  55. wfMsgHtml( 'viewapc-display-stored-value' ),
  56. wfMsgExt( 'viewapc-display-too-big', 'parseinline' ) );
  57. } else {
  58. $value = var_export( apc_fetch( $entry[$this->fieldKey] ), true );
  59. $s .= APCUtils::tableRow( $r = 1 - $r,
  60. wfMsgHtml( 'viewapc-display-stored-value' ),
  61. Xml::element( 'pre', null, $value ) );
  62. }
  63. }
  64. }
  65. }
  66. $s .= '</tbody></table></div>';
  67. return $s;
  68. }
  69. // sortable table header in "scripts for this host" view
  70. protected function sortHeader( $title, $overrides ) {
  71. $changed = $this->opts->getChangedValues();
  72. $target = $this->title->getLocalURL( wfArrayToCGI( $overrides, $changed ) );
  73. return Xml::tags( 'a', array( 'href' => $target ), $title );
  74. }
  75. protected function formatValue( $type, $value ) {
  76. global $wgLang;
  77. switch ( $type ) {
  78. case 'deletion_time':
  79. if ( !$value ) {
  80. $value = false; break;
  81. }
  82. case 'mtime':
  83. case 'creation_time':
  84. case 'access_time':
  85. $value = $wgLang->timeanddate( $value );
  86. break;
  87. case 'ref_count':
  88. case 'num_hits':
  89. $value = $wgLang->formatNum( $value );
  90. break;
  91. case 'mem_size':
  92. $value = $wgLang->formatSize( $value );
  93. break;
  94. case 'ttl':
  95. $value = $wgLang->formatTimePeriod( $value );
  96. break;
  97. case 'type':
  98. $value = wfMsg( 'viewapc-display-type-' . $value );
  99. break;
  100. }
  101. return $value;
  102. }
  103. public function cacheView() {
  104. global $wgOut, $wgLang;
  105. $object = $this->opts->getValue( 'display' );
  106. if ( $object ) {
  107. $wgOut->addHTML( $this->displayObject( $object ) );
  108. return;
  109. }
  110. $wgOut->addHTML( $this->options() );
  111. $wgOut->addHTML( '<div><table><tbody><tr>' );
  112. $fields = array( 'name', 'hits', 'size', 'accessed', 'modified', 'created' );
  113. if ( $this->userMode ) $fields[] = 'timeout';
  114. $fields[] = 'deleted';
  115. $fieldKeys = array(
  116. 'name' => $this->userMode ? 'info' : 'filename',
  117. 'hits' => 'num_hits',
  118. 'size' => 'mem_size',
  119. 'accessed' => 'access_time',
  120. 'modified' => 'mtime',
  121. 'created' => 'creation_time',
  122. 'timeout' => 'ttl',
  123. 'deleted' => 'deletion_time',
  124. );
  125. $scope = $this->opts->getValue( 'scope' );
  126. $sort = $this->opts->getValue( 'sort' );
  127. $sortdir = $this->opts->getValue( 'sortdir' );
  128. $limit = $this->opts->getValue( 'limit' );
  129. $offset = $this->opts->getValue( 'offset' );
  130. $search = $this->opts->getValue( 'searchi' );
  131. foreach ( $fields as $field ) {
  132. $extra = array();
  133. if ( $sort === $field ) {
  134. $extra = array( 'sortdir' => 1 - $sortdir );
  135. }
  136. $wgOut->addHTML(
  137. Xml::tags( 'th', null, $this->sortHeader(
  138. wfMsgHtml( 'viewapc-ls-header-' . $field ),
  139. array( 'sort' => $field ) + $extra ) )
  140. );
  141. }
  142. $wgOut->addHTML( '</tr>' );
  143. $cache = apc_cache_info( $this->userMode ? 'user' : 'opcode' );
  144. $list = array();
  145. if ( $scope === 'active' || $scope === 'both' ) {
  146. foreach ( $cache['cache_list'] as $entry ) {
  147. if ( $search && stripos( $entry[$fieldKeys['name']], $search ) === false ) continue;
  148. $sortValue = sprintf( '%015d-', $entry[$fieldKeys[$sort]] );
  149. $list[$sortValue . $entry[$fieldKeys['name']]] = $entry;
  150. }
  151. }
  152. if ( $scope === 'deleted' || $scope === 'both' ) {
  153. foreach ( $cache['deleted_list'] as $entry ) {
  154. if ( $search && stripos( $entry[$fieldKeys['name']], $search ) === false ) continue;
  155. $sortValue = sprintf( '%015d-', $entry[$fieldKeys[$sort]] );
  156. $list[$sortValue . $entry[$fieldKeys['name']]] = $entry;
  157. }
  158. }
  159. $sortdir ? krsort( $list ) : ksort( $list );
  160. $i = 0;
  161. if ( count( $list ) ) {
  162. $r = 1;
  163. foreach ( $list as $name => $entry ) {
  164. if ( $limit === $i++ ) break;
  165. $wgOut->addHTML(
  166. Xml::openElement( 'tr', array( 'class' => 'mw-apc-tr-' . ( $r = 1 - $r ) ) )
  167. );
  168. foreach ( $fields as $field ) {
  169. $index = $fieldKeys[$field];
  170. if ( $field === 'name' ) {
  171. if ( !$this->userMode ) {
  172. $pos = strrpos( $entry[$index], '/' );
  173. if ( $pos !== false ) $value = substr( $entry[$index], $pos + 1 );
  174. } else {
  175. $value = $entry[$index];
  176. }
  177. $value = $this->sortHeader( htmlspecialchars( $value ), array( 'display' => md5( $entry[$this->fieldKey] ) ) );
  178. } elseif ( $field === 'deleted' && $this->userMode && !$entry[$index] ) {
  179. $value = $this->sortHeader(
  180. wfMsgHtml( 'viewapc-ls-delete' ),
  181. array( 'delete' => $entry[$this->fieldKey] )
  182. );
  183. } else {
  184. $value = $this->formatValue( $index, $entry[$index] );
  185. }
  186. $wgOut->addHTML( Xml::tags( 'td', null, $value ) );
  187. }
  188. $wgOut->addHTML( '</tr>' );
  189. }
  190. }
  191. if ( $i < count( $list ) ) {
  192. $left = $wgLang->formatNum( count( $list ) - ( $i + $offset ) );
  193. $wgOut->addHTML(
  194. Xml::tags( 'tr', array( 'colspan' => count( $fields ) ),
  195. Xml::tags( 'td', null, $this->sortHeader(
  196. wfMsgExt( 'viewapc-ls-more', 'parseinline', $left ),
  197. array( 'offset' => $offset + $limit ) ) ) )
  198. );
  199. } elseif ( !count( $list ) ) {
  200. $wgOut->addHTML(
  201. Xml::tags( 'tr', array( 'colspan' => count( $fields ) ),
  202. Xml::tags( 'td', null, wfMsgExt( 'viewapc-ls-nodata', 'parseinline' ) ) )
  203. );
  204. }
  205. $wgOut->addHTML( '</tbody></table></div>' );
  206. }
  207. protected function options() {
  208. global $wgLang, $wgScript;
  209. $s =
  210. Xml::openElement( 'fieldset' ) .
  211. Xml::element( 'legend', null, wfMsg( 'viewapc-ls-options-legend' ) ) .
  212. Xml::openElement( 'form', array( 'action' => $wgScript ) );
  213. $s .= Html::Hidden( 'title', $this->title->getPrefixedText() );
  214. $options = array();
  215. $scope = $this->opts->consumeValue( 'scope' );
  216. $scopeOptions = array( 'active', 'deleted', 'both' );
  217. foreach ( $scopeOptions as $name ) {
  218. $options[] = Xml::option( wfMsg( 'viewapc-ls-scope-' . $name ), $name, $scope === $name );
  219. }
  220. $scopeSelector = Xml::tags( 'select', array( 'name' => 'scope' ), implode( "\n", $options ) );
  221. $options = array();
  222. $sort = $this->opts->consumeValue( 'sort' );
  223. $sortOptions = array( 'hits', 'size', 'name', 'accessed', 'modified', 'created', 'deleted' );
  224. if ( $this->userMode ) $sortOptions[] = 'timeout';
  225. foreach ( $sortOptions as $name ) {
  226. $options[] = Xml::option( wfMsg( 'viewapc-ls-sort-' . $name ), $name, $sort === $name );
  227. }
  228. $sortSelector = Xml::tags( 'select', array( 'name' => 'sort' ), implode( "\n", $options ) );
  229. $options = array();
  230. $sortdir = $this->opts->consumeValue( 'sortdir' );
  231. $options[] = Xml::option( wfMsg( 'ascending_abbrev' ), 0, !$sortdir );
  232. $options[] = Xml::option( wfMsg( 'descending_abbrev' ), 1, $sortdir );
  233. $sortdirSelector = Xml::tags( 'select', array( 'name' => 'sortdir' ), implode( "\n", $options ) );
  234. $options = array();
  235. $limit = $this->opts->consumeValue( 'limit' );
  236. $limitOptions = array( 10, 20, 50, 150, 200, 500, $limit );
  237. sort( $limitOptions );
  238. foreach ( $limitOptions as $name ) {
  239. $options[] = Xml::option( $wgLang->formatNum( $name ), $name, $limit === $name );
  240. }
  241. $options[] = Xml::option( wfMsg( 'viewapc-ls-limit-none' ), 0, $limit === $name );
  242. $limitSelector = Xml::tags( 'select', array( 'name' => 'limit' ), implode( "\n", $options ) );
  243. $searchBox = Xml::input( 'searchi', 25, $this->opts->consumeValue( 'searchi' ) );
  244. $submit = Xml::submitButton( wfMsg( 'viewapc-ls-submit' ) );
  245. foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
  246. $s .= Html::Hidden( $key, $value );
  247. }
  248. $s .= wfMsgHtml( 'viewapc-ls-options', $scopeSelector, $sortSelector,
  249. $sortdirSelector, $limitSelector, $searchBox, $submit );
  250. $s .= '</form></fieldset><br />';
  251. return $s;
  252. }
  253. }