Index: trunk/extensions/GlobalUsage/GlobalUsageQuery.php |
— | — | @@ -12,15 +12,17 @@ |
13 | 13 | private $reversed = false; |
14 | 14 | |
15 | 15 | /** |
16 | | - * @param $target mixed Title or db key, or array of db keys of target(s) |
| 16 | + * @param $target mixed Title or db key, or array of db keys of target(s). |
| 17 | + * If a title, can be a category or a file |
17 | 18 | */ |
18 | 19 | public function __construct( $target ) { |
19 | 20 | global $wgGlobalUsageDatabase; |
20 | 21 | $this->db = wfGetDB( DB_SLAVE, array(), $wgGlobalUsageDatabase ); |
21 | | - if ( $target instanceof Title ) |
22 | | - $this->target = $target->getDBKey(); |
23 | | - else |
| 22 | + if ( $target instanceof Title ) { |
24 | 23 | $this->target = $target; |
| 24 | + } else { |
| 25 | + $this->target = Title::makeTitleSafe( NS_FILE, $target ); |
| 26 | + } |
25 | 27 | $this->offset = array(); |
26 | 28 | |
27 | 29 | } |
— | — | @@ -101,10 +103,29 @@ |
102 | 104 | * Executes the query |
103 | 105 | */ |
104 | 106 | public function execute() { |
105 | | - /* Construct a where clause */ |
| 107 | + /* Construct the SQL query */ |
| 108 | + $tables = array( 'globalimagelinks' ); |
| 109 | + |
106 | 110 | // Add target image(s) |
107 | | - $where = array( 'gil_to' => $this->target ); |
| 111 | + switch ( $this->target->getNamespace() ) { |
| 112 | + case NS_FILE: |
| 113 | + $where = array( 'gil_to' => $this->target ); |
| 114 | + break; |
| 115 | + case NS_CATEGORY: |
| 116 | + $tables[] = 'categorylinks'; |
| 117 | + $tables[] = 'page'; |
| 118 | + $where = array( |
| 119 | + 'cl_to' => $this->target->getDbKey(), |
| 120 | + 'cl_from = page_id', |
| 121 | + 'page_namespace = ' . NS_FILE, |
| 122 | + 'gil_to = page_title', |
| 123 | + ); |
| 124 | + break; |
| 125 | + default: |
| 126 | + return array(); |
| 127 | + } |
108 | 128 | |
| 129 | + |
109 | 130 | if ( $this->filterLocal ) |
110 | 131 | // Don't show local file usage |
111 | 132 | $where[] = 'gil_wiki != ' . $this->db->addQuotes( wfWikiId() ); |
— | — | @@ -135,7 +156,7 @@ |
136 | 157 | } |
137 | 158 | |
138 | 159 | /* Perform select (Duh.) */ |
139 | | - $res = $this->db->select( 'globalimagelinks', |
| 160 | + $res = $this->db->select( $tables, |
140 | 161 | array( |
141 | 162 | 'gil_to', |
142 | 163 | 'gil_wiki', |