cleanup and fixes for secondary data updates
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index d6fd4fb..3e8e362 100644 (file)
  *
  * @todo document (e.g. one-sentence top-level class description).
  */
-class LinksUpdate extends SecondaryDBDataUpdate {
+class LinksUpdate extends SqlDataUpdate {
 
-       /**@{{
-        * @private
-        */
-       var $mId,            //!< Page ID of the article linked from
+       // @todo: make members protected, but make sure extensions don't break
+
+       public $mId,         //!< Page ID of the article linked from
                $mTitle,         //!< Title object of the article linked from
-               $mParserOutput,  //!< Whether to queue jobs for recursive update
+               $mParserOutput,  //!< Parser output
                $mLinks,         //!< Map of title strings to IDs for the links in the document
                $mImages,        //!< DB keys of the images used, in the array key only
                $mTemplates,     //!< Map of title strings to IDs for the template references, including broken ones
@@ -41,7 +40,6 @@ class LinksUpdate extends SecondaryDBDataUpdate {
                $mInterlangs,    //!< Map of language codes to titles
                $mProperties,    //!< Map of arbitrary name to value
                $mRecursive;     //!< Whether to queue jobs for recursive updates
-       /**@}}*/
 
        /**
         * Constructor
@@ -53,12 +51,12 @@ class LinksUpdate extends SecondaryDBDataUpdate {
        function __construct( $title, $parserOutput, $recursive = true ) {
                parent::__construct( );
 
-               if ( !is_object( $title ) ) {
+               if ( !( $title instanceof Title ) ) {
                        throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
                                "Please see Article::editUpdates() for an invocation example.\n" );
                }
 
-               if ( !is_object( $parserOutput ) ) {
+               if ( !( $parserOutput instanceof ParserOutput ) ) {
                        throw new MWException( "The calling convention to LinksUpdate::__construct() has changed. " .
                                "Please see WikiPage::doEditUpdates() for an invocation example.\n" );
                }
@@ -66,6 +64,10 @@ class LinksUpdate extends SecondaryDBDataUpdate {
                $this->mTitle = $title;
                $this->mId = $title->getArticleID();
 
+               if ( !$this->mId ) {
+                       throw new MWException( "The Title object did not provide an article ID. Perhaps the page doesn't exist?" );
+               }
+
                $this->mParserOutput = $parserOutput;
 
                $this->mLinks = $parserOutput->getLinks();
@@ -812,13 +814,9 @@ class LinksUpdate extends SecondaryDBDataUpdate {
 /**
  * Update object handling the cleanup of links tables after a page was deleted.
  **/
-class LinksDeletionUpdate extends SecondaryDBDataUpdate {
+class LinksDeletionUpdate extends SqlDataUpdate {
 
-       /**@{{
-        * @private
-        */
-       var $mWikiPage;     //!< WikiPage the wikipage that was deleted
-       /**@}}*/
+       protected $mTitle;     //!< Title the title of page that was deleted
 
        /**
         * Constructor
@@ -827,18 +825,22 @@ class LinksDeletionUpdate extends SecondaryDBDataUpdate {
         * @param $parserOutput ParserOutput: output from a full parse of this page
         * @param $recursive Boolean: queue jobs for recursive updates?
         */
-       function __construct( WikiPage $page ) {
+       function __construct( Title $title ) {
                parent::__construct( );
 
-               $this->mPage = $page;
+               $this->mTitle = $title;
+
+               if ( !$title->getArticleID() ) {
+                       throw new MWException( "The Title object did not provide an article ID. Perhaps the page doesn't exist?" );
+               }
        }
 
        /**
         * Do some database updates after deletion
         */
        public function doUpdate() {
-               $title = $this->mPage->getTitle();
-               $id = $this->mPage->getId();
+               $title = $this->mTitle;
+               $id = $title->getArticleID();
 
                # Delete restrictions for it
                $this->mDb->delete( 'page_restrictions', array ( 'pr_page' => $id ), __METHOD__ );
@@ -851,7 +853,7 @@ class LinksDeletionUpdate extends SecondaryDBDataUpdate {
                        $cats [] = $row->cl_to;
                }
 
-               $this->mPage->updateCategoryCounts( array(), $cats );
+               $this->updateCategoryCounts( array(), $cats );
 
                # If using cascading deletes, we can skip some explicit deletes
                if ( !$this->mDb->cascadingDeletes() ) {
@@ -873,13 +875,25 @@ class LinksDeletionUpdate extends SecondaryDBDataUpdate {
                if ( !$this->mDb->cleanupTriggers() ) {
                        # Clean up recentchanges entries...
                        $this->mDb->delete( 'recentchanges',
-                                     array( 'rc_type != ' . RC_LOG,
-                                          'rc_namespace' => $title->getNamespace(),
-                                          'rc_title' => $title->getDBkey() ),
-                                     __METHOD__ );
+                               array( 'rc_type != ' . RC_LOG,
+                                       'rc_namespace' => $title->getNamespace(),
+                                       'rc_title' => $title->getDBkey() ),
+                               __METHOD__ );
                        $this->mDb->delete( 'recentchanges',
-                                     array( 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ),
-                                     __METHOD__ );
+                               array( 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ),
+                               __METHOD__ );
                }
        }
+
+       /**
+        * Update all the appropriate counts in the category table.
+        * @param $added array associative array of category name => sort key
+        * @param $deleted array associative array of category name => sort key
+        */
+       function updateCategoryCounts( $added, $deleted ) {
+               $a = WikiPage::factory( $this->mTitle );
+               $a->updateCategoryCounts(
+                       array_keys( $added ), array_keys( $deleted )
+               );
+       }
 }