r107574 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107573‎ | r107574 | r107575 >
Date:15:12, 29 December 2011
Author:ialex
Status:ok
Tags:
Comment:
* Added Title::getLinksFrom() and Title::getTemplateLinksFrom() for consistency with Title::getLinksTo() and Title::getTemplateLinksTo()
* Deprecated WikiPage::getUsedTemplates() in favour of Title::getTemplateLinksFrom() and updated to it in core
Modified paths:
  • /trunk/phase3/includes/EditPage.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/includes/WikiPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/EditPage.php
@@ -2507,7 +2507,7 @@
25082508 }
25092509 return $templates;
25102510 } else {
2511 - return $this->mArticle->getUsedTemplates();
 2511+ return $this->mTitle->getTemplateLinksFrom();
25122512 }
25132513 }
25142514
Index: trunk/phase3/includes/OutputPage.php
@@ -2256,8 +2256,7 @@
22572257 $this->addHTML( Html::element( 'textarea', $params, $source ) );
22582258
22592259 // Show templates used by this article
2260 - $page = WikiPage::factory( $this->getTitle() );
2261 - $templates = Linker::formatTemplates( $page->getUsedTemplates() );
 2260+ $templates = Linker::formatTemplates( $this->getTitle()->getTemplateLinksFrom() );
22622261 $this->addHTML( "<div class='templatesUsed'>
22632262 $templates
22642263 </div>
Index: trunk/phase3/includes/Title.php
@@ -3126,8 +3126,6 @@
31273127 * @return Array of Title objects linking here
31283128 */
31293129 public function getLinksTo( $options = array(), $table = 'pagelinks', $prefix = 'pl' ) {
3130 - $linkCache = LinkCache::singleton();
3131 -
31323130 if ( count( $options ) > 0 ) {
31333131 $db = wfGetDB( DB_MASTER );
31343132 } else {
@@ -3146,7 +3144,8 @@
31473145 );
31483146
31493147 $retVal = array();
3150 - if ( $db->numRows( $res ) ) {
 3148+ if ( $res->numRows() ) {
 3149+ $linkCache = LinkCache::singleton();
31513150 foreach ( $res as $row ) {
31523151 $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title );
31533152 if ( $titleObj ) {
@@ -3173,6 +3172,76 @@
31743173 }
31753174
31763175 /**
 3176+ * Get an array of Title objects linked from this Title
 3177+ * Also stores the IDs in the link cache.
 3178+ *
 3179+ * WARNING: do not use this function on arbitrary user-supplied titles!
 3180+ * On heavily-used templates it will max out the memory.
 3181+ *
 3182+ * @param $options Array: may be FOR UPDATE
 3183+ * @param $table String: table name
 3184+ * @param $prefix String: fields prefix
 3185+ * @return Array of Title objects linking here
 3186+ */
 3187+ public function getLinksFrom( $options = array(), $table = 'pagelinks', $prefix = 'pl' ) {
 3188+ $id = $this->getArticleId();
 3189+
 3190+ # If the page doesn't exist; there can't be any link from this page
 3191+ if ( !$id ) {
 3192+ return array();
 3193+ }
 3194+
 3195+ if ( count( $options ) > 0 ) {
 3196+ $db = wfGetDB( DB_MASTER );
 3197+ } else {
 3198+ $db = wfGetDB( DB_SLAVE );
 3199+ }
 3200+
 3201+ $namespaceFiled = "{$prefix}_namespace";
 3202+ $titleField = "{$prefix}_title";
 3203+
 3204+ $res = $db->select(
 3205+ array( $table, 'page' ),
 3206+ array( $namespaceFiled, $titleField, 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
 3207+ array( "{$prefix}_from" => $id ),
 3208+ __METHOD__,
 3209+ $options,
 3210+ array( 'page' => array( 'LEFT JOIN', array( "page_namespace=$namespaceFiled", "page_title=$titleField" ) ) )
 3211+ );
 3212+
 3213+ $retVal = array();
 3214+ if ( $res->numRows() ) {
 3215+ $linkCache = LinkCache::singleton();
 3216+ foreach ( $res as $row ) {
 3217+ $titleObj = Title::makeTitle( $row->$namespaceFiled, $row->$titleField );
 3218+ if ( $titleObj ) {
 3219+ if ( $row->page_id ) {
 3220+ $linkCache->addGoodLinkObjFromRow( $titleObj, $row );
 3221+ } else {
 3222+ $linkCache->addBadLinkObj( $titleObj );
 3223+ }
 3224+ $retVal[] = $titleObj;
 3225+ }
 3226+ }
 3227+ }
 3228+ return $retVal;
 3229+ }
 3230+
 3231+ /**
 3232+ * Get an array of Title objects used on this Title as a template
 3233+ * Also stores the IDs in the link cache.
 3234+ *
 3235+ * WARNING: do not use this function on arbitrary user-supplied titles!
 3236+ * On heavily-used templates it will max out the memory.
 3237+ *
 3238+ * @param $options Array: may be FOR UPDATE
 3239+ * @return Array of Title the Title objects used here
 3240+ */
 3241+ public function getTemplateLinksFrom( $options = array() ) {
 3242+ return $this->getLinksFrom( $options, 'templatelinks', 'tl' );
 3243+ }
 3244+
 3245+ /**
31773246 * Get an array of Title objects referring to non-existent articles linked from this page
31783247 *
31793248 * @todo check if needed (used only in SpecialBrokenRedirects.php, and should use redirect table in this case)
Index: trunk/phase3/includes/WikiPage.php
@@ -2306,35 +2306,6 @@
23072307 /**#@-*/
23082308
23092309 /**
2310 - * Return a list of templates used by this article.
2311 - * Uses the templatelinks table
2312 - *
2313 - * @return Array of Title objects
2314 - */
2315 - public function getUsedTemplates() {
2316 - $result = array();
2317 - $id = $this->mTitle->getArticleID();
2318 -
2319 - if ( $id == 0 ) {
2320 - return array();
2321 - }
2322 -
2323 - $dbr = wfGetDB( DB_SLAVE );
2324 - $res = $dbr->select( array( 'templatelinks' ),
2325 - array( 'tl_namespace', 'tl_title' ),
2326 - array( 'tl_from' => $id ),
2327 - __METHOD__ );
2328 -
2329 - if ( $res !== false ) {
2330 - foreach ( $res as $row ) {
2331 - $result[] = Title::makeTitle( $row->tl_namespace, $row->tl_title );
2332 - }
2333 - }
2334 -
2335 - return $result;
2336 - }
2337 -
2338 - /**
23392310 * Returns a list of hidden categories this page is a member of.
23402311 * Uses the page_props and categorylinks tables.
23412312 *
@@ -2628,6 +2599,17 @@
26292600 }
26302601
26312602 /**
 2603+ * Return a list of templates used by this article.
 2604+ * Uses the templatelinks table
 2605+ *
 2606+ * @deprecated in 1.19; use Title::getTemplateLinksFrom()
 2607+ * @return Array of Title objects
 2608+ */
 2609+ public function getUsedTemplates() {
 2610+ return $this->mTitle->getTemplateLinksFrom();
 2611+ }
 2612+
 2613+ /**
26322614 * Perform article updates on a special page creation.
26332615 *
26342616 * @param $rev Revision object

Status & tagging log