123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- class Category {
-
- private $mName = null;
- private $mID = null;
-
- private $mTitle = null;
-
- private $mPages = null, $mSubcats = null, $mFiles = null;
- const LOAD_ONLY = 0;
- const LAZY_INIT_ROW = 1;
- private function __construct() {
- }
-
- protected function initialize( $mode = self::LOAD_ONLY ) {
- if ( $this->mName === null && $this->mID === null ) {
- throw new MWException( __METHOD__ . ' has both names and IDs null' );
- } elseif ( $this->mID === null ) {
- $where = [ 'cat_title' => $this->mName ];
- } elseif ( $this->mName === null ) {
- $where = [ 'cat_id' => $this->mID ];
- } else {
-
- return true;
- }
- $dbr = wfGetDB( DB_REPLICA );
- $row = $dbr->selectRow(
- 'category',
- [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
- $where,
- __METHOD__
- );
- if ( !$row ) {
-
- if ( $this->mTitle ) {
-
-
- $this->mID = false;
- $this->mName = $this->mTitle->getDBkey();
- $this->mPages = 0;
- $this->mSubcats = 0;
- $this->mFiles = 0;
-
- if ( $mode === self::LAZY_INIT_ROW && $this->mTitle->exists() ) {
- DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
- }
- return true;
- } else {
- return false;
- }
- }
- $this->mID = $row->cat_id;
- $this->mName = $row->cat_title;
- $this->mPages = $row->cat_pages;
- $this->mSubcats = $row->cat_subcats;
- $this->mFiles = $row->cat_files;
-
-
-
- if ( $this->mPages < 0 || $this->mSubcats < 0 || $this->mFiles < 0 ) {
- $this->mPages = max( $this->mPages, 0 );
- $this->mSubcats = max( $this->mSubcats, 0 );
- $this->mFiles = max( $this->mFiles, 0 );
- if ( $mode === self::LAZY_INIT_ROW ) {
- DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
- }
- }
- return true;
- }
-
- public static function newFromName( $name ) {
- $cat = new self();
- $title = Title::makeTitleSafe( NS_CATEGORY, $name );
- if ( !is_object( $title ) ) {
- return false;
- }
- $cat->mTitle = $title;
- $cat->mName = $title->getDBkey();
- return $cat;
- }
-
- public static function newFromTitle( $title ) {
- $cat = new self();
- $cat->mTitle = $title;
- $cat->mName = $title->getDBkey();
- return $cat;
- }
-
- public static function newFromID( $id ) {
- $cat = new self();
- $cat->mID = intval( $id );
- return $cat;
- }
-
- public static function newFromRow( $row, $title = null ) {
- $cat = new self();
- $cat->mTitle = $title;
-
-
-
-
- if ( $row->cat_title === null ) {
- if ( $title === null ) {
-
-
- return false;
- } else {
-
- $cat->mName = $title->getDBkey();
- }
- $cat->mID = false;
- $cat->mSubcats = 0;
- $cat->mPages = 0;
- $cat->mFiles = 0;
- } else {
- $cat->mName = $row->cat_title;
- $cat->mID = $row->cat_id;
- $cat->mSubcats = $row->cat_subcats;
- $cat->mPages = $row->cat_pages;
- $cat->mFiles = $row->cat_files;
- }
- return $cat;
- }
-
- public function getName() {
- return $this->getX( 'mName' );
- }
-
- public function getID() {
- return $this->getX( 'mID' );
- }
-
- public function getPageCount() {
- return $this->getX( 'mPages' );
- }
-
- public function getSubcatCount() {
- return $this->getX( 'mSubcats' );
- }
-
- public function getFileCount() {
- return $this->getX( 'mFiles' );
- }
-
- public function getTitle() {
- if ( $this->mTitle ) {
- return $this->mTitle;
- }
- if ( !$this->initialize( self::LAZY_INIT_ROW ) ) {
- return false;
- }
- $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
- return $this->mTitle;
- }
-
- public function getMembers( $limit = false, $offset = '' ) {
- $dbr = wfGetDB( DB_REPLICA );
- $conds = [ 'cl_to' => $this->getName(), 'cl_from = page_id' ];
- $options = [ 'ORDER BY' => 'cl_sortkey' ];
- if ( $limit ) {
- $options['LIMIT'] = $limit;
- }
- if ( $offset !== '' ) {
- $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
- }
- $result = TitleArray::newFromResult(
- $dbr->select(
- [ 'page', 'categorylinks' ],
- [ 'page_id', 'page_namespace', 'page_title', 'page_len',
- 'page_is_redirect', 'page_latest' ],
- $conds,
- __METHOD__,
- $options
- )
- );
- return $result;
- }
-
- private function getX( $key ) {
- if ( !$this->initialize( self::LAZY_INIT_ROW ) ) {
- return false;
- }
- return $this->{$key};
- }
-
- public function refreshCounts() {
- if ( wfReadOnly() ) {
- return false;
- }
-
-
-
- if ( !$this->initialize( self::LOAD_ONLY ) ) {
- return false;
- }
- $dbw = wfGetDB( DB_MASTER );
-
- $name = __METHOD__ . ':' . md5( $this->mName );
- $scopedLock = $dbw->getScopedLockAndFlush( $name, __METHOD__, 0 );
- if ( !$scopedLock ) {
- return false;
- }
- $dbw->startAtomic( __METHOD__ );
- $cond1 = $dbw->conditional( [ 'page_namespace' => NS_CATEGORY ], 1, 'NULL' );
- $cond2 = $dbw->conditional( [ 'page_namespace' => NS_FILE ], 1, 'NULL' );
- $result = $dbw->selectRow(
- [ 'categorylinks', 'page' ],
- [ 'pages' => 'COUNT(*)',
- 'subcats' => "COUNT($cond1)",
- 'files' => "COUNT($cond2)"
- ],
- [ 'cl_to' => $this->mName, 'page_id = cl_from' ],
- __METHOD__,
- [ 'LOCK IN SHARE MODE' ]
- );
- $shouldExist = $result->pages > 0 || $this->getTitle()->exists();
- if ( $this->mID ) {
- if ( $shouldExist ) {
-
-
-
- $dbw->update(
- 'category',
- [
- 'cat_pages' => $result->pages,
- 'cat_subcats' => $result->subcats,
- 'cat_files' => $result->files
- ],
- [ 'cat_title' => $this->mName ],
- __METHOD__
- );
- } else {
-
- $dbw->delete(
- 'category',
- [ 'cat_title' => $this->mName ],
- __METHOD__
- );
- $this->mID = false;
- }
- } elseif ( $shouldExist ) {
-
-
- $dbw->upsert(
- 'category',
- [
- 'cat_title' => $this->mName,
- 'cat_pages' => $result->pages,
- 'cat_subcats' => $result->subcats,
- 'cat_files' => $result->files
- ],
- [ 'cat_title' ],
- [
- 'cat_pages' => $result->pages,
- 'cat_subcats' => $result->subcats,
- 'cat_files' => $result->files
- ],
- __METHOD__
- );
-
-
- }
- $dbw->endAtomic( __METHOD__ );
-
- $this->mPages = $result->pages;
- $this->mSubcats = $result->subcats;
- $this->mFiles = $result->files;
- return true;
- }
- }
|